1- import type { CloudFrontHeaders , CloudFrontRequestEvent } from " aws-lambda" ;
2- import { z } from " zod" ;
1+ import type { CloudFrontHeaders , CloudFrontRequestEvent } from ' aws-lambda' ;
2+ import { z } from ' zod' ;
33import {
44 CognitoIdentityProviderClient ,
55 GetUserCommand ,
6- } from " @aws-sdk/client-cognito-identity-provider" ;
7- import { jwtDecode } from " jwt-decode" ;
8- import { verify } from " jsonwebtoken" ;
9- import getJwksClient from " jwks-rsa" ;
6+ } from ' @aws-sdk/client-cognito-identity-provider' ;
7+ import { jwtDecode } from ' jwt-decode' ;
8+ import { verify } from ' jsonwebtoken' ;
9+ import getJwksClient from ' jwks-rsa' ;
1010
1111const cognitoClient = new CognitoIdentityProviderClient ( {
12- region : " eu-west-2" ,
12+ region : ' eu-west-2' ,
1313} ) ;
1414
1515const $AccessToken = z . object ( {
@@ -19,16 +19,16 @@ const $AccessToken = z.object({
1919} ) ;
2020
2121const deny = {
22- status : " 403" ,
23- statusDescription : " Forbidden" ,
24- body : " <h1>Access Denied</h1>" ,
22+ status : ' 403' ,
23+ statusDescription : ' Forbidden' ,
24+ body : ' <h1>Access Denied</h1>' ,
2525} ;
2626
2727function authFromCookie ( headers : CloudFrontHeaders ) {
2828 const cookie = headers . cookie ?. [ 0 ] ?. value ;
29- const parts = ( cookie ?? "" ) . split ( "; " ) ;
30- const kvParts = parts . map ( ( p ) => p . split ( "=" ) ) ;
31- const [ , t ] = kvParts . find ( ( [ k ] ) => k . endsWith ( " accessToken" ) ) ?? [ ] ;
29+ const parts = ( cookie ?? '' ) . split ( '; ' ) ;
30+ const kvParts = parts . map ( ( p ) => p . split ( '=' ) ) ;
31+ const [ , t ] = kvParts . find ( ( [ k ] ) => k . endsWith ( ' accessToken' ) ) ?? [ ] ;
3232 return t ;
3333}
3434
@@ -41,15 +41,15 @@ export const handler = async (event: CloudFrontRequestEvent) => {
4141
4242 const authorizationToken = authFromCookie ( request . headers ) ;
4343 const userPoolId =
44- request . origin ?. s3 ?. customHeaders [ " x-user-pool-id" ] ?. [ 0 ] . value ;
44+ request . origin ?. s3 ?. customHeaders [ ' x-user-pool-id' ] ?. [ 0 ] . value ;
4545 const userPoolClientId =
46- request . origin ?. s3 ?. customHeaders [ " x-user-pool-client-id" ] ?. [ 0 ] . value ;
46+ request . origin ?. s3 ?. customHeaders [ ' x-user-pool-client-id' ] ?. [ 0 ] . value ;
4747
4848 console . log ( userPoolId , userPoolClientId ) ;
4949
5050 try {
5151 if ( ! authorizationToken ) {
52- console . warn ( " no token" ) ;
52+ console . warn ( ' no token' ) ;
5353 return deny ;
5454 }
5555
@@ -64,7 +64,7 @@ export const handler = async (event: CloudFrontRequestEvent) => {
6464 const { kid } = decodedToken ;
6565
6666 if ( ! kid ) {
67- console . warn ( " Authorization token missing kid" ) ;
67+ console . warn ( ' Authorization token missing kid' ) ;
6868 return deny ;
6969 }
7070
@@ -86,7 +86,7 @@ export const handler = async (event: CloudFrontRequestEvent) => {
8686 }
8787
8888 // token_use claim
89- if ( tokenUse !== " access" ) {
89+ if ( tokenUse !== ' access' ) {
9090 console . warn (
9191 `Token has invalid token_use, expected access but received ${ tokenUse } `
9292 ) ;
@@ -101,19 +101,19 @@ export const handler = async (event: CloudFrontRequestEvent) => {
101101 ) ;
102102
103103 if ( ! Username || ! UserAttributes ) {
104- console . warn ( " Missing user" ) ;
104+ console . warn ( ' Missing user' ) ;
105105 return deny ;
106106 }
107107
108- const sub = UserAttributes . find ( ( { Name } ) => Name === " sub" ) ?. Value ;
108+ const sub = UserAttributes . find ( ( { Name } ) => Name === ' sub' ) ?. Value ;
109109
110110 if ( ! sub ) {
111- console . warn ( " Missing user subject" ) ;
111+ console . warn ( ' Missing user subject' ) ;
112112 return deny ;
113113 }
114114
115115 if ( ownerPath !== sub ) {
116- console . warn ( " owner !== sub" ) ;
116+ console . warn ( ' owner !== sub' ) ;
117117 return deny ;
118118 }
119119
@@ -122,4 +122,4 @@ export const handler = async (event: CloudFrontRequestEvent) => {
122122 console . error ( error ) ;
123123 return deny ;
124124 }
125- } ;
125+ } ;
0 commit comments