Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

feat(studio): scoped oauth apps, admins can revoke an app (#50980)

Gildas Garcia4f23e9117452
diff --git a/apps/studio/components/interfaces/Organization/OAuthApps/OAuthAppsAuthorizedList.test.tsx b/apps/studio/components/interfaces/Organization/OAuthApps/OAuthAppsAuthorizedList.test.tsx
index 0eb418be695..65d374a1253 100644
--- a/apps/studio/components/interfaces/Organization/OAuthApps/OAuthAppsAuthorizedList.test.tsx
+++ b/apps/studio/components/interfaces/Organization/OAuthApps/OAuthAppsAuthorizedList.test.tsx
@@ -1,6 +1,7 @@
-import { screen, within } from '@testing-library/react'
+import { screen, waitFor, within } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import { mockIntersectionObserver } from 'jsdom-testing-mocks'
+import { toast } from 'sonner'
import { beforeEach, describe, expect, test, vi } from 'vitest'
import { OAuthAppsAuthorizedList } from '@/components/interfaces/Organization/OAuthApps/OAuthAppsAuthorizedList'
@@ -69,4 +70,42 @@ describe('OAuthAppsAuthorizedList', () => {
expect(await screen.findByText('Organization-wide')).toBeInTheDocument()
expect(await screen.findByText(/All projects/)).toBeInTheDocument()
})
+
+ test('only offers Revoke to an admin on an active row', async () => {
+ canRevoke = false
+ const { rerender } = customRender(<OAuthAppsAuthorizedList />)
+
+ await openRowMenu('Vercel')
+ expect(await screen.findByText('View grants')).toBeInTheDocument()
+ expect(screen.queryByText('Revoke grants')).not.toBeInTheDocument()
+
+ canRevoke = true
+ rerender(<OAuthAppsAuthorizedList />)
+
+ await openRowMenu('Contoso Analytics')
+ expect(await screen.findByText('View grants')).toBeInTheDocument()
+ expect(await screen.findByText('Revoke grants')).toBeInTheDocument()
+ })
+
+ test('renders the revoke caveats and revokes on confirm', async () => {
+ customRender(<OAuthAppsAuthorizedList />)
+
+ await openRowMenu('Vercel')
+ await userEvent.click(await screen.findByText('Revoke grants'))
+
+ expect(await screen.findByText('Revoke access for Vercel')).toBeInTheDocument()
+ expect(screen.queryByText(/organization-wide grant will be revoked/)).not.toBeInTheDocument()
+ expect(
+ await screen.findByText("All members lose access on the app's next request")
+ ).toBeInTheDocument()
+ expect(
+ await screen.findByText('Members must authorize the app again to reconnect')
+ ).toBeInTheDocument()
+
+ await userEvent.click(screen.getByRole('button', { name: 'Revoke' }))
+
+ await waitFor(() => {
+ expect(toast.success).toHaveBeenCalledWith('Revoked access for Vercel')
+ })
+ })
})
diff --git a/apps/studio/components/interfaces/Organization/OAuthApps/OAuthAppsAuthorizedList.tsx b/apps/studio/components/interfaces/Organization/OAuthApps/OAuthAppsAuthorizedList.tsx
index c4d272242fb..16d41f40043 100644
--- a/apps/studio/components/interfaces/Organization/OAuthApps/OAuthAppsAuthorizedList.tsx
+++ b/apps/studio/components/interfaces/Organization/OAuthApps/OAuthAppsAuthorizedList.tsx
@@ -27,6 +27,11 @@ export const OAuthAppsAuthorizedList = () => {
'approved_oauth_apps'
)
+ const { can: canRevokeOAuthApps } = useAsyncCheckPermissions(
+ PermissionAction.DELETE,
+ 'approved_oauth_apps'
+ )
+
const {
data,
isPending,
@@ -136,6 +141,7 @@ export const OAuthAppsAuthorizedList = () => {
!isFetchingNextPage &&
'border-none'
)}
+ canRevoke={canRevokeOAuthApps}
/>
))}
</Fragment>
diff --git a/apps/studio/components/interfaces/Organization/OAuthApps/OAuthAppsAuthorizedRow.tsx b/apps/studio/components/interfaces/Organization/OAuthApps/OAuthAppsAuthorizedRow.tsx
index af2400a35bf..df27f9fb3ca 100644
--- a/apps/studio/components/interfaces/Organization/OAuthApps/OAuthAppsAuthorizedRow.tsx
+++ b/apps/studio/components/interfaces/Organization/OAuthApps/OAuthAppsAuthorizedRow.tsx
@@ -1,4 +1,4 @@
-import { MoreVerticalIcon } from 'lucide-react'
+import { Lock, MoreVertical, X } from 'lucide-react'
import { useRef, useState } from 'react'
import {
Button,
@@ -7,6 +7,7 @@ import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuItem,
+ DropdownMenuSeparator,
DropdownMenuTrigger,
TableCell,
TableRow,
@@ -16,14 +17,20 @@ import {
} from 'ui'
import { OAuthAppsMemberGrantsDialogContent } from './OAuthAppsMemberGrantsDialogContent'
+import { OAuthAppsRevokeDialogContent } from './OAuthAppsRevokeDialogContent'
import type { OAuthApprovalItem, OAuthApprovalTarget } from '@/data/oauth-apps/types'
export interface OAuthAppsAuthorizedRowProps {
approval: OAuthApprovalItem
className?: string
+ canRevoke: boolean
}
-export const OAuthAppsAuthorizedRow = ({ approval, className }: OAuthAppsAuthorizedRowProps) => {
+export const OAuthAppsAuthorizedRow = ({
+ approval,
+ className,
+ canRevoke,
+}: OAuthAppsAuthorizedRowProps) => {
const [isDialogOpen, setIsDialogOpen] = useState(false)
const [dialogContent, setDialogContent] = useState<'grants' | 'revoke' | null>(null)
const menuTriggerRef = useRef<HTMLButtonElement | null>(null)
@@ -67,29 +74,44 @@ export const OAuthAppsAuthorizedRow = ({ approval, className }: OAuthAppsAuthori
<DropdownMenuTrigger asChild>
<Button
ref={menuTriggerRef}
- icon={<MoreVerticalIcon />}
+ icon={<MoreVertical />}
className="px-1"
aria-label="Manage app"
aria-describedby={undefined}
/>
</DropdownMenuTrigger>
</TooltipTrigger>
- <TooltipContent>Manage app</TooltipContent>
+ <TooltipContent side="bottom">Manage app</TooltipContent>
</Tooltip>
<DropdownMenuContent align="end" side="bottom" className="w-40">
<DialogTrigger asChild>
- <DropdownMenuItem onClick={() => setDialogContent('grants')}>
+ <DropdownMenuItem className="gap-x-2" onClick={() => setDialogContent('grants')}>
+ <Lock size={14} />
View grants
</DropdownMenuItem>
</DialogTrigger>
+ <DropdownMenuSeparator />
+ {canRevoke && (
+ <DialogTrigger asChild>
+ <DropdownMenuItem className="gap-x-2" onClick={() => setDialogContent('revoke')}>
+ <X size={14} />
+ Revoke grants
+ </DropdownMenuItem>
+ </DialogTrigger>
+ )}
</DropdownMenuContent>
</DropdownMenu>
{dialogContent === 'grants' && (
<OAuthAppsMemberGrantsDialogContent
approval={approval}
- onCloseAutoFocus={(event) => {
- event.preventDefault()
- }}
+ onCloseAutoFocus={preventDefault}
+ />
+ )}
+ {dialogContent === 'revoke' && (
+ <OAuthAppsRevokeDialogContent
+ approval={approval}
+ onClose={() => setDialogContent(null)}
+ onCloseAutoFocus={preventDefault}
/>
)}
</Dialog>
@@ -98,6 +120,10 @@ export const OAuthAppsAuthorizedRow = ({ approval, className }: OAuthAppsAuthori
)
}
+const preventDefault = (event: Event) => {
+ event.preventDefault()
+}
+
const getGrantTargetLabel = (target: OAuthApprovalTarget) => {
if (target === 'members') return 'Members'
if (target === 'organization') return 'Organization'
diff --git a/apps/studio/components/interfaces/Organization/OAuthApps/OAuthAppsMemberGrantsDialogContent.tsx b/apps/studio/components/interfaces/Organization/OAuthApps/OAuthAppsMemberGrantsDialogContent.tsx
index 53fe6aa020c..72f6334f614 100644
--- a/apps/studio/components/interfaces/Organization/OAuthApps/OAuthAppsMemberGrantsDialogContent.tsx
+++ b/apps/studio/components/interfaces/Organization/OAuthApps/OAuthAppsMemberGrantsDialogContent.tsx
@@ -1,6 +1,6 @@
import { useIntersectionObserver } from '@uidotdev/usehooks'
import { useParams } from 'common'
-import { Building2, User } from 'lucide-react'
+import { Building, User } from 'lucide-react'
import { ComponentProps, Fragment, useEffect, useMemo } from 'react'
import {
Accordion,
@@ -13,6 +13,7 @@ import {
DialogContent,
DialogFooter,
DialogHeader,
+ DialogSectionSeparator,
DialogTitle,
ScrollArea,
} from 'ui'
@@ -21,6 +22,7 @@ import { ShimmeringLoader } from 'ui-patterns/ShimmeringLoader'
import { AlertError } from '@/components/ui/AlertError'
import { useOAuthAppMemberGrantsQuery } from '@/data/oauth-apps/oauth-apps-member-grants-query'
import type { OAuthApprovalItem, OAuthGrantItem } from '@/data/oauth-apps/types'
+import { pluralize } from '@/lib/helpers'
export interface OAuthAppsMemberGrantsDialogProps extends ComponentProps<typeof DialogContent> {
approval?: OAuthApprovalItem
@@ -69,10 +71,11 @@ export const OAuthAppsMemberGrantsDialogContent = ({
if (!approval) return null
return (
- <DialogContent size="small" {...props}>
+ <DialogContent size="medium" {...props}>
<DialogHeader>
<DialogTitle>Member grants for {approval.app.name}</DialogTitle>
</DialogHeader>
+ <DialogSectionSeparator />
<p aria-live="polite" className="sr-only">
{statusText}
@@ -87,7 +90,7 @@ export const OAuthAppsMemberGrantsDialogContent = ({
{isError && <AlertError subject="Failed to retrieve grants" error={error} />}
{isSuccess && (
- <ScrollArea className="px-4 h-90">
+ <ScrollArea className="px-4 h-94">
<p
className={cn('text-sm text-foreground-lighter', !isEmpty && 'sr-only')}
aria-live="polite"
@@ -125,39 +128,36 @@ const GrantAccordionItem = ({ grant }: { grant: OAuthGrantItem }) => {
return (
<AccordionItem value={grant.grant_id} className="border-b-0">
- <AccordionTrigger className="hover:no-underline">
+ <AccordionTrigger className="hover:no-underline gap-x-4 py-3.5">
{isOrganizationBound ? (
- <Building2 size={16} className="shrink-0 text-foreground-lighter rotate-0!" />
+ <Building size={16} className="shrink-0 text-foreground-lighter rotate-0!" />
) : (
<User size={16} className="shrink-0 text-foreground-lighter rotate-0!" />
)}
- <div className="min-w-0 flex-1">
+ <div className="min-w-0 flex-1 flex flex-col gap-y-1">
<p className="truncate text-sm text-foreground">{getGrantLabel(grant)}</p>
<p className="text-xs text-foreground-lighter">
{grant.projects == null
? 'All projects'
- : `${grant.projects.length} ${grant.projects.length === 1 ? 'project' : 'projects'}`}
+ : `${grant.projects.length} ${pluralize(grant.projects.length, 'project')}`}
{' · '}
- {permissionCount} {permissionCount === 1 ? 'permission' : 'permissions'}
+ {permissionCount} {pluralize(permissionCount, 'permission')}
</p>
</div>
</AccordionTrigger>
<AccordionContent>
- <div className="flex flex-col gap-3 pl-6">
+ <div className="flex flex-col gap-y-2 pl-8">
{grant.projects != null && grant.projects.length > 0 && (
<div className="flex flex-col gap-1">
- <p className="font-mono text-[11px] uppercase tracking-wider text-foreground-light">
- Projects
- </p>
+ <p className="font-mono text-xs uppercase text-foreground-light">Projects</p>
<p className="text-xs text-foreground">
{grant.projects.map((project) => project.name).join(', ')}
</p>
</div>
)}
+
<div className="flex flex-col gap-1">
- <p className="font-mono text-[11px] uppercase tracking-wider text-foreground-light">
- Permissions
- </p>
+ <p className="font-mono text-xs uppercase text-foreground-light">Permissions</p>
<p className="text-xs text-foreground">{grant.approved_scopes.join(', ')}</p>
</div>
</div>
diff --git a/apps/studio/components/interfaces/Organization/OAuthApps/OAuthAppsRevokeDialogContent.tsx b/apps/studio/components/interfaces/Organization/OAuthApps/OAuthAppsRevokeDialogContent.tsx
new file mode 100644
index 00000000000..6be55d8c09e
--- /dev/null
+++ b/apps/studio/components/interfaces/Organization/OAuthApps/OAuthAppsRevokeDialogContent.tsx
@@ -0,0 +1,97 @@
+import { useParams } from 'common'
+import { ComponentProps } from 'react'
+import { toast } from 'sonner'
+import {
+ Button,
+ DialogClose,
+ DialogContent,
+ DialogFooter,
+ DialogHeader,
+ DialogSection,
+ DialogSectionSeparator,
+ DialogTitle,
+} from 'ui'
+
+import { AlertError } from '@/components/ui/AlertError'
+import { useOAuthAppRevokeMutation } from '@/data/oauth-apps/oauth-apps-revoke-mutation'
+import type { OAuthApprovalItem } from '@/data/oauth-apps/types'
+
+export interface OAuthAppsRevokeDialogProps extends ComponentProps<typeof DialogContent> {
+ approval?: OAuthApprovalItem
+ onClose: () => void
+}
+
+export const OAuthAppsRevokeDialogContent = ({
+ approval,
+ onClose,
+ ...props
+}: OAuthAppsRevokeDialogProps) => {
+ const { slug } = useParams()
+
+ const {
+ mutate: revokeApp,
+ isPending,
+ isError,
+ error,
+ reset,
+ } = useOAuthAppRevokeMutation({
+ onSuccess: () => {
+ toast.success(`Revoked access for ${approval?.app.name}`)
+ onClose()
+ },
+ onError: () => {
+ // Do nothing, we just want to avoid the default toast.
+ // Error is displayed in the AlertError below
+ },
+ })
+
+ if (!approval) return null
+ const hasOrgGrant = Boolean(approval.org_grant)
+
+ return (
+ <DialogContent size="medium" {...props}>
+ <DialogHeader>
+ <DialogTitle>Revoke access for {approval.app.name}</DialogTitle>
+ </DialogHeader>
+ <DialogSectionSeparator />
+
+ {isError && (
+ <AlertError
+ subject={`An error occurred while revoking this ${approval.app.name} grant`}
+ className="mb-0 rounded-none border-x-0 border-t-0"
+ error={error}
+ />
+ )}
+
+ <DialogSection className="flex flex-col gap-y-2 text-sm text-foreground-light">
+ <p>
+ Revoking access removes app for the{' '}
+ <span className="font-medium text-foreground">entire organization</span>:
+ </p>
+
+ <ul className="flex flex-col gap-y-1 list-disc pl-6">
+ {hasOrgGrant && <li>The organization-wide grant is removed</li>}
+ <li>All members lose access on the app's next request</li>
+ <li>Members must authorize the app again to reconnect</li>
+ </ul>
+ </DialogSection>
+
+ <DialogFooter>
+ <DialogClose asChild>
+ <Button variant="default" disabled={isPending} onClick={() => reset()}>
+ Cancel
+ </Button>
+ </DialogClose>
+ <Button
+ variant="danger"
+ loading={isPending}
+ onClick={() => {
+ if (approval && slug) revokeApp({ slug, appId: approval.app.id })
+ }}
+ >
+ Revoke
+ </Button>
+ </DialogFooter>
+ </DialogContent>
+ )
+}
diff --git a/knip.jsonc b/knip.jsonc
index 717c5fc0285..ced31dc07a5 100644
--- a/knip.jsonc
+++ b/knip.jsonc
@@ -69,12 +69,8 @@
// tables that consume it. Scoped to the remaining files rather than the whole
// folder so everything else here stays gated.
// TODO(FE-4330): remove — used from #50529, drop this entry in that PR.
- "data/oauth-apps/oauth-apps-approvals-query.ts": ["files"],
"data/oauth-apps/oauth-apps-grants-query.ts": ["files"],
- "data/oauth-apps/oauth-apps-member-grants-query.ts": ["files"],
- "data/oauth-apps/oauth-apps-preflight-validation-query.ts": ["files"],
"data/oauth-apps/oauth-apps-revoke-grant-mutation.ts": ["files"],
- "data/oauth-apps/oauth-apps-revoke-mutation.ts": ["files"],
"hooks/misc/useTrackExperimentExposure.ts": ["files"],
// Staging-only pinned Postgres image + FDW request payload for creating
// Warehouse test projects by hand. Nothing imports it yet — it's kept