The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
fix(oauth): provision the Twenty CLI OAuth client on non-seeded servers (#26389)
martmull22b76f949110
diff --git a/packages/twenty-server/src/engine/core-modules/application/application-oauth/controllers/__tests__/oauth-discovery.controller.spec.ts b/packages/twenty-server/src/engine/core-modules/application/application-oauth/controllers/__tests__/oauth-discovery.controller.spec.tsindex 38a089c8c61..20223d15f94 100644--- a/packages/twenty-server/src/engine/core-modules/application/application-oauth/controllers/__tests__/oauth-discovery.controller.spec.ts+++ b/packages/twenty-server/src/engine/core-modules/application/application-oauth/controllers/__tests__/oauth-discovery.controller.spec.ts@@ -10,6 +10,10 @@ import { TwentyConfigService } from 'src/engine/core-modules/twenty-config/twentdescribe('OAuthDiscoveryController', () => {let controller: OAuthDiscoveryController;+ const findOrCreateCliRegistrationMock = jest+ .fn()+ .mockResolvedValue({ oAuthClientId: 'cli-client-id' });+const buildMockRequest = (host: string, protocol = 'https') =>({protocol,@@ -37,7 +41,9 @@ describe('OAuthDiscoveryController', () => {},{provide: ApplicationRegistrationService,- useValue: { findOneByUniversalIdentifierGlobal: jest.fn() },+ useValue: {+ findOrCreateCliRegistration: findOrCreateCliRegistrationMock,+ },},],}).compile();@@ -93,7 +99,9 @@ describe('OAuthDiscoveryController', () => {},{provide: ApplicationRegistrationService,- useValue: { findOneByUniversalIdentifierGlobal: jest.fn() },+ useValue: {+ findOrCreateCliRegistration: findOrCreateCliRegistrationMock,+ },},],}).compile();@@ -133,5 +141,15 @@ describe('OAuthDiscoveryController', () => {authorization_response_iss_parameter_supported: true,});});++ it('exposes the CLI client id, provisioning the registration if missing', async () => {+ const request = buildMockRequest('crm.acme.com');++ expect(+ await controller.getAuthorizationServerMetadata(request),+ ).toMatchObject({ cli_client_id: 'cli-client-id' });+ expect(findOrCreateCliRegistrationMock).toHaveBeenCalledTimes(1);+ expect(findOrCreateCliRegistrationMock).toHaveBeenCalledWith();+ });});});diff --git a/packages/twenty-server/src/engine/core-modules/application/application-oauth/controllers/oauth-discovery.controller.ts b/packages/twenty-server/src/engine/core-modules/application/application-oauth/controllers/oauth-discovery.controller.tsindex 84e00ad25a8..af00995499b 100644--- a/packages/twenty-server/src/engine/core-modules/application/application-oauth/controllers/oauth-discovery.controller.ts+++ b/packages/twenty-server/src/engine/core-modules/application/application-oauth/controllers/oauth-discovery.controller.ts@@ -11,7 +11,6 @@ import { NoPermissionGuard } from 'src/engine/guards/no-permission.guard';import { PublicEndpointGuard } from 'src/engine/guards/public-endpoint.guard';import { cleanServerUrl } from 'src/utils/clean-server-url';import { getRequestBaseUrl } from 'src/utils/get-request-base-url.util';-import { TWENTY_CLI_APPLICATION_REGISTRATION } from 'src/engine/workspace-manager/twenty-standard-application/constants/twenty-cli-application-registration.constant';@Controller(ApiPath.WellKnown)export class OAuthDiscoveryController {@@ -39,9 +38,7 @@ export class OAuthDiscoveryController {: `${authorizeBase}/authorize?iss=${encodeURIComponent(issuer)}`;const cliRegistration =- await this.applicationRegistrationService.findOneByUniversalIdentifierGlobal(- TWENTY_CLI_APPLICATION_REGISTRATION.universalIdentifier,- );+ await this.applicationRegistrationService.findOrCreateCliRegistration();return {issuer,@@ -65,9 +62,7 @@ export class OAuthDiscoveryController {// RFC 9207: advertise `iss` in authorization responses to defend against// OAuth mix-up attacks. Required by OAuth 2.1 security BCP.authorization_response_iss_parameter_supported: true,- ...(cliRegistration- ? { cli_client_id: cliRegistration.oAuthClientId }- : {}),+ cli_client_id: cliRegistration.oAuthClientId,};}diff --git a/packages/twenty-server/src/engine/core-modules/application/application-registration/application-registration.service.ts b/packages/twenty-server/src/engine/core-modules/application/application-registration/application-registration.service.tsindex 8bd00239ccb..46667151a53 100644--- a/packages/twenty-server/src/engine/core-modules/application/application-registration/application-registration.service.ts+++ b/packages/twenty-server/src/engine/core-modules/application/application-registration/application-registration.service.ts@@ -975,34 +975,51 @@ export class ApplicationRegistrationService {return registration;}- async createCliRegistrationIfNotExists(): Promise<ApplicationRegistrationEntity | null> {+ async findOrCreateCliRegistration(): Promise<ApplicationRegistrationEntity> {const existing = await this.findOneByUniversalIdentifierGlobal(TWENTY_CLI_APPLICATION_REGISTRATION.universalIdentifier,);if (isDefined(existing)) {- return null;+ return existing;}- const registration = this.applicationRegistrationRepository.create({- universalIdentifier:- TWENTY_CLI_APPLICATION_REGISTRATION.universalIdentifier,- name: TWENTY_CLI_APPLICATION_REGISTRATION.name,- oAuthClientId: v4(),- oAuthClientSecretHash: null,- oAuthRedirectUris: [],- oAuthScopes: TWENTY_CLI_APPLICATION_REGISTRATION.oAuthScopes,- ownerWorkspaceId: null,- sourceType: ApplicationRegistrationSourceType.OAUTH_ONLY,- createdByUserId: null,- });+ const insertResult = await this.applicationRegistrationRepository+ .createQueryBuilder()+ .insert()+ .into(ApplicationRegistrationEntity)+ .values({+ universalIdentifier:+ TWENTY_CLI_APPLICATION_REGISTRATION.universalIdentifier,+ name: TWENTY_CLI_APPLICATION_REGISTRATION.name,+ oAuthClientId: v4(),+ oAuthClientSecretHash: null,+ oAuthRedirectUris: [],+ oAuthScopes: TWENTY_CLI_APPLICATION_REGISTRATION.oAuthScopes,+ ownerWorkspaceId: null,+ sourceType: ApplicationRegistrationSourceType.OAUTH_ONLY,+ createdByUserId: null,+ })+ .orIgnore()+ .returning('id')+ .execute();++ if (insertResult.raw.length > 0) {+ await this.invalidateMarketplaceAppsCache();+ }- const saved =- await this.applicationRegistrationRepository.save(registration);+ const registration = await this.findOneByUniversalIdentifierGlobal(+ TWENTY_CLI_APPLICATION_REGISTRATION.universalIdentifier,+ );- await this.invalidateMarketplaceAppsCache();+ if (!isDefined(registration)) {+ throw new ApplicationException(+ 'Failed to create the Twenty CLI application registration',+ ApplicationExceptionCode.APPLICATION_NOT_FOUND,+ );+ }- return saved;+ return registration;}async findManyListedCatalogCards(): Promise<diff --git a/packages/twenty-server/src/engine/workspace-manager/dev-seeder/services/dev-seeder.service.ts b/packages/twenty-server/src/engine/workspace-manager/dev-seeder/services/dev-seeder.service.tsindex 85ce87ecaae..c1756fa300d 100644--- a/packages/twenty-server/src/engine/workspace-manager/dev-seeder/services/dev-seeder.service.ts+++ b/packages/twenty-server/src/engine/workspace-manager/dev-seeder/services/dev-seeder.service.ts@@ -109,7 +109,7 @@ export class DevSeederService {initialCursor,});- await this.applicationRegistrationService.createCliRegistrationIfNotExists();+ await this.applicationRegistrationService.findOrCreateCliRegistration();const schemaName =await this.workspaceSchemaService.createWorkspaceDBSchema(workspaceId);