|
| 1 | +import { useUserContext } from "@/context/user-context"; |
| 2 | +import { useMutation, useQuery } from "@tanstack/react-query"; |
| 3 | +import { Navigate, useNavigate } from "react-router"; |
| 4 | +import { useLocation } from "react-router"; |
| 5 | +import { |
| 6 | + Card, |
| 7 | + CardHeader, |
| 8 | + CardTitle, |
| 9 | + CardDescription, |
| 10 | + CardFooter, |
| 11 | + CardContent, |
| 12 | +} from "@/components/ui/card"; |
| 13 | +import { getOidcClientInfoSchema } from "@/schemas/oidc-schemas"; |
| 14 | +import { Button } from "@/components/ui/button"; |
| 15 | +import axios from "axios"; |
| 16 | +import { toast } from "sonner"; |
| 17 | +import { useOIDCParams } from "@/lib/hooks/oidc"; |
| 18 | +import { useTranslation } from "react-i18next"; |
| 19 | +import { TFunction } from "i18next"; |
| 20 | +import { Mail, Shield, User, Users } from "lucide-react"; |
| 21 | + |
| 22 | +type Scope = { |
| 23 | + id: string; |
| 24 | + name: string; |
| 25 | + description: string; |
| 26 | + icon: React.ReactNode; |
| 27 | +}; |
| 28 | + |
| 29 | +const scopeMapIconProps = { |
| 30 | + className: "stroke-card stroke-2.5", |
| 31 | +}; |
| 32 | + |
| 33 | +const createScopeMap = (t: TFunction<"translation", undefined>): Scope[] => { |
| 34 | + return [ |
| 35 | + { |
| 36 | + id: "openid", |
| 37 | + name: t("openidScopeName"), |
| 38 | + description: t("openidScopeDescription"), |
| 39 | + icon: <Shield {...scopeMapIconProps} />, |
| 40 | + }, |
| 41 | + { |
| 42 | + id: "email", |
| 43 | + name: t("emailScopeName"), |
| 44 | + description: t("emailScopeDescription"), |
| 45 | + icon: <Mail {...scopeMapIconProps} />, |
| 46 | + }, |
| 47 | + { |
| 48 | + id: "profile", |
| 49 | + name: t("profileScopeName"), |
| 50 | + description: t("profileScopeDescription"), |
| 51 | + icon: <User {...scopeMapIconProps} />, |
| 52 | + }, |
| 53 | + { |
| 54 | + id: "groups", |
| 55 | + name: t("groupsScopeName"), |
| 56 | + description: t("groupsScopeDescription"), |
| 57 | + icon: <Users {...scopeMapIconProps} />, |
| 58 | + }, |
| 59 | + ]; |
| 60 | +}; |
| 61 | + |
| 62 | +export const AuthorizePage = () => { |
| 63 | + const { isLoggedIn } = useUserContext(); |
| 64 | + const { search } = useLocation(); |
| 65 | + const { t } = useTranslation(); |
| 66 | + const navigate = useNavigate(); |
| 67 | + const scopeMap = createScopeMap(t); |
| 68 | + |
| 69 | + const searchParams = new URLSearchParams(search); |
| 70 | + const { |
| 71 | + values: props, |
| 72 | + missingParams, |
| 73 | + isOidc, |
| 74 | + compiled: compiledOIDCParams, |
| 75 | + } = useOIDCParams(searchParams); |
| 76 | + const scopes = props.scope ? props.scope.split(" ").filter(Boolean) : []; |
| 77 | + |
| 78 | + const getClientInfo = useQuery({ |
| 79 | + queryKey: ["client", props.client_id], |
| 80 | + queryFn: async () => { |
| 81 | + const res = await fetch(`/api/oidc/clients/${props.client_id}`); |
| 82 | + const data = await getOidcClientInfoSchema.parseAsync(await res.json()); |
| 83 | + return data; |
| 84 | + }, |
| 85 | + enabled: isOidc, |
| 86 | + }); |
| 87 | + |
| 88 | + const authorizeMutation = useMutation({ |
| 89 | + mutationFn: () => { |
| 90 | + return axios.post("/api/oidc/authorize", { |
| 91 | + scope: props.scope, |
| 92 | + response_type: props.response_type, |
| 93 | + client_id: props.client_id, |
| 94 | + redirect_uri: props.redirect_uri, |
| 95 | + state: props.state, |
| 96 | + }); |
| 97 | + }, |
| 98 | + mutationKey: ["authorize", props.client_id], |
| 99 | + onSuccess: (data) => { |
| 100 | + toast.info(t("authorizeSuccessTitle"), { |
| 101 | + description: t("authorizeSuccessSubtitle"), |
| 102 | + }); |
| 103 | + window.location.replace(data.data.redirect_uri); |
| 104 | + }, |
| 105 | + onError: (error) => { |
| 106 | + window.location.replace( |
| 107 | + `/error?error=${encodeURIComponent(error.message)}`, |
| 108 | + ); |
| 109 | + }, |
| 110 | + }); |
| 111 | + |
| 112 | + if (missingParams.length > 0) { |
| 113 | + return ( |
| 114 | + <Navigate |
| 115 | + to={`/error?error=${encodeURIComponent(t("authorizeErrorMissingParams", { missingParams: missingParams.join(", ") }))}`} |
| 116 | + replace |
| 117 | + /> |
| 118 | + ); |
| 119 | + } |
| 120 | + |
| 121 | + if (!isLoggedIn) { |
| 122 | + return <Navigate to={`/login?${compiledOIDCParams}`} replace />; |
| 123 | + } |
| 124 | + |
| 125 | + if (getClientInfo.isLoading) { |
| 126 | + return ( |
| 127 | + <Card className="min-w-xs sm:min-w-sm"> |
| 128 | + <CardHeader> |
| 129 | + <CardTitle className="text-3xl"> |
| 130 | + {t("authorizeLoadingTitle")} |
| 131 | + </CardTitle> |
| 132 | + <CardDescription>{t("authorizeLoadingSubtitle")}</CardDescription> |
| 133 | + </CardHeader> |
| 134 | + </Card> |
| 135 | + ); |
| 136 | + } |
| 137 | + |
| 138 | + if (getClientInfo.isError) { |
| 139 | + return ( |
| 140 | + <Navigate |
| 141 | + to={`/error?error=${encodeURIComponent(t("authorizeErrorClientInfo"))}`} |
| 142 | + replace |
| 143 | + /> |
| 144 | + ); |
| 145 | + } |
| 146 | + |
| 147 | + return ( |
| 148 | + <Card className="min-w-xs sm:min-w-sm mx-4"> |
| 149 | + <CardHeader> |
| 150 | + <CardTitle className="text-3xl"> |
| 151 | + {t("authorizeCardTitle", { |
| 152 | + app: getClientInfo.data?.name || "Unknown", |
| 153 | + })} |
| 154 | + </CardTitle> |
| 155 | + <CardDescription> |
| 156 | + {scopes.includes("openid") |
| 157 | + ? t("authorizeSubtitle") |
| 158 | + : t("authorizeSubtitleOAuth")} |
| 159 | + </CardDescription> |
| 160 | + </CardHeader> |
| 161 | + {scopes.includes("openid") && ( |
| 162 | + <CardContent className="flex flex-col gap-4"> |
| 163 | + {scopes.map((id) => { |
| 164 | + const scope = scopeMap.find((s) => s.id === id); |
| 165 | + if (!scope) return null; |
| 166 | + return ( |
| 167 | + <div key={scope.id} className="flex flex-row items-center gap-3"> |
| 168 | + <div className="p-2 flex flex-col items-center justify-center bg-card-foreground rounded-md"> |
| 169 | + {scope.icon} |
| 170 | + </div> |
| 171 | + <div className="flex flex-col gap-0.5"> |
| 172 | + <div className="text-md">{scope.name}</div> |
| 173 | + <div className="text-sm text-muted-foreground"> |
| 174 | + {scope.description} |
| 175 | + </div> |
| 176 | + </div> |
| 177 | + </div> |
| 178 | + ); |
| 179 | + })} |
| 180 | + </CardContent> |
| 181 | + )} |
| 182 | + <CardFooter className="flex flex-col items-stretch gap-2"> |
| 183 | + <Button |
| 184 | + onClick={() => authorizeMutation.mutate()} |
| 185 | + loading={authorizeMutation.isPending} |
| 186 | + > |
| 187 | + {t("authorizeTitle")} |
| 188 | + </Button> |
| 189 | + <Button |
| 190 | + onClick={() => navigate("/")} |
| 191 | + disabled={authorizeMutation.isPending} |
| 192 | + variant="outline" |
| 193 | + > |
| 194 | + {t("cancelTitle")} |
| 195 | + </Button> |
| 196 | + </CardFooter> |
| 197 | + </Card> |
| 198 | + ); |
| 199 | +}; |
0 commit comments