Files
Charis 757040ffc7 feat(studio): add reveal refinement for single api-key reads (#52965)
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)
2026-11-05 15:21:32 -05:00

53 lines
1.5 KiB
TypeScript

import { useCallback, useRef, useState } from 'react'
import { getAPIKeysById } from '@/data/api-keys/api-key-id-query'
import { useGrant } from '@/lib/grants'
interface UseRevealedSecretOptions {
projectRef?: string
id?: string
}
export function useRevealedSecret({ projectRef, id }: UseRevealedSecretOptions) {
const [data, setData] = useState<string | undefined | null>()
const [isLoading, setIsLoading] = useState(false)
const requestIdRef = useRef(0)
const revealGrant = useGrant(
'GET /v1/projects/{ref}/api-keys/{id}',
{ projectRef },
{ variant: 'reveal' }
)
const grant = revealGrant.grant
const reveal = useCallback(async () => {
if (!projectRef || !id || !grant) return
const requestId = ++requestIdRef.current
setIsLoading(true)
try {
const result = await getAPIKeysById({ projectRef, id, grant })
if (requestId !== requestIdRef.current) return
setData(result.api_key)
return result.api_key
} catch (error) {
if (requestId !== requestIdRef.current) return
console.error('Failed to reveal secret key:', error)
throw error
} finally {
if (requestId === requestIdRef.current) {
setIsLoading(false)
}
}
}, [projectRef, id, grant])
const clear = useCallback(() => {
requestIdRef.current++
setData(undefined)
}, [])
/** Result of the reveal grant, for rendering denied and error states. `reveal()` does nothing unless granted. */
return { data, isLoading, reveal, clear, revealGrant }
}