mirror of
https://github.com/supabase/supabase.git
synced 2026-12-04 07:29:16 +00:00
## 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
113 lines
4.0 KiB
TypeScript
113 lines
4.0 KiB
TypeScript
import { InfiniteData, useInfiniteQuery, useQueryClient } from '@tanstack/react-query'
|
|
import { platformComponents as components } from 'api-types'
|
|
import { useCallback } from 'react'
|
|
|
|
import { INFINITE_PROJECTS_KEY_PREFIX, projectKeys } from './keys'
|
|
import { get, handleError } from '@/data/fetchers'
|
|
import type { StudioError } from '@/lib/errors/studio-errors'
|
|
import { useProfile } from '@/lib/profile'
|
|
import type { UseCustomInfiniteQueryOptions } from '@/types'
|
|
|
|
/**
|
|
* [Joshen] Try to keep this value a multiple of 6 (common denominator of 2 and 3) to fit the cards
|
|
* view on the project home page, So that the last row will always be a full row of cards while
|
|
* there's a next page. API max rows is 100, I'm just choosing 96 here as the highest value thats a
|
|
* multiple of 6.
|
|
*
|
|
* Also deliberately omitting limit as a param in this RQ hook just so we can consistently use the same
|
|
* cached data and not unnecessarily ping API for different limit values. (Let me know if there happens
|
|
* to be a UI that might need a specific limit value that we need to reintroduce that param)
|
|
*/
|
|
const DEFAULT_LIMIT = 96
|
|
|
|
interface GetOrgProjectsInfiniteVariables {
|
|
slug?: string
|
|
sort?: 'name_asc' | 'name_desc' | 'created_asc' | 'created_desc'
|
|
search?: string
|
|
page?: number
|
|
statuses?: string[]
|
|
refs?: string[]
|
|
}
|
|
|
|
export type OrgProjectsResponse = components['schemas']['OrganizationProjectsResponse_Output']
|
|
export type OrgProject = OrgProjectsResponse['projects'][number]
|
|
|
|
export async function getOrganizationProjects(
|
|
{
|
|
slug,
|
|
page = 0,
|
|
sort = 'name_asc',
|
|
search: _search = '',
|
|
refs: _refs = [],
|
|
statuses: _statuses = [],
|
|
}: GetOrgProjectsInfiniteVariables,
|
|
signal?: AbortSignal,
|
|
headers?: Record<string, string>
|
|
) {
|
|
if (!slug) throw new Error('Slug is required')
|
|
|
|
const limit = DEFAULT_LIMIT
|
|
const offset = page * limit
|
|
const search = _search.length === 0 ? undefined : _search
|
|
const statuses = _statuses.length === 0 ? undefined : _statuses.join(',')
|
|
const refs = _refs.length === 0 ? undefined : _refs.join(',')
|
|
|
|
const { data, error } = await get('/platform/organizations/{slug}/projects', {
|
|
params: { path: { slug }, query: { limit, offset, sort, search, statuses, refs } },
|
|
signal,
|
|
headers,
|
|
})
|
|
|
|
if (error) handleError(error)
|
|
return data
|
|
}
|
|
|
|
export type OrgProjectsInfiniteData = Awaited<ReturnType<typeof getOrganizationProjects>>
|
|
export type OrgProjectsInfiniteError = StudioError
|
|
|
|
export const useOrgProjectsInfiniteQuery = <TData = OrgProjectsInfiniteData>(
|
|
{ slug, sort = 'name_asc', search, statuses = [], refs = [] }: GetOrgProjectsInfiniteVariables,
|
|
{
|
|
enabled = true,
|
|
...options
|
|
}: UseCustomInfiniteQueryOptions<
|
|
OrgProjectsInfiniteData,
|
|
OrgProjectsInfiniteError,
|
|
InfiniteData<TData>,
|
|
readonly unknown[],
|
|
number | undefined
|
|
> = {}
|
|
) => {
|
|
const { profile } = useProfile()
|
|
return useInfiniteQuery({
|
|
queryKey: projectKeys.infiniteListByOrg(slug, { sort, search, statuses, refs }),
|
|
queryFn: ({ signal, pageParam }) =>
|
|
getOrganizationProjects({ slug, page: pageParam, sort, search, statuses, refs }, signal),
|
|
enabled: enabled && profile !== undefined && typeof slug !== 'undefined',
|
|
staleTime: 30 * 60 * 1000, // 30 minutes
|
|
initialPageParam: 0,
|
|
getNextPageParam(lastPage, pages) {
|
|
const page = pages.length
|
|
const currentTotalCount = page * DEFAULT_LIMIT
|
|
const totalCount = lastPage.pagination?.count
|
|
|
|
if (totalCount === undefined || currentTotalCount >= totalCount) return undefined
|
|
return page
|
|
},
|
|
...options,
|
|
})
|
|
}
|
|
|
|
export const getComputeSize = (project: OrgProject) => {
|
|
const primaryDatabase = project.databases.find((db) => db.identifier === project.ref)
|
|
return primaryDatabase?.infra_compute_size
|
|
}
|
|
|
|
export const useInvalidateProjectsInfiniteQuery = () => {
|
|
const queryClient = useQueryClient()
|
|
const invalidateProjectsQuery = useCallback(() => {
|
|
return queryClient.invalidateQueries({ queryKey: [INFINITE_PROJECTS_KEY_PREFIX] })
|
|
}, [queryClient])
|
|
return { invalidateProjectsQuery }
|
|
}
|