The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
fix(twenty-server): honor the identity provider's advertised id_token signing algorithm (#24165)
Manoj1dfc2413fc93
diff --git a/packages/twenty-server/src/engine/core-modules/sso/services/sso.service.ts b/packages/twenty-server/src/engine/core-modules/sso/services/sso.service.tsindex 9fb8d241d30..2c0f2ee6507 100644--- a/packages/twenty-server/src/engine/core-modules/sso/services/sso.service.ts+++ b/packages/twenty-server/src/engine/core-modules/sso/services/sso.service.ts@@ -5,6 +5,7 @@ import { InjectRepository } from '@nestjs/typeorm';import { msg } from '@lingui/core/macro';import { custom, Issuer } from 'openid-client';+import { isDefined } from 'twenty-shared/utils';import { Repository } from 'typeorm';import {@@ -25,6 +26,7 @@ import {type SamlConfiguration,type SsoConfiguration,} from 'src/engine/core-modules/sso/types/sso-configurations.type';+import { resolveIdTokenSigningAlgorithm } from 'src/engine/core-modules/sso/utils/resolve-id-token-signing-algorithm.util';import { TwentyConfigService } from 'src/engine/core-modules/twenty-config/twenty-config.service';@Injectable()@@ -216,11 +218,18 @@ export class SsoService {issuer[custom.http_options] = this.oidcHttpOptions;+ const idTokenSigningAlgorithm = resolveIdTokenSigningAlgorithm(+ issuer.metadata,+ );+const client = new issuer.Client({client_id: identityProvider.clientID,client_secret: identityProvider.clientSecret,redirect_uris: [this.buildCallbackUrl(identityProvider)],response_types: [OidcResponseType.CODE],+ ...(isDefined(idTokenSigningAlgorithm) && {+ id_token_signed_response_alg: idTokenSigningAlgorithm,+ }),});client[custom.http_options] = this.oidcHttpOptions;diff --git a/packages/twenty-server/src/engine/core-modules/sso/utils/__tests__/resolve-id-token-signing-algorithm.util.spec.ts b/packages/twenty-server/src/engine/core-modules/sso/utils/__tests__/resolve-id-token-signing-algorithm.util.spec.tsnew file mode 100644index 00000000000..99d142479e0--- /dev/null+++ b/packages/twenty-server/src/engine/core-modules/sso/utils/__tests__/resolve-id-token-signing-algorithm.util.spec.ts@@ -0,0 +1,48 @@+/* @license Enterprise */++import { type IssuerMetadata } from 'openid-client';++import { resolveIdTokenSigningAlgorithm } from 'src/engine/core-modules/sso/utils/resolve-id-token-signing-algorithm.util';++const buildIssuerMetadata = (+ idTokenSigningAlgorithms?: unknown,+): IssuerMetadata => ({+ issuer: 'https://idp.example.com',+ ...(idTokenSigningAlgorithms !== undefined && {+ id_token_signing_alg_values_supported: idTokenSigningAlgorithms,+ }),+});++describe('resolveIdTokenSigningAlgorithm', () => {+ it('uses the algorithm the issuer advertises when it does not support RS256', () => {+ expect(resolveIdTokenSigningAlgorithm(buildIssuerMetadata(['ES384']))).toBe(+ 'ES384',+ );+ });++ it('prefers RS256 when the issuer supports it', () => {+ expect(+ resolveIdTokenSigningAlgorithm(buildIssuerMetadata(['ES384', 'RS256'])),+ ).toBe('RS256');+ });++ it('never picks unsigned id_tokens', () => {+ expect(+ resolveIdTokenSigningAlgorithm(buildIssuerMetadata(['none', 'ES256'])),+ ).toBe('ES256');+ expect(+ resolveIdTokenSigningAlgorithm(buildIssuerMetadata(['none'])),+ ).toBeUndefined();+ });++ it.each([undefined, 'RS256', [], [42, '']])(+ 'leaves the library default when the advertised list is %p',+ (idTokenSigningAlgorithms) => {+ expect(+ resolveIdTokenSigningAlgorithm(+ buildIssuerMetadata(idTokenSigningAlgorithms),+ ),+ ).toBeUndefined();+ },+ );+});diff --git a/packages/twenty-server/src/engine/core-modules/sso/utils/resolve-id-token-signing-algorithm.util.ts b/packages/twenty-server/src/engine/core-modules/sso/utils/resolve-id-token-signing-algorithm.util.tsnew file mode 100644index 00000000000..edec22e2e07--- /dev/null+++ b/packages/twenty-server/src/engine/core-modules/sso/utils/resolve-id-token-signing-algorithm.util.ts@@ -0,0 +1,29 @@+/* @license Enterprise */++import { isNonEmptyString } from '@sniptt/guards';+import { type IssuerMetadata } from 'openid-client';++const PREFERRED_ID_TOKEN_SIGNING_ALGORITHM = 'RS256';++export const resolveIdTokenSigningAlgorithm = (+ issuerMetadata: IssuerMetadata,+): string | undefined => {+ const supportedAlgorithms =+ issuerMetadata.id_token_signing_alg_values_supported;++ if (!Array.isArray(supportedAlgorithms)) {+ return undefined;+ }++ // An unsigned id_token would be accepted without any signature check+ const signedAlgorithms = supportedAlgorithms.filter(+ (algorithm): algorithm is string =>+ isNonEmptyString(algorithm) && algorithm !== 'none',+ );++ if (signedAlgorithms.includes(PREFERRED_ID_TOKEN_SIGNING_ALGORITHM)) {+ return PREFERRED_ID_TOKEN_SIGNING_ALGORITHM;+ }++ return signedAlgorithms[0];+};