Skip to content

Commit 04b5207

Browse files
committed
Different approach
1 parent b4e7537 commit 04b5207

File tree

2 files changed

+68
-48
lines changed

2 files changed

+68
-48
lines changed

api/sockets/wsAuthorizer.ts

Lines changed: 62 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import { CognitoJwtVerifier } from "aws-jwt-verify";
2-
31
interface 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-
179
export 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
};

serverless.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,12 @@ functions:
331331
- websocket:
332332
route: $disconnect
333333

334+
auth:
335+
handler: api/sockets/authHandler.handler
336+
events:
337+
- websocket:
338+
route: auth
339+
334340
messageHandler:
335341
handler: api/sockets/messageHandler.handler
336342
logRetentionInDays: 7

0 commit comments

Comments
 (0)