1- import { CognitoJwtVerifier } from "aws-jwt-verify" ;
2-
31interface WebSocketAuthorizerEvent {
42 type : string ;
53 methodArn : string ;
@@ -8,58 +6,74 @@ interface WebSocketAuthorizerEvent {
86 identitySource ?: string [ ] ;
97}
108
11- const verifier = CognitoJwtVerifier . create ( {
12- userPoolId : process . env . userpoolId ! ,
13- tokenUse : "id" ,
14- clientId : process . env . userpoolClient ! ,
15- } ) ;
16-
179export const wsAuthorizer = async ( event : WebSocketAuthorizerEvent ) => {
1810 console . log ( "Authorizer event:" , JSON . stringify ( event ) ) ;
19- const token =
11+ const proto =
2012 event . headers ?. [ "Sec-WebSocket-Protocol" ] ||
2113 event . multiValueHeaders ?. [ "Sec-WebSocket-Protocol" ] ?. [ 0 ] ;
2214
23- if ( ! token ) {
24- console . error ( "Missing Sec-WebSocket-Protocol header" ) ;
25- return { isAuthorized : false } ;
26- }
27-
28- try {
29- console . log ( `Verifying JWT: ${ token } ` ) ;
30- const payload = await verifier . verify ( token ) ;
31- console . log ( `Validated: ${ JSON . stringify ( payload ) } ` ) ;
32- return {
33- "principalId" : payload . sub ,
34- "policyDocument" : {
35- "Version" : "2012-10-17" ,
36- "Statement" : [
37- {
38- "Action" : "execute-api:Invoke" ,
39- "Effect" : "Allow" ,
40- "Resource" : "*"
41- }
42- ]
43- } ,
44- "context" : {
45- "userId" : payload . sub ,
46- "email" : payload . email
47- }
48- } ;
49- } catch ( err ) {
50- console . error ( "JWT verification failed" , err ) ;
15+ if ( proto !== "auth" ) {
5116 return {
52- "principalId" : "anonymous" ,
53- "policyDocument" : {
54- "Version" : "2012-10-17" ,
55- "Statement" : [
56- {
57- "Action" : "execute-api:Invoke" ,
58- "Effect" : "Deny" ,
59- "Resource" : "*"
60- }
61- ]
62- }
17+ principalId : "anonymous" ,
18+ policyDocument : {
19+ Version : "2012-10-17" ,
20+ Statement : [ { Action : "execute-api:Invoke" , Effect : "Deny" , Resource : "*" } ]
21+ }
6322 } ;
6423 }
24+
25+ return {
26+ principalId : "user" ,
27+ policyDocument : {
28+ Version : "2012-10-17" ,
29+ Statement : [ { Action : "execute-api:Invoke" , Effect : "Allow" , Resource : "*" } ]
30+ }
31+ } ;
32+
33+ // const token =
34+ // event.headers?.["Sec-WebSocket-Protocol"] ||
35+ // event.multiValueHeaders?.["Sec-WebSocket-Protocol"]?.[0];
36+
37+ // if (!token) {
38+ // console.error("Missing Sec-WebSocket-Protocol header");
39+ // return { isAuthorized: false };
40+ // }
41+
42+ // try {
43+ // console.log(`Verifying JWT: ${token}`);
44+ // const payload = await verifier.verify(token);
45+ // console.log(`Validated: ${JSON.stringify(payload)}`);
46+ // return {
47+ // "principalId": payload.sub,
48+ // "policyDocument": {
49+ // "Version": "2012-10-17",
50+ // "Statement": [
51+ // {
52+ // "Action": "execute-api:Invoke",
53+ // "Effect": "Allow",
54+ // "Resource": "*"
55+ // }
56+ // ]
57+ // },
58+ // "context": {
59+ // "userId": payload.sub,
60+ // "email": payload.email
61+ // }
62+ // };
63+ // } catch (err) {
64+ // console.error("JWT verification failed", err);
65+ // return {
66+ // "principalId": "anonymous",
67+ // "policyDocument": {
68+ // "Version": "2012-10-17",
69+ // "Statement": [
70+ // {
71+ // "Action": "execute-api:Invoke",
72+ // "Effect": "Deny",
73+ // "Resource": "*"
74+ // }
75+ // ]
76+ // }
77+ // };
78+ // }
6579} ;
0 commit comments