Skip to content

Commit 9ff97b8

Browse files
author
toshke
committed
cognito updates
1 parent c34c0fd commit 9ff97b8

File tree

5 files changed

+133
-0
lines changed

5 files changed

+133
-0
lines changed

cognito-user-pool-client/README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
## CognitoUserPoolClient
2+
3+
Creates user pool client, as there is no cloudformation resource supporting
4+
all of the properties.
5+
6+
runtime: `nodejs6.10`
7+
handler: `cognito-user-pool-client/index.handler`
8+
9+
Required parameters:
10+
11+
Please look at `cognito-user-pool-client/schema.json`

cognito-user-pool-client/index.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,22 @@ let logic = {
1717
'GenerateSecret',
1818
'AllowedOAuthFlowsUserPoolClient'
1919
],
20+
keys: [
21+
'ClientName',
22+
'ExplicitAuthFlows',
23+
'GenerateSecret',
24+
'UserPoolId',
25+
'AllowedOAuthFlows',
26+
'AllowedOAuthFlowsUserPoolClient',
27+
'AllowedOAuthScopes',
28+
'CallbackURLs',
29+
'DefaultRedirectURI',
30+
'LogoutURLs',
31+
'ReadAttributes',
32+
'RefreshTokenValidity',
33+
'SupportedIdentityProviders',
34+
'WriteAttributes'
35+
],
2036
returnAttrs: [
2137
'UserPoolClient.ClientId',
2238
'UserPoolClient.CreationDate',

cognito-user-pool-domain/README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
## Cognito UserPoolDomain
2+
3+
Creates UserPoolDomain for cognito user pool. This is currently not
4+
covered by CloudFormation.
5+
6+
runtime: `nodejs6.10`
7+
handler: `cognito-user-pool-domain/index.handler`
8+
9+
Required parameters:
10+
11+
Please look at `cognito-user-pool-domain/schema.json`
12+
13+
Optional parameter `GenerateRandomIfNotAvailable` will try to append random
14+
string to requested domain name, if given name is not available. This process
15+
is repeated 5 times until handler reports an error.

cognito-user-pool-domain/index.js

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/**
2+
* A Lambda function to manage Cognito User Pool Clients
3+
* Api Doc - https://docs.aws.amazon.com/cognito-user-identity-pools/latest/APIReference/API_CreateUserPoolClient.html
4+
**/
5+
6+
let AWS = require('aws-sdk'),
7+
CfnLambda = require('cfn-lambda'),
8+
CognitoApi = new AWS.CognitoIdentityServiceProvider({
9+
apiVersion: '2016-04-18'
10+
}), randomSuffix = () => {
11+
return Math.random().toString(36).replace(/[^a-z]+/g, '').substr(0, 5);
12+
};
13+
14+
// empty create
15+
var Create = (cfnRequestParams, reply) => {
16+
17+
var doCreate = (cb) => {
18+
CognitoApi.createUserPoolDomain({
19+
Domain: cfnRequestParams.Domain,
20+
UserPoolId: cfnRequestParams.UserPoolId
21+
}, cb);
22+
}, retry = 0;
23+
24+
var handler = (err, data) => {
25+
if (err) {
26+
if (cfnRequestParams.GenerateRandomIfNotAvailable
27+
&& cfnRequestParams.GenerateRandomIfNotAvailable == 'true') {
28+
console.log(cfnRequestParams.Domain + ' is not available');
29+
cfnRequestParams.Domain = `${cfnRequestParams.Domain}${randomSuffix()}`;
30+
console.log(`Retrying with domain name ${cfnRequestParams.Domain}`);
31+
retry = retry + 1;
32+
if (retry == 5) {
33+
console.log('Failed after 5 attempts to generate random domain name, probably not a name issue');
34+
reply(err);
35+
} else {
36+
doCreate(handler);
37+
}
38+
} else {
39+
reply(err)
40+
}
41+
} else {
42+
var domain = `https://${cfnRequestParams.Domain}.auth.${process.env.AWS_REGION}.amazoncognito.com`;
43+
reply(err, domain, {DomainFull: domain, Domain: cfnRequestParams.Domain});
44+
}
45+
};
46+
doCreate(handler);
47+
};
48+
49+
var Update = (requestPhysicalID, cfnRequestParams, oldCfnRequestParams, reply) => {
50+
domain = requestPhysicalID.split('/')[2].split('.')[0];
51+
reply(null, requestPhysicalID, {DomainFull: requestPhysicalID, Domain: domain});
52+
};
53+
54+
55+
var Delete = (requestPhysicalID, cfnRequestParams, reply) => {
56+
CognitoApi.deleteUserPoolDomain({
57+
Domain: requestPhysicalID.split('/')[2].split('.')[0],
58+
UserPoolId: cfnRequestParams.UserPoolId
59+
}, function (err, data) {
60+
reply(err, requestPhysicalID, null);
61+
});
62+
};
63+
64+
// empty update
65+
66+
exports.handler = CfnLambda({
67+
Create: Create,
68+
Update: Update,
69+
Delete: Delete,
70+
TriggersReplacement: [],
71+
SchemaPath: [__dirname, 'schema.json']
72+
});

cognito-user-pool-domain/schema.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"$schema": "http://json-schema.org/draft-04/schema#",
3+
"type": "object",
4+
"required": [
5+
"UserPoolId",
6+
"Domain"
7+
],
8+
"properties": {
9+
"UserPoolId": {
10+
"type": "string"
11+
},
12+
"GenerateRandomIfNotAvailable": {
13+
"type": "string"
14+
},
15+
"Domain": {
16+
"type": "string"
17+
}
18+
}
19+
}

0 commit comments

Comments
 (0)