Skip to content

Commit 7492d05

Browse files
committed
chore: adding basic auth route to dev server
1 parent c07fbeb commit 7492d05

File tree

3 files changed

+45
-1
lines changed

3 files changed

+45
-1
lines changed

dev-api/auth-basic.route.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import express from 'express';
2+
import { BaseApi } from './base-api.js';
3+
4+
const router = express.Router();
5+
export default router;
6+
7+
class AuthBaiscRoute extends BaseApi {
8+
sendUnauthorized(res) {
9+
res.status(401);
10+
res.set('WWW-Authenticate', 'Basic realm="This resource is protected"');
11+
res.send('Auth required');
12+
}
13+
14+
requireAuthorized(req, res) {
15+
const auth = req.headers['authorization'];
16+
if (!auth) {
17+
this.sendUnauthorized(res);
18+
return;
19+
}
20+
const { username, password } = req.params;
21+
try {
22+
const parsed = auth.replace(/basic| /ig).trim();
23+
const buff = new Buffer(parsed, 'base64');
24+
const str = buff.toString('ascii');
25+
const [aUname, aPasswd] = str.split(':');
26+
if (username !== aUname || password !== aPasswd) {
27+
this.sendUnauthorized(res);
28+
return;
29+
}
30+
} catch (e) {
31+
this.sendUnauthorized(res);
32+
return;
33+
}
34+
this.printDefaultResponse(req, res);
35+
}
36+
}
37+
38+
const api = new AuthBaiscRoute();
39+
api.setCors(router);
40+
api.wrapApi(router, [
41+
['/:username/:password', 'requireAuthorized'],
42+
]);

dev-api/routes.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import express from 'express';
22
import statusCodesRoute from './status-codes.route.js';
3+
import authBasicRoute from './auth-basic.route.js';
34

45
/* eslint-disable no-console */
56

@@ -8,6 +9,7 @@ export default router;
89

910
// Test scenarios for status codes
1011
router.use('/status', statusCodesRoute);
12+
router.use('/auth/basic', authBasicRoute);
1113

1214
// Errors
1315
router.use((req, res) => {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
},
1414
"scripts": {
1515
"start": "electron . --inspect --debug --debug-level=\"silly\" --skip-app-update --skip-themes-update --workspace-path=\"~/arc-dev/workspace\" --settings-file=\"~/arc-dev/dev-settings.json\" --themes-path=\"~/arc-dev/themes\"",
16-
"start:api": "node --inspect=9227 dev-api/api.js --PORT=8080",
16+
"start:api": "node dev-api/api.js",
1717
"postinstall": "electron-builder install-app-deps",
1818
"prepare": "pika-web && node tasks/prepare-app.js",
1919
"test": "npm run test:main && npm run test:renderer && npm run test:app",

0 commit comments

Comments
 (0)