mirror of
https://github.com/supabase/supabase.git
synced 2026-12-04 07:29:16 +00:00
Resolves [FE-4732](https://linear.app/supabase/issue/FE-4732/grants-rollout-jwt-signing-keys-legacy-api-keys-and-third-party-auth). ## Changes Gates the JWT signing keys, legacy API keys, and third-party auth endpoints on grants instead of `unguarded.migrating`, and replaces their V2 permission guards with `useGrant`: - **Data layer (11 sites):** `data/jwt-signing-keys/*`, `data/api-keys/legacy-api-keys-status-query`, `data/api-keys/legacy-api-key-toggle-mutation`, and `data/third-party-auth/*`. Queries become grant-gated query option factories; mutations take the grant in their variables. - **Consumers:** the JWT settings page, the JWT signing keys table and its dialogs, `jwt-settings`, `ToggleLegacyApiKeys`, `Project/APIKeys`, and the third-party auth page, form, and create dialogs. Each consumer renders denied and error states. - **JWT signing keys table:** - It's gated only on the signing keys reads (`GET signing-keys` and `GET signing-keys/legacy`, both `auth_signing_keys_read`). - The legacy API keys status is only used by the revoke dialogs. If it can't be read, revoking the legacy JWT secret fails closed: the user sees the "disable legacy API keys first" warning, with copy explaining why, instead of the confirm modal. - **Legacy JWT secret query:** a 404 (secret not imported as a signing key yet) resolves to `null` and shows the "Migrate JWT secret" panel. Other errors now render an error instead of being treated as "not migrated". - **Write actions:** - `getWriteGrantTooltip` moves from `APIKeys.utils` to `lib/grants` and takes a resource noun. Disabled write actions use it to explain why they're disabled. - New `getWriteGrantToastMessage`: handlers that run without a grant now show a toast instead of silently doing nothing. This includes the API keys handlers from #52966. ## Permission changes - **Legacy API keys toggle and status** (`GET`/`PUT /v1/projects/{ref}/api-keys/legacy`): the guard moves from `auth_signing_keys_read`/`_write` to the endpoint annotation, `api_gateway_keys_read`/`_write`. The annotation was confirmed against the backend controller (FE-4728). As a result, the JWT signing keys table's legacy revoke check now depends on `api_gateway_keys_read`. - **API keys last-used analytics** (`/platform/projects/{ref}/analytics/endpoints/api_keys.last_used.otel`): this endpoint uses legacy permissions (`analytics:Read` on `logflare`), not FGA. Its check moves from the V2 `auth_signing_keys_read` check to `useAsyncCheckPermissions(PermissionAction.ANALYTICS_READ, 'logflare')`. - Every other guard already matched its annotation: - `auth_signing_keys_read`/`_write` for signing keys - `auth_config_read`/`_write` for third-party auth ## Testing - Data layer: `jwt-signing-keys.test.ts`, `legacy-api-keys.test.ts`, and `third-party-auth.test.ts` cover every migrated fetcher, check that factories stay disabled without a grant, and cover legacy-key 404 vs. 500. - `jwt-secret-keys-table/index.platform.test.tsx`: - Legacy status denied: the table still renders, and revoking the legacy key shows only the warning. - Legacy status known enabled or disabled. - Signing keys read denied. - Legacy key 404 vs. 500. - `create-key-dialog.platform.test.tsx`: triggering create without a grant shows the toast and sends no request. - `ToggleLegacyApiKeys.platform.test.tsx`, `ThirdPartyAuthForm.platform.test.tsx`, and `settings/jwt/index.platform.test.tsx`: granted, denied, error, and partial-permission cases. - `lib/grants.test.ts`: the tooltip and toast helpers. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
219 lines
7.2 KiB
TypeScript
219 lines
7.2 KiB
TypeScript
import { zodResolver } from '@hookform/resolvers/zod'
|
|
import { useParams } from 'common'
|
|
import { PlusIcon, ShieldCheckIcon } from 'lucide-react'
|
|
import { parseAsString, useQueryState } from 'nuqs'
|
|
import { useRef } from 'react'
|
|
import { useForm, type SubmitHandler } from 'react-hook-form'
|
|
import { toast } from 'sonner'
|
|
import {
|
|
Alert,
|
|
AlertDescription,
|
|
AlertTitle,
|
|
Button,
|
|
Dialog,
|
|
DialogContent,
|
|
DialogDescription,
|
|
DialogFooter,
|
|
DialogHeader,
|
|
DialogSection,
|
|
DialogSectionSeparator,
|
|
DialogTitle,
|
|
DialogTrigger,
|
|
Form,
|
|
FormControl,
|
|
FormField,
|
|
Input,
|
|
} from 'ui'
|
|
import { FormItemLayout } from 'ui-patterns/form/FormItemLayout/FormItemLayout'
|
|
import * as z from 'zod'
|
|
|
|
import { ButtonTooltip } from '@/components/ui/ButtonTooltip'
|
|
import { Shortcut } from '@/components/ui/Shortcut'
|
|
import { useAPIKeyCreateMutation } from '@/data/api-keys/api-key-create-mutation'
|
|
import { getWriteGrantToastMessage, getWriteGrantTooltip, useGrant } from '@/lib/grants'
|
|
import { SHORTCUT_IDS } from '@/state/shortcuts/registry'
|
|
|
|
const NAME_SCHEMA = z
|
|
.string()
|
|
.min(4, 'Name must be at least 4 characters')
|
|
.max(64, "Name can't be more than 64 characters long")
|
|
.regex(/^[a-z0-9_]+$/, 'Name can only contain lowercased letters, digits and underscore')
|
|
.refine((val: string) => !val.match(/^[0-9].+$/), 'Name must not start with a digit')
|
|
.refine(
|
|
(val: string) => val !== 'anon' && val !== 'service_role',
|
|
'Using "anon" or "service_role" for API key name is not possible'
|
|
)
|
|
|
|
const FORM_ID = 'create-secret-api-key'
|
|
const SCHEMA = z.object({
|
|
name: NAME_SCHEMA,
|
|
description: z.string().max(256, "Description shouldn't be too long").trim(),
|
|
})
|
|
|
|
export const CreateSecretAPIKeyDialog = () => {
|
|
const { ref: projectRef } = useParams()
|
|
const [visible, setVisible] = useQueryState('new', parseAsString)
|
|
const formRef = useRef<HTMLFormElement>(null)
|
|
|
|
const onOpenChange = (value: boolean) => {
|
|
if (value) setVisible('secret')
|
|
else setVisible('')
|
|
}
|
|
const openDialog = () => {
|
|
if (canCreateAPIKeys) setVisible('secret')
|
|
}
|
|
|
|
const defaultValues = { name: '', description: '' }
|
|
const form = useForm<z.infer<typeof SCHEMA>>({
|
|
resolver: zodResolver(SCHEMA),
|
|
defaultValues,
|
|
})
|
|
|
|
const createGrant = useGrant('POST /v1/projects/{ref}/api-keys', { projectRef })
|
|
const canCreateAPIKeys = createGrant.status === 'granted'
|
|
|
|
const { mutate: createAPIKey, isPending: isCreatingAPIKey } = useAPIKeyCreateMutation()
|
|
|
|
const onSubmit: SubmitHandler<z.infer<typeof SCHEMA>> = async (values) => {
|
|
if (!createGrant.grant) {
|
|
toast.error(getWriteGrantToastMessage(createGrant.status, 'create', 'API keys'))
|
|
return
|
|
}
|
|
createAPIKey(
|
|
{
|
|
projectRef,
|
|
grant: createGrant.grant,
|
|
type: 'secret',
|
|
name: values.name,
|
|
description: values.description,
|
|
},
|
|
{
|
|
onSuccess: (data) => {
|
|
toast.success(`Your secret API key ${data.prefix}... is ready.`)
|
|
form.reset(defaultValues)
|
|
onOpenChange(false)
|
|
},
|
|
}
|
|
)
|
|
}
|
|
|
|
return (
|
|
<Dialog open={visible === 'secret'} onOpenChange={onOpenChange}>
|
|
<DialogTrigger asChild>
|
|
<Shortcut
|
|
id={SHORTCUT_IDS.API_KEYS_NEW_SECRET}
|
|
onTrigger={openDialog}
|
|
side="bottom"
|
|
// Hide the shortcut hint while blocked so the permission tooltip shows instead
|
|
tooltipOpen={visible === 'secret' || !canCreateAPIKeys ? false : undefined}
|
|
>
|
|
<ButtonTooltip
|
|
className="mt-2"
|
|
icon={<PlusIcon />}
|
|
onClick={openDialog}
|
|
disabled={!canCreateAPIKeys}
|
|
tooltip={{
|
|
content: {
|
|
side: 'bottom',
|
|
text: getWriteGrantTooltip(createGrant.status, 'create', 'API keys'),
|
|
},
|
|
}}
|
|
>
|
|
New secret key
|
|
</ButtonTooltip>
|
|
</Shortcut>
|
|
</DialogTrigger>
|
|
<DialogContent>
|
|
<DialogHeader>
|
|
<DialogTitle>Create new secret API key</DialogTitle>
|
|
<DialogDescription>
|
|
Secret API keys allow elevated access to your project's data, bypassing Row-Level
|
|
security.
|
|
</DialogDescription>
|
|
</DialogHeader>
|
|
<DialogSectionSeparator />
|
|
<DialogSection className="flex flex-col gap-4">
|
|
<Form {...form}>
|
|
<form
|
|
ref={formRef}
|
|
className="flex flex-col gap-4"
|
|
id={FORM_ID}
|
|
onSubmit={form.handleSubmit(onSubmit)}
|
|
>
|
|
<FormField
|
|
key="name"
|
|
name="name"
|
|
control={form.control}
|
|
render={({ field }) => (
|
|
<FormItemLayout
|
|
label="Name"
|
|
description="A short, unique name of lowercased letters, digits and underscore"
|
|
>
|
|
<FormControl>
|
|
<Input {...field} placeholder="Example: my_super_secret_key_123" />
|
|
</FormControl>
|
|
</FormItemLayout>
|
|
)}
|
|
/>
|
|
<FormField
|
|
key="description"
|
|
name="description"
|
|
control={form.control}
|
|
render={({ field }) => (
|
|
<FormItemLayout label="Description" labelOptional="Optional">
|
|
<FormControl>
|
|
<Input
|
|
{...field}
|
|
placeholder="Short notes on how or where this key will be used"
|
|
/>
|
|
</FormControl>
|
|
</FormItemLayout>
|
|
)}
|
|
/>
|
|
</form>
|
|
</Form>
|
|
<Alert variant="warning">
|
|
<ShieldCheckIcon />
|
|
<AlertTitle>Securing your API key</AlertTitle>
|
|
<AlertDescription className="">
|
|
<ul className="list-disc">
|
|
<li>Keep this key secret.</li>
|
|
<li>Do not use on the web, in mobile or desktop apps.</li>
|
|
<li>Don't post it publicly or commit in source control.</li>
|
|
<li>
|
|
This key provides elevated access to your data, bypassing Row-Level Security.
|
|
</li>
|
|
<li>
|
|
If it leaks or is revealed, swap it with a new secret API key and then delete it.
|
|
</li>
|
|
<li>
|
|
If used in a browser, it will always return HTTP 401 Unauthorized. Delete
|
|
immediately.
|
|
</li>
|
|
</ul>
|
|
</AlertDescription>
|
|
</Alert>
|
|
</DialogSection>
|
|
<DialogFooter>
|
|
<Shortcut
|
|
id={SHORTCUT_IDS.API_KEYS_CREATE_SECRET}
|
|
onTrigger={() => formRef.current?.requestSubmit()}
|
|
options={{ enabled: visible === 'secret' && !isCreatingAPIKey }}
|
|
side="top"
|
|
>
|
|
<Button
|
|
variant="primary"
|
|
form={FORM_ID}
|
|
type="submit"
|
|
loading={isCreatingAPIKey}
|
|
disabled={!canCreateAPIKeys}
|
|
>
|
|
Create API key
|
|
</Button>
|
|
</Shortcut>
|
|
</DialogFooter>
|
|
</DialogContent>
|
|
</Dialog>
|
|
)
|
|
}
|