-
Notifications
You must be signed in to change notification settings - Fork 114
BE-476: Add Google OIDC SSO support via Ory Kratos #8576
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
TimDiekmann
wants to merge
9
commits into
main
Choose a base branch
from
t/be-476-set-up-google-oidc-sso
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
65d5f2d
BE-476: Add Google OIDC SSO support via Ory Kratos
TimDiekmann a9bdbd8
BE-476: Bump Kratos from v25.4 to v26.2
TimDiekmann 8b25dbe
BE-476: Improve account linking UX on sign-in page
TimDiekmann bab6ec8
BE-476: Address review feedback
TimDiekmann 0c81236
BE-476: Fix type mismatch in SsoProviderButtons onFlowError prop
TimDiekmann bf57e4d
BE-476: Custom account linking message and error handling fixes
TimDiekmann 642c7ac
BE-476: Improve account linking UX and fix review feedback
TimDiekmann 50a0fe4
BE-476: Style flow messages based on type (red for errors, blue for iβ¦
TimDiekmann d4c797b
BE-476: Guard email claim in Google Jsonnet mapper, fix ESLint id-length
TimDiekmann File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| FROM oryd/kratos:v25.4 | ||
| FROM oryd/kratos:v26.2 | ||
|
|
||
| USER root | ||
|
|
||
|
|
||
19 changes: 19 additions & 0 deletions
19
apps/hash-external-services/kratos/hooks/oidc.google.jsonnet
This file contains hidden or 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,19 @@ | ||
| local claims = std.extVar('claims'); | ||
|
|
||
| local email = | ||
| if "email" in claims && claims.email != "" then claims.email | ||
| else error "Google OIDC: no email claim found in token"; | ||
|
|
||
| { | ||
| identity: { | ||
| traits: { | ||
| emails: [email], | ||
| }, | ||
| verified_addresses: if "email_verified" in claims && claims.email_verified then [ | ||
| { | ||
| value: email, | ||
| via: "email", | ||
| }, | ||
| ] else [], | ||
| }, | ||
| } |
This file contains hidden or 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
41 changes: 41 additions & 0 deletions
41
apps/hash-frontend/src/pages/shared/format-kratos-message.tsx
This file contains hidden or 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,41 @@ | ||
| import type { UiText } from "@ory/client"; | ||
| import type { ReactNode } from "react"; | ||
|
|
||
| export const providerDisplayNames: Record<string, string> = { | ||
| google: "Google", | ||
| apple: "Apple", | ||
| microsoft: "Microsoft", | ||
| github: "GitHub", | ||
| gitlab: "GitLab", | ||
| }; | ||
|
|
||
| /** | ||
| * Formats a Kratos UI message for display, replacing machine-generated | ||
| * messages with human-friendly versions where possible. | ||
| */ | ||
| export const formatKratosMessage = (message: UiText): ReactNode => { | ||
| const context = message.context as Record<string, unknown> | undefined; | ||
|
|
||
| // Account linking (Kratos message 1010016): | ||
| // Original: 'You tried to sign in with "email", but that email is already | ||
| // used by another account...' | ||
| if (message.id === 1010016 && context) { | ||
| const email = (context.duplicateIdentifier ?? | ||
| context.duplicate_identifier) as string | undefined; | ||
| const providerId = context.provider as string | undefined; | ||
| const provider = providerId | ||
| ? (providerDisplayNames[providerId] ?? providerId) | ||
| : undefined; | ||
|
|
||
| return ( | ||
| <> | ||
| An account with {email ? <strong>{email}</strong> : "this email"}{" "} | ||
| already exists. Sign in with your password to link{" "} | ||
| {provider ? <strong>{provider}</strong> : "the external provider"} as | ||
| another way to sign in. | ||
| </> | ||
| ); | ||
| } | ||
|
|
||
| return message.text; | ||
| }; |
125 changes: 125 additions & 0 deletions
125
apps/hash-frontend/src/pages/shared/sso-provider-buttons.tsx
This file contains hidden or 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,125 @@ | ||
| import type { SvgIconProps } from "@mui/material"; | ||
| import { Box, Typography } from "@mui/material"; | ||
| import type { LoginFlow } from "@ory/client"; | ||
| import { isUiNodeInputAttributes } from "@ory/integrations/ui"; | ||
| import type { AxiosError } from "axios"; | ||
| import type { FunctionComponent } from "react"; | ||
|
|
||
| import { AppleIcon } from "../../shared/icons/apple-icon"; | ||
| import { GitHubIcon } from "../../shared/icons/github-icon"; | ||
| import { GitLabIcon } from "../../shared/icons/gitlab-icon"; | ||
| import { GoogleIcon } from "../../shared/icons/google-icon"; | ||
| import { MicrosoftIcon } from "../../shared/icons/microsoft-icon"; | ||
| import { Button } from "../../shared/ui"; | ||
| import { providerDisplayNames } from "./format-kratos-message"; | ||
| import { mustGetCsrfTokenFromFlow, oryKratosClient } from "./ory-kratos"; | ||
|
|
||
| const providerIcons: Record<string, FunctionComponent<SvgIconProps>> = { | ||
| google: GoogleIcon, | ||
| apple: AppleIcon, | ||
| microsoft: MicrosoftIcon, | ||
| github: GitHubIcon, | ||
| gitlab: GitLabIcon, | ||
| }; | ||
|
|
||
| const ssoButtonSx = { | ||
| borderRadius: 2, | ||
| border: "1px solid", | ||
| borderColor: "gray.30", | ||
| color: "gray.90", | ||
| fontWeight: 500, | ||
| px: 2, | ||
| py: 1, | ||
| minWidth: 0, | ||
| "& .MuiButton-startIcon": { | ||
| display: "flex", | ||
| alignItems: "center", | ||
| }, | ||
| "&:hover": { | ||
| borderColor: "gray.50", | ||
| background: "gray.10", | ||
| }, | ||
| } as const; | ||
|
|
||
| type FlowErrorHandler = (err: AxiosError) => void | Promise<void>; | ||
|
|
||
| export const SsoProviderButtons: FunctionComponent<{ | ||
| flow: LoginFlow; | ||
| onFlowError: FlowErrorHandler; | ||
| }> = ({ flow, onFlowError }) => { | ||
| const oidcNodes = flow.ui.nodes.filter(({ group }) => group === "oidc"); | ||
|
|
||
| if (oidcNodes.length === 0) { | ||
| return null; | ||
| } | ||
|
|
||
| const handleProviderClick = (provider: string) => { | ||
| const csrf_token = mustGetCsrfTokenFromFlow(flow); | ||
| void oryKratosClient | ||
| .updateLoginFlow({ | ||
TimDiekmann marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| flow: flow.id, | ||
| updateLoginFlowBody: { | ||
| method: "oidc", | ||
| provider, | ||
| csrf_token, | ||
| }, | ||
| }) | ||
| .catch((err: AxiosError) => { | ||
| const data = err.response?.data as | ||
| | { redirect_browser_to?: string } | ||
| | undefined; | ||
| if (err.response?.status === 422 && data?.redirect_browser_to) { | ||
| window.location.href = data.redirect_browser_to; | ||
| return; | ||
| } | ||
| void onFlowError(err); | ||
| }); | ||
cursor[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }; | ||
cursor[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| return ( | ||
| <Box sx={{ mt: 4, maxWidth: 350 }}> | ||
| <Typography | ||
| sx={{ | ||
| color: "gray.70", | ||
| fontSize: 14, | ||
| mb: 2, | ||
| }} | ||
| > | ||
| If you use SSO, or have previously linked your account to another | ||
| service, sign in with them below | ||
| </Typography> | ||
| <Box | ||
| sx={{ | ||
| display: "grid", | ||
| gridTemplateColumns: "repeat(3, 1fr)", | ||
| gap: 1, | ||
| }} | ||
| > | ||
| {oidcNodes.map((node) => { | ||
| const attrs = node.attributes; | ||
| if (!isUiNodeInputAttributes(attrs)) { | ||
| return null; | ||
| } | ||
| const providerId = attrs.value as string; | ||
| const providerName = providerDisplayNames[providerId] ?? providerId; | ||
| const Icon = providerIcons[providerId]; | ||
| return ( | ||
| <Button | ||
| key={providerId} | ||
| type="button" | ||
| variant="tertiary" | ||
| size="small" | ||
| sx={ssoButtonSx} | ||
| startIcon={ | ||
| Icon ? <Icon sx={{ width: 20, height: 20 }} /> : undefined | ||
| } | ||
| onClick={() => handleProviderClick(providerId)} | ||
| > | ||
| {providerName} | ||
| </Button> | ||
| ); | ||
| })} | ||
| </Box> | ||
| </Box> | ||
| ); | ||
| }; | ||
This file contains hidden or 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 hidden or 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,12 @@ | ||
| import type { SvgIconProps } from "@mui/material"; | ||
| import { SvgIcon } from "@mui/material"; | ||
| import type { FunctionComponent } from "react"; | ||
|
|
||
| export const AppleIcon: FunctionComponent<SvgIconProps> = (props) => ( | ||
| <SvgIcon {...props} viewBox="0 0 24 24"> | ||
| <path | ||
| d="M17.05 20.28c-.98.95-2.05.88-3.08.4-1.09-.5-2.08-.53-3.23 0-1.44.62-2.2.44-3.06-.4C3.79 16.17 4.36 9.02 8.82 8.78c1.28.07 2.17.74 2.92.78.98-.2 1.92-.89 3-.81 1.51.12 2.63.71 3.36 1.78-3.07 1.87-2.34 5.98.44 7.13-.58 1.52-1.33 3.02-2.49 4.62zM12.03 8.7c-.15-2.34 1.81-4.29 4.01-4.48.3 2.65-2.38 4.63-4.01 4.48z" | ||
| fill="currentColor" | ||
| /> | ||
| </SvgIcon> | ||
| ); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.