-
Notifications
You must be signed in to change notification settings - Fork 0
/
authorizer.js
46 lines (36 loc) · 1.07 KB
/
authorizer.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
const jwt = require('jsonwebtoken')
const JWT_SECRET = 'rocketseat-api-secret'
module.exports.handler = async (event) => {
const authorization = event.authorizationToken
const methodArn = event.methodArn
if (!authorization) {
return generateAuthResponse('Deny', methodArn)
}
try {
const token = authorization.replace('Bearer ', '')
const decoded = jwt.verify(token, JWT_SECRET)
console.log('Allow', decoded)
return generateAuthResponse('Allow', methodArn)
} catch (err) {
return generateAuthResponse('Deny', methodArn)
}
function generateAuthResponse (effect, methodArn) {
const policyDocument = generatePolicyDocument(effect, methodArn)
return {
principalId: 'lambda-authorizer',
policyDocument,
}
}
function generatePolicyDocument (effect, methodArn) {
if (!effect || !methodArn) return null
const policyDocument = {
Version: '2012-10-17',
Statement: [{
Action: 'execute-api:invoke',
Effect: effect,
Resouce: methodArn,
}],
}
return policyDocument
}
}