-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauthorizer.js
472 lines (413 loc) · 15.5 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
'use strict';
var rfr = require('rfr');
var logger = rfr('/logger');
var config = rfr('/config');
var jwt = require('jsonwebtoken');
var request = require('request');
var jwkToPem = require('jwk-to-pem');
var env = rfr('/environment');
var PEMS = null;
console.log('Loading function');
/**
* AuthPolicy receives a set of allowed and denied methods and generates a valid
* AWS policy for the API Gateway authorizer. The constructor receives the calling
* user principal, the AWS account ID of the API owner, and an apiOptions object.
* The apiOptions can contain an API Gateway RestApi Id, a region for the RestApi, and a
* stage that calls should be allowed/denied for. For example
* {
* restApiId: 'xxxxxxxxxx,
* region: 'us-east-1,
* stage: 'dev',
* }
*
* const testPolicy = new AuthPolicy("[principal user identifier]", "[AWS account id]", apiOptions);
* testPolicy.allowMethod(AuthPolicy.HttpVerb.GET, "/users/username");
* testPolicy.denyMethod(AuthPolicy.HttpVerb.POST, "/pets");
* callback(null, testPolicy.build());
*
* @class AuthPolicy
* @constructor
*/
function AuthPolicy(principal, awsAccountId, apiOptions) {
/**
* The AWS account id the policy will be generated for. This is used to create
* the method ARNs.
*
* @property awsAccountId
* @type {String}
*/
this.awsAccountId = awsAccountId;
/**
* The principal used for the policy, this should be a unique identifier for
* the end user.
*
* @property principalId
* @type {String}
*/
this.principalId = principal;
/**
* The policy version used for the evaluation. This should always be "2012-10-17"
*
* @property version
* @type {String}
* @default "2012-10-17"
*/
this.version = '2012-10-17';
/**
* The regular expression used to validate resource paths for the policy
*
* @property pathRegex
* @type {RegExp}
* @default '^\/[/.a-zA-Z0-9-\*]+$'
*/
this.pathRegex = new RegExp('^[/.a-zA-Z0-9-\*]+$');
// These are the internal lists of allowed and denied methods. These are lists
// of objects and each object has two properties: a resource ARN and a nullable
// conditions statement. The build method processes these lists and generates
// the appropriate statements for the final policy.
this.allowMethods = [];
this.denyMethods = [];
if (!apiOptions || !apiOptions.restApiId) {
this.restApiId = '*';
} else {
this.restApiId = apiOptions.restApiId;
}
if (!apiOptions || !apiOptions.region) {
this.region = '*';
} else {
this.region = apiOptions.region;
}
if (!apiOptions || !apiOptions.stage) {
this.stage = '*';
} else {
this.stage = apiOptions.stage;
}
}
/**
* A set of existing HTTP verbs supported by API Gateway. This property is here
* only to avoid spelling mistakes in the policy.
*
* @property HttpVerb
* @type {Object}
*/
AuthPolicy.HttpVerb = {
GET: 'GET',
POST: 'POST',
PUT: 'PUT',
PATCH: 'PATCH',
HEAD: 'HEAD',
DELETE: 'DELETE',
OPTIONS: 'OPTIONS',
ALL: '*',
};
AuthPolicy.prototype = (function AuthPolicyClass() {
/**
* Adds a method to the internal lists of allowed or denied methods. Each object in
* the internal list contains a resource ARN and a condition statement. The condition
* statement can be null.
*
* @method addMethod
* @param {String} The effect for the policy. This can only be "Allow" or "Deny".
* @param {String} The HTTP verb for the method, this should ideally come from the
* AuthPolicy.HttpVerb object to avoid spelling mistakes
* @param {String} The resource path. For example "/pets"
* @param {Object} The conditions object in the format specified by the AWS docs.
* @return {void}
*/
function addMethod(effect, verb, resource, conditions) {
if (verb !== '*' && !Object.prototype.hasOwnProperty.call(AuthPolicy.HttpVerb, verb)) {
throw new Error(`Invalid HTTP verb ${verb}. Allowed verbs in AuthPolicy.HttpVerb`);
}
if (!this.pathRegex.test(resource)) {
throw new Error(`Invalid resource path: ${resource}. Path should match ${this.pathRegex}`);
}
let cleanedResource = resource;
if (resource.substring(0, 1) === '/') {
cleanedResource = resource.substring(1, resource.length);
}
const resourceArn = `arn:aws:execute-api:${this.region}:${this.awsAccountId}:${this.restApiId}/${this.stage}/${verb}/${cleanedResource}`;
if (effect.toLowerCase() === 'allow') {
this.allowMethods.push({
resourceArn,
conditions,
});
} else if (effect.toLowerCase() === 'deny') {
this.denyMethods.push({
resourceArn,
conditions,
});
}
}
/**
* Returns an empty statement object prepopulated with the correct action and the
* desired effect.
*
* @method getEmptyStatement
* @param {String} The effect of the statement, this can be "Allow" or "Deny"
* @return {Object} An empty statement object with the Action, Effect, and Resource
* properties prepopulated.
*/
function getEmptyStatement(effect) {
const statement = {};
statement.Action = 'execute-api:Invoke';
statement.Effect = effect.substring(0, 1).toUpperCase() + effect.substring(1, effect.length).toLowerCase();
statement.Resource = [];
return statement;
}
/**
* This function loops over an array of objects containing a resourceArn and
* conditions statement and generates the array of statements for the policy.
*
* @method getStatementsForEffect
* @param {String} The desired effect. This can be "Allow" or "Deny"
* @param {Array} An array of method objects containing the ARN of the resource
* and the conditions for the policy
* @return {Array} an array of formatted statements for the policy.
*/
function getStatementsForEffect(effect, methods) {
const statements = [];
if (methods.length > 0) {
const statement = getEmptyStatement(effect);
for (let i = 0; i < methods.length; i++) {
const curMethod = methods[i];
if (curMethod.conditions === null || curMethod.conditions.length === 0) {
statement.Resource.push(curMethod.resourceArn);
} else {
const conditionalStatement = getEmptyStatement(effect);
conditionalStatement.Resource.push(curMethod.resourceArn);
conditionalStatement.Condition = curMethod.conditions;
statements.push(conditionalStatement);
}
}
if (statement.Resource !== null && statement.Resource.length > 0) {
statements.push(statement);
}
}
return statements;
}
return {
constructor: AuthPolicy,
/**
* Adds an allow "*" statement to the policy.
*
* @method allowAllMethods
*/
allowAllMethods() {
addMethod.call(this, 'allow', '*', '*', null);
},
/**
* Adds a deny "*" statement to the policy.
*
* @method denyAllMethods
*/
denyAllMethods() {
addMethod.call(this, 'deny', '*', '*', null);
},
/**
* Adds an API Gateway method (Http verb + Resource path) to the list of allowed
* methods for the policy
*
* @method allowMethod
* @param {String} The HTTP verb for the method, this should ideally come from the
* AuthPolicy.HttpVerb object to avoid spelling mistakes
* @param {string} The resource path. For example "/pets"
* @return {void}
*/
allowMethod(verb, resource) {
addMethod.call(this, 'allow', verb, resource, null);
},
/**
* Adds an API Gateway method (Http verb + Resource path) to the list of denied
* methods for the policy
*
* @method denyMethod
* @param {String} The HTTP verb for the method, this should ideally come from the
* AuthPolicy.HttpVerb object to avoid spelling mistakes
* @param {string} The resource path. For example "/pets"
* @return {void}
*/
denyMethod(verb, resource) {
addMethod.call(this, 'deny', verb, resource, null);
},
/**
* Adds an API Gateway method (Http verb + Resource path) to the list of allowed
* methods and includes a condition for the policy statement. More on AWS policy
* conditions here: http://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies_elements.html#Condition
*
* @method allowMethodWithConditions
* @param {String} The HTTP verb for the method, this should ideally come from the
* AuthPolicy.HttpVerb object to avoid spelling mistakes
* @param {string} The resource path. For example "/pets"
* @param {Object} The conditions object in the format specified by the AWS docs
* @return {void}
*/
allowMethodWithConditions(verb, resource, conditions) {
addMethod.call(this, 'allow', verb, resource, conditions);
},
/**
* Adds an API Gateway method (Http verb + Resource path) to the list of denied
* methods and includes a condition for the policy statement. More on AWS policy
* conditions here: http://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies_elements.html#Condition
*
* @method denyMethodWithConditions
* @param {String} The HTTP verb for the method, this should ideally come from the
* AuthPolicy.HttpVerb object to avoid spelling mistakes
* @param {string} The resource path. For example "/pets"
* @param {Object} The conditions object in the format specified by the AWS docs
* @return {void}
*/
denyMethodWithConditions(verb, resource, conditions) {
addMethod.call(this, 'deny', verb, resource, conditions);
},
/**
* Generates the policy document based on the internal lists of allowed and denied
* conditions. This will generate a policy with two main statements for the effect:
* one statement for Allow and one statement for Deny.
* Methods that includes conditions will have their own statement in the policy.
*
* @method build
* @return {Object} The policy object that can be serialized to JSON.
*/
build() {
if ((!this.allowMethods || this.allowMethods.length === 0) &&
(!this.denyMethods || this.denyMethods.length === 0)) {
throw new Error('No statements defined for the policy');
}
const policy = {};
policy.principalId = this.principalId;
const doc = {};
doc.Version = this.version;
doc.Statement = [];
doc.Statement = doc.Statement.concat(getStatementsForEffect.call(this, 'Allow', this.allowMethods));
doc.Statement = doc.Statement.concat(getStatementsForEffect.call(this, 'Deny', this.denyMethods));
policy.policyDocument = doc;
return policy;
},
};
}());
function processAuthRequest(event, tokenIssuer, awsAccountId, apiOptions, callback) {
var token = event.authorizationToken;
//Fail if the token is not jwt
var decodedJwt = jwt.decode(token, {complete: true});
if (!decodedJwt) {
let policy = new AuthPolicy('', awsAccountId, apiOptions);
logger.info("Not valid JWT token, returning deny all policy");
policy.denyAllMethods();
let iamPolicy = policy.build();
callback(null, iamPolicy);
return;
}
//Fail if token is not from your User Pool
if (decodedJwt.payload['iss'] != tokenIssuer) {
logger.info("Provided Token not from UserPool, returning deny all policy");
let policy = new AuthPolicy('', awsAccountId, apiOptions);
policy.denyAllMethods();
let iamPolicy = policy.build();
callback(null, iamPolicy);
return;
}
//Reject the jwt if it's not an 'Identity Token'
if (decodedJwt.payload['token_use'] != 'id') {
console.log("Not an Identity token");
logger.info("Provided Token is not and identity token, returning deny all policy");
let policy = new AuthPolicy('', awsAccountId, apiOptions);
policy.denyAllMethods();
let iamPolicy = policy.build();
callback(null, iamPolicy);
return;
}
//Get the kid from the token and retrieve corresponding PEM
var kid = decodedJwt.header.kid;
var pem = PEMS[kid];
if (!pem) {
logger.info("Invalid Identity token, returning deny all policy");
let policy = new AuthPolicy('', awsAccountId, apiOptions);
policy.denyAllMethods();
let iamPolicy = policy.build();
callback(null, iamPolicy);
return;
}
//Verify the signature of the JWT token to ensure it's really coming from your User Pool
jwt.verify(token, pem, {issuer: tokenIssuer}, function (err, payload) {
if (err) {
logger.info("Error while trying to verify the Token, returning deny-all policy");
let policy = new AuthPolicy('', awsAccountId, apiOptions);
policy.denyAllMethods();
let iamPolicy = policy.build();
callback(null, iamPolicy);
} else {
//Valid token. Generate the API Gateway policy for the user
//Always generate the policy on value of 'sub' claim and not for
// 'username' because username is reassignable
//sub is UUID for a user which is never reassigned to another user.
let admin = null;
const pId = payload.sub;
let policy = new AuthPolicy(pId, awsAccountId, apiOptions);
policy.allowAllMethods();
//Check the Cognito group entry for Admin.
//Assuming here that the Admin group has always higher
//precedence
if (payload['cognito:groups'] &&
payload['cognito:groups'][0] === 'adminGroup') {
admin = true;
}
if (!admin) {
policy.denyMethod(AuthPolicy.HttpVerb.DELETE, '/locations');
policy.denyMethod(AuthPolicy.HttpVerb.DELETE, '/locations/*');
policy.denyMethod(AuthPolicy.HttpVerb.POST, '/locations');
policy.denyMethod(AuthPolicy.HttpVerb.POST, '/locations/*');
}
let iamPolicy = policy.build();
logger.info('Generated IAM Policy', iamPolicy);
logger.info('Effective IAM statement', iamPolicy.policyDocument.Statement);
callback(null, iamPolicy);
}
});
}
function toPem(keyDictionary) {
var modulus = keyDictionary.n;
var exponent = keyDictionary.e;
var key_type = keyDictionary.kty;
var jwk = {kty: key_type, n: modulus, e: exponent};
var pem = jwkToPem(jwk);
return pem;
}
exports.Custom = (event, context, callback) => {
const apiOptions = {};
const tmp = event.methodArn.split(':');
const apiGatewayArnTmp = tmp[5].split('/');
const awsAccountId = tmp[4];
apiOptions.region = tmp[3];
apiOptions.restApiId = apiGatewayArnTmp[0];
apiOptions.stage = apiGatewayArnTmp[1];
let userPoolURI = 'https://cognito-idp.' + config.AWS_REGION
+ '.amazonaws.com/' + env.config.USER_POOL_ID;
let jwtKeySetURI = userPoolURI + '/.well-known/jwks.json';
logger.info("Requesting keys from "+jwtKeySetURI);
if (!PEMS) {
request({url: jwtKeySetURI, json: true},
function (error, response, body) {
if (!error && response.statusCode === 200) {
PEMS = {};
var keys = body['keys'];
for (let keyIndex = 0; keyIndex < keys.length; keyIndex++) {
var kid = keys[keyIndex].kid;
PEMS[kid] = toPem(keys[keyIndex]);
}
processAuthRequest(event, userPoolURI, awsAccountId, apiOptions, callback);
} else {
logger.info("Failed to retrieve the keys from " +
"the well known user-pool URI, ");
logger.info('Error-Code: ', response.statusCode);
logger.info(error);
let policy = new AuthPolicy('', awsAccountId, apiOptions);
policy.denyAllMethods();
let iamPolicy = policy.build();
callback(null, iamPolicy);
}
}
);
} else {
processAuthRequest(event, userPoolURI, awsAccountId, apiOptions, callback);
}
};