-
-
Notifications
You must be signed in to change notification settings - Fork 239
feat: oidc #605
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
Merged
+2,573
−64
Merged
feat: oidc #605
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
7dc3525
chore: add oidc base config
steveiliop56 6ae7c1c
wip: authorize page
steveiliop56 97e90ea
feat: implement basic oidc functionality
steveiliop56 c817e35
refactor: implement oidc following tinyauth patterns
steveiliop56 71bc396
feat: adapt frontend to oidc flow
steveiliop56 cf1a613
fix: review comments
steveiliop56 8af233b
fix: oidc review comments
steveiliop56 46f25aa
feat: refresh token grant type support
steveiliop56 8dd731b
feat: cleanup expired oidc sessions
steveiliop56 fae1345
feat: frontend i18n
steveiliop56 9cbcd62
fix: fix typo in error screen
steveiliop56 e498ee4
tests: add basic testing
steveiliop56 fe391fc
fix: more review comments
steveiliop56 3280649
refactor: rework oidc error messages
steveiliop56 a8f57e5
feat: openid discovery endpoint
steveiliop56 63fcc65
feat: jwk endpoint
steveiliop56 01e491c
i18n: fix typo
steveiliop56 673f556
fix: more rabbit nitpicks
steveiliop56 fb705ea
fix: final review comments
steveiliop56 627fd05
i18n: authorize page error messages
steveiliop56 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
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,53 @@ | ||
| export type OIDCValues = { | ||
| scope: string; | ||
| response_type: string; | ||
| client_id: string; | ||
| redirect_uri: string; | ||
| state: string; | ||
| }; | ||
|
|
||
| interface IuseOIDCParams { | ||
| values: OIDCValues; | ||
| compiled: string; | ||
| isOidc: boolean; | ||
| missingParams: string[]; | ||
| } | ||
|
|
||
| const optionalParams: string[] = ["state"]; | ||
|
|
||
| export function useOIDCParams(params: URLSearchParams): IuseOIDCParams { | ||
| let compiled: string = ""; | ||
| let isOidc = false; | ||
| const missingParams: string[] = []; | ||
|
|
||
| const values: OIDCValues = { | ||
| scope: params.get("scope") ?? "", | ||
| response_type: params.get("response_type") ?? "", | ||
| client_id: params.get("client_id") ?? "", | ||
| redirect_uri: params.get("redirect_uri") ?? "", | ||
| state: params.get("state") ?? "", | ||
| }; | ||
|
|
||
| for (const key of Object.keys(values)) { | ||
| if (!values[key as keyof OIDCValues]) { | ||
| if (!optionalParams.includes(key)) { | ||
| missingParams.push(key); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if (missingParams.length === 0) { | ||
| isOidc = true; | ||
| } | ||
|
|
||
| if (isOidc) { | ||
| compiled = new URLSearchParams(values).toString(); | ||
| } | ||
|
|
||
| return { | ||
| values, | ||
| compiled, | ||
| isOidc, | ||
| missingParams, | ||
| }; | ||
| } | ||
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
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,199 @@ | ||
| import { useUserContext } from "@/context/user-context"; | ||
| import { useMutation, useQuery } from "@tanstack/react-query"; | ||
| import { Navigate, useNavigate } from "react-router"; | ||
| import { useLocation } from "react-router"; | ||
| import { | ||
| Card, | ||
| CardHeader, | ||
| CardTitle, | ||
| CardDescription, | ||
| CardFooter, | ||
| CardContent, | ||
| } from "@/components/ui/card"; | ||
| import { getOidcClientInfoSchema } from "@/schemas/oidc-schemas"; | ||
| import { Button } from "@/components/ui/button"; | ||
| import axios from "axios"; | ||
| import { toast } from "sonner"; | ||
| import { useOIDCParams } from "@/lib/hooks/oidc"; | ||
| import { useTranslation } from "react-i18next"; | ||
| import { TFunction } from "i18next"; | ||
| import { Mail, Shield, User, Users } from "lucide-react"; | ||
|
|
||
| type Scope = { | ||
| id: string; | ||
| name: string; | ||
| description: string; | ||
| icon: React.ReactNode; | ||
| }; | ||
|
|
||
| const scopeMapIconProps = { | ||
| className: "stroke-card stroke-2.5", | ||
| }; | ||
|
|
||
| const createScopeMap = (t: TFunction<"translation", undefined>): Scope[] => { | ||
| return [ | ||
| { | ||
| id: "openid", | ||
| name: t("openidScopeName"), | ||
| description: t("openidScopeDescription"), | ||
| icon: <Shield {...scopeMapIconProps} />, | ||
| }, | ||
| { | ||
| id: "email", | ||
| name: t("emailScopeName"), | ||
| description: t("emailScopeDescription"), | ||
| icon: <Mail {...scopeMapIconProps} />, | ||
| }, | ||
| { | ||
| id: "profile", | ||
| name: t("profileScopeName"), | ||
| description: t("profileScopeDescription"), | ||
| icon: <User {...scopeMapIconProps} />, | ||
| }, | ||
| { | ||
| id: "groups", | ||
| name: t("groupsScopeName"), | ||
| description: t("groupsScopeDescription"), | ||
| icon: <Users {...scopeMapIconProps} />, | ||
| }, | ||
| ]; | ||
| }; | ||
|
|
||
| export const AuthorizePage = () => { | ||
| const { isLoggedIn } = useUserContext(); | ||
| const { search } = useLocation(); | ||
| const { t } = useTranslation(); | ||
| const navigate = useNavigate(); | ||
| const scopeMap = createScopeMap(t); | ||
|
|
||
| const searchParams = new URLSearchParams(search); | ||
| const { | ||
| values: props, | ||
| missingParams, | ||
| isOidc, | ||
| compiled: compiledOIDCParams, | ||
| } = useOIDCParams(searchParams); | ||
| const scopes = props.scope ? props.scope.split(" ").filter(Boolean) : []; | ||
|
|
||
| const getClientInfo = useQuery({ | ||
| queryKey: ["client", props.client_id], | ||
| queryFn: async () => { | ||
| const res = await fetch(`/api/oidc/clients/${props.client_id}`); | ||
| const data = await getOidcClientInfoSchema.parseAsync(await res.json()); | ||
| return data; | ||
| }, | ||
| enabled: isOidc, | ||
| }); | ||
|
steveiliop56 marked this conversation as resolved.
|
||
|
|
||
| const authorizeMutation = useMutation({ | ||
| mutationFn: () => { | ||
| return axios.post("/api/oidc/authorize", { | ||
| scope: props.scope, | ||
| response_type: props.response_type, | ||
| client_id: props.client_id, | ||
| redirect_uri: props.redirect_uri, | ||
| state: props.state, | ||
| }); | ||
| }, | ||
| mutationKey: ["authorize", props.client_id], | ||
| onSuccess: (data) => { | ||
| toast.info(t("authorizeSuccessTitle"), { | ||
| description: t("authorizeSuccessSubtitle"), | ||
| }); | ||
| window.location.replace(data.data.redirect_uri); | ||
| }, | ||
| onError: (error) => { | ||
| window.location.replace( | ||
| `/error?error=${encodeURIComponent(error.message)}`, | ||
| ); | ||
| }, | ||
| }); | ||
|
|
||
| if (missingParams.length > 0) { | ||
| return ( | ||
| <Navigate | ||
| to={`/error?error=${encodeURIComponent(t("authorizeErrorMissingParams", { missingParams: missingParams.join(", ") }))}`} | ||
| replace | ||
| /> | ||
| ); | ||
| } | ||
|
|
||
| if (!isLoggedIn) { | ||
| return <Navigate to={`/login?${compiledOIDCParams}`} replace />; | ||
| } | ||
|
|
||
| if (getClientInfo.isLoading) { | ||
| return ( | ||
| <Card className="min-w-xs sm:min-w-sm"> | ||
| <CardHeader> | ||
| <CardTitle className="text-3xl"> | ||
| {t("authorizeLoadingTitle")} | ||
| </CardTitle> | ||
| <CardDescription>{t("authorizeLoadingSubtitle")}</CardDescription> | ||
| </CardHeader> | ||
| </Card> | ||
| ); | ||
| } | ||
|
|
||
| if (getClientInfo.isError) { | ||
| return ( | ||
| <Navigate | ||
| to={`/error?error=${encodeURIComponent(t("authorizeErrorClientInfo"))}`} | ||
| replace | ||
| /> | ||
| ); | ||
| } | ||
|
steveiliop56 marked this conversation as resolved.
|
||
|
|
||
| return ( | ||
| <Card className="min-w-xs sm:min-w-sm mx-4"> | ||
| <CardHeader> | ||
| <CardTitle className="text-3xl"> | ||
| {t("authorizeCardTitle", { | ||
| app: getClientInfo.data?.name || "Unknown", | ||
| })} | ||
| </CardTitle> | ||
| <CardDescription> | ||
| {scopes.includes("openid") | ||
| ? t("authorizeSubtitle") | ||
| : t("authorizeSubtitleOAuth")} | ||
| </CardDescription> | ||
| </CardHeader> | ||
| {scopes.includes("openid") && ( | ||
| <CardContent className="flex flex-col gap-4"> | ||
| {scopes.map((id) => { | ||
| const scope = scopeMap.find((s) => s.id === id); | ||
| if (!scope) return null; | ||
| return ( | ||
| <div key={scope.id} className="flex flex-row items-center gap-3"> | ||
| <div className="p-2 flex flex-col items-center justify-center bg-card-foreground rounded-md"> | ||
| {scope.icon} | ||
| </div> | ||
| <div className="flex flex-col gap-0.5"> | ||
| <div className="text-md">{scope.name}</div> | ||
| <div className="text-sm text-muted-foreground"> | ||
| {scope.description} | ||
| </div> | ||
| </div> | ||
| </div> | ||
| ); | ||
| })} | ||
| </CardContent> | ||
| )} | ||
| <CardFooter className="flex flex-col items-stretch gap-2"> | ||
| <Button | ||
| onClick={() => authorizeMutation.mutate()} | ||
| loading={authorizeMutation.isPending} | ||
| > | ||
| {t("authorizeTitle")} | ||
| </Button> | ||
| <Button | ||
| onClick={() => navigate("/")} | ||
| disabled={authorizeMutation.isPending} | ||
| variant="outline" | ||
| > | ||
| {t("cancelTitle")} | ||
| </Button> | ||
| </CardFooter> | ||
| </Card> | ||
| ); | ||
| }; | ||
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
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.