-
Notifications
You must be signed in to change notification settings - Fork 0
/
authorize.js
61 lines (55 loc) · 1.93 KB
/
authorize.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import * as jwt from "jsonwebtoken";
// Set in `environment` of serverless.yml
const AUTH0_CLIENT_PUBLIC_KEY = process.env.AUTH0_CLIENT_PUBLIC_KEY;
const AUTH0_AUDIENCE = process.env.AUTH0_AUDIENCE
const AUTH0_TEST_ID = process.env.AUTH0_TEST_ID.trim()
export function main(event, context, callback) {
console.log("Authorizing ", event)
if (!event.authorizationToken) {
return callback('Unauthorized');
}
const tokenParts = event.authorizationToken.split(' ');
const tokenValue = tokenParts[1];
if (!(tokenParts[0].toLowerCase() === 'bearer' && tokenValue)) {
// no auth token!
return callback('Unauthorized');
}
const options = {
audience: AUTH0_AUDIENCE
};
if (tokenValue === AUTH0_TEST_ID) {
return callback(null, generatePolicy(AUTH0_TEST_ID, 'Allow', event.methodArn));
}
try {
jwt.verify(tokenValue, AUTH0_CLIENT_PUBLIC_KEY, options, (verifyError, decoded) => {
if (verifyError) {
// 401 Unauthorized
console.log(`Token invalid. ${verifyError}`);
return callback('Unauthorized');
}
// is custom authorizer function
console.log('valid from customAuthorizer', decoded);
return callback(null, generatePolicy(decoded.sub, 'Allow', event.methodArn));
});
} catch (err) {
console.log('catch error. Invalid token', err);
return callback('Unauthorized');
}
}
// Help function to generate an IAM policy
let generatePolicy = function(principalId, effect, resource) {
let authResponse = {};
authResponse.principalId = principalId;
if (effect && resource) {
let policyDocument = {};
policyDocument.Version = '2012-10-17';
policyDocument.Statement = [];
let statementOne = {};
statementOne.Action = 'execute-api:Invoke';
statementOne.Effect = effect;
statementOne.Resource = resource;
policyDocument.Statement[0] = statementOne;
authResponse.policyDocument = policyDocument;
}
return authResponse;
}