forked from davidgf/serverless-http-basic-auth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauthorizer.js
43 lines (36 loc) · 1.19 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
exports.handler = function (event, context, callback) {
var authorizationHeader = event.headers.Authorization
if (!authorizationHeader) return callback('Unauthorized')
var encodedCreds = authorizationHeader.split(' ')[1]
var plainCreds = (new Buffer(encodedCreds, 'base64')).toString().split(':')
var username = plainCreds[0]
var password = plainCreds[1]
if (!(username === 'admin' && password === 'secret')) return callback('Unauthorized')
var authResponse = buildAllowAllPolicy(event, username)
callback(null, authResponse)
}
function buildAllowAllPolicy (event, principalId) {
var apiOptions = {}
var tmp = event.methodArn.split(':')
var apiGatewayArnTmp = tmp[5].split('/')
var awsAccountId = tmp[4]
var awsRegion = tmp[3]
var restApiId = apiGatewayArnTmp[0]
var stage = apiGatewayArnTmp[1]
var apiArn = 'arn:aws:execute-api:' + awsRegion + ':' + awsAccountId + ':' +
restApiId + '/' + stage + '/*/*'
const policy = {
principalId: principalId,
policyDocument: {
Version: '2012-10-17',
Statement: [
{
Action: 'execute-api:Invoke',
Effect: 'Allow',
Resource: [apiArn]
}
]
}
}
return policy
}