Files
supabase/apps/studio/data/oauth-apps/oauth-apps-authorize-approve-mutation.ts
Gildas Garcia 10488ed799 feat(studio): Wire the OAuth apps consent screen to the backend (#53511)
## Description

Part of the work for OAuth apps with the scoped permissions. This PR
wires consent screen to the backend.

## Review instructions

### Setup
Copy the `examples/oauth-app-authorization-flow/.env.example` to
`examples/oauth-app-authorization-flow/.env`

> [!IMPORTANT]  
> In next steps, make sure to copy the apps ids/secrets in the
`examples/oauth-app-authorization-flow/.env` with a way to differentiate
them:
> ```
> # org bound all projects
> SUPABASE_CLIENT_ID=xxx
> SUPABASE_CLIENT_SECRET=xxx
> ```

As an organization admin, go to the _Organization settings/OAuth Apps_
and create the following _Publishable apps_:

- `test_org_bound`: 
  - _Use the permissions of the person who connects_ unchecked
  - _Ask for specific projects_ unchecked
  - Select only _Database Read_ for the permissions
 
- `test_org_bound_projects`: 
  - _Use the permissions of the person who connects_ unchecked
  - _Ask for specific projects_ checked
  - Select only _Database Read_ for the permissions

- `test_member_bound`: 
  - _Use the permissions of the person who connects_ checked
  - _Ask for specific projects_ unchecked
  - Select only _Database Read/Write_ for the permissions
 
- `test_member_bound_projects`: 
  - _Use the permissions of the person who connects_ checked
  - _Ask for specific projects_ checked
  - Select only _Database Read/Write_ for the permissions

Make sure you have 3 projects in the org. Add a team member that have
Administration rights on the first project, read-only access to the 2nd
and no access to the third.

Install the `examples/oauth-app-authorization-flow` dependencies and
start the app.
- `test_org_bound`: 
  - connected on studio as the org admin
    - click the `oauth-app-authorization-flow` button
- check that you don't have to select a project on the consent screen
- check that the permissions requested are correct and you can approve
the app
- update the published app to include the _Database Write_ permissions
- return to the `oauth-app-authorization-flow` index and click its
button again
- check that the consent screen shows the permissions difference and
approve it again
- connected as a simple team member, check that you have a message
indicating you can't approve this app
  
- `test_org_bound_projects`: 
  - connected on studio as the org admin
    - click the `oauth-app-authorization-flow` button
    - check that you have to select projects on the consent screen
- check that the permissions requested are correct and you can approve
the app
- update the published app to include the _Database Write_ permissions
- return to the `oauth-app-authorization-flow` index and click its
button again
- check that the consent screen shows the permissions difference and
approve it again
- connected as a simple team member, check that you have a message
indicating you can't approve this app

- `test_member_bound`: 
  - connected on studio as a member 
    - click the `oauth-app-authorization-flow` button
- check that you don't have to select a project on the consent screen
- check that the permissions requested are correct and you can approve
the app
    - update the published app to add another permission
- return to the `oauth-app-authorization-flow` index and click its
button again
- check that the consent screen shows the permissions difference and
approve it again
   
- `test_member_bound_projects`: 
  - connected on studio as the org admin
    - click the `oauth-app-authorization-flow` button
- check that you have to select projects on the consent screen (you
should see only the two you have access to) and select them all one by
one
    - check that the permissions requested are correct
- approve the app and check that you have an error related to the
project with read-only access
    - unselect it and approve the app
- update the published app to include the _Database Write_ permissions
- return to the `oauth-app-authorization-flow` index and click its
button again
- check that the consent screen shows the permissions difference and
approve it again
2026-11-19 17:47:03 +01:00

64 lines
1.8 KiB
TypeScript

import { useMutation } from '@tanstack/react-query'
import { components } from 'api-types/types/platform'
import { toast } from 'sonner'
import { isOAuthRoleValidationError } from './types'
import { handleError, post } from '@/data/fetchers'
import type { StudioError } from '@/lib/errors/studio-errors'
import type { UseCustomMutationOptions } from '@/types'
type OAuthAuthorizeApproveRequest = components['schemas']['ApproveAuthorizationBody']
export type OAuthAppsAuthorizeApproveVariables = {
slug?: string
auth_id?: string
body: OAuthAuthorizeApproveRequest
}
export async function approveOAuthAppsAuthorize({
slug,
auth_id,
body,
}: OAuthAppsAuthorizeApproveVariables) {
if (!auth_id) throw new Error('Authorization request id is required')
if (!slug) throw new Error('Organization slug is required')
const { data, error } = await post('/platform/organizations/{slug}/oauth/authorizations/{id}', {
params: { path: { slug, id: auth_id } },
body,
})
if (error && isOAuthRoleValidationError(error)) {
throw error
}
if (error) handleError(error)
return data
}
export type OAuthAppsAuthorizeApproveData = Awaited<ReturnType<typeof approveOAuthAppsAuthorize>>
export const useOAuthAppsAuthorizeApproveMutation = ({
onError,
...options
}: Omit<
UseCustomMutationOptions<
OAuthAppsAuthorizeApproveData,
StudioError,
OAuthAppsAuthorizeApproveVariables
>,
'mutationFn'
> = {}) => {
return useMutation<
OAuthAppsAuthorizeApproveData,
StudioError,
OAuthAppsAuthorizeApproveVariables
>({
mutationFn: (vars) => approveOAuthAppsAuthorize(vars),
async onError(data, variables, context) {
if (onError === undefined) {
toast.error(`Failed to authorize access to OAuth app: ${data.message}`)
} else {
onError(data, variables, context)
}
},
...options,
})
}