|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const getObjectPath = require('./utils').getObjectPath; |
| 4 | + |
| 5 | +module.exports.authorizerValidationCallback = function( |
| 6 | + identitySource, |
| 7 | + identityValidationRegExp, |
| 8 | + { |
| 9 | + regionId, |
| 10 | + accountId, |
| 11 | + apiId, |
| 12 | + } |
| 13 | +) { |
| 14 | + return function(req, res, next) { |
| 15 | + const sources = { |
| 16 | + method: { |
| 17 | + request: { |
| 18 | + header: req.headers, |
| 19 | + }, |
| 20 | + }, |
| 21 | + }; |
| 22 | + const source = getObjectPath( |
| 23 | + (identitySource || 'method.request.header.Auth').toLowerCase(), |
| 24 | + sources |
| 25 | + ); |
| 26 | + if (identityValidationRegExp && identityValidationRegExp.exec(source) === null) { |
| 27 | + res.sendStatus(403); |
| 28 | + return; |
| 29 | + } |
| 30 | + const method = req.method.toUpperCase(); |
| 31 | + const resourcePath = req.path; |
| 32 | + req.lambda = (req.lambda || {}); |
| 33 | + req.lambda.authorizer = { |
| 34 | + event: { |
| 35 | + type: 'TOKEN', |
| 36 | + authorizationToken: source, |
| 37 | + methodArn: `arn:serverlessify:execute-api:${regionId}:${accountId}:${apiId}/${method}${resourcePath}`, |
| 38 | + }, |
| 39 | + }; |
| 40 | + next(); |
| 41 | + }; |
| 42 | +}; |
| 43 | + |
| 44 | +// event format |
| 45 | +// { |
| 46 | +// "type":"TOKEN", |
| 47 | +// "authorizationToken":"<caller-supplied-token>", |
| 48 | +// "methodArn":"arn:aws:execute-api:<regionId>:<accountId>:<apiId>/<stage>/<method>/<resourcePath>" |
| 49 | +// } |
| 50 | +// awsAuthDocument format |
| 51 | +// { |
| 52 | +// // The principal user identification associated with the token send by the client. |
| 53 | +// "principalId": "xxxxxxxx", |
| 54 | +// "policyDocument": { |
| 55 | +// "Version": "2012-10-17", |
| 56 | +// "Statement": [ |
| 57 | +// { |
| 58 | +// "Action": "execute-api:Invoke", |
| 59 | +// "Effect": "Allow|Deny", |
| 60 | +// "Resource": "arn:aws:execute-api:<regionId>:<accountId>:<appId>/<stage>/<httpVerb>/[<resource>/<httpVerb>/[...]]" |
| 61 | +// } |
| 62 | +// ] |
| 63 | +// } |
| 64 | +// } |
| 65 | +module.exports.authorizerCheckCallback = function( |
| 66 | + authorizer, |
| 67 | + setCacheEntry, |
| 68 | + getCacheEntry |
| 69 | +) { |
| 70 | + return function(req, res, next) { |
| 71 | + if (!authorizer) { |
| 72 | + return next(); |
| 73 | + } |
| 74 | + |
| 75 | + function authorizerCallback(err, awsAuthDocument) { |
| 76 | + // TODO addCacheEntry(req.lambda.authorizer.event.authorizationToken, awsAuthDocument).then |
| 77 | + if (err) { |
| 78 | + res.status(500).send(err); |
| 79 | + } else { |
| 80 | + // TODO consider other statement values |
| 81 | + const effect = getObjectPath( |
| 82 | + ['policyDocument', 'Statement', 0, 'Effect'], |
| 83 | + awsAuthDocument |
| 84 | + ); |
| 85 | + if (effect === 'Allow') { |
| 86 | + next(); |
| 87 | + } else { |
| 88 | + res.sendStatus(401); |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + const cacheKey = req.lambda.authorizer.event.authorizationToken; |
| 94 | + getCacheEntry( |
| 95 | + cacheKey, |
| 96 | + function(err, cachedAuthorization) { |
| 97 | + if (cachedAuthorization) { |
| 98 | + authorizerCallback(err, cachedAuthorization); |
| 99 | + } else { |
| 100 | + authorizer( |
| 101 | + req.lambda.authorizer.event, |
| 102 | + req.lambda.context, |
| 103 | + function(innerErr, awsAuthDocument) { |
| 104 | + setCacheEntry({ |
| 105 | + key: cacheKey, |
| 106 | + value: awsAuthDocument, |
| 107 | + }, function() { |
| 108 | + authorizerCallback(innerErr, awsAuthDocument); |
| 109 | + }); |
| 110 | + } |
| 111 | + ); |
| 112 | + } |
| 113 | + } |
| 114 | + ); |
| 115 | + }; |
| 116 | +}; |
0 commit comments