-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
42 lines (35 loc) · 977 Bytes
/
index.js
File metadata and controls
42 lines (35 loc) · 977 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
const jwt = require('jsonwebtoken');
// JWT constants.
const JWT_PUBLIC_KEY = `-----BEGIN PUBLIC KEY-----\n${process.env.JWT_PUBLIC_KEY}\n-----END PUBLIC KEY-----`;
/**
* @param {AWSLambda.APIGatewayRequestAuthorizerEventV2} event
* @returns {Promise<boolean>}
*/
exports.handler = async (event) => {
const token = event.identitySource?.[0]?.split('Bearer ')?.[1];
if (!token) {
console.log('Missing token');
return { isAuthorized: false };
}
const jwtSettings = {
publicKey: JWT_PUBLIC_KEY,
options: {
algorithms: ['RS256'],
},
};
try {
const decoded = jwt.verify(
token,
jwtSettings.publicKey,
jwtSettings.options
);
if (!decoded.sub) {
console.log('Missing or invalid sub claim:', decoded.sub);
return { isAuthorized: false };
}
return { isAuthorized: true };
} catch (error) {
console.log('Token verification failed', token);
return { isAuthorized: false };
}
};