File tree Expand file tree Collapse file tree 3 files changed +45
-1
lines changed Expand file tree Collapse file tree 3 files changed +45
-1
lines changed Original file line number Diff line number Diff line change
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 ( / b a s i c | / 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
+ ] ) ;
Original file line number Diff line number Diff line change 1
1
import express from 'express' ;
2
2
import statusCodesRoute from './status-codes.route.js' ;
3
+ import authBasicRoute from './auth-basic.route.js' ;
3
4
4
5
/* eslint-disable no-console */
5
6
@@ -8,6 +9,7 @@ export default router;
8
9
9
10
// Test scenarios for status codes
10
11
router . use ( '/status' , statusCodesRoute ) ;
12
+ router . use ( '/auth/basic' , authBasicRoute ) ;
11
13
12
14
// Errors
13
15
router . use ( ( req , res ) => {
Original file line number Diff line number Diff line change 13
13
},
14
14
"scripts" : {
15
15
"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" ,
17
17
"postinstall" : " electron-builder install-app-deps" ,
18
18
"prepare" : " pika-web && node tasks/prepare-app.js" ,
19
19
"test" : " npm run test:main && npm run test:renderer && npm run test:app" ,
You can’t perform that action at this time.
0 commit comments