Skip to content

Commit 109cd1b

Browse files
authored
feat: add support for generating user pool groups (#1002)
* feat: add support for generating user pool groups * chore: fix tests * chore: add changeset * chore: update api * chore: fix lint * chore: fix bug with assumeBy conditions * fix: bug with group role policy * fix: conditions on role * chore: cleanup * chore: add precedence to group roles * chore: add tests for generating groups and group roles * chore: cleanup * chore: cleanup constructor code by moving code blocks to functions * chore: fix test * chore: change the access pattern for groups to use group names instead of array index * chore: fix test * chore: update api * chore: cleanup, docs * chore: cleanup
1 parent baeb68f commit 109cd1b

File tree

8 files changed

+229
-74
lines changed

8 files changed

+229
-74
lines changed

.changeset/mean-frogs-visit.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
'@aws-amplify/auth-construct-alpha': patch
3+
'@aws-amplify/backend-data': patch
4+
'@aws-amplify/plugin-types': patch
5+
---
6+
7+
Add support for generating user pool groups.

packages/auth-construct/API.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ export type AuthProps = {
4040
userAttributes?: StandardAttributes;
4141
multifactor?: MFA;
4242
accountRecovery?: keyof typeof aws_cognito.AccountRecovery;
43+
groups?: string[];
4344
outputStorageStrategy?: BackendOutputStorageStrategy<AuthOutput>;
4445
};
4546

packages/auth-construct/src/construct.test.ts

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,70 @@ void describe('Auth construct', () => {
142142
});
143143
});
144144

145+
void it('creates user groups and group roles', () => {
146+
const app = new App();
147+
const stack = new Stack(app);
148+
const auth = new AmplifyAuth(stack, 'test', {
149+
loginWith: { email: true },
150+
groups: ['admins', 'managers'],
151+
});
152+
// validate the generated resources
153+
assert.equal(Object.keys(auth.resources.groups).length, 2);
154+
assert.equal(
155+
auth.resources.groups['admins'].cfnUserGroup.groupName,
156+
'admins'
157+
);
158+
assert.equal(
159+
auth.resources.groups['managers'].cfnUserGroup.groupName,
160+
'managers'
161+
);
162+
// validate generated template
163+
const template = Template.fromStack(stack);
164+
template.hasResourceProperties('AWS::Cognito::UserPool', {
165+
UsernameAttributes: ['email'],
166+
AutoVerifiedAttributes: ['email'],
167+
});
168+
template.hasResourceProperties('AWS::Cognito::UserPoolGroup', {
169+
GroupName: 'admins',
170+
Precedence: 0,
171+
});
172+
template.hasResourceProperties('AWS::Cognito::UserPoolGroup', {
173+
GroupName: 'managers',
174+
Precedence: 1,
175+
});
176+
// validate the generated policies
177+
const idpRef = template['template']['Outputs']['identityPoolId']['Value'];
178+
// There should be 3 matching roles, one for the auth role,
179+
// and one for each of the 'admins' and 'managers' roles
180+
const matchingRoleCount = 3;
181+
template.resourcePropertiesCountIs(
182+
'AWS::IAM::Role',
183+
{
184+
AssumeRolePolicyDocument: {
185+
Version: '2012-10-17',
186+
Statement: [
187+
{
188+
Action: 'sts:AssumeRoleWithWebIdentity',
189+
Effect: 'Allow',
190+
Principal: {
191+
Federated: 'cognito-identity.amazonaws.com',
192+
},
193+
Condition: {
194+
'ForAnyValue:StringLike': {
195+
'cognito-identity.amazonaws.com:amr': 'authenticated',
196+
},
197+
StringEquals: {
198+
'cognito-identity.amazonaws.com:aud': idpRef,
199+
},
200+
},
201+
},
202+
],
203+
},
204+
},
205+
matchingRoleCount
206+
);
207+
});
208+
145209
void it('creates email login mechanism if settings is empty object', () => {
146210
const app = new App();
147211
const stack = new Stack(app);
@@ -1774,7 +1838,7 @@ void describe('Auth construct', () => {
17741838
email: true,
17751839
externalProviders: {
17761840
scopes: ['EMAIL', 'PROFILE'],
1777-
callbackUrls: [],
1841+
callbackUrls: ['http://localhost'],
17781842
logoutUrls: ['http://localhost'],
17791843
domainPrefix: 'https://localhost',
17801844
},

0 commit comments

Comments
 (0)