1
- 'use strict' ;
2
-
3
1
const BASIC_AUTH_USERS = {
4
- ' user' : 'pass'
2
+ user : 'pass'
5
3
} ;
6
4
7
5
module . exports . basicAuth = ( event , context , callback ) => {
@@ -11,22 +9,26 @@ module.exports.basicAuth = (event, context, callback) => {
11
9
12
10
if ( authorization ) {
13
11
const encoded = authorization [ 0 ] . value . split ( ' ' ) [ 1 ] ;
14
- const userAndPassword = new Buffer ( encoded , 'base64' ) . toString ( ) ;
15
- for ( let user in BASIC_AUTH_USERS ) {
12
+ const userAndPassword = Buffer . from ( encoded , 'base64' ) . toString ( ) ;
13
+ const result = Object . keys ( BASIC_AUTH_USERS ) . filter ( ( user ) => {
16
14
const password = BASIC_AUTH_USERS [ user ] ;
17
15
if ( `${ user } :${ password } ` === userAndPassword ) {
18
- callback ( null , request ) ;
19
- return ;
16
+ return true ;
20
17
}
18
+ return false ;
19
+ } ) ;
20
+ if ( result . length > 0 ) {
21
+ callback ( null , request ) ;
22
+ return ;
21
23
}
22
24
}
23
25
24
26
const response = {
25
27
status : '401' ,
26
28
statusDescription : 'Authorization Required' ,
27
29
headers : {
28
- 'www-authenticate' : [ { key : 'WWW-Authenticate' , value : 'Basic' } ] ,
29
- 'content-type' : [ { key : 'Content-Type' , value : 'text/plain; charset=utf-8' } ]
30
+ 'www-authenticate' : [ { key : 'WWW-Authenticate' , value : 'Basic' } ] ,
31
+ 'content-type' : [ { key : 'Content-Type' , value : 'text/plain; charset=utf-8' } ]
30
32
} ,
31
33
body : '401 Authorization Required'
32
34
} ;
0 commit comments