The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.
TypeScript aialternativeauthdatabasedenoembeddingsexamplefirebasenextjsoauth2pgvectorpostgispostgrespostgresqlpostgrestrealtimesupabasevectorswebsockets
feat(studio): add never option to scoped pat expiry (#51273)
kemal.earthb02890813626
diff --git a/apps/studio/components/interfaces/Account/AccessTokens/Scoped/Form/NewScopedTokenForm.utils.test.ts b/apps/studio/components/interfaces/Account/AccessTokens/Scoped/Form/NewScopedTokenForm.utils.test.tsindex ec481a840bb..ce500e6e2eb 100644--- a/apps/studio/components/interfaces/Account/AccessTokens/Scoped/Form/NewScopedTokenForm.utils.test.ts+++ b/apps/studio/components/interfaces/Account/AccessTokens/Scoped/Form/NewScopedTokenForm.utils.test.ts@@ -1,7 +1,7 @@import dayjs from 'dayjs'import { describe, expect, test } from 'vitest'-import { TokenFormSchema } from './NewScopedTokenForm.utils'+import { getExpiryDate, TokenFormSchema } from './NewScopedTokenForm.utils'const BASE_VALUES = {tokenName: 'test',@@ -35,3 +35,19 @@ describe('TokenFormSchema custom expiry', () => {)})})++describe('TokenFormSchema never expiry', () => {+ test('accepts the never preset without a date', () => {+ const result = TokenFormSchema.safeParse({ ...BASE_VALUES, expiresAt: 'never' })+ expect(result.success).toBe(true)+ })+})++describe('getExpiryDate', () => {+ test('returns no date for the never preset', () => {+ expect(getExpiryDate('never')).toBeUndefined()+ })+ test('returns no date for the custom preset', () => {+ expect(getExpiryDate('custom')).toBeUndefined()+ })+})diff --git a/apps/studio/components/interfaces/Account/AccessTokens/Scoped/Form/NewScopedTokenForm.utils.ts b/apps/studio/components/interfaces/Account/AccessTokens/Scoped/Form/NewScopedTokenForm.utils.tsindex 1ef60b1b2f5..6aa9589f18a 100644--- a/apps/studio/components/interfaces/Account/AccessTokens/Scoped/Form/NewScopedTokenForm.utils.ts+++ b/apps/studio/components/interfaces/Account/AccessTokens/Scoped/Form/NewScopedTokenForm.utils.ts@@ -4,7 +4,7 @@ import { z } from 'zod'import type { PermissionMode } from '../../AccessToken.permissions'import { getMaxCustomExpiryDate } from '../../AccessToken.utils'-export const EXPIRY_PRESETS = ['24h', '7d', '30d', '90d', 'custom'] as const+export const EXPIRY_PRESETS = ['24h', '7d', '30d', '90d', 'custom', 'never'] as constexport type ExpiryPreset = (typeof EXPIRY_PRESETS)[number]export const DEFAULT_EXPIRY: ExpiryPreset = '7d'@@ -21,9 +21,13 @@ export const EXPIRY_OPTIONS: ExpiryOption[] = [{ value: '30d', label: '30 days' },{ value: '90d', label: '90 days' },{ value: 'custom', label: 'Custom' },+ { value: 'never', label: 'Never' },]-/** Resolves the final ISO expiry date for a preset, or `undefined` when the caller supplies a custom date. */+/**+ * Resolves the final ISO expiry date for a preset. Returns `undefined` for `custom`, where the+ * caller supplies the date, and for `never`, where the token is created without an expiry.+ */export const getExpiryDate = (preset: ExpiryPreset): string | undefined => {switch (preset) {case '24h':@@ -34,6 +38,8 @@ export const getExpiryDate = (preset: ExpiryPreset): string | undefined => {return dayjs().add(30, 'days').toISOString()case '90d':return dayjs().add(90, 'days').toISOString()+ case 'never':+ return undefineddefault:return undefined}diff --git a/apps/studio/components/interfaces/Account/AccessTokens/Scoped/Form/TokenDetails.tsx b/apps/studio/components/interfaces/Account/AccessTokens/Scoped/Form/TokenDetails.tsxindex 48d2897c301..ff25c9b8de8 100644--- a/apps/studio/components/interfaces/Account/AccessTokens/Scoped/Form/TokenDetails.tsx+++ b/apps/studio/components/interfaces/Account/AccessTokens/Scoped/Form/TokenDetails.tsx@@ -14,6 +14,7 @@ import {SelectValue,useWatch,} from 'ui'+import { Admonition } from 'ui-patterns/Admonition'import {DatePicker,DatePickerButton,@@ -35,6 +36,7 @@ interface TokenDetailsProps {}export const TokenDetails = ({ control, setValue }: TokenDetailsProps) => {+ const expiresAt = useWatch({ control, name: 'expiresAt' })const customExpiryDate = useWatch({ control, name: 'customExpiryDate' })const maxExpiryDate = getMaxCustomExpiryDate().toDate()@@ -136,6 +138,15 @@ export const TokenDetails = ({ control, setValue }: TokenDetailsProps) => {</FormItemLayout>)}/>++ {expiresAt === 'never' && (+ <Admonition+ type="warning"+ className="mb-0"+ title="This token never expires"+ description="Anyone with the token keeps access until you delete it."+ />+ )}</section>)}diff --git a/apps/studio/components/interfaces/Account/AccessTokens/Scoped/NewScopedTokenSheet.test.tsx b/apps/studio/components/interfaces/Account/AccessTokens/Scoped/NewScopedTokenSheet.test.tsxindex 3f40605cffe..cc15bd2c105 100644--- a/apps/studio/components/interfaces/Account/AccessTokens/Scoped/NewScopedTokenSheet.test.tsx+++ b/apps/studio/components/interfaces/Account/AccessTokens/Scoped/NewScopedTokenSheet.test.tsx@@ -255,6 +255,49 @@ describe('NewScopedTokenSheet', () => {)}, 10_000)+ test('creates a token without an expiry when the Never preset is selected', async () => {+ let createBody: Record<string, unknown> | undefined+ addAPIMock({+ method: 'post',+ path: '/platform/profile/scoped-access-tokens',+ response: async ({ request }) => {+ createBody = (await request.json()) as Record<string, unknown>+ return HttpResponse.json<CreateTokenResponse>({+ created_at: '',+ expires_at: null,+ id: 'plop',+ last_used_at: null,+ name: 'test',+ token: 'a_token_value',+ token_alias: '',+ permissions: [],+ })+ },+ })+ renderSheet()+ fireEvent.click(await screen.findByRole('button', { name: 'Generate new token' }))+ await screen.findByRole('dialog')+ await user.type(await screen.findByLabelText('Name'), 'test')+ fireEvent.click(await screen.findByRole('combobox', { name: 'Expires in' }))+ fireEvent.click(await screen.findByRole('option', { name: 'Never' }))+ fireEvent.click(await screen.findByRole('combobox', { name: 'Organization' }))+ fireEvent.click(await screen.findByRole('option', { name: 'Acme Production' }))+ fireEvent.click(await screen.findByRole('combobox', { name: 'Projects' }))+ fireEvent.click(await screen.findByRole('option', { name: 'Project 1' }))+ await expandPermissionCategory('Project')+ fireEvent.click(await screen.findByLabelText('Project Settings', { exact: false }))+ fireEvent.click(await screen.findByRole('option', { name: 'Read' }))+ fireEvent.click(await screen.findByRole('button', { name: 'Review access' }))+ await screen.findByText('Never')+ fireEvent.click(await screen.findByRole('button', { name: 'Create token' }))+ await waitFor(() => expect(createBody).toBeDefined())+ expect(createBody?.expires_at).toBeUndefined()+ expect(mockTrack).toHaveBeenCalledWith(+ 'access_token_created',+ expect.objectContaining({ expiryPreset: 'never' })+ )+ }, 10_000)+test('tracks dismissal with the in-progress resourceAccess and touched state on Cancel', async () => {renderSheet()fireEvent.click(await screen.findByRole('button', { name: 'Generate new token' }))