Skip to content

Commit 51a867b

Browse files
authored
Merge pull request #1 from inventivegroup/master
Added Support for multiple UserPool-UserPoolClient Custom Attribute Associations
2 parents 9ae9e6c + 74d2b76 commit 51a867b

9 files changed

+1869
-83
lines changed

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,7 @@ jspm_packages
3939
#serverless
4040
.serverless
4141
.vscode/*
42-
!.vscode/extensions.json
42+
!.vscode/extensions.json
43+
44+
# Webstorm IDE
45+
.idea/

.idea/serverless-cognito-add-custom-attributes.iml

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 50 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ custom:
2727
AttributeDataType: String
2828
DeveloperOnlyAttribute: False
2929
Mutable: True
30-
Name: "another" # this will end up being custom:another
30+
Name: "another" # this will end up being custom:another
3131
Required: False
3232

3333
# Only add this if not already outputting the Cognito User Pool and Client IDs, otherwise, refer to the existing outputs in the custom:CognitoAddCustomAttributes section
@@ -43,21 +43,66 @@ resources:
4343
4444
# Details
4545
46-
1. Output your UserPoolId via `resouces.Outputs`
47-
1. Output your UserPoolClientId via `resouces.Outputs`
48-
2. Add `CognitoAddCustomAttributes` to `custom` with the following structure:
46+
1. Output your UserPoolId via `resources.Outputs`
47+
2. Output your UserPoolClientId via `resources.Outputs`
48+
3. Add `CognitoAddCustomAttributes` to `custom` with the following structure:
4949
```yml
5050
CognitoUserPoolIdOutputKey: "UserPool Output Key as a String"
5151
CognitoUserPoolClientIdOutputKey: "UserPoolClient Output Key as a String"
5252
CustomAttributes:
53-
-
53+
-
5454
AttributeDataType: String
5555
DeveloperOnlyAttribute: False
5656
Mutable: True
5757
Name: "another"
5858
Required: False
5959
```
6060

61+
Note: If you have multiple userPool-userPoolClients you can specify them as an array as well
62+
63+
Example:
64+
```yml
65+
plugins:
66+
- serverless-cognito-add-custom-attributes
67+
68+
custom:
69+
CognitoAddCustomAttributes:
70+
-
71+
CognitoUserPoolIdOutputKey: "CognitoUserPoolApplicationUserPoolId"
72+
CognitoUserPoolClientIdOutputKey: "CognitoUserPoolApplicationUserPoolClientId"
73+
CustomAttributes:
74+
-
75+
AttributeDataType: String
76+
DeveloperOnlyAttribute: False
77+
Mutable: True
78+
Name: "another" # this will end up being custom:another
79+
Required: False
80+
-
81+
CognitoUserPoolIdOutputKey: "CognitoUserPoolApplicationUserPoolId"
82+
CognitoUserPoolClientIdOutputKey: "CognitoUserPoolApplicationUserPoolClientId2"
83+
CustomAttributes:
84+
-
85+
AttributeDataType: String
86+
DeveloperOnlyAttribute: False
87+
Mutable: True
88+
Name: "another" # this will end up being custom:another
89+
Required: False
90+
91+
resources:
92+
Outputs:
93+
CognitoUserPoolApplicationUserPoolId:
94+
Value:
95+
Ref: CognitoUserPoolApplicationUserPool
96+
CognitoUserPoolApplicationUserPoolClientId:
97+
Value:
98+
Ref: CognitoUserPoolApplicationUserPoolClient
99+
CognitoUserPoolApplicationUserPoolClientId2:
100+
Value:
101+
Ref: CognitoUserPoolApplicationUserPoolClient2
102+
```
103+
104+
105+
61106
The names of your attributes supplied here will appear as `custom:{name}` when deployed.
62107

63108
For more information on the schema of attributes see:

helperMethods.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
const _ = require('lodash');
2+
3+
const Params = {
4+
CognitoUserPoolIdOutputKey: 'CognitoUserPoolIdOutputKey',
5+
CustomAttributes: 'CustomAttributes',
6+
CognitoUserPoolClientIdOutputKey: 'CognitoUserPoolClientIdOutputKey',
7+
};
8+
9+
const describeStack = async (AWS) => {
10+
const response = await AWS.request('CloudFormation', 'describeStacks', { StackName: AWS.naming.getStackName() });
11+
return _.first(response.Stacks);
12+
};
13+
14+
const loadCustom = (log, custom) => {
15+
let result = [];
16+
if (custom && custom.CognitoAddCustomAttributes) {
17+
18+
if(Array.isArray(custom.CognitoAddCustomAttributes)) {
19+
custom.CognitoAddCustomAttributes.forEach(cognitoCustomAttributeMappingItem => {
20+
result.push(parseCustomItem(log, cognitoCustomAttributeMappingItem));
21+
});
22+
} else {
23+
result.push(parseCustomItem(log, custom.CognitoAddCustomAttributes));
24+
}
25+
}
26+
27+
return result;
28+
};
29+
30+
const parseCustomItem = (log, item) => {
31+
const result = {};
32+
let skippingItem = false;
33+
34+
const CognitoUserPoolIdOutputKey = _.get(item, Params.CognitoUserPoolIdOutputKey);
35+
const CustomAttributes = _.get(item, Params.CustomAttributes);
36+
const CognitoUserPoolClientIdOutputKey = _.get(item, Params.CognitoUserPoolClientIdOutputKey);
37+
38+
if (!CognitoUserPoolIdOutputKey || !(typeof(CognitoUserPoolIdOutputKey) === 'string')) {
39+
log('CognitoUserPoolIdOutputKey is required.');
40+
skippingItem = true;
41+
} else if (!CustomAttributes || !Array.isArray(CustomAttributes)) {
42+
log('CustomAttributes array is required.');
43+
skippingItem = true;
44+
} else {
45+
result.CognitoUserPoolIdOutputKey = CognitoUserPoolIdOutputKey;
46+
result.CustomAttributes = CustomAttributes;
47+
result.CognitoUserPoolClientIdOutputKey = CognitoUserPoolClientIdOutputKey;
48+
}
49+
50+
if(skippingItem) {
51+
log(`Custom Attribute is being skipped due to missing information. [CognitoUserPoolIdOutputKey: ${CognitoUserPoolIdOutputKey}] [CognitoUserPoolClientIdOutputKey: ${CognitoUserPoolClientIdOutputKey}]`);
52+
}
53+
54+
return result;
55+
};
56+
57+
module.exports = {
58+
Params,
59+
loadCustom,
60+
describeStack
61+
};

0 commit comments

Comments
 (0)