Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cognito): Add support for standard & custom attributes #5879

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 74 additions & 2 deletions packages/@aws-cdk/aws-cognito/lib/user-pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ export enum UserPoolAttribute {
/**
* The End-User's time zone
*/
TIMEZONE = 'timezone',
TIMEZONE = 'zoneinfo',

/**
* Time the End-User's information was last updated.
Expand All @@ -120,6 +120,58 @@ export enum UserPoolAttribute {
WEBSITE = 'website'
}

interface SchemaAttribute {
/**
* Specifies whether the value of the attribute can be changed.
*
* @default - false
*/
readonly mutable?: boolean,

/**
* Specifies whether a user pool attribute is required.
* If the attribute is required and the user does not provide a value, registration or sign-in will fail.
*
* @default - false
*/
readonly required?: boolean
}

/**
* Standard attributes for all users in a user-pool
* @see https://docs.aws.amazon.com/cognito/latest/developerguide/user-pool-settings-attributes.html#cognito-user-pools-standard-attributes
*/
export interface StandardSchemaAttribute extends SchemaAttribute {
/**
* Name of one of the standard attributes
*/
readonly name: UserPoolAttribute,

/**
* Standard attributes are all strings. No other type is allowed.
* @default - 'String'
*/
readonly type?: 'String'
}

/**
* Custom attributes for all users in a user-pool
* @see https://docs.aws.amazon.com/cognito/latest/developerguide/user-pool-settings-attributes.html#user-pool-settings-custom-attributes
*/
export interface CustomSchemaAttribute extends SchemaAttribute {
/**
* Name of a custom attribute.
* This will get an automatic prefix `custom:` in Cognito, once deployed.
*/
readonly name: string,

/**
* Custom attributes can be strings or numbers only.
* @default - 'String'
*/
readonly type?: 'String' | 'Number',
}

/**
* Methods of user sign-in
*/
Expand Down Expand Up @@ -250,6 +302,13 @@ export interface UserPoolProps {
* @default - No Lambda triggers.
*/
readonly lambdaTriggers?: UserPoolTriggers;

/**
* Standard or Custom attributes to add to all users in this user pool
* @see https://docs.aws.amazon.com/cognito/latest/developerguide/user-pool-settings-attributes.html
* @default - no additional attributes
*/
readonly userAttributes?: Array<StandardSchemaAttribute | CustomSchemaAttribute>;
}

export interface UserPoolAttributes {
Expand Down Expand Up @@ -402,12 +461,25 @@ export class UserPool extends Resource implements IUserPool {
}
}

let schema: CfnUserPool.SchemaAttributeProperty[] | undefined;
if (props.userAttributes) {
schema = props.userAttributes.map(attribute => {
return {
name: attribute.name,
attributeDataType: attribute.type || 'String',
required: attribute.required || false,
mutable: attribute.mutable || false,
};
});
}

const userPool = new CfnUserPool(this, 'Resource', {
userPoolName: this.physicalName,
usernameAttributes,
aliasAttributes,
autoVerifiedAttributes: props.autoVerifiedAttributes,
lambdaConfig: Lazy.anyValue({ produce: () => this.triggers })
lambdaConfig: Lazy.anyValue({ produce: () => this.triggers }),
schema
});

this.userPoolId = this.getResourceNameAttribute(userPool.ref);
Expand Down
49 changes: 49 additions & 0 deletions packages/@aws-cdk/aws-cognito/test/test.user-pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export = {

test.done();
},

'support tags'(test: Test) {
// GIVEN
const stack = new cdk.Stack();
Expand Down Expand Up @@ -258,6 +259,54 @@ export = {

// THEN
test.throws(() => toThrow(), /'autoVerifiedAttributes' can only include EMAIL or PHONE_NUMBER/);
test.done();
},

'support adding additional attributes for all the users'(test: Test) {
// GIVEN
const stack = new cdk.Stack();

// WHEN
const { ADDRESS, LOCALE, TIMEZONE } = cognito.UserPoolAttribute;
new cognito.UserPool(stack, 'Pool', {
userAttributes: [
{ name: ADDRESS },
{ name: LOCALE },
{ name: TIMEZONE, mutable: true },
{ name: 'another_attribute' }
]
});

// THEN
expect(stack).to(haveResourceLike('AWS::Cognito::UserPool', {
Schema: [
{
Name: 'address',
AttributeDataType: 'String',
Mutable: false,
Required: false
},
{
Name: 'locale',
AttributeDataType: 'String',
Mutable: false,
Required: false
},
{
Name: 'zoneinfo',
AttributeDataType: 'String',
Mutable: true,
Required: false
},
{
Name: 'another_attribute',
AttributeDataType: 'String',
Mutable: false,
Required: false
}
]
}));

test.done();
}
};