-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauthorizer.js
46 lines (42 loc) · 1.23 KB
/
authorizer.js
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
43
44
45
46
const { CognitoJwtVerifier } = require("aws-jwt-verify");
const COGNITO_USERPOOL_ID = process.env.COGNITO_USERPOOL_ID;
const COGNITO_WEB_CLIENT_ID = process.env.COGNITO_WEB_CLIENT_ID;
const jwtVerifier = CognitoJwtVerifier.create({
userPoolId: COGNITO_USERPOOL_ID,
tokenUse: "id",
clientId: COGNITO_WEB_CLIENT_ID
});
const generatePolicy = (principalId, effect, resource) => {
var authReponse = {};
authReponse.principalId = principalId;
if (effect && resource) {
let policyDocument = {
Version: "2012-10-17",
Statement: [
{
Effect: effect,
Resource: resource,
Action: "execute-api:Invoke",
},
],
};
authReponse.policyDocument = policyDocument;
}
authReponse.context = {
foo: "bar",
};
console.log(JSON.stringify(authReponse));
return authReponse;
};
exports.handler = async (event, context, callback) => {
// lambda authorizer code
var token = event.authorizationToken; // "allow" or "deny"
console.log(token);
try {
const payload = await jwtVerifier.verify(token);
console.log(JSON.stringify(payload));
callback(null, generatePolicy("user", "Allow", event.methodArn));
} catch (error) {
callback("Error: Invalid token");
}
};