|
| 1 | +import { useFetcher, useRevalidator } from "@remix-run/react"; |
| 2 | +import { useState } from "react"; |
| 3 | +import { typedjson, useTypedLoaderData } from "remix-typedjson"; |
| 4 | +import { z } from "zod"; |
| 5 | +import { Button } from "~/components/primitives/Buttons"; |
| 6 | +import { Callout } from "~/components/primitives/Callout"; |
| 7 | +import { Paragraph } from "~/components/primitives/Paragraph"; |
| 8 | +import { Popover, PopoverContent, PopoverTrigger } from "~/components/primitives/Popover"; |
| 9 | +import { |
| 10 | + Table, |
| 11 | + TableBlankRow, |
| 12 | + TableBody, |
| 13 | + TableCell, |
| 14 | + TableHeader, |
| 15 | + TableHeaderCell, |
| 16 | + TableRow, |
| 17 | +} from "~/components/primitives/Table"; |
| 18 | +import { env } from "~/env.server"; |
| 19 | +import { logger } from "~/services/logger.server"; |
| 20 | +import { oidcSigningKeyService } from "~/services/oidc/instance.server"; |
| 21 | +import { OidcSigningKeyError } from "~/services/oidc/signingKeyService.server"; |
| 22 | +import { dashboardAction, dashboardLoader } from "~/services/routeBuilders/dashboardBuilder"; |
| 23 | +import { requestUrl } from "~/utils/requestUrl.server"; |
| 24 | + |
| 25 | +const MutationSchema = z.discriminatedUnion("intent", [ |
| 26 | + z.object({ intent: z.literal("generate") }), |
| 27 | + z.object({ intent: z.literal("activate"), keyId: z.string().min(1) }), |
| 28 | + z.object({ intent: z.literal("disable") }), |
| 29 | +]); |
| 30 | + |
| 31 | +type MutationResult = { success?: boolean; error?: string }; |
| 32 | + |
| 33 | +export const loader = dashboardLoader({ authorization: { requireSuper: true } }, async () => { |
| 34 | + const status = await oidcSigningKeyService.getSigningStatus(); |
| 35 | + return typedjson( |
| 36 | + { issuer: env.OIDC_ISSUER_URL, status, now: Date.now() }, |
| 37 | + { |
| 38 | + headers: { "Cache-Control": "no-store" }, |
| 39 | + } |
| 40 | + ); |
| 41 | +}); |
| 42 | + |
| 43 | +export const action = dashboardAction( |
| 44 | + { authorization: { requireSuper: true } }, |
| 45 | + async ({ request, user }) => { |
| 46 | + if (request.method !== "POST") { |
| 47 | + return typedjson( |
| 48 | + { error: "Method not allowed" }, |
| 49 | + { status: 405, headers: { Allow: "POST" } } |
| 50 | + ); |
| 51 | + } |
| 52 | + // SameSite cookies alone do not protect against other origins on the same site. |
| 53 | + if (request.headers.get("Origin") !== requestUrl(request).origin) { |
| 54 | + return typedjson({ error: "Invalid request origin" }, { status: 403 }); |
| 55 | + } |
| 56 | + if (!env.OIDC_ISSUER_URL) { |
| 57 | + return typedjson( |
| 58 | + { error: "Configure OIDC_ISSUER_URL before managing keys" }, |
| 59 | + { status: 400 } |
| 60 | + ); |
| 61 | + } |
| 62 | + |
| 63 | + const result = MutationSchema.safeParse(Object.fromEntries(await request.formData())); |
| 64 | + if (!result.success) { |
| 65 | + return typedjson({ error: "Invalid key operation" }, { status: 400 }); |
| 66 | + } |
| 67 | + |
| 68 | + try { |
| 69 | + switch (result.data.intent) { |
| 70 | + case "generate": |
| 71 | + await oidcSigningKeyService.generateStagedKey(user.id); |
| 72 | + break; |
| 73 | + case "activate": |
| 74 | + await oidcSigningKeyService.activateKey(result.data.keyId, user.id); |
| 75 | + break; |
| 76 | + case "disable": |
| 77 | + await oidcSigningKeyService.disableSigning(user.id); |
| 78 | + break; |
| 79 | + } |
| 80 | + return typedjson({ success: true }); |
| 81 | + } catch (error) { |
| 82 | + if (error instanceof OidcSigningKeyError) { |
| 83 | + return typedjson({ error: error.message }, { status: 409 }); |
| 84 | + } |
| 85 | + logger.error("OIDC signing key operation failed", { |
| 86 | + operation: result.data.intent, |
| 87 | + userId: user.id, |
| 88 | + }); |
| 89 | + return typedjson( |
| 90 | + { |
| 91 | + error: "Key operation failed. Refresh the page and check the key status before retrying.", |
| 92 | + }, |
| 93 | + { status: 409 } |
| 94 | + ); |
| 95 | + } |
| 96 | + } |
| 97 | +); |
| 98 | + |
| 99 | +export default function AdminOidcRoute() { |
| 100 | + const { issuer, status, now } = useTypedLoaderData<typeof loader>(); |
| 101 | + const generateFetcher = useFetcher<MutationResult>(); |
| 102 | + const revalidator = useRevalidator(); |
| 103 | + const keys = [ |
| 104 | + ...(status.activeKey ? [status.activeKey] : []), |
| 105 | + ...(status.stagedKey ? [status.stagedKey] : []), |
| 106 | + ...status.retiredKeys, |
| 107 | + ]; |
| 108 | + |
| 109 | + return ( |
| 110 | + <main className="flex h-full flex-col gap-4 overflow-y-auto px-4 pb-4"> |
| 111 | + <div className="flex items-center justify-between gap-4"> |
| 112 | + <div className="space-y-1"> |
| 113 | + <Paragraph className="text-text-bright">OIDC signing keys</Paragraph> |
| 114 | + <Paragraph variant="small"> |
| 115 | + {issuer |
| 116 | + ? `Issuer: ${issuer}` |
| 117 | + : "OIDC is disabled. Configure OIDC_ISSUER_URL to enable it."} |
| 118 | + </Paragraph> |
| 119 | + </div> |
| 120 | + <div className="flex gap-2"> |
| 121 | + <Button |
| 122 | + variant="tertiary/small" |
| 123 | + onClick={() => revalidator.revalidate()} |
| 124 | + disabled={revalidator.state !== "idle"} |
| 125 | + > |
| 126 | + Refresh |
| 127 | + </Button> |
| 128 | + <generateFetcher.Form method="post"> |
| 129 | + <input type="hidden" name="intent" value="generate" /> |
| 130 | + <Button |
| 131 | + type="submit" |
| 132 | + variant="primary/small" |
| 133 | + disabled={!issuer || !!status.stagedKey || generateFetcher.state !== "idle"} |
| 134 | + > |
| 135 | + Generate key |
| 136 | + </Button> |
| 137 | + </generateFetcher.Form> |
| 138 | + </div> |
| 139 | + </div> |
| 140 | + |
| 141 | + {generateFetcher.data?.error && ( |
| 142 | + <Callout variant="error">{generateFetcher.data.error}</Callout> |
| 143 | + )} |
| 144 | + <Callout variant={issuer && status.activeKey ? "info" : "warning"}> |
| 145 | + {!issuer |
| 146 | + ? "Token signing is disabled because OIDC_ISSUER_URL is not configured. Stored signing keys are unchanged." |
| 147 | + : status.activeKey |
| 148 | + ? "One key signs new tokens. Staged keys are published before activation, and retired public keys remain available until existing tokens expire." |
| 149 | + : "No active signing key. Token signing is disabled. Retained public keys can still verify existing tokens."} |
| 150 | + </Callout> |
| 151 | + {issuer && ( |
| 152 | + <div className="flex gap-4 text-sm text-text-link"> |
| 153 | + <a href={`${issuer}/.well-known/openid-configuration`} target="_blank" rel="noreferrer"> |
| 154 | + Discovery document |
| 155 | + </a> |
| 156 | + <a href={`${issuer}/.well-known/jwks.json`} target="_blank" rel="noreferrer"> |
| 157 | + Public JWKS |
| 158 | + </a> |
| 159 | + </div> |
| 160 | + )} |
| 161 | + <Table> |
| 162 | + <TableHeader> |
| 163 | + <TableRow> |
| 164 | + <TableHeaderCell>Key ID</TableHeaderCell> |
| 165 | + <TableHeaderCell>Status</TableHeaderCell> |
| 166 | + <TableHeaderCell>Generated</TableHeaderCell> |
| 167 | + <TableHeaderCell>Activation ready</TableHeaderCell> |
| 168 | + <TableHeaderCell>Public until</TableHeaderCell> |
| 169 | + <TableHeaderCell>Actions</TableHeaderCell> |
| 170 | + </TableRow> |
| 171 | + </TableHeader> |
| 172 | + <TableBody> |
| 173 | + {keys.length === 0 ? ( |
| 174 | + <TableBlankRow colSpan={6}> |
| 175 | + <Paragraph>No signing keys configured</Paragraph> |
| 176 | + </TableBlankRow> |
| 177 | + ) : ( |
| 178 | + keys.map((key) => ( |
| 179 | + <TableRow key={key.id}> |
| 180 | + <TableCell> |
| 181 | + <span className="font-mono text-xs">{key.kid}</span> |
| 182 | + </TableCell> |
| 183 | + <TableCell>{key.status}</TableCell> |
| 184 | + <TableCell>{key.createdAt.toLocaleString()}</TableCell> |
| 185 | + <TableCell> |
| 186 | + {key.status === "STAGED" ? key.readyAt.toLocaleString() : "Not applicable"} |
| 187 | + </TableCell> |
| 188 | + <TableCell> |
| 189 | + {key.publicUntil ? key.publicUntil.toLocaleString() : "While staged or active"} |
| 190 | + </TableCell> |
| 191 | + <TableCell> |
| 192 | + {key.status === "STAGED" && ( |
| 193 | + <KeyMutation |
| 194 | + intent="activate" |
| 195 | + keyId={key.id} |
| 196 | + disabled={!issuer || key.readyAt.getTime() > now} |
| 197 | + label="Activate" |
| 198 | + confirmation="Activate this key? It will sign new tokens. Any current signing key will be retired, but its public key will remain available for existing tokens." |
| 199 | + /> |
| 200 | + )} |
| 201 | + {key.status === "ACTIVE" && ( |
| 202 | + <KeyMutation |
| 203 | + intent="disable" |
| 204 | + disabled={!issuer} |
| 205 | + label="Disable signing" |
| 206 | + confirmation="Disable token signing? No new tokens can be signed until another key is activated. Existing tokens remain valid until expiry." |
| 207 | + /> |
| 208 | + )} |
| 209 | + </TableCell> |
| 210 | + </TableRow> |
| 211 | + )) |
| 212 | + )} |
| 213 | + </TableBody> |
| 214 | + </Table> |
| 215 | + <Paragraph variant="small"> |
| 216 | + Keys use RS256. Private keys are encrypted and are never displayed. Refresh after the |
| 217 | + activation-ready time to enable activation. |
| 218 | + </Paragraph> |
| 219 | + </main> |
| 220 | + ); |
| 221 | +} |
| 222 | + |
| 223 | +function KeyMutation({ |
| 224 | + intent, |
| 225 | + keyId, |
| 226 | + disabled, |
| 227 | + label, |
| 228 | + confirmation, |
| 229 | +}: { |
| 230 | + intent: "activate" | "disable"; |
| 231 | + keyId?: string; |
| 232 | + disabled: boolean; |
| 233 | + label: string; |
| 234 | + confirmation: string; |
| 235 | +}) { |
| 236 | + const [open, setOpen] = useState(false); |
| 237 | + const fetcher = useFetcher<MutationResult>(); |
| 238 | + const pending = fetcher.state !== "idle"; |
| 239 | + |
| 240 | + return ( |
| 241 | + <div className="space-y-2"> |
| 242 | + <Popover open={open} onOpenChange={setOpen}> |
| 243 | + <PopoverTrigger asChild> |
| 244 | + <Button |
| 245 | + variant={intent === "disable" ? "danger/small" : "secondary/small"} |
| 246 | + disabled={disabled || pending} |
| 247 | + > |
| 248 | + {label} |
| 249 | + </Button> |
| 250 | + </PopoverTrigger> |
| 251 | + <PopoverContent align="end" className="w-80 space-y-3"> |
| 252 | + <Paragraph variant="small">{confirmation}</Paragraph> |
| 253 | + <div className="flex justify-end gap-2"> |
| 254 | + <Button variant="tertiary/small" onClick={() => setOpen(false)}> |
| 255 | + Cancel |
| 256 | + </Button> |
| 257 | + <fetcher.Form method="post" onSubmit={() => setOpen(false)}> |
| 258 | + <input type="hidden" name="intent" value={intent} /> |
| 259 | + {keyId && <input type="hidden" name="keyId" value={keyId} />} |
| 260 | + <Button |
| 261 | + type="submit" |
| 262 | + variant={intent === "disable" ? "danger/small" : "primary/small"} |
| 263 | + disabled={pending} |
| 264 | + > |
| 265 | + {label} |
| 266 | + </Button> |
| 267 | + </fetcher.Form> |
| 268 | + </div> |
| 269 | + </PopoverContent> |
| 270 | + </Popover> |
| 271 | + {fetcher.data?.error && ( |
| 272 | + <Paragraph variant="small" className="text-error"> |
| 273 | + {fetcher.data.error} |
| 274 | + </Paragraph> |
| 275 | + )} |
| 276 | + </div> |
| 277 | + ); |
| 278 | +} |
0 commit comments