-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauthorizer.js
44 lines (41 loc) · 1.14 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
/**
* Implements basic auth
*/
module.exports.auth = (event, context, callback) => {
try {
// Grab the header
const authorizationHeader = event.headers.Authorization || event.headers.authorization
const authUser = process.env.USERNAME // Get creds from env
const authPass = process.env.PASSWORD
// Let's build the auth string
const authString = 'Basic ' + new Buffer(authUser + ':' + authPass).toString('base64')
if (authString === authorizationHeader) {
return callback(null, {
principalId: authUser,
policyDocument: {
Version: '2012-10-17',
Statement: [
{
Action: 'execute-api:Invoke',
Effect: 'Allow', // Allow!
Resource: event.methodArn,
},
],
},
context: {}
})
} else {
return context.fail("Unauthorized") // Booh!
}
} catch (error) {
console.error(error)
return {
statusCode: 500,
body: JSON.stringify({
statusCode: 500,
message: 'Something went wrong trying to authorize',
requestId: context.awsRequestId
})
}
}
};