diff --git a/packages/amplify-gen1-codegen-auth-adapter/src/auth_render_adapter.test.ts b/packages/amplify-gen1-codegen-auth-adapter/src/auth_render_adapter.test.ts index 729140c0f40..177b74d6781 100644 --- a/packages/amplify-gen1-codegen-auth-adapter/src/auth_render_adapter.test.ts +++ b/packages/amplify-gen1-codegen-auth-adapter/src/auth_render_adapter.test.ts @@ -600,7 +600,7 @@ void describe('auth codegen', () => { const result = getAuthDefinition({ userPool: { Name: 'test' }, }); - assert.deepEqual(result.userPoolOverrides, { userPoolName: 'test', usernameAttributes: [] }); + assert.deepEqual(result.userPoolOverrides, { userPoolName: 'test', usernameAttributes: undefined }); }); }); }); diff --git a/packages/amplify-gen1-codegen-auth-adapter/src/auth_render_adapter.ts b/packages/amplify-gen1-codegen-auth-adapter/src/auth_render_adapter.ts index 185d2e58a42..df2795bcb38 100644 --- a/packages/amplify-gen1-codegen-auth-adapter/src/auth_render_adapter.ts +++ b/packages/amplify-gen1-codegen-auth-adapter/src/auth_render_adapter.ts @@ -40,6 +40,7 @@ export type AuthTriggerConnectionSourceMap = Partial Object.assign(userPoolOverrides, userNamePolicy); } if (userPool.UsernameAttributes === undefined || userPool.UsernameAttributes.length === 0) { - userPoolOverrides.usernameAttributes = []; + userPoolOverrides.usernameAttributes = undefined; } return userPoolOverrides; }; @@ -118,7 +119,7 @@ const getStandardUserAttributes = ( required: attribute.Required, mutable: attribute.Mutable, }; - if (attribute.Name !== undefined && attribute.Name in mappedUserAttributeName) { + if (attribute.Name !== undefined && attribute.Name in mappedUserAttributeName && attribute.Required) { return { ...standardAttributes, [mappedUserAttributeName[attribute.Name as keyof typeof mappedUserAttributeName] as Attribute]: standardAttribute, @@ -235,6 +236,7 @@ function filterAttributeMapping( */ export const getAuthDefinition = ({ userPool, + identityPoolName, identityProviders, identityProvidersDetails, identityGroups, @@ -354,6 +356,7 @@ export const getAuthDefinition = ({ userPoolOverrides, lambdaTriggers: getAuthTriggers(userPool.LambdaConfig ?? {}, authTriggerConnections ?? {}), guestLogin, + identityPoolName, oAuthFlows: webClient?.AllowedOAuthFlows, readAttributes: webClient?.ReadAttributes, writeAttributes: webClient?.WriteAttributes, diff --git a/packages/amplify-gen2-codegen/src/auth/source_builder.ts b/packages/amplify-gen2-codegen/src/auth/source_builder.ts index 908796c2422..eb7820fe0db 100644 --- a/packages/amplify-gen2-codegen/src/auth/source_builder.ts +++ b/packages/amplify-gen2-codegen/src/auth/source_builder.ts @@ -137,6 +137,7 @@ export interface AuthDefinition { userPoolOverrides?: PolicyOverrides; lambdaTriggers?: Partial; guestLogin?: boolean; + identityPoolName?: string; oAuthFlows?: string[]; readAttributes?: string[]; writeAttributes?: string[]; diff --git a/packages/amplify-gen2-codegen/src/backend/synthesizer.test.ts b/packages/amplify-gen2-codegen/src/backend/synthesizer.test.ts index 504ffc7db99..5a1ebc3d57c 100644 --- a/packages/amplify-gen2-codegen/src/backend/synthesizer.test.ts +++ b/packages/amplify-gen2-codegen/src/backend/synthesizer.test.ts @@ -27,6 +27,7 @@ describe('BackendRenderer', () => { 'Policies.PasswordPolicy.RequireUppercase': false, 'Policies.PasswordPolicy.TemporaryPasswordValidityDays': 10, userPoolName: 'Test_Name', + userNameAttributes: undefined, }; const mappedPolicyType: Record = { MinimumLength: 'minimumLength', @@ -58,7 +59,9 @@ describe('BackendRenderer', () => { } else if (typeof value === 'boolean') { assert(output.includes(`cfnUserPool.policies = {passwordPolicy:{${policyKey}:${value}}}`)); } - } else if (value) { + } else if (value === undefined) { + assert(output.includes(`cfnUserPool.${key} = ${value}`)); + } else { assert(output.includes(`cfnUserPool.${key} = "${value}"`)); } }); @@ -113,6 +116,31 @@ describe('BackendRenderer', () => { assert(!output.includes('cfnIdentityPool')); }); }); + describe('Identity Pool Name', () => { + it('Renders cfnIdentityPool accessor if identityPoolName is defined', () => { + const renderer = new BackendSynthesizer(); + const rendered = renderer.render({ + auth: { + importFrom: './auth/resource.ts', + identityPoolName: 'Test_Name', + guestLogin: true, + }, + }); + const output = printNodeArray(rendered); + assert(output.includes('cfnIdentityPool')); + }); + it('Does not render cfnIdentityPool accessor if identityPoolName is undefined and guestLogin is true', () => { + const renderer = new BackendSynthesizer(); + const rendered = renderer.render({ + auth: { + importFrom: './auth/resource.ts', + guestLogin: true, + }, + }); + const output = printNodeArray(rendered); + assert(!output.includes('cfnIdentityPool')); + }); + }); describe('OAuth Flows', () => { it('Renders cfnUserPoolClient accessor if oAuthFlows is defined', () => { const renderer = new BackendSynthesizer(); diff --git a/packages/amplify-gen2-codegen/src/backend/synthesizer.ts b/packages/amplify-gen2-codegen/src/backend/synthesizer.ts index 0b59c28abc6..553d6b4c6aa 100644 --- a/packages/amplify-gen2-codegen/src/backend/synthesizer.ts +++ b/packages/amplify-gen2-codegen/src/backend/synthesizer.ts @@ -22,6 +22,7 @@ export interface BackendRenderParameters { importFrom: string; userPoolOverrides?: PolicyOverrides; guestLogin?: boolean; + identityPoolName?: string; oAuthFlows?: string[]; readAttributes?: string[]; writeAttributes?: string[]; @@ -83,7 +84,7 @@ export class BackendSynthesizer { private setPropertyValue( objectIdentifier: Identifier, propertyPath: string, - value: number | string | boolean | string[] | object, + value: number | string | boolean | string[] | object | undefined, ): ExpressionStatement { const propertyAccessExpression = this.createPropertyAccessExpression(objectIdentifier, propertyPath); const overrideValue = this.getOverrideValue(value); @@ -91,7 +92,7 @@ export class BackendSynthesizer { return factory.createExpressionStatement(factory.createAssignment(propertyAccessExpression, overrideValue)); } - private getOverrideValue(value: number | string | boolean | string[] | object): Expression { + private getOverrideValue(value: number | string | boolean | string[] | object | undefined): Expression { if (typeof value === 'number') { return factory.createNumericLiteral(value); } else if (typeof value === 'string') { @@ -107,6 +108,8 @@ export class BackendSynthesizer { properties.push(property); } return factory.createObjectLiteralExpression(properties, true); + } else if (value === undefined) { + return factory.createIdentifier('undefined'); } throw new TypeError(`Unrecognized type: ${typeof value}`); } @@ -217,7 +220,7 @@ export class BackendSynthesizer { if (value !== undefined && policyKey in mappedPolicyType) { policies.passwordPolicy[mappedPolicyType[policyKey] as string] = value; } - } else if (value) { + } else { nodes.push(this.setPropertyValue(factory.createIdentifier('cfnUserPool'), overridePath, value)); } } @@ -230,12 +233,19 @@ export class BackendSynthesizer { ); } - if (renderArgs.auth?.guestLogin === false) { + if (renderArgs.auth?.guestLogin === false || renderArgs.auth?.identityPoolName) { const cfnIdentityPoolVariableStatement = this.createVariableStatement( this.createVariableDeclaration('cfnIdentityPool', 'auth.resources.cfnResources.cfnIdentityPool'), ); nodes.push(cfnIdentityPoolVariableStatement); - nodes.push(this.setPropertyValue(factory.createIdentifier('cfnIdentityPool'), 'allowUnauthenticatedIdentities', false)); + if (renderArgs.auth?.identityPoolName) { + nodes.push( + this.setPropertyValue(factory.createIdentifier('cfnIdentityPool'), 'identityPoolName', renderArgs.auth.identityPoolName), + ); + } + if (renderArgs.auth?.guestLogin === false) { + nodes.push(this.setPropertyValue(factory.createIdentifier('cfnIdentityPool'), 'allowUnauthenticatedIdentities', false)); + } } if (renderArgs.auth?.oAuthFlows || renderArgs.auth?.readAttributes || renderArgs.auth?.writeAttributes) { diff --git a/packages/amplify-gen2-codegen/src/index.ts b/packages/amplify-gen2-codegen/src/index.ts index dd3164ccd3b..0c145246750 100644 --- a/packages/amplify-gen2-codegen/src/index.ts +++ b/packages/amplify-gen2-codegen/src/index.ts @@ -144,6 +144,7 @@ export const createGen2Renderer = ({ importFrom: './auth/resource', userPoolOverrides: auth?.userPoolOverrides, guestLogin: auth?.guestLogin, + identityPoolName: auth?.identityPoolName, oAuthFlows: auth?.oAuthFlows, readAttributes: auth?.readAttributes, writeAttributes: auth?.writeAttributes, diff --git a/packages/amplify-migration/src/app_auth_definition_fetcher.ts b/packages/amplify-migration/src/app_auth_definition_fetcher.ts index f9b7b2a0d80..76fb81d24a0 100644 --- a/packages/amplify-migration/src/app_auth_definition_fetcher.ts +++ b/packages/amplify-migration/src/app_auth_definition_fetcher.ts @@ -84,7 +84,7 @@ export class AppAuthDefinitionFetcher { }), ); - const { AllowUnauthenticatedIdentities: guestLogin } = await this.cognitoIdentityPoolClient.send( + const { AllowUnauthenticatedIdentities: guestLogin, IdentityPoolName: identityPoolName } = await this.cognitoIdentityPoolClient.send( new DescribeIdentityPoolCommand({ IdentityPoolId: resourcesByLogicalId['IdentityPool'].PhysicalResourceId, }), @@ -95,6 +95,7 @@ export class AppAuthDefinitionFetcher { assert(userPool, 'User pool not found'); return getAuthDefinition({ userPool, + identityPoolName, identityProviders, identityProvidersDetails, identityGroups,