@@ -44,7 +44,6 @@ export class BasicAuth {
4444 }
4545
4646 async handler ( event , context , callback ) {
47- console . log ( 'debugging' , JSON . stringify ( event . Records [ 0 ] . cf ) ) ;
4847 this . initEvent ( event , callback ) ;
4948
5049 try {
@@ -66,11 +65,14 @@ export class BasicAuth {
6665
6766 const headers = this . request . headers ;
6867 const authenticatedStr = isArray ( headers . authorization ) ? headers . authorization [ 0 ] . value : undefined ;
69- if ( ! ( authenticatedStr && this . config . htpasswdAuthenticated ( authenticatedStr ) ) ) {
70- return this . unauthorized ( ) ;
68+
69+ const isAuthenticated = await this . config . htpasswdAuthenticated ( authenticatedStr ) ;
70+
71+ if ( isAuthenticated ) {
72+ return this . authorized ( ) ;
7173 }
7274
73- return this . authorized ( ) ;
75+ return this . unauthorized ( ) ;
7476 }
7577 catch ( e ) {
7678 console . error ( e ) ;
@@ -80,7 +82,6 @@ export class BasicAuth {
8082
8183 private forward ( ) {
8284 const reply = this . isResponse ? this . response : this . request ;
83- console . log ( 456 , JSON . stringify ( reply ) ) ;
8485 return this . callback ( null , reply ) ;
8586 }
8687
@@ -114,76 +115,6 @@ export class BasicAuth {
114115 }
115116
116117 private authorized ( ) {
117- // const expires = config.generateExpires();
118- // const hash = config.generateSecurePathHash(expires, request.origin.custom.path + request.uri);
119- // // const signature = `md5=` + config.generateSecurePathHash(expires, request.origin.custom.path + request.uri) + "&expires=" + expires;
120- // const signature = `md5=${hash}&expires=expires`;
121- // if (request.querystring) {
122- // request.querystring = request.querystring + "&" + signature;
123- // } else {
124- // request.querystring = signature;
125- // }
126-
127- // const options = '; Domain=' + config.websiteDomain + '; Path=/; Secure; HttpOnly';
128- // {
129- // 'Set-Cookie': 'CloudFront-Policy=' + signedCookies['CloudFront-Policy'] + options,
130- // 'SEt-Cookie': 'CloudFront-Signature=' + signedCookies['CloudFront-Signature'] + options,
131- // 'SET-Cookie': 'CloudFront-Key-Pair-Id=' + signedCookies['CloudFront-Key-Pair-Id'] + options
132- // } const authCookie = 'My-Special-Auth-Cookie=AFHJAkfhasfhlafskaj';
133-
134- // const request = event.Records[0].cf.request;
135- // const headers = request.headers;
136- // headers.cookie = headers.cookie || [];
137- // headers.cookie.push({ key:'Cookie', value: 'sample-cookie' });
138- // console.log('set cookie', headers);
139-
140- // headers: {
141- // "Access-Control-Allow-Origin" : "*", // Required for CORS support to work
142- // "Access-Control-Allow-Credentials" : true, // Required for cookies, authorization headers with HTTPS
143- // "Set-Cookie": 'mycookiee=test; domain=localhost; expires=Thu, 19 Apr 2018 20:41:27 GMT;"',
144- // "Cookie": 'anotherCookie=test; domain=localhost; expires=Thu, 19 Apr 2018 20:41:27 GMT;"'
145- // },
146-
147-
148- // extend(request.headers, {
149- // "Access-Control-Allow-Origin": [{
150- // key: 'Access-Control-Allow-Origin',
151- // value: "*"
152- // }],
153- // "Access-Control-Allow-Credentials": [{
154- // key: 'Access-Control-Allow-Credentials',
155- // value: true
156- // }],
157- // "Set-Cookie": [{
158- // key: 'Set-Cookie',
159- // value: 'mycookiee=test; domain=booppi.website; expires=Thu, 19 Apr 2019 20:41:27 GMT;'
160- // }],
161- // "Cookie": [{
162- // key: 'Cookie',
163- // value: 'anotherCookie=test; domain=booppi.website; expires=Thu, 19 Apr 2019 20:41:27 GMT;'
164- // }]
165- // });
166-
167- // const response = event.Records[0].cf.response;
168- // const request = event.Records[0].cf.request;
169-
170- // if (this.isResponse) {
171- // extend(this.response.headers, {
172- // "Set-Cookie": [{
173- // key: 'Set-Cookie',
174- // value: this.config.generateCookieValue(this.cookieDomain)
175- // }]
176- // });
177- // }
178-
179- //
180- // console.log(response.headers);
181- // console.log(response.headers.length);
182-
183- // const reply = this.isResponse ? this.response : this.request;
184- // console.log(JSON.stringify(reply));
185- // console.log(123, this.isResponse);
186-
187118 if ( this . isResponse ) {
188119 extend ( this . response . headers , {
189120 "Set-Cookie" : [ {
@@ -194,111 +125,10 @@ export class BasicAuth {
194125 }
195126
196127 const reply = this . isResponse ? this . response : this . request ;
197- console . log ( 123 , JSON . stringify ( reply ) ) ;
198128 return this . callback ( null , reply ) ;
199129 }
200130
201- // generateCookieValue(): string {
202- // const hash = this.config.generateHash(this.cookieDomain);
203- // return `${CookieName}=${hash}; domain=${this.cookieDomain}; max-age=${this.config.cookieMaxAgeInSeconds};`;
204- // }
205-
206131 get isResponse ( ) : boolean {
207132 return this . eventType . endsWith ( 'response' ) ;
208133 }
209134}
210-
211- // "request": {
212- // "clientIp": "171.232.156.243",
213- // "headers": {
214- // "host": [
215- // {
216- // "key": "Host",
217- // "value": "mysite.booppi.website"
218- // }
219- // ],
220- // "user-agent": [
221- // {
222- // "key": "User-Agent",
223- // "value": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36"
224- // }
225- // ],
226- // "upgrade-insecure-requests": [
227- // {
228- // "key": "upgrade-insecure-requests",
229- // "value": "1"
230- // }
231- // ],
232- // "dnt": [
233- // {
234- // "key": "dnt",
235- // "value": "1"
236- // }
237- // ],
238- // "accept": [
239- // {
240- // "key": "accept",
241- // "value": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8"
242- // }
243- // ],
244- // "accept-encoding": [
245- // {
246- // "key": "accept-encoding",
247- // "value": "gzip, deflate, br"
248- // }
249- // ],
250- // "accept-language": [
251- // {
252- // "key": "accept-language",
253- // "value": "en-US,en;q=0.9,vi;q=0.8,ja;q=0.7"
254- // }
255- // ],
256- // "cookie": [
257- // {
258- // "key": "cookie",
259- // "value": "mycookiee=test"
260- // }
261- // ]
262- // },
263- // "method": "GET",
264- // "querystring": "",
265- // "uri": "/sample.png"
266- // },
267-
268- // 'use strict';
269- //
270- // const sourceCoookie = 'X-Source';
271- // const sourceMain = 'main';
272- // const sourceExperiment = 'experiment';
273- // const experimentTraffic = 0.5;
274- //
275- // // Viewer request handler
276- // exports.handler = (event, context, callback) => {
277- // const request = event.Records[0].cf.request;
278- // const headers = request.headers;
279- //
280- // // Look for source cookie
281- // if ( headers.cookie ) {
282- // for (let i = 0; i < headers.cookie.length; i++) {
283- // ss if (headers.cookie[i].value.indexOf(sourceCoookie) >= 0) {
284- // console.log('Source cookie found. Forwarding request as-is');
285- // // Forward request as-is
286- // callback(null, request);
287- // return;
288- // }
289- // }
290- // }
291- //
292- // console.log('Source cookie has not been found. Throwing dice...');
293- // const source = ( Math.random() < experimentTraffic ) ? sourceExperiment : sourceMain;
294- // console.log(`Source: ${source}`)
295- //
296- // // Add Source cookie
297- // const cookie = `${sourceCoookie}=${source}`
298- // console.log(`Adding cookie header: ${cookie}`);
299- // headers.cookie = headers.cookie || [];
300- // headers.cookie.push({ key:'Cookie', value: cookie });
301- //
302- // // Forwarding request
303- // callback(null, request);
304- // };
0 commit comments