-
Notifications
You must be signed in to change notification settings - Fork 821
fix: add idp codegen and fix a couple of minor bugs #14143
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -201,11 +201,25 @@ export class BackendSynthesizer { | |
| userPoolClientAttributesMap.set('ExplicitAuthFlows', 'authFlows'); | ||
| userPoolClientAttributesMap.set('AllowedOAuthFlows', 'flows'); | ||
|
|
||
| const nativeUserPoolClientExpressionStatement = factory.createExpressionStatement( | ||
| factory.createCallExpression( | ||
| factory.createPropertyAccessExpression(factory.createIdentifier('userPool'), factory.createIdentifier('addClient')), | ||
| undefined, | ||
| [factory.createStringLiteral('NativeAppClient'), this.createNestedObjectExpression(userPoolClient, userPoolClientAttributesMap)], | ||
| const userPoolClientDeclaration = factory.createVariableStatement( | ||
| undefined, | ||
| factory.createVariableDeclarationList( | ||
| [ | ||
| factory.createVariableDeclaration( | ||
| factory.createIdentifier('userPoolClient'), | ||
| undefined, | ||
| undefined, | ||
| factory.createCallExpression( | ||
| factory.createPropertyAccessExpression(factory.createIdentifier('userPool'), factory.createIdentifier('addClient')), | ||
| undefined, | ||
| [ | ||
| factory.createStringLiteral('NativeAppClient'), | ||
| this.createNestedObjectExpression(userPoolClient, userPoolClientAttributesMap), | ||
| ], | ||
| ), | ||
| ), | ||
| ], | ||
| ts.NodeFlags.Const, | ||
| ), | ||
| ); | ||
|
|
||
|
|
@@ -225,7 +239,147 @@ export class BackendSynthesizer { | |
| } | ||
| } | ||
|
|
||
| return nativeUserPoolClientExpressionStatement; | ||
| return userPoolClientDeclaration; | ||
| } | ||
|
|
||
| private createPropertyAccessChain(identifiers: string[]): ts.Expression { | ||
| return identifiers | ||
| .slice(1) | ||
| .reduce<ts.Expression>( | ||
| (acc, curr) => factory.createPropertyAccessExpression(acc, factory.createIdentifier(curr)), | ||
| factory.createIdentifier(identifiers[0]), | ||
| ); | ||
| } | ||
|
|
||
| private createProviderSetupCode(): ts.Statement[] { | ||
| // Create const providerSetupResult = (backend.auth.stack.node.children.find(child => child.node.id === "amplifyAuth") as any).providerSetupResult; | ||
| const providerSetupDeclaration = factory.createVariableStatement( | ||
| undefined, | ||
| factory.createVariableDeclarationList( | ||
| [ | ||
| factory.createVariableDeclaration( | ||
| factory.createIdentifier('providerSetupResult'), | ||
| undefined, | ||
| undefined, | ||
| factory.createPropertyAccessExpression( | ||
| factory.createParenthesizedExpression( | ||
| factory.createAsExpression( | ||
| factory.createCallExpression( | ||
| factory.createPropertyAccessExpression( | ||
| this.createPropertyAccessChain(['backend', 'auth', 'stack', 'node', 'children']), | ||
| factory.createIdentifier('find'), | ||
| ), | ||
| undefined, | ||
| [ | ||
| factory.createArrowFunction( | ||
| undefined, | ||
| undefined, | ||
| [factory.createParameterDeclaration(undefined, undefined, factory.createIdentifier('child'))], | ||
| undefined, | ||
| factory.createToken(ts.SyntaxKind.EqualsGreaterThanToken), | ||
| factory.createBinaryExpression( | ||
| this.createPropertyAccessChain(['child', 'node', 'id']), | ||
| factory.createToken(ts.SyntaxKind.EqualsEqualsEqualsToken), | ||
| factory.createStringLiteral('amplifyAuth'), | ||
| ), | ||
| ), | ||
| ], | ||
| ), | ||
| factory.createKeywordTypeNode(ts.SyntaxKind.AnyKeyword), | ||
| ), | ||
| ), | ||
| factory.createIdentifier('providerSetupResult'), | ||
| ), | ||
| ), | ||
| ], | ||
| ts.NodeFlags.Const, | ||
| ), | ||
| ); | ||
|
|
||
| // Create Object.keys(providerSetupResult).forEach(...) | ||
| const forEachStatement = factory.createExpressionStatement( | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we move the for loop contents into a separate method to reduce the size of this method? |
||
| factory.createCallExpression( | ||
| factory.createPropertyAccessExpression( | ||
| factory.createCallExpression( | ||
| factory.createPropertyAccessExpression(factory.createIdentifier('Object'), factory.createIdentifier('keys')), | ||
| undefined, | ||
| [factory.createIdentifier('providerSetupResult')], | ||
| ), | ||
| factory.createIdentifier('forEach'), | ||
| ), | ||
| undefined, | ||
| [ | ||
| factory.createArrowFunction( | ||
| undefined, | ||
| undefined, | ||
| [factory.createParameterDeclaration(undefined, undefined, factory.createIdentifier('provider'))], | ||
| undefined, | ||
| factory.createToken(ts.SyntaxKind.EqualsGreaterThanToken), | ||
| factory.createBlock( | ||
| [ | ||
| // const providerSetupPropertyValue = providerSetupResult[provider] | ||
| factory.createVariableStatement( | ||
| undefined, | ||
| factory.createVariableDeclarationList( | ||
| [ | ||
| factory.createVariableDeclaration( | ||
| factory.createIdentifier('providerSetupPropertyValue'), | ||
| undefined, | ||
| undefined, | ||
| factory.createElementAccessExpression( | ||
| factory.createIdentifier('providerSetupResult'), | ||
| factory.createIdentifier('provider'), | ||
| ), | ||
| ), | ||
| ], | ||
| ts.NodeFlags.Const, | ||
| ), | ||
| ), | ||
| // if condition | ||
| factory.createIfStatement( | ||
| factory.createLogicalAnd( | ||
| factory.createPropertyAccessExpression( | ||
| factory.createIdentifier('providerSetupPropertyValue'), | ||
| factory.createIdentifier('node'), | ||
| ), | ||
| factory.createCallExpression( | ||
| factory.createPropertyAccessExpression( | ||
| factory.createCallExpression( | ||
| factory.createPropertyAccessExpression( | ||
| this.createPropertyAccessChain(['providerSetupPropertyValue', 'node', 'id']), | ||
| factory.createIdentifier('toLowerCase'), | ||
| ), | ||
| undefined, | ||
| [], | ||
| ), | ||
| factory.createIdentifier('endsWith'), | ||
| ), | ||
| undefined, | ||
| [factory.createStringLiteral('idp')], | ||
| ), | ||
| ), | ||
| factory.createBlock( | ||
| [ | ||
| factory.createExpressionStatement( | ||
| factory.createCallExpression( | ||
| this.createPropertyAccessChain(['userPoolClient', 'node', 'addDependency']), | ||
| undefined, | ||
| [factory.createIdentifier('providerSetupPropertyValue')], | ||
| ), | ||
| ), | ||
| ], | ||
| true, | ||
| ), | ||
| ), | ||
| ], | ||
| true, | ||
| ), | ||
| ), | ||
| ], | ||
| ), | ||
| ); | ||
|
|
||
| return [providerSetupDeclaration, forEachStatement]; | ||
| } | ||
|
|
||
| private createNestedObjectExpression(object: Record<string, any>, gen2PropertyMap: Map<string, string>): ts.ObjectLiteralExpression { | ||
|
|
@@ -743,6 +897,9 @@ export class BackendSynthesizer { | |
| const userPoolVariableStatement = this.createVariableStatement(this.createVariableDeclaration('userPool', 'auth.resources.userPool')); | ||
| nodes.push(userPoolVariableStatement); | ||
| nodes.push(this.createUserPoolClientAssignment(renderArgs.auth?.userPoolClient, imports)); | ||
|
|
||
| const idpStatements = this.createProviderSetupCode(); | ||
| nodes.push(...idpStatements); | ||
| } | ||
|
|
||
| if (renderArgs.storage && renderArgs.storage.hasS3Bucket) { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -40,7 +40,7 @@ export class AppAuthDefinitionFetcher { | |
| return JSON.parse(contents); | ||
| }; | ||
|
|
||
| private getReferenceAuth = async () => { | ||
| private authCategory = async () => { | ||
| const backendEnvironment = await this.backendEnvironmentResolver.selectBackendEnvironment(); | ||
| if (!backendEnvironment?.deploymentArtifacts) return undefined; | ||
| const currentCloudBackendDirectory = await this.ccbFetcher.getCurrentCloudBackend(backendEnvironment.deploymentArtifacts); | ||
|
|
@@ -51,12 +51,19 @@ export class AppAuthDefinitionFetcher { | |
| } | ||
|
|
||
| const amplifyMeta = (await this.readJsonFile(amplifyMetaPath)) ?? {}; | ||
| const isImported = | ||
| 'auth' in amplifyMeta && | ||
| Object.keys(amplifyMeta.auth).length > 0 && | ||
| Object.entries(amplifyMeta.auth).some( | ||
| ([, value]) => typeof value === 'object' && value !== null && 'serviceType' in value && value.serviceType === 'imported', | ||
| ); | ||
| const authCategory = 'auth' in amplifyMeta && Object.keys(amplifyMeta.auth).length > 0; | ||
|
|
||
| if (authCategory) { | ||
| return amplifyMeta.auth; | ||
| } else { | ||
| return undefined; | ||
| } | ||
| }; | ||
|
|
||
| private getReferenceAuth = async (authCategory: any) => { | ||
| const isImported = Object.entries(authCategory).some( | ||
| ([, value]) => typeof value === 'object' && value !== null && 'serviceType' in value && value.serviceType === 'imported', | ||
| ); | ||
| if (!isImported) { | ||
| return undefined; | ||
| } | ||
|
|
@@ -65,7 +72,7 @@ export class AppAuthDefinitionFetcher { | |
| UserPoolId: userPoolId, | ||
| AppClientIDWeb: userPoolClientId, | ||
| IdentityPoolId: identityPoolId, | ||
| } = Object.keys(amplifyMeta.auth).map((key) => amplifyMeta.auth[key])[0].output; | ||
| } = Object.keys(authCategory).map((key) => authCategory[key])[0].output; | ||
| if (!userPoolId && !userPoolClientId && !identityPoolId) { | ||
| throw new Error('No user pool or identity pool found for import.'); | ||
| } | ||
|
|
@@ -116,7 +123,11 @@ export class AppAuthDefinitionFetcher { | |
| }; | ||
|
|
||
| getDefinition = async (): Promise<AuthDefinition | undefined> => { | ||
| const referenceAuth = await this.getReferenceAuth(); | ||
| const authCategory = await this.authCategory(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: rename |
||
| if (!authCategory) { | ||
| return undefined; | ||
| } | ||
| const referenceAuth = await this.getReferenceAuth(authCategory); | ||
| if (referenceAuth) { | ||
| return { | ||
| referenceAuth, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -230,9 +230,10 @@ export async function updateAmplifyYmlFile(amplifyClient: AmplifyClient, appId: | |
|
|
||
| assert(getAppResponse.app, 'App not found'); | ||
| const buildSpec = getAppResponse.app.buildSpec; | ||
| assert(buildSpec, 'buildSpec not found in the app'); | ||
|
|
||
| await writeToAmplifyYmlFile(amplifyYmlPath, buildSpec); | ||
| if (buildSpec) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nice! |
||
| await writeToAmplifyYmlFile(amplifyYmlPath, buildSpec); | ||
| } | ||
| } else { | ||
| // Throw the original error if it's not related to file not found | ||
| throw error; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: move
providerSetupResultto a constant since it's used in 2 places.