import { OAuthScope } from '@supabase/shared-types/out/constants'
import { CheckIcon, ChevronDownIcon } from 'lucide-react'
import { useTheme } from 'next-themes'
import { useMemo, useState } from 'react'
import {
Badge,
Card,
CardContent,
cn,
Collapsible,
CollapsibleContent,
CollapsibleTrigger,
} from 'ui'
import { Admonition } from 'ui-patterns/Admonition'
import { InfoTooltip } from 'ui-patterns/info-tooltip'
import { PERMISSIONS_DESCRIPTIONS } from './OAuthApps.constants'
import { getOAuthImpersonationWarning, getRequesterLogo } from './OAuthApps.utils'
import {
CONNECT_LOGO_LIGHT_TILE_CLASSNAME,
LogoBox,
LogoPair,
SupabaseLogo,
} from '@/components/layouts/InterstitialLayout'
import { InlineLink } from '@/components/ui/InlineLink'
import { DOCS_URL } from '@/lib/constants'
const OAUTH_SCOPES_DOCS_URL = `${DOCS_URL}/guides/integrations/build-a-supabase-oauth-integration/oauth-scopes`
const PERMISSION_DETAILS_TRIGGER_CLASSNAME =
'mx-auto flex h-7 cursor-pointer items-center justify-center gap-1.5 rounded-md px-2 text-xs text-foreground-lighter transition-colors hover:bg-surface-200 hover:text-foreground'
export interface AuthorizeRequesterDetailsProps {
name: string
domain: string
scopes: OAuthScope[]
showOnlyScopes?: boolean
}
export const ScopeSection = ({
description,
hasReadScope,
hasWriteScope,
}: {
description: string
hasReadScope: boolean
hasWriteScope: boolean
}) => {
if (!hasReadScope && !hasWriteScope) return null
const permissions = [hasReadScope ? 'Read' : null, hasWriteScope ? 'Write' : null]
.filter(Boolean)
.join(' and ')
return (
{permissions} {description}
)
}
type PermissionLevel = 'read' | 'write' | 'read-write'
type PermissionItem = {
label: string
description: string
read?: OAuthScope
write?: OAuthScope
}
type PermissionGroup = {
label: string
items: PermissionItem[]
}
const PERMISSION_GROUPS: PermissionGroup[] = [
{
label: 'Data & authentication',
items: [
{
label: 'Database management',
description: PERMISSIONS_DESCRIPTIONS.DATABASE,
read: OAuthScope.DATABASE_READ,
write: OAuthScope.DATABASE_WRITE,
},
{
label: 'Database token access',
description: 'connect to Postgres as the app’s database role with an access token.',
read: OAuthScope.DATABASE_TOKEN_ACCESS_READ,
write: OAuthScope.DATABASE_TOKEN_ACCESS_WRITE,
},
{
label: 'Secrets',
description: PERMISSIONS_DESCRIPTIONS.SECRETS,
read: OAuthScope.SECRETS_READ,
write: OAuthScope.SECRETS_WRITE,
},
{
label: 'Auth',
description: PERMISSIONS_DESCRIPTIONS.AUTH,
read: OAuthScope.AUTH_READ,
write: OAuthScope.AUTH_WRITE,
},
],
},
{
label: 'Code execution',
items: [
{
label: 'Edge Functions',
description: PERMISSIONS_DESCRIPTIONS.EDGE_FUNCTIONS,
read: OAuthScope.EDGE_FUNCTIONS_READ,
write: OAuthScope.EDGE_FUNCTIONS_WRITE,
},
],
},
{
label: 'Platform management',
items: [
{
label: 'Environment',
description: PERMISSIONS_DESCRIPTIONS.ENVIRONMENT,
read: OAuthScope.ENVIRONMENT_READ,
write: OAuthScope.ENVIRONMENT_WRITE,
},
{
label: 'Organizations',
description: PERMISSIONS_DESCRIPTIONS.ORGANIZATIONS,
read: OAuthScope.ORGANIZATIONS_READ,
write: OAuthScope.ORGANIZATIONS_WRITE,
},
{
label: 'Projects',
description: PERMISSIONS_DESCRIPTIONS.PROJECTS,
read: OAuthScope.PROJECTS_READ,
write: OAuthScope.PROJECTS_WRITE,
},
{
label: 'Domains',
description: PERMISSIONS_DESCRIPTIONS.DOMAINS,
read: OAuthScope.DOMAINS_READ,
write: OAuthScope.DOMAINS_WRITE,
},
{
label: 'PostgREST',
description: PERMISSIONS_DESCRIPTIONS.REST,
read: OAuthScope.REST_READ,
write: OAuthScope.REST_WRITE,
},
],
},
{
label: 'Files & monitoring',
items: [
{
label: 'Analytics',
description: PERMISSIONS_DESCRIPTIONS.ANALYTICS,
read: OAuthScope.ANALYTICS_READ,
write: OAuthScope.ANALYTICS_WRITE,
},
{
label: 'Analytics configuration',
description: PERMISSIONS_DESCRIPTIONS.ANALYTICS_CONFIG,
read: OAuthScope.ANALYTICS_CONFIG_READ,
write: OAuthScope.ANALYTICS_CONFIG_WRITE,
},
{
label: 'Storage',
description: PERMISSIONS_DESCRIPTIONS.STORAGE,
read: OAuthScope.STORAGE_READ,
write: OAuthScope.STORAGE_WRITE,
},
],
},
]
/**
* Connect interstitial header mark for `/authorize`.
* Curated logos resolve from allowlisted redirect_uri hosts; otherwise pair only
* when a usable remote icon is present, else show Supabase alone.
*
* Uploaded / unknown bitmaps have no light/dark metadata, so both tiles use
* fixed light chrome (`forceLight`) across Studio themes. Curated partners keep
* theme-reactive tiles and may swap dark assets when available.
*/
export const AuthorizeConnectLogo = ({
icon,
name,
redirectUri,
}: {
icon: string | null
name: string
redirectUri?: string | null
}) => {
const [failedIcon, setFailedIcon] = useState(null)
const { resolvedTheme } = useTheme()
const logo = useMemo(
() =>
getRequesterLogo({
icon,
name,
redirectUri,
useDarkVariant: resolvedTheme === 'dark',
}),
[icon, name, redirectUri, resolvedTheme]
)
const hasUsableLogo = Boolean(logo.src) && failedIcon !== logo.src
if (!hasUsableLogo) {
return
}
const forceLightPair = !logo.isKnownClient
return (
setFailedIcon(logo.src)}
/>
}
right={}
/>
)
}
export const AuthorizeImpersonationWarning = ({
name,
redirectUri,
}: {
name: string
redirectUri?: string | null
}) => {
const warning = getOAuthImpersonationWarning({ name, redirectUri })
if (!warning) return null
return (
)
}
export const AuthorizeRequesterDetails = ({
name,
scopes,
showOnlyScopes = false,
}: AuthorizeRequesterDetailsProps) => {
const [showDetails, setShowDetails] = useState(showOnlyScopes)
const requestedPermissions = useMemo(() => getRequestedPermissions(scopes), [scopes])
const writablePermissions = requestedPermissions.filter(({ level }) => level !== 'read')
const readOnlyPermissions = requestedPermissions.filter(({ level }) => level === 'read')
return (
{requestedPermissions.length === 0 ? (
No permissions requested.
) : (
<>
{!showOnlyScopes && (
<>
Permissions
Authorizing {name} grants it the following{' '}
access permissions to the
selected organization. Only continue if you trust this app.
{writablePermissions.length > 0 && (
)}
{readOnlyPermissions.length > 0 && (
)}
>
)}
{showDetails ? 'Hide detailed permissions' : 'Show detailed permissions'}
>
)}
)
}
function PermissionSummaryRow({
permissions,
level,
}: {
permissions: Array
level: PermissionLevel
}) {
return (
{permissions.map(({ label }) => label).join(', ')}
{formatPermissionLevel(level)}
)
}
function PermissionDetails({
requestedPermissions,
}: {
requestedPermissions: Array
}) {
const requestedByLabel = new Map(
requestedPermissions.map((permission) => [permission.label, permission])
)
return (
{PERMISSION_GROUPS.map((group) => {
const groupPermissions = group.items
.map((item) => requestedByLabel.get(item.label))
.filter(Boolean) as RequestedPermission[]
if (groupPermissions.length === 0) return null
return (
{group.label}
{groupPermissions.map((permission) => (
{permission.label}
{formatPermissionDescription(permission.description)}
{formatPermissionLevel(permission.level)}
))}
)
})}
)
}
type RequestedPermission = PermissionItem & {
level: PermissionLevel
}
function getRequestedPermissions(scopes: OAuthScope[]): RequestedPermission[] {
return PERMISSION_GROUPS.flatMap((group) =>
group.items.flatMap((item) => {
const hasReadScope = !!item.read && scopes.includes(item.read)
const hasWriteScope = !!item.write && scopes.includes(item.write)
if (!hasReadScope && !hasWriteScope) return []
return {
...item,
level: hasReadScope && hasWriteScope ? 'read-write' : hasWriteScope ? 'write' : 'read',
}
})
)
}
function formatPermissionLevel(level: PermissionLevel) {
if (level === 'read') return 'Read'
if (level === 'write') return 'Write'
return 'Read + Write'
}
function formatPermissionDescription(description: string) {
return description.charAt(0).toUpperCase() + description.slice(1)
}
function getPermissionLevelBadgeVariant(level: PermissionLevel) {
return level === 'read' ? 'default' : 'warning'
}