forked from kopertop/lambda-jwt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
57 lines (55 loc) · 1.7 KB
/
index.js
File metadata and controls
57 lines (55 loc) · 1.7 KB
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
/**
* Lambda function to support JWT.
* Used for authenticating API requests for API Gateway
* as a custom authorizor:
*
* @see https://jwt.io/introduction/
* @see http://docs.aws.amazon.com/apigateway/latest/developerguide/use-custom-authorizer.html
* @author Chris Moyer <cmoyer@aci.info>
*/
var jwt = require('jsonwebtoken');
var fs = require('fs');
var cert = fs.readFileSync('cert.pem');
function generatePolicyDocument(principalId, effect, resource) {
var authResponse = {};
authResponse.principalId = principalId;
if (effect && resource) {
var policyDocument = {};
policyDocument.Version = '2012-10-17'; // default version
policyDocument.Statement = [];
var statementOne = {};
statementOne.Action = 'execute-api:Invoke'; // default action
statementOne.Effect = effect;
statementOne.Resource = resource;
policyDocument.Statement[0] = statementOne;
authResponse.policyDocument = policyDocument;
}
return authResponse;
}
/**
* Handle requests from API Gateway
* "event" is an object with an "authorizationToken"
*/
exports.handler = function jwtHandler(event, context){
var token = event.authorizationToken.split(' ');
if(token[0] === 'Bearer'){
// Token-based re-authorization
// Verify
jwt.verify(token[1], cert, function(err, data){
if(err){
console.log('Verification Failure', err);
context.fail('Unauthorized');
} else if (data && data.id){
console.log('LOGIN', data);
context.succeed(generatePolicyDocument(data.id, 'Allow', event.methodArn));
} else {
console.log('Invalid User', data);
context.fail('Unauthorized');
}
});
} else {
// Require a "Bearer" token
console.log('Wrong token type', token[0]);
context.fail('Unauthorized');
}
};