Skip to content

Commit 5383dcf

Browse files
author
Vasileios Lekakis (lex)
committed
fixing comments from pull request
1 parent 21f0755 commit 5383dcf

File tree

8 files changed

+140
-64
lines changed

8 files changed

+140
-64
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ app/platforms/
1111
app/plugins/
1212
app/src/assets/vendor/bower_components/
1313
app/www/assets
14+
app/www/build/
1415
api/dist/
1516
api/lambda/node_modules/
1617
api/later/

README.md

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -67,15 +67,8 @@ The framework relies on [Node.js] and [npm].
6767
cd ..
6868
gulp deploy
6969

70-
# Generate some sample data
71-
gulp generate_sample_users
72-
gulp generate_sample_data
73-
74-
# Generate Cognito User Groups
75-
gulp generate_sample_groups
76-
77-
# Assign user to groups
78-
gulp assign_users_to_cognito_user_groups
70+
# Bootstrap your application with Sample data
71+
gulp bootstrap
7972

8073
----
8174

api/gulpfile.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,16 @@ gulp.task('deploy', gulp.series(
176176
'create_cognito_pools',
177177
'deploy_lambda',
178178
'deploy_api',
179-
'generate_client_config'
179+
'generate_client_config',
180+
'generate_sample_data'
181+
));
182+
183+
gulp.task('bootstrap', gulp.series(
184+
'deploy',
185+
'generate_sample_users',
186+
'generate_sample_groups',
187+
'sleep',
188+
'assign_users_to_cognito_user_groups'
180189
));
181190

182191
gulp.task('undeploy', gulp.series(
@@ -187,3 +196,4 @@ gulp.task('undeploy', gulp.series(
187196
'delete_cloudwatch_logs'
188197
));
189198

199+
gulp.task('default', gulp.series('bootstrap'));

api/lambda/authorizer.js

Lines changed: 45 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -320,27 +320,52 @@ AuthPolicy.prototype = (function AuthPolicyClass() {
320320

321321

322322
function processAuthRequest(event, tokenIssuer, callback) {
323+
324+
const apiOptions = {};
325+
const tmp = event.methodArn.split(':');
326+
const apiGatewayArnTmp = tmp[5].split('/');
327+
const awsAccountId = tmp[4];
328+
329+
apiOptions.region = tmp[3];
330+
apiOptions.restApiId = apiGatewayArnTmp[0];
331+
apiOptions.stage = apiGatewayArnTmp[1];
332+
333+
334+
335+
336+
337+
323338
var token = event.authorizationToken;
324339

325340
//Fail if the token is not jwt
326341
var decodedJwt = jwt.decode(token, {complete: true});
327342
if (!decodedJwt) {
328-
console.log('Not a valid JWT token');
329-
callback('Unauthorized');
343+
let policy = new AuthPolicy('', awsAccountId, apiOptions);
344+
logger.info("Not valid JWT token, returning deny all policy");
345+
policy.denyAllMethods();
346+
let iamPolicy = policy.build();
347+
callback(null, iamPolicy);
330348
return;
331349
}
332350

333351
//Fail if token is not from your User Pool
334352
if (decodedJwt.payload['iss'] != tokenIssuer) {
335-
console.log("invalid Issuer");
336-
callback("Unauthorized");
353+
logger.info("Provided Token not from UserPool, returning deny all policy");
354+
let policy = new AuthPolicy('', awsAccountId, apiOptions);
355+
policy.denyAllMethods();
356+
let iamPolicy = policy.build();
357+
callback(null, iamPolicy);
337358
return;
338359
}
339360

340-
//Reject the jwt if it's not an 'Access Token'
361+
//Reject the jwt if it's not an 'Identity Token'
341362
if (decodedJwt.payload['token_use'] != 'id') {
342363
console.log("Not an Identity token");
343-
callback("Unauthorized");
364+
logger.info("Provided Token is not and identity token, returning deny all policy");
365+
let policy = new AuthPolicy('', awsAccountId, apiOptions);
366+
policy.denyAllMethods();
367+
let iamPolicy = policy.build();
368+
callback(null, iamPolicy);
344369
return;
345370
}
346371

@@ -349,39 +374,39 @@ function processAuthRequest(event, tokenIssuer, callback) {
349374
var pem = PEMS[kid];
350375
if (!pem) {
351376
console.log('Invalid Identity token');
352-
callback("Unauthorized");
377+
logger.info("Invalid Identity token, returning deny all policy");
378+
let policy = new AuthPolicy('', awsAccountId, apiOptions);
379+
policy.denyAllMethods();
380+
let iamPolicy = policy.build();
381+
callback(null, iamPolicy);
353382
return;
354383
}
355384

356385
//Verify the signature of the JWT token to ensure it's really coming from your User Pool
357386

358387
jwt.verify(token, pem, {issuer: tokenIssuer}, function (err, payload) {
359388
if (err) {
360-
callback("Unauthorized");
389+
logger.info("Error while trying to verify the Token, returning deny-all policy");
390+
let policy = new AuthPolicy('', awsAccountId, apiOptions);
391+
policy.denyAllMethods();
392+
let iamPolicy = policy.build();
393+
callback(null, iamPolicy);
361394
} else {
362395
//Valid token. Generate the API Gateway policy for the user
363396
//Always generate the policy on value of 'sub' claim and not for
364397
// 'username' because username is reassignable
365398
//sub is UUID for a user which is never reassigned to another user.
366399

367-
const apiOptions = {};
368-
const tmp = event.methodArn.split(':');
369-
const apiGatewayArnTmp = tmp[5].split('/');
370-
const principalId = payload.sub;
371-
const awsAccountId = tmp[4];
372-
373-
apiOptions.region = tmp[3];
374-
apiOptions.restApiId = apiGatewayArnTmp[0];
375-
apiOptions.stage = apiGatewayArnTmp[1];
376-
377400
let admin = null;
378-
let policy = new AuthPolicy(principalId, awsAccountId, apiOptions);
401+
const pId = payload.sub;
402+
logger.info(pId);
403+
let policy = new AuthPolicy(pId, awsAccountId, apiOptions);
379404
policy.allowAllMethods();
380405

381-
382406
//Check the Cognito group entry for Admin.
383407
//Assuming here that the Admin group has always higher
384408
//precedence
409+
const principalId = payload.sub;
385410

386411
if (payload['cognito:groups'] &&
387412
payload['cognito:groups'][0] === 'adminGroup') {

api/lambda/locations.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ let LocationsTable = new data.LocationsTable();
66

77

88
function Create(event) {
9+
910
return LocationsTable.put(JSON.parse(event.body));
1011
}
1112

api/util/cognito.js

Lines changed: 65 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -348,45 +348,52 @@ function deleteUserPool() {
348348
});
349349
}
350350

351+
/**
352+
* The function creates a Cognito UserGroup based on the input argument.
353+
* If the group already exists, then it ignores the request.
354+
* @param group The group we want to create
355+
* @returns {Promise.<TResult>|*}
356+
*/
351357
function adminCreateGroup(group) {
352358
return getUserPoolId().then((userPoolId) => {
353359

360+
logger.info('Incoming request to crate group %s', group.name);
361+
let listParams = {
362+
UserPoolId: userPoolId
363+
};
364+
354365
let params = {
355366
UserPoolId: userPoolId,
356367
GroupName: group.name,
357368
Description: group.description,
358369
Precedence: group.precedence
359370
};
360-
userPools.createGroup(params, function (err, data) {
371+
userPools.listGroups(listParams, function (err, listData) {
361372
if (err) {
362373
throw (new Error(err));
363374
}
364-
return (data)
365-
});
366-
});
375+
for (let userGroupIndex in listData.Groups) {
367376

368-
}
377+
if (listData.Groups[userGroupIndex].GroupName === group.name) {
378+
logger.info('Group %s already exists, ignoring', group.name);
379+
return;
380+
}
381+
}
382+
userPools.createGroup(params, function (err, data) {
369383

370384

371-
function adminAssignUserToGroup(user, group) {
372-
return getUserPoolId().then((userPoolId) => {
373-
let params = {
374-
UserPoolId: userPoolId,
375-
Username: user.username,
376-
GroupName: group.name
377-
};
378-
379-
userPools.adminAddUserToGroup(params, function (err, data) {
380-
if (err) {
381-
throw (new Error(err));
382-
}
383-
return (data)
384-
})
385+
if (err) {
386+
throw (new Error(err));
387+
}
388+
return (data);
389+
})
390+
});
385391
});
386392
}
387393

388-
389394
function adminCreateUser(userData) {
395+
396+
390397
return getUserPoolId().then((userPoolId) => {
391398
let createUserParams = {
392399
UserPoolId: userPoolId,
@@ -408,15 +415,51 @@ function adminCreateUser(userData) {
408415
}
409416
]
410417
};
411-
userPools.adminCreateUser(createUserParams, function (err) {
418+
419+
let listUserParams = {
420+
UserPoolId: userPoolId
421+
}
422+
423+
userPools.listUsers(listUserParams, function (err, listUsersData) {
412424
if (err) {
413425
throw (new Error(err));
414426
}
415-
return initialChangePassword(userData);
427+
428+
for (let poolUserIndex in listUsersData.Users) {
429+
if (listUsersData.Users[poolUserIndex].Username === userData.username) {
430+
logger.info('User %s already exists, ignoring', userData.username);
431+
return;
432+
}
433+
}
434+
userPools.adminCreateUser(createUserParams, function (err) {
435+
if (err) {
436+
throw (new Error(err));
437+
}
438+
return initialChangePassword(userData);
439+
});
440+
416441
});
442+
443+
});
444+
}
445+
446+
function adminAssignUserToGroup(user, group) {
447+
return getUserPoolId().then((userPoolId) => {
448+
let params = {
449+
UserPoolId: userPoolId,
450+
Username: user.username,
451+
GroupName: group.name
452+
};
453+
userPools.adminAddUserToGroup(params, function (err, data) {
454+
if (err) {
455+
throw (new Error(err));
456+
}
457+
return (data);
458+
})
417459
});
418460
}
419461

462+
420463
function initialChangePassword(userData) {
421464
return new Promise((resolve, reject) => {
422465
getUserPoolId().then((userPoolId) => {

api/util/importer.js

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
'use strict';
22
var rfr = require('rfr');
3+
var logger = rfr('util/logger');
34
var locations = rfr('lambda/locations');
45
var resources = rfr('lambda/resources');
56
var bookings = rfr('lambda/bookings');
67
var cognito = rfr('util/cognito');
78

9+
/*
10+
More information about the UserGroups and the precedence
11+
http://docs.aws.amazon.com/cognito/latest/developerguide/cognito-user-pools-user-groups.html#assigning-precedence-values-to-groups
12+
*/
813

914
var SampleGroups = [
1015
{
@@ -16,7 +21,7 @@ var SampleGroups = [
1621
{
1722
name: 'clientGroup',
1823
description: 'Cognito user group for spacefinder users',
19-
precedence: 0,
24+
precedence: 1,
2025
iam: 'something'
2126
},
2227
];
@@ -29,6 +34,7 @@ var SampleUsers = [
2934
givenName: 'Admin',
3035
familyName: 'User',
3136
password: 'Test123!'
37+
3238
},
3339
{
3440
username: 'user1',
@@ -103,6 +109,7 @@ class SampleData {
103109

104110
generateSampleLocation(name, description, imageUrl) {
105111
return new Promise((resolve, reject) => {
112+
106113
locations.Create({
107114
body: JSON.stringify({
108115
name: name,
@@ -264,13 +271,12 @@ class SampleData {
264271
return promise;
265272
}
266273

274+
267275
static assignUsersToGroups() {
268276
let promises = [];
269277
for (let user of SampleUsers) {
270278
let group = null;
271-
//Just a trivial assignment to demonstrate adding users
272-
//to a group. For the example admin1 user goes to admin group
273-
//and user1 user goes to client group
279+
274280
if (user.username === "admin1") {
275281
group = SampleGroups[0];
276282
} else {
@@ -279,12 +285,16 @@ class SampleData {
279285
let promise = SampleData.createUserAssignmentToGroupPromise(user, group);
280286
promises.push(promise);
281287
}
288+
logger.info(promises.length);
282289
return Promise.all(promises);
290+
291+
283292
}
284293

285294

286295
} // end class
287296

288-
module.exports = {
297+
module
298+
.exports = {
289299
SampleData
290300
};

app/src/config/config-overrides-generated.ts

Lines changed: 0 additions & 7 deletions
This file was deleted.

0 commit comments

Comments
 (0)