Files
supabase/apps/studio/data/oauth-apps/oauth-apps-preflight-validation-query.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

74 lines
2.5 KiB
TypeScript

import { useQuery, useQueryClient } from '@tanstack/react-query'
import { MAX_RETRY_FAILURE_COUNT } from '../query-client'
import { oauthAppsKeys } from './keys'
import { isOAuthRoleValidationError } from './types'
import { get, handleError } from '@/data/fetchers'
import type { StudioError } from '@/lib/errors/studio-errors'
import type { UseCustomQueryOptions } from '@/types'
export type OAuthAppsPreflightValidationVariables = {
slug?: string
auth_id?: string
}
export async function getOAuthAppsPreflightValidation(
{ slug, auth_id }: OAuthAppsPreflightValidationVariables,
signal?: AbortSignal
) {
if (!slug) throw new Error('Organization slug is required')
if (!auth_id) throw new Error('Authorization request id is required')
const { data, error } = await get(
'/platform/organizations/{slug}/oauth/authorizations/{id}/preflight',
{
params: { path: { slug, id: auth_id } },
signal,
}
)
if (error && isOAuthRoleValidationError(error)) {
throw error
}
if (error) handleError(error)
return data
}
export type OAuthAppsPreflightValidationData = Awaited<
ReturnType<typeof getOAuthAppsPreflightValidation>
>
export type OAuthAppsPreflightValidationError = StudioError
export const useOAuthAppsPreflightValidationQuery = <TData = OAuthAppsPreflightValidationData>(
{ slug, auth_id }: OAuthAppsPreflightValidationVariables,
{
enabled = true,
...options
}: UseCustomQueryOptions<
OAuthAppsPreflightValidationData,
OAuthAppsPreflightValidationError,
TData
> = {}
) => {
const queryClient = useQueryClient()
return useQuery<OAuthAppsPreflightValidationData, OAuthAppsPreflightValidationError, TData>({
queryKey: oauthAppsKeys.preflightValidation(slug, auth_id),
queryFn: ({ signal }) => getOAuthAppsPreflightValidation({ slug, auth_id }, signal),
enabled: enabled && Boolean(slug) && Boolean(auth_id),
retry: (failureCount, error) => {
if (isOAuthRoleValidationError(error)) return false
const defaultOptions = queryClient.getDefaultOptions()
if (defaultOptions.queries?.retry != null) {
if (typeof defaultOptions.queries.retry === 'function') {
return defaultOptions.queries.retry(failureCount, error)
}
if (typeof defaultOptions.queries.retry === 'number') {
return failureCount < defaultOptions.queries.retry
}
return defaultOptions.queries.retry
}
return failureCount < MAX_RETRY_FAILURE_COUNT
},
...options,
})
}