|
1 | | -const jwt = require('jsonwebtoken') |
| 1 | +const jwt = require('jsonwebtoken'); |
2 | 2 |
|
3 | 3 | // Set in `enviroment` of serverless.yml |
4 | | -const AUTH0_CLIENT_ID = process.env.AUTH0_CLIENT_ID |
5 | | -const AUTH0_CLIENT_SECRET = process.env.AUTH0_CLIENT_SECRET |
| 4 | +const AUTH0_CLIENT_ID = process.env.AUTH0_CLIENT_ID; |
| 5 | +const AUTH0_CLIENT_PUBLIC_KEY = process.env.AUTH0_CLIENT_PUBLIC_KEY; |
6 | 6 |
|
7 | 7 | // Policy helper function |
8 | 8 | const generatePolicy = (principalId, effect, resource) => { |
9 | | - const authResponse = {} |
10 | | - authResponse.principalId = principalId |
| 9 | + const authResponse = {}; |
| 10 | + authResponse.principalId = principalId; |
11 | 11 | if (effect && resource) { |
12 | | - const policyDocument = {} |
13 | | - policyDocument.Version = '2012-10-17' |
14 | | - policyDocument.Statement = [] |
15 | | - const statementOne = {} |
16 | | - statementOne.Action = 'execute-api:Invoke' |
17 | | - statementOne.Effect = effect |
18 | | - statementOne.Resource = resource |
19 | | - policyDocument.Statement[0] = statementOne |
20 | | - authResponse.policyDocument = policyDocument |
| 12 | + const policyDocument = {}; |
| 13 | + policyDocument.Version = '2012-10-17'; |
| 14 | + policyDocument.Statement = []; |
| 15 | + const statementOne = {}; |
| 16 | + statementOne.Action = 'execute-api:Invoke'; |
| 17 | + statementOne.Effect = effect; |
| 18 | + statementOne.Resource = resource; |
| 19 | + policyDocument.Statement[0] = statementOne; |
| 20 | + authResponse.policyDocument = policyDocument; |
21 | 21 | } |
22 | | - return authResponse |
23 | | -} |
| 22 | + return authResponse; |
| 23 | +}; |
24 | 24 |
|
25 | 25 | // Reusable Authorizer function, set on `authorizer` field in serverless.yml |
26 | 26 | module.exports.auth = (event, context, callback) => { |
27 | | - console.log('event', event) |
| 27 | + console.log('event', event); |
28 | 28 | if (!event.authorizationToken) { |
29 | | - return callback('Unauthorized') |
| 29 | + return callback('Unauthorized'); |
30 | 30 | } |
31 | 31 |
|
32 | | - const tokenParts = event.authorizationToken.split(' ') |
33 | | - const tokenValue = tokenParts[1] |
| 32 | + const tokenParts = event.authorizationToken.split(' '); |
| 33 | + const tokenValue = tokenParts[1]; |
34 | 34 |
|
35 | 35 | if (!(tokenParts[0].toLowerCase() === 'bearer' && tokenValue)) { |
36 | 36 | // no auth token! |
37 | | - return callback('Unauthorized') |
| 37 | + return callback('Unauthorized'); |
38 | 38 | } |
39 | 39 | const options = { |
40 | 40 | audience: AUTH0_CLIENT_ID, |
41 | | - } |
42 | | - // decode base64 secret. ref: http://bit.ly/2hA6CrO |
43 | | - const secret = new Buffer.from(AUTH0_CLIENT_SECRET, 'base64') |
| 41 | + }; |
| 42 | + |
44 | 43 | try { |
45 | | - jwt.verify(tokenValue, secret, options, (verifyError, decoded) => { |
| 44 | + jwt.verify(tokenValue, AUTH0_CLIENT_PUBLIC_KEY, options, (verifyError, decoded) => { |
46 | 45 | if (verifyError) { |
47 | | - console.log('verifyError', verifyError) |
| 46 | + console.log('verifyError', verifyError); |
48 | 47 | // 401 Unauthorized |
49 | | - console.log(`Token invalid. ${verifyError}`) |
50 | | - return callback('Unauthorized') |
| 48 | + console.log(`Token invalid. ${verifyError}`); |
| 49 | + return callback('Unauthorized'); |
51 | 50 | } |
52 | 51 | // is custom authorizer function |
53 | | - console.log('valid from customAuthorizer', decoded) |
54 | | - return callback(null, generatePolicy(decoded.sub, 'Allow', event.methodArn)) |
55 | | - }) |
56 | | - } catch (err) { |
57 | | - console.log('catch error. Invalid token', err) |
58 | | - return callback('Unauthorized') |
| 52 | + console.log('valid from customAuthorizer', decoded); |
| 53 | + return callback(null, generatePolicy(decoded.sub, 'Allow', event.methodArn)); |
| 54 | + }); |
| 55 | + } catch (err) { |
| 56 | + console.log('catch error. Invalid token', err); |
| 57 | + return callback('Unauthorized'); |
59 | 58 | } |
60 | | -} |
| 59 | + |
| 60 | + // if for any reason you get here... |
| 61 | + return callback('Unauthorized'); |
| 62 | +}; |
61 | 63 |
|
62 | 64 | // Public API |
63 | | -module.exports.publicEndpoint = (event, context, callback) => { |
64 | | - return callback(null, { |
65 | | - statusCode: 200, |
66 | | - headers: { |
| 65 | +module.exports.publicEndpoint = (event, context, callback) => callback(null, { |
| 66 | + statusCode: 200, |
| 67 | + headers: { |
67 | 68 | /* Required for CORS support to work */ |
68 | | - "Access-Control-Allow-Origin": "*", |
| 69 | + 'Access-Control-Allow-Origin': '*', |
69 | 70 | /* Required for cookies, authorization headers with HTTPS */ |
70 | | - "Access-Control-Allow-Credentials": true |
71 | | - }, |
72 | | - body: JSON.stringify({ |
73 | | - message: 'Hi ⊂◉‿◉つ from Public API', |
74 | | - }), |
75 | | - }) |
76 | | -} |
| 71 | + 'Access-Control-Allow-Credentials': true, |
| 72 | + }, |
| 73 | + body: JSON.stringify({ |
| 74 | + message: 'Hi ⊂◉‿◉つ from Public API', |
| 75 | + }), |
| 76 | +}); |
77 | 77 |
|
78 | 78 | // Private API |
79 | | -module.exports.privateEndpoint = (event, context, callback) => { |
80 | | - return callback(null, { |
81 | | - statusCode: 200, |
82 | | - headers: { |
| 79 | +module.exports.privateEndpoint = (event, context, callback) => callback(null, { |
| 80 | + statusCode: 200, |
| 81 | + headers: { |
83 | 82 | /* Required for CORS support to work */ |
84 | | - "Access-Control-Allow-Origin": "*", |
| 83 | + 'Access-Control-Allow-Origin': '*', |
85 | 84 | /* Required for cookies, authorization headers with HTTPS */ |
86 | | - "Access-Control-Allow-Credentials": true |
87 | | - }, |
88 | | - body: JSON.stringify({ |
89 | | - message: 'Hi ⊂◉‿◉つ from Private API. Only logged in users can see this', |
90 | | - }), |
91 | | - }) |
92 | | -} |
| 85 | + 'Access-Control-Allow-Credentials': true, |
| 86 | + }, |
| 87 | + body: JSON.stringify({ |
| 88 | + message: 'Hi ⊂◉‿◉つ from Private API. Only logged in users can see this', |
| 89 | + }), |
| 90 | +}); |
0 commit comments