-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauthorizer-rest.js
42 lines (36 loc) · 1.01 KB
/
authorizer-rest.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
"use strict";
const jwt = require("jsonwebtoken");
const SecretsManager = require("secretsManager");
function extractTokenFromHeader(e) {
if (e.authorizationToken && e.authorizationToken.split(" ")[0] === "Bearer") {
return e.authorizationToken.split(" ")[1];
} else {
return e.authorizationToken;
}
}
async function validateToken(token, event, callback) {
try {
const secret = await new SecretsManager().getSecret("teste-jwt-rest");
jwt.verify(token, secret, { expiresIn: 3600 });
callback(null, {
principalId: "user",
policyDocument: {
Version: "2012-10-17",
Statement: [
{
Action: "execute-api:Invoke",
Effect: "Allow",
Resource: event.methodArn,
},
],
},
});
} catch (err) {
console.log(err);
callback("Unauthorized");
}
}
module.exports.handler = async (event, context, callback) => {
let token = extractTokenFromHeader(event) || "";
await validateToken(token, event, callback);
};