From fa89d33252a40b71613abc14bb928747e5daaf30 Mon Sep 17 00:00:00 2001 From: wangsijie Date: Tue, 23 Jan 2024 13:14:03 +0800 Subject: [PATCH] feat(core): sync site configs and oidc metadata for custom domain (#5260) --- .../core/src/libraries/protected-app.test.ts | 27 +++++++++++++++ packages/core/src/libraries/protected-app.ts | 33 ++++++++++++++----- ...application-protected-app-metadata.test.ts | 16 +++++++-- .../application-protected-app-metadata.ts | 19 +++++++++-- 4 files changed, 83 insertions(+), 12 deletions(-) diff --git a/packages/core/src/libraries/protected-app.test.ts b/packages/core/src/libraries/protected-app.test.ts index e36496cee..14cdf0354 100644 --- a/packages/core/src/libraries/protected-app.test.ts +++ b/packages/core/src/libraries/protected-app.test.ts @@ -109,6 +109,33 @@ describe('syncAppConfigsToRemote()', () => { } ); }); + + it('should sync custom domains configs to remote', async () => { + findApplicationById.mockResolvedValueOnce({ + ...mockProtectedApplication, + protectedAppMetadata: { + ...mockProtectedApplication.protectedAppMetadata, + customDomains: [mockCustomDomain], + }, + }); + await expect(syncAppConfigsToRemote(mockProtectedApplication.id)).resolves.not.toThrow(); + const { protectedAppMetadata, id, secret } = mockProtectedApplication; + expect(updateProtectedAppSiteConfigs).toHaveBeenLastCalledWith( + protectedAppConfigProviderConfig, + mockCustomDomain.domain, + { + ...protectedAppMetadata, + host: mockCustomDomain.domain, + sdkConfig: { + appId: id, + appSecret: secret, + // Avoid mocking envset + // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment + endpoint: expect.anything(), + }, + } + ); + }); }); describe('checkAndBuildProtectedAppData()', () => { diff --git a/packages/core/src/libraries/protected-app.ts b/packages/core/src/libraries/protected-app.ts index 25805daf0..55e3175f6 100644 --- a/packages/core/src/libraries/protected-app.ts +++ b/packages/core/src/libraries/protected-app.ts @@ -113,18 +113,35 @@ export const createProtectedAppLibrary = (queries: Queries) => { return; } + const { customDomains, ...rest } = protectedAppMetadata; + + const siteConfigs = { + ...rest, + sdkConfig: { + appId: id, + appSecret: secret, + endpoint: EnvSet.values.endpoint.href, + }, + }; + + // Update default host (subdomain of the default domain) await updateProtectedAppSiteConfigs( protectedAppConfigProviderConfig, protectedAppMetadata.host, - { - ...protectedAppMetadata, - sdkConfig: { - appId: id, - appSecret: secret, - endpoint: EnvSet.values.endpoint.href, - }, - } + siteConfigs ); + + // Update custom domains sites + if (customDomains && customDomains.length > 0) { + await Promise.all( + customDomains.map(async ({ domain }) => { + await updateProtectedAppSiteConfigs(protectedAppConfigProviderConfig, domain, { + ...siteConfigs, + host: domain, + }); + }) + ); + } }; /** diff --git a/packages/core/src/routes/applications/application-protected-app-metadata.test.ts b/packages/core/src/routes/applications/application-protected-app-metadata.test.ts index 2b5ab21aa..a9415e40b 100644 --- a/packages/core/src/routes/applications/application-protected-app-metadata.test.ts +++ b/packages/core/src/routes/applications/application-protected-app-metadata.test.ts @@ -37,6 +37,7 @@ const syncAppCustomDomainStatus = jest.fn(async () => ({ customDomains: [mockDomainResponse], }, })); +const syncAppConfigsToRemote = jest.fn(); await mockIdGenerators(); @@ -51,7 +52,7 @@ const tenantContext = new MockTenant( }, undefined, { - protectedApps: { addDomainToRemote, syncAppCustomDomainStatus }, + protectedApps: { addDomainToRemote, syncAppCustomDomainStatus, syncAppConfigsToRemote }, applications: { validateProtectedApplicationById: jest.fn() }, } ); @@ -78,7 +79,7 @@ describe('application protected app metadata routes', () => { }); describe('POST /applications/:applicationId/protected-app-metadata/custom-domains', () => { - it('should return 201', async () => { + it('should return 201 and update OIDC metadata and sync site configs', async () => { const response = await requester .post(`/applications/${mockProtectedApplication.id}/protected-app-metadata/custom-domains`) .send({ @@ -90,7 +91,18 @@ describe('application protected app metadata routes', () => { ...mockProtectedApplication.protectedAppMetadata, customDomains: [mockDomainResponse], }, + oidcClientMetadata: { + postLogoutRedirectUris: [ + `https://${mockProtectedApplication.protectedAppMetadata.host}`, + `https://${mockDomain}`, + ], + redirectUris: [ + `https://${mockProtectedApplication.protectedAppMetadata.host}/callback`, + `https://${mockDomain}/callback`, + ], + }, }); + expect(syncAppConfigsToRemote).toHaveBeenCalledWith(mockProtectedApplication.id); }); it('throw when domain exists', async () => { diff --git a/packages/core/src/routes/applications/application-protected-app-metadata.ts b/packages/core/src/routes/applications/application-protected-app-metadata.ts index 060fa685f..867b776e6 100644 --- a/packages/core/src/routes/applications/application-protected-app-metadata.ts +++ b/packages/core/src/routes/applications/application-protected-app-metadata.ts @@ -20,7 +20,7 @@ export default function applicationProtectedAppMetadataRoutes @@ -68,7 +68,7 @@ export default function applicationProtectedAppMetadataRoutes