mirror of
https://github.com/supabase/supabase.git
synced 2026-12-06 08:29:01 +00:00
Part 5 of 6 in a stack for [FE-4731](https://linear.app/supabase/issue/FE-4731/grants-rollout-api-keys-consumers-that-only-need-publishable-keys). Based on #52964. ## Stack 1. #52961 Docs and project API docs consumers 2. #52962 Edge functions and vector bucket examples 3. #52963 Integrations landing data 4. #52964 Connect sheet and project connection popover; remove the transitional api-keys hooks 5. 👉 #52965 `reveal` refinement for single api-key reads; API key pill 6. #52966 API key create and delete ## Changes - Adds a `reveal` refinement for `GET /v1/projects/{ref}/api-keys/{id}`, mirroring the list endpoint: the `reveal` variant adds `api_gateway_keys_secret_read`, and the wrapper owns the `reveal` query param. - `getAPIKeysById` takes a plain or reveal grant instead of `unguarded.migrating` and a manual `reveal` flag. - `useRevealedSecret` mints the reveal grant, returns it, and doesn't request unless it's granted. - `ApiKeyPill` uses the reveal grant instead of its V2 check. Reveal and copy are disabled while the check loads, when denied, or when it fails. Publishable keys aren't gated. - `useConnectServerEnv`: the secret can only be revealed once the reveal grant is granted. **Guard drift:** `ApiKeyPill` checked only `api_gateway_keys_secret_read`, but the endpoint needs `api_gateway_keys_read` and `api_gateway_keys_secret_read`. Users with only the secret permission saw an enabled button and got a 403; they now see it as restricted. ## Testing - `data/guarded-fetch.test.ts`: type tests for the by-id endpoint (plain and reveal grants match the raw client, `reveal` rejected as a caller param, a list-endpoint reveal grant rejected), and a runtime test that `reveal` is set from the grant. The "variant the endpoint doesn't have" negative now uses the branches endpoint. - `ApiKeyPill.platform.test.tsx` (renamed from `ApiKeyPill.test.tsx`, now on real grants through MSW): reveal and copy when granted, denied (no request), permission-check error, publishable keys not gated. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
158 lines
4.5 KiB
TypeScript
158 lines
4.5 KiB
TypeScript
import { useParams } from 'common'
|
|
import { EyeIcon, EyeOffIcon } from 'lucide-react'
|
|
import { useEffect, useState } from 'react'
|
|
import { toast } from 'sonner'
|
|
import { Button, cn, InputVariants, Tooltip, TooltipContent, TooltipTrigger } from 'ui'
|
|
|
|
import { useRevealedSecret } from './useRevealedSecret'
|
|
import CopyButton from '@/components/ui/CopyButton'
|
|
import { APIKeysData } from '@/data/api-keys/api-keys-query'
|
|
|
|
export function ApiKeyPill({
|
|
apiKey,
|
|
}: {
|
|
apiKey: Extract<APIKeysData[number], { type: 'secret' | 'publishable' }>
|
|
}) {
|
|
const { ref: projectRef } = useParams()
|
|
const [show, setShow] = useState(false)
|
|
|
|
const isSecret = apiKey.type === 'secret'
|
|
|
|
const {
|
|
data: revealedKey,
|
|
isLoading,
|
|
reveal,
|
|
clear,
|
|
revealGrant,
|
|
} = useRevealedSecret({
|
|
projectRef,
|
|
id: apiKey.id as string,
|
|
})
|
|
|
|
// Publishable keys are shown as-is, so only secret keys depend on the reveal grant
|
|
const isLoadingPermission = isSecret && revealGrant.status === 'loading'
|
|
const canManageSecretKeys = revealGrant.status === 'granted'
|
|
|
|
// Auto-hide timer for the API key (security feature)
|
|
useEffect(() => {
|
|
if (show && revealedKey) {
|
|
const timer = setTimeout(() => {
|
|
setShow(false)
|
|
clear()
|
|
}, 10000)
|
|
|
|
return () => clearTimeout(timer)
|
|
}
|
|
}, [show, revealedKey, clear])
|
|
|
|
async function onToggleShow() {
|
|
if (isSecret && !canManageSecretKeys) return
|
|
if (isLoadingPermission) return
|
|
|
|
if (show) {
|
|
setShow(false)
|
|
clear()
|
|
} else {
|
|
setShow(true)
|
|
try {
|
|
await reveal()
|
|
} catch {
|
|
toast.error('Failed to reveal secret API key')
|
|
setShow(false)
|
|
}
|
|
}
|
|
}
|
|
|
|
async function onCopy() {
|
|
if (!isSecret) return apiKey.api_key
|
|
if (revealedKey) return revealedKey
|
|
|
|
try {
|
|
const key = await reveal()
|
|
clear()
|
|
return key ?? ''
|
|
} catch {
|
|
toast.error('Failed to copy secret API key')
|
|
return ''
|
|
}
|
|
}
|
|
|
|
const isRestricted = isSecret && !isLoadingPermission && !canManageSecretKeys
|
|
const restrictedReason = (action: 'reveal' | 'copy') =>
|
|
revealGrant.status === 'error'
|
|
? `Failed to check permissions to ${action} secret API keys`
|
|
: `You need additional permissions to ${action} secret API keys`
|
|
|
|
return (
|
|
<>
|
|
<div
|
|
className={cn(
|
|
InputVariants({ size: 'tiny' }),
|
|
'w-[100px] sm:w-[140px] md:w-[180px] lg:w-[340px] gap-0 font-mono rounded-full',
|
|
isSecret ? 'overflow-hidden' : '',
|
|
show ? 'ring-1 ring-foreground-lighter/50' : 'ring-0 ring-foreground-lighter/0',
|
|
'transition-all cursor-text relative'
|
|
)}
|
|
style={{ userSelect: 'all' }}
|
|
>
|
|
{isSecret ? (
|
|
<>
|
|
<span>{apiKey?.api_key.slice(0, 15)}</span>
|
|
<span>{show && revealedKey ? revealedKey.slice(15) : '••••••••••••••••'}</span>
|
|
</>
|
|
) : (
|
|
<span title={apiKey.api_key} className="truncate">
|
|
{apiKey.api_key}
|
|
</span>
|
|
)}
|
|
</div>
|
|
|
|
{/* Toggle button */}
|
|
{isSecret && (
|
|
<Tooltip>
|
|
<TooltipTrigger asChild>
|
|
<Button
|
|
variant="outline"
|
|
aria-label={show ? 'Hide API key' : 'Reveal API key'}
|
|
className="rounded-full px-2 pointer-events-auto"
|
|
loading={show && isLoading}
|
|
icon={show ? <EyeOffIcon strokeWidth={2} /> : <EyeIcon strokeWidth={2} />}
|
|
onClick={onToggleShow}
|
|
disabled={isRestricted || isLoadingPermission}
|
|
/>
|
|
</TooltipTrigger>
|
|
<TooltipContent side="bottom">
|
|
{isRestricted
|
|
? restrictedReason('reveal')
|
|
: isLoadingPermission
|
|
? 'Loading permissions...'
|
|
: show
|
|
? 'Hide API key'
|
|
: 'Reveal API key'}
|
|
</TooltipContent>
|
|
</Tooltip>
|
|
)}
|
|
|
|
<Tooltip>
|
|
<TooltipTrigger asChild>
|
|
<CopyButton
|
|
variant="default"
|
|
aria-label="Copy API key"
|
|
asyncText={onCopy}
|
|
iconOnly
|
|
className="rounded-full px-2 pointer-events-auto"
|
|
disabled={isRestricted || isLoadingPermission}
|
|
/>
|
|
</TooltipTrigger>
|
|
<TooltipContent side="bottom">
|
|
{isRestricted
|
|
? restrictedReason('copy')
|
|
: isLoadingPermission
|
|
? 'Loading permissions...'
|
|
: 'Copy API key'}
|
|
</TooltipContent>
|
|
</Tooltip>
|
|
</>
|
|
)
|
|
}
|