|
1 | | -'use strict'; |
2 | | - |
3 | 1 | require('dotenv').config({ silent: true }); |
4 | | -var jwksClient = require('jwks-rsa'); |
5 | | -var jwt = require('jsonwebtoken'); |
6 | | - |
7 | | -var getPolicyDocument = function (effect, resource) { |
8 | 2 |
|
9 | | - var policyDocument = {}; |
10 | | - policyDocument.Version = '2012-10-17'; // default version |
11 | | - policyDocument.Statement = []; |
12 | | - var statementOne = {}; |
13 | | - statementOne.Action = 'execute-api:Invoke'; // default action |
14 | | - statementOne.Effect = effect; |
15 | | - statementOne.Resource = resource; |
16 | | - policyDocument.Statement[0] = statementOne; |
| 3 | +const jwksClient = require('jwks-rsa'); |
| 4 | +const jwt = require('jsonwebtoken'); |
| 5 | +const util = require('util'); |
| 6 | + |
| 7 | +const getPolicyDocument = (effect, resource) => { |
| 8 | + const policyDocument = { |
| 9 | + Version: '2012-10-17', // default version |
| 10 | + Statement: [{ |
| 11 | + Action: 'execute-api:Invoke', // default action |
| 12 | + Effect: effect, |
| 13 | + Resource: resource, |
| 14 | + }] |
| 15 | + }; |
17 | 16 | return policyDocument; |
18 | 17 | } |
19 | 18 |
|
20 | 19 |
|
21 | 20 | // extract and return the Bearer Token from the Lambda event parameters |
22 | | -var getToken = function (params) { |
23 | | - var token; |
24 | | - |
| 21 | +const getToken = (params) => { |
25 | 22 | if (!params.type || params.type !== 'TOKEN') { |
26 | | - throw new Error("Expected 'event.type' parameter to have value TOKEN"); |
| 23 | + throw new Error('Expected "event.type" parameter to have value "TOKEN"'); |
27 | 24 | } |
28 | 25 |
|
29 | | - var tokenString = params.authorizationToken; |
| 26 | + const tokenString = params.authorizationToken; |
30 | 27 | if (!tokenString) { |
31 | | - throw new Error("Expected 'event.authorizationToken' parameter to be set"); |
| 28 | + throw new Error('Expected "event.authorizationToken" parameter to be set'); |
32 | 29 | } |
33 | 30 |
|
34 | | - var match = tokenString.match(/^Bearer (.*)$/); |
| 31 | + const match = tokenString.match(/^Bearer (.*)$/); |
35 | 32 | if (!match || match.length < 2) { |
36 | | - throw new Error("Invalid Authorization token - '" + tokenString + "' does not match 'Bearer .*'"); |
| 33 | + throw new Error(`Invalid Authorization token - ${tokenString} does not match "Bearer .*"`); |
37 | 34 | } |
38 | 35 | return match[1]; |
39 | 36 | } |
40 | 37 |
|
41 | | -module.exports.authenticate = function (params, cb) { |
| 38 | +const jwtOptions = { |
| 39 | + audience: process.env.AUDIENCE, |
| 40 | + issuer: process.env.TOKEN_ISSUER |
| 41 | +}; |
| 42 | + |
| 43 | +module.exports.authenticate = (params) => { |
42 | 44 | console.log(params); |
43 | | - var token = getToken(params); |
| 45 | + const token = getToken(params); |
44 | 46 |
|
45 | | - var client = jwksClient({ |
| 47 | + const decoded = jwt.decode(token, { complete: true }); |
| 48 | + if (!decoded || !decoded.header || !decoded.header.kid) { |
| 49 | + throw new Error('invalid token'); |
| 50 | + } |
| 51 | + |
| 52 | + const client = jwksClient({ |
46 | 53 | cache: true, |
47 | 54 | rateLimit: true, |
48 | 55 | jwksRequestsPerMinute: 10, // Default value |
49 | 56 | jwksUri: process.env.JWKS_URI |
50 | 57 | }); |
51 | 58 |
|
52 | | - var decoded = jwt.decode(token, { complete: true }); |
53 | | - var kid = decoded.header.kid; |
54 | | - client.getSigningKey(kid, function (err, key) { |
55 | | - if(err) |
56 | | - { |
57 | | - cb(err); |
58 | | - } |
59 | | - else |
60 | | - { |
61 | | - var signingKey = key.publicKey || key.rsaPublicKey; |
62 | | - jwt.verify(token, signingKey, { audience: process.env.AUDIENCE, issuer: process.env.TOKEN_ISSUER }, |
63 | | - function (err, decoded) { |
64 | | - if (err) { |
65 | | - cb(err); |
66 | | - |
67 | | - } |
68 | | - else { |
69 | | - |
70 | | - cb(null, { |
71 | | - principalId: decoded.sub, |
72 | | - policyDocument: getPolicyDocument('Allow', params.methodArn), |
73 | | - context: { |
74 | | - scope: decoded.scope |
75 | | - } |
76 | | - }); |
77 | | - } |
78 | | - }); |
79 | | - } |
80 | | - |
81 | | - }); |
82 | | - |
83 | | - |
84 | | - |
| 59 | + const getSigningKey = util.promisify(client.getSigningKey); |
| 60 | + return getSigningKey(decoded.header.kid) |
| 61 | + .then((key) => { |
| 62 | + const signingKey = key.publicKey || key.rsaPublicKey; |
| 63 | + return jwt.verify(token, signingKey, jwtOptions); |
| 64 | + }) |
| 65 | + .then((decoded)=> ({ |
| 66 | + principalId: decoded.sub, |
| 67 | + policyDocument: getPolicyDocument('Allow', params.methodArn), |
| 68 | + context: { scope: decoded.scope } |
| 69 | + })); |
85 | 70 | } |
0 commit comments