The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.
TypeScript aialternativeauthdatabasedenoembeddingsexamplefirebasenextjsoauth2pgvectorpostgispostgrespostgresqlpostgrestrealtimesupabasevectorswebsockets
Recovery Codes: improve code format (#50646)
Gildas Garciace683aa0e388
diff --git a/apps/studio/components/interfaces/Account/TOTPFactors/GenerateRecoveryCodesModal.test.tsx b/apps/studio/components/interfaces/Account/TOTPFactors/GenerateRecoveryCodesModal.test.tsxindex 4ac820f96a1..562ca916e2a 100644--- a/apps/studio/components/interfaces/Account/TOTPFactors/GenerateRecoveryCodesModal.test.tsx+++ b/apps/studio/components/interfaces/Account/TOTPFactors/GenerateRecoveryCodesModal.test.tsx@@ -3,6 +3,7 @@ import { fireEvent, screen, waitFor } from '@testing-library/react'import { describe, expect, test, vi } from 'vitest'import { GenerateRecoveryCodesModal } from './GenerateRecoveryCodesModal'+import { formatRecoveryCode } from './RecoveryCodesModal.utils'import { auth } from '@/lib/gotrue'import { customRender } from '@/tests/lib/custom-render'@@ -16,7 +17,18 @@ vi.mock('ui', async (importOriginal) => ({copyToClipboard: mockCopyToClipboard,}))-const codes = Array.from(Array(10).keys()).map((i) => `code_${i}`)+const codes = [+ 'wto24t5xbeulvjmi',+ 'ade6in2sufndbbwd',+ 'oge3npsrhrr66k25',+ 'pxbkfvxlbi6ggvnf',+ 'kc3oaioqdjn4htc3',+ '363gseoknplambiy',+ '3urgae2p2jegem4m',+ 'tbxog2guayp6uvud',+ 'rpcvcf4owbclxrfp',+ 'kyyfsp3eqjydj53t',+]describe('GenerateRecoveryCodesModal', () => {test('generate the recovery codes and allow users to copy them', async () => {@@ -34,8 +46,8 @@ describe('GenerateRecoveryCodesModal', () => {// Codes are generatedawait screen.findByText('Save your recovery codes')- await screen.findByText('code_0')- await screen.findByText('code_9')+ await screen.findByText('WTO2-4T5X-BEUL-VJMI')+ await screen.findByText('KYYF-SP3E-QJYD-J53T')// Users have to copy the codes to close the modal, next click should fail if they managed to close itexpect(await screen.findAllByRole('button', { name: 'Close' })).toHaveLength(1)@@ -44,7 +56,10 @@ describe('GenerateRecoveryCodesModal', () => {await waitFor(() =>expect(screen.getByRole('checkbox', { name: 'I have copied the codes' })).toBeChecked())- expect(mockCopyToClipboard).toHaveBeenCalledWith(codes.join('\n'), expect.any(Function))+ expect(mockCopyToClipboard).toHaveBeenCalledWith(+ codes.map((code) => formatRecoveryCode(code)).join('\n'),+ expect.any(Function)+ )// We should have 2 close buttons (header icon and a standard button)expect(await screen.findAllByRole('button', { name: 'Close' })).toHaveLength(2)@@ -82,7 +97,7 @@ describe('GenerateRecoveryCodesModal', () => {fireEvent.click(await screen.findByRole('button', { name: 'Generate recovery codes' }))// Codes are generatedawait screen.findByText('Save your recovery codes')- await screen.findByText('code_0')- await screen.findByText('code_9')+ await screen.findByText('WTO2-4T5X-BEUL-VJMI')+ await screen.findByText('KYYF-SP3E-QJYD-J53T')})})diff --git a/apps/studio/components/interfaces/Account/TOTPFactors/RecoveryCodesModal.tsx b/apps/studio/components/interfaces/Account/TOTPFactors/RecoveryCodesModal.tsxindex 4efec68cf56..c16cb52bfa3 100644--- a/apps/studio/components/interfaces/Account/TOTPFactors/RecoveryCodesModal.tsx+++ b/apps/studio/components/interfaces/Account/TOTPFactors/RecoveryCodesModal.tsx@@ -14,6 +14,7 @@ import {DialogTitle,} from 'ui'+import { formatRecoveryCode } from './RecoveryCodesModal.utils'import { recoveryCodeKeys } from '@/data/recovery-codes/keys'interface RecoveryCodesModalProps<T>@@ -82,10 +83,13 @@ export const RecoveryCodesModal = <T = unknown,>({<Buttonvariant="primary"onClick={() =>- copyToClipboard(mutation.data?.codes.join('\n') ?? '', () => {- setCopiedToClipboard(true)- setCopied(true)- })+ copyToClipboard(+ mutation.data?.codes.map((code) => formatRecoveryCode(code)).join('\n') ?? '',+ () => {+ setCopiedToClipboard(true)+ setCopied(true)+ }+ )}>Copy to clipboard@@ -139,7 +143,7 @@ const GenerateRecoveryCodesModalContent = ({<pre className="relative bg-muted rounded-md py-2 px-4"><code className="flex gap-2 flex-wrap justify-between">{codes?.map((code) => (- <span key={code}>{code}</span>+ <span key={code}>{formatRecoveryCode(code)}</span>))}</code></pre>diff --git a/apps/studio/components/interfaces/Account/TOTPFactors/RecoveryCodesModal.utils.test.ts b/apps/studio/components/interfaces/Account/TOTPFactors/RecoveryCodesModal.utils.test.tsnew file mode 100644index 00000000000..1ffe070ebd2--- /dev/null+++ b/apps/studio/components/interfaces/Account/TOTPFactors/RecoveryCodesModal.utils.test.ts@@ -0,0 +1,12 @@+import { describe, expect, test } from 'vitest'++import { formatRecoveryCode } from './RecoveryCodesModal.utils'++describe('formatRecoveryCode', () => {+ test('transforms a backend code to a human readable format', () => {+ expect(formatRecoveryCode('p6pcl32vl6q6l6nt')).toEqual('P6PC-L32V-L6Q6-L6NT')+ })+ test('returns the code unchanged if it does not match the expected format', () => {+ expect(formatRecoveryCode('bazinga')).toEqual('bazinga')+ })+})diff --git a/apps/studio/components/interfaces/Account/TOTPFactors/RecoveryCodesModal.utils.ts b/apps/studio/components/interfaces/Account/TOTPFactors/RecoveryCodesModal.utils.tsnew file mode 100644index 00000000000..3f4e499ac69--- /dev/null+++ b/apps/studio/components/interfaces/Account/TOTPFactors/RecoveryCodesModal.utils.ts@@ -0,0 +1,11 @@+// Codes are 4 groups of alphanumeric characters+const CodeRegex = new RegExp('([a-zA-Z0-9]{4})([a-zA-Z0-9]{4})([a-zA-Z0-9]{4})([a-zA-Z0-9]{4})')++/*+ * Format recovery code to make it easier to read by humans by separating the 4 groups of alphanumeric characters by a+ * dash and transforming them to uppercase+ */+export const formatRecoveryCode = (code: string) => {+ if (code.length !== 16) return code+ return code.toUpperCase().replace(CodeRegex, '$1-$2-$3-$4')+}diff --git a/apps/studio/components/interfaces/Account/TOTPFactors/RegenerateRecoveryCodesModal.test.tsx b/apps/studio/components/interfaces/Account/TOTPFactors/RegenerateRecoveryCodesModal.test.tsxindex bc12a9f8181..85836832e08 100644--- a/apps/studio/components/interfaces/Account/TOTPFactors/RegenerateRecoveryCodesModal.test.tsx+++ b/apps/studio/components/interfaces/Account/TOTPFactors/RegenerateRecoveryCodesModal.test.tsx@@ -3,6 +3,7 @@ import { fireEvent, screen, waitFor, within } from '@testing-library/react'import userEvent from '@testing-library/user-event'import { describe, expect, test, vi } from 'vitest'+import { formatRecoveryCode } from './RecoveryCodesModal.utils'import { RegenerateRecoveryCodesModal } from './RegenerateRecoveryCodesModal'import { auth } from '@/lib/gotrue'import { customRender } from '@/tests/lib/custom-render'@@ -17,7 +18,18 @@ vi.mock('ui', async (importOriginal) => ({copyToClipboard: mockCopyToClipboard,}))-const codes = Array.from(Array(10).keys()).map((i) => `code_${i}`)+const codes = [+ 'wto24t5xbeulvjmi',+ 'ade6in2sufndbbwd',+ 'oge3npsrhrr66k25',+ 'pxbkfvxlbi6ggvnf',+ 'kc3oaioqdjn4htc3',+ '363gseoknplambiy',+ '3urgae2p2jegem4m',+ 'tbxog2guayp6uvud',+ 'rpcvcf4owbclxrfp',+ 'kyyfsp3eqjydj53t',+]describe('RegenerateRecoveryCodesModal', () => {test('regenerate the recovery codes after confirmation and allow users to copy them', async () => {@@ -46,8 +58,8 @@ describe('RegenerateRecoveryCodesModal', () => {// Codes are generatedawait screen.findByText('Save your recovery codes')- await screen.findByText('code_0')- await screen.findByText('code_9')+ await screen.findByText('WTO2-4T5X-BEUL-VJMI')+ await screen.findByText('KYYF-SP3E-QJYD-J53T')// Users have to copy the codes to close the modal, next click should fail if they managed to close itexpect(await screen.findAllByRole('button', { name: 'Close' })).toHaveLength(1)@@ -56,7 +68,10 @@ describe('RegenerateRecoveryCodesModal', () => {await waitFor(() =>expect(screen.getByRole('checkbox', { name: 'I have copied the codes' })).toBeChecked())- expect(mockCopyToClipboard).toHaveBeenCalledWith(codes.join('\n'), expect.any(Function))+ expect(mockCopyToClipboard).toHaveBeenCalledWith(+ codes.map((code) => formatRecoveryCode(code)).join('\n'),+ expect.any(Function)+ )// We should have 2 close buttons (header icon and a standard button)expect(await screen.findAllByRole('button', { name: 'Close' })).toHaveLength(2)@@ -115,7 +130,7 @@ describe('RegenerateRecoveryCodesModal', () => {)// Codes are generatedawait screen.findByText('Save your recovery codes')- await screen.findByText('code_0')- await screen.findByText('code_9')+ await screen.findByText('WTO2-4T5X-BEUL-VJMI')+ await screen.findByText('KYYF-SP3E-QJYD-J53T')})})diff --git a/apps/studio/components/interfaces/Account/TOTPFactors/index.tsx b/apps/studio/components/interfaces/Account/TOTPFactors/index.tsxindex e328a2824e5..5bf6bba9cb1 100644--- a/apps/studio/components/interfaces/Account/TOTPFactors/index.tsx+++ b/apps/studio/components/interfaces/Account/TOTPFactors/index.tsx@@ -34,7 +34,7 @@ export const TOTPFactors = () => {const totpFactors = data?.totp ?? []const canAddApp = isSuccess && totpFactors.length < 2const shouldShowLockoutWarning = isSuccess && totpFactors.length === 1- const shouldVerifyRecoveryCodes = enableAuthRecoveryCodes && totpFactors.length === 1+ const shouldVerifyRecoveryCodes = enableAuthRecoveryCodesconst { data: recoveryCodesStatus } = useRecoveryCodesStatusQuery({enabled: shouldVerifyRecoveryCodes,