|
| 1 | +import { OidcAuthResult } from '@backstage/plugin-auth-backend-module-oidc-provider'; |
| 2 | +import { |
| 3 | + AuthResolverContext, |
| 4 | + createSignInResolverFactory, |
| 5 | + OAuthAuthenticatorResult, |
| 6 | + SignInInfo, |
| 7 | +} from '@backstage/plugin-auth-node'; |
| 8 | + |
| 9 | +import { decodeJwt } from 'jose'; |
| 10 | + |
| 11 | +const KEYCLOAK_ID_ANNOTATION = 'keycloak.org/id'; |
| 12 | +const PING_IDENTITY_ID_ANNOTATION = 'pingidentity.org/id'; |
| 13 | + |
| 14 | +/** |
| 15 | + * Creates an OIDC sign-in resolver that looks up the user using a specific annotation key. |
| 16 | + * |
| 17 | + * @param annotationKey - The annotation key to match the user's `sub` claim. |
| 18 | + * @param providerName - The name of the identity provider to report in error message if the `sub` claim is missing. |
| 19 | + */ |
| 20 | +const createOidcSubClaimResolver = (userIdKey: string, providerName: string) => |
| 21 | + createSignInResolverFactory({ |
| 22 | + create() { |
| 23 | + return async ( |
| 24 | + info: SignInInfo<OAuthAuthenticatorResult<OidcAuthResult>>, |
| 25 | + ctx: AuthResolverContext, |
| 26 | + ) => { |
| 27 | + const sub = info.result.fullProfile.userinfo.sub; |
| 28 | + if (!sub) { |
| 29 | + throw new Error( |
| 30 | + `The user profile from ${providerName} is missing a 'sub' claim, likely due to a misconfiguration in the provider. Please contact your system administrator for assistance.`, |
| 31 | + ); |
| 32 | + } |
| 33 | + |
| 34 | + const idToken = info.result.fullProfile.tokenset.id_token; |
| 35 | + if (!idToken) { |
| 36 | + throw new Error( |
| 37 | + `The user ID token from ${providerName} is missing a 'sub' claim, likely due to a misconfiguration in the provider. Please contact your system administrator for assistance.`, |
| 38 | + ); |
| 39 | + } |
| 40 | + |
| 41 | + const subFromIdToken = decodeJwt(idToken)?.sub; |
| 42 | + if (sub !== subFromIdToken) { |
| 43 | + throw new Error( |
| 44 | + `There was a problem verifying your identity with ${providerName} due to a mismatching 'sub' claim. Please contact your system administrator for assistance.`, |
| 45 | + ); |
| 46 | + } |
| 47 | + |
| 48 | + return ctx.signInWithCatalogUser({ |
| 49 | + annotations: { [userIdKey]: sub }, |
| 50 | + }); |
| 51 | + }; |
| 52 | + }, |
| 53 | + }); |
| 54 | + |
| 55 | +/** |
| 56 | + * Additional sign-in resolvers for the Oidc auth provider. |
| 57 | + * |
| 58 | + * @public |
| 59 | + */ |
| 60 | +export namespace rhdhSignInResolvers { |
| 61 | + /** |
| 62 | + * An OIDC resolver that looks up the user using their Keycloak user ID. |
| 63 | + */ |
| 64 | + export const oidcSubClaimMatchingKeycloakUserId = createOidcSubClaimResolver( |
| 65 | + KEYCLOAK_ID_ANNOTATION, |
| 66 | + 'Keycloak', |
| 67 | + ); |
| 68 | + |
| 69 | + /** |
| 70 | + * An OIDC resolver that looks up the user using their Ping Identity user ID. |
| 71 | + */ |
| 72 | + export const oidcSubClaimMatchingPingIdentityUserId = |
| 73 | + createOidcSubClaimResolver(PING_IDENTITY_ID_ANNOTATION, 'Ping Identity'); |
| 74 | +} |
0 commit comments