Files
supabase/apps/studio/components/interfaces/APIKeys/ApiKeyPill.tsx
T
fffb1e23ca feat: implement profile permissions v2 api (#50966)
## Problem

Migrating Studio's permission checks from the old RLS json logic to
openfga backed permissions.

### New hooks and helpers

- `usePermissionsQueryV2` fetches the new profile openfga permisisons 
- `useAsyncCheckPermissionsV2` the replacement for the old check hook;
takes an FGA permission string (or a list)
- `FGA_PERMISSIONS` permission constants derived from
`@supabase/shared-types`. At the moment, the hook also accepts plain
strings for permissions not yet published in the package
- `useAsyncCheckUserContentPermissions` new hook for permission checks
on user content (SQL snippets, reports, log snippets). This wraps
``useAsyncCheckPermissionsV2` and adds existing content level rules on
top of the fga permission check

### Notes

- ~~Need api to support new permissions: project_operations_write,
billing_read/write, audit_logs_read~~ [DONE]
- ~~Still need to support: organization_config_read/write, auth hooks
and users permissions~~ [DONE]

---------

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
Co-authored-by: Charis Lam <26616127+charislam@users.noreply.github.com>
2026-10-21 06:54:16 -04:00

154 lines
4.3 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'
import { FGA_PERMISSIONS, useAsyncCheckPermissionsV2 } from '@/hooks/misc/useCheckPermissionsV2'
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 { can: canManageSecretKeys, isLoading: isLoadingPermission } = useAsyncCheckPermissionsV2([
FGA_PERMISSIONS.PROJECT.API_GATEWAY_KEYS_SECRET_READ,
])
const {
data: revealedKey,
isLoading,
reveal,
clear,
} = useRevealedSecret({
projectRef,
id: apiKey.id as string,
})
// 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 && !canManageSecretKeys
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}
/>
</TooltipTrigger>
<TooltipContent side="bottom">
{isRestricted
? 'You need additional permissions to reveal secret API keys'
: 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
? 'You need additional permissions to copy secret API keys'
: isLoadingPermission
? 'Loading permissions...'
: 'Copy API key'}
</TooltipContent>
</Tooltip>
</>
)
}