File tree 1 file changed +42
-1
lines changed
1 file changed +42
-1
lines changed Original file line number Diff line number Diff line change 1
1
'use strict' ;
2
2
3
3
const unleash = require ( 'unleash-server' ) ;
4
+ const auth = require ( 'basic-auth' ) ;
5
+ const compare = require ( 'tsscmp' ) ;
6
+ const cors = require ( 'cors' ) ;
4
7
5
- let options = { } ;
8
+ function check ( credentials ) {
9
+ let valid = true ;
10
+ valid = compare ( credentials . name , process . env . BASIC_AUTH_USERNAME ) && valid ;
11
+ valid = compare ( credentials . pass , process . env . BASIC_AUTH_PASSWORD ) && valid ;
12
+ return valid ;
13
+ }
14
+
15
+ function basicAuth ( req , res , next ) {
16
+ const credentials = auth ( req ) ;
17
+
18
+ if ( credentials && check ( credentials ) ) {
19
+ const user = new unleash . User ( { email :
'[email protected] ' } ) ;
20
+ req . user = user ;
21
+ return next ( ) ;
22
+ }
23
+
24
+ return res
25
+ . status ( '401' )
26
+ . set ( { 'WWW-Authenticate' : 'Basic realm="unleash"' } )
27
+ . end ( 'access denied' ) ;
28
+ }
29
+
30
+ function preHook ( app ) {
31
+ app . use ( cors ( ) ) ;
32
+
33
+ app . use ( ( req , res , next ) => {
34
+ if ( req . path . startsWith ( '/api/client' ) || req . path === '/health' ) {
35
+ return next ( ) ;
36
+ }
37
+
38
+ return basicAuth ( req , res , next ) ;
39
+ } ) ;
40
+ }
41
+
42
+ const options = {
43
+ enableLegacyRoutes : false ,
44
+ adminAuthentication : 'custom' ,
45
+ preHook : preHook
46
+ }
6
47
7
48
unleash . start ( options ) ;
You can’t perform that action at this time.
0 commit comments