-
Notifications
You must be signed in to change notification settings - Fork 154
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Jessica He <[email protected]>
- Loading branch information
1 parent
16746e2
commit 6ad8b29
Showing
7 changed files
with
96 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import { OidcAuthResult } from '@backstage/plugin-auth-backend-module-oidc-provider'; | ||
import { | ||
AuthResolverContext, | ||
createSignInResolverFactory, | ||
OAuthAuthenticatorResult, | ||
SignInInfo, | ||
} from '@backstage/plugin-auth-node'; | ||
|
||
import { decodeJwt } from 'jose'; | ||
|
||
const KEYCLOAK_ID_ANNOTATION = 'keycloak.org/id'; | ||
const PING_IDENTITY_ID_ANNOTATION = 'pingidentity.org/id'; | ||
|
||
/** | ||
* Creates an OIDC sign-in resolver that looks up the user using a specific annotation key. | ||
* | ||
* @param annotationKey - The annotation key to match the user's `sub` claim. | ||
* @param providerName - The name of the identity provider to report in error message if the `sub` claim is missing. | ||
*/ | ||
const createOidcSubClaimResolver = (userIdKey: string, providerName: string) => | ||
createSignInResolverFactory({ | ||
create() { | ||
return async ( | ||
info: SignInInfo<OAuthAuthenticatorResult<OidcAuthResult>>, | ||
ctx: AuthResolverContext, | ||
) => { | ||
const sub = info.result.fullProfile.userinfo.sub; | ||
if (!sub) { | ||
throw new Error( | ||
`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.`, | ||
); | ||
} | ||
|
||
const idToken = info.result.fullProfile.tokenset.id_token; | ||
if (!idToken) { | ||
throw new Error( | ||
`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.`, | ||
); | ||
} | ||
|
||
const subFromIdToken = decodeJwt(idToken)?.sub; | ||
if (sub !== subFromIdToken) { | ||
throw new Error( | ||
`There was a problem verifying your identity with ${providerName} due to a mismatching 'sub' claim. Please contact your system administrator for assistance.`, | ||
); | ||
} | ||
|
||
return ctx.signInWithCatalogUser({ | ||
annotations: { [userIdKey]: sub }, | ||
}); | ||
}; | ||
}, | ||
}); | ||
|
||
/** | ||
* Additional sign-in resolvers for the Oidc auth provider. | ||
* | ||
* @public | ||
*/ | ||
export namespace rhdhSignInResolvers { | ||
/** | ||
* An OIDC resolver that looks up the user using their Keycloak user ID. | ||
*/ | ||
export const oidcSubClaimMatchingKeycloakUserId = createOidcSubClaimResolver( | ||
KEYCLOAK_ID_ANNOTATION, | ||
'Keycloak', | ||
); | ||
|
||
/** | ||
* An OIDC resolver that looks up the user using their Ping Identity user ID. | ||
*/ | ||
export const oidcSubClaimMatchingPingIdentityUserId = | ||
createOidcSubClaimResolver(PING_IDENTITY_ID_ANNOTATION, 'Ping Identity'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters