From 46faeb6f59b19c963fb137c858347525b1cd9e19 Mon Sep 17 00:00:00 2001 From: panteliselef Date: Thu, 14 Nov 2024 05:34:36 -0500 Subject: [PATCH] chore(types,shared): Update reverification config values to snake_case (#4556) --- .changeset/mean-foxes-protect.md | 14 +++++++++ .../src/app/(reverification)/actions.ts | 5 ++-- .../core/resources/__tests__/Session.test.ts | 30 +++++++++---------- .../useUserVerificationSession.tsx | 8 +++-- .../react/src/hooks/__tests__/useAuth.test.ts | 10 +++---- packages/shared/src/authorization.ts | 24 ++++++++------- packages/types/src/sessionVerification.ts | 4 +-- 7 files changed, 58 insertions(+), 37 deletions(-) create mode 100644 .changeset/mean-foxes-protect.md diff --git a/.changeset/mean-foxes-protect.md b/.changeset/mean-foxes-protect.md new file mode 100644 index 0000000000..a5ccb5a4fd --- /dev/null +++ b/.changeset/mean-foxes-protect.md @@ -0,0 +1,14 @@ +--- +'@clerk/shared': minor +'@clerk/types': minor +--- + +Update reverification config values to snake_case. + +For `__experimental_ReverificationConfig` +- `strictMfa` changes to `strict_mfa` + +For `__experimental_SessionVerificationLevel` +- `firstFactor` changes to `first_factor` +- - `secondFactor` changes to `second_factor` +- - `multiFactor` changes to `multi_factor` diff --git a/integration/templates/next-app-router/src/app/(reverification)/actions.ts b/integration/templates/next-app-router/src/app/(reverification)/actions.ts index a28f785e2e..89c8378894 100644 --- a/integration/templates/next-app-router/src/app/(reverification)/actions.ts +++ b/integration/templates/next-app-router/src/app/(reverification)/actions.ts @@ -2,14 +2,15 @@ import { auth } from '@clerk/nextjs/server'; import { __experimental_reverificationMismatch as reverificationMismatch } from '@clerk/shared/authorization-errors'; +import { __experimental_ReverificationConfig } from '@clerk/types'; const logUserIdActionReverification = async () => { const { userId, has } = await auth.protect(); const config = { - level: 'secondFactor', + level: 'second_factor', afterMinutes: 1, - } as const; + } satisfies __experimental_ReverificationConfig; const userNeedsReverification = !has({ __experimental_reverification: config, diff --git a/packages/clerk-js/src/core/resources/__tests__/Session.test.ts b/packages/clerk-js/src/core/resources/__tests__/Session.test.ts index da51f952a8..0eaf4626f2 100644 --- a/packages/clerk-js/src/core/resources/__tests__/Session.test.ts +++ b/packages/clerk-js/src/core/resources/__tests__/Session.test.ts @@ -312,7 +312,7 @@ describe('Session', () => { const isAuthorized = session.checkAuthorization({ permission: 'org:sys_profile:delete', __experimental_reverification: { - level: 'multiFactor', + level: 'multi_factor', afterMinutes: 10, }, }); @@ -339,7 +339,7 @@ describe('Session', () => { const isAuthorized = session.checkAuthorization({ permission: 'org:sys_profile:delete', __experimental_reverification: { - level: 'multiFactor', + level: 'multi_factor', afterMinutes: 10, }, }); @@ -363,7 +363,7 @@ describe('Session', () => { const isAuthorized = session.checkAuthorization({ __experimental_reverification: { - level: 'multiFactor', + level: 'multi_factor', afterMinutes: 10, }, }); @@ -387,7 +387,7 @@ describe('Session', () => { const isAuthorized = session.checkAuthorization({ __experimental_reverification: { - level: 'multiFactor', + level: 'multi_factor', afterMinutes: 10, }, }); @@ -411,7 +411,7 @@ describe('Session', () => { const isAuthorized = session.checkAuthorization({ __experimental_reverification: { - level: 'multiFactor', + level: 'multi_factor', afterMinutes: 10, }, }); @@ -435,7 +435,7 @@ describe('Session', () => { const isAuthorized = session.checkAuthorization({ __experimental_reverification: { - level: 'multiFactor', + level: 'multi_factor', afterMinutes: 10, }, }); @@ -459,7 +459,7 @@ describe('Session', () => { const isAuthorized = session.checkAuthorization({ __experimental_reverification: { - level: 'multiFactor', + level: 'multi_factor', afterMinutes: 10, }, }); @@ -503,7 +503,7 @@ describe('Session', () => { } as SessionJSON); const isAuthorized = session.checkAuthorization({ - __experimental_reverification: 'strictMfa', + __experimental_reverification: 'strict_mfa', }); expect(isAuthorized).toBe(true); @@ -525,7 +525,7 @@ describe('Session', () => { const isAuthorized = session.checkAuthorization({ __experimental_reverification: { - level: 'multiFactor', + level: 'multi_factor', afterMinutes: 10, }, }); @@ -549,7 +549,7 @@ describe('Session', () => { const isAuthorized = session.checkAuthorization({ __experimental_reverification: { - level: 'multiFactor', + level: 'multi_factor', afterMinutes: 10, }, }); @@ -645,7 +645,7 @@ describe('Session', () => { const isAuthorized = session.checkAuthorization({ __experimental_reverification: { - level: 'firstFactor', + level: 'first_factor', afterMinutes: 10, }, }); @@ -786,7 +786,7 @@ describe('Session', () => { const isAuthorized = session.checkAuthorization({ __experimental_reverification: { - level: 'firstFactor', + level: 'first_factor', //@ts-expect-error afterMinutes: '10', }, @@ -814,7 +814,7 @@ describe('Session', () => { const isAuthorized = session.checkAuthorization({ __experimental_reverification: { - level: 'multiFactor', + level: 'multi_factor', afterMinutes: 10, }, }); @@ -822,7 +822,7 @@ describe('Session', () => { expect(isAuthorized).toBe(false); }); - it('first factor not enrolled should NOT be authorized for firstFactor assurance', async () => { + it('first factor not enrolled should NOT be authorized for first_factor assurance', async () => { const session = new Session({ status: 'active', id: 'session_1', @@ -838,7 +838,7 @@ describe('Session', () => { const isAuthorized = session.checkAuthorization({ __experimental_reverification: { - level: 'firstFactor', + level: 'first_factor', afterMinutes: 10, }, }); diff --git a/packages/clerk-js/src/ui/components/UserVerification/useUserVerificationSession.tsx b/packages/clerk-js/src/ui/components/UserVerification/useUserVerificationSession.tsx index 9ac6740d84..aa5fee307b 100644 --- a/packages/clerk-js/src/ui/components/UserVerification/useUserVerificationSession.tsx +++ b/packages/clerk-js/src/ui/components/UserVerification/useUserVerificationSession.tsx @@ -1,4 +1,5 @@ import { useSession } from '@clerk/shared/react'; +import type { __experimental_SessionVerificationLevel } from '@clerk/types'; import { useMemo } from 'react'; import { useUserVerification } from '../../contexts'; @@ -8,9 +9,10 @@ import { useFetch } from '../../hooks'; const useUserVerificationSessionKey = () => { const { level } = useUserVerification(); return useMemo( - () => ({ - level: level || 'secondFactor', - }), + () => + ({ + level: level || 'second_factor', + }) satisfies { level: __experimental_SessionVerificationLevel }, [level], ); }; diff --git a/packages/react/src/hooks/__tests__/useAuth.test.ts b/packages/react/src/hooks/__tests__/useAuth.test.ts index d9c9bdfc4a..4fae6bf32b 100644 --- a/packages/react/src/hooks/__tests__/useAuth.test.ts +++ b/packages/react/src/hooks/__tests__/useAuth.test.ts @@ -29,7 +29,7 @@ describe('useAuth type tests', () => { expectTypeOf({ role: 'org:admin', __experimental_reverification: { - level: 'firstFactor', + level: 'first_factor', afterMinutes: 10, }, } as const).toMatchTypeOf(); @@ -39,7 +39,7 @@ describe('useAuth type tests', () => { expectTypeOf({ permission: 'org:edit:posts', __experimental_reverification: { - level: 'firstFactor', + level: 'first_factor', afterMinutes: 10, }, } as const).toMatchTypeOf(); @@ -48,7 +48,7 @@ describe('useAuth type tests', () => { it('has({reverification: {level, maxAge}}) is allowed', () => { expectTypeOf({ __experimental_reverification: { - level: 'firstFactor', + level: 'first_factor', afterMinutes: 10, }, } as const).toMatchTypeOf(); @@ -57,7 +57,7 @@ describe('useAuth type tests', () => { it('reverification with other values as maxAge should throw', () => { expectTypeOf({ __experimental_reverification: { - level: 'firstFactor', + level: 'first_factor', afterMinutes: '10', }, } as const).not.toMatchTypeOf(); @@ -65,7 +65,7 @@ describe('useAuth type tests', () => { it('veryStrict reverification is allowed', () => { expectTypeOf({ - __experimental_reverification: 'strictMfa', + __experimental_reverification: 'strict_mfa', } as const).toMatchTypeOf(); }); diff --git a/packages/shared/src/authorization.ts b/packages/shared/src/authorization.ts index 654c0bdce3..888e73c6ae 100644 --- a/packages/shared/src/authorization.ts +++ b/packages/shared/src/authorization.ts @@ -32,27 +32,31 @@ type CheckStepUpAuthorization = ( ) => boolean | null; const TYPES_TO_OBJECTS: TypesToConfig = { - strictMfa: { + strict_mfa: { afterMinutes: 10, - level: 'multiFactor', + level: 'multi_factor', }, strict: { afterMinutes: 10, - level: 'secondFactor', + level: 'second_factor', }, moderate: { afterMinutes: 60, - level: 'secondFactor', + level: 'second_factor', }, lax: { afterMinutes: 1_440, - level: 'secondFactor', + level: 'second_factor', }, }; -const ALLOWED_LEVELS = new Set<__experimental_SessionVerificationLevel>(['firstFactor', 'secondFactor', 'multiFactor']); +const ALLOWED_LEVELS = new Set<__experimental_SessionVerificationLevel>([ + 'first_factor', + 'second_factor', + 'multi_factor', +]); -const ALLOWED_TYPES = new Set<__experimental_SessionVerificationTypes>(['strictMfa', 'strict', 'moderate', 'lax']); +const ALLOWED_TYPES = new Set<__experimental_SessionVerificationTypes>(['strict_mfa', 'strict', 'moderate', 'lax']); // Helper functions const isValidMaxAge = (maxAge: any) => typeof maxAge === 'number' && maxAge > 0; @@ -126,11 +130,11 @@ const checkStepUpAuthorization: CheckStepUpAuthorization = (params, { __experime const isValidFactor2 = factor2Age !== -1 ? afterMinutes > factor2Age : null; switch (level) { - case 'firstFactor': + case 'first_factor': return isValidFactor1; - case 'secondFactor': + case 'second_factor': return factor2Age !== -1 ? isValidFactor2 : isValidFactor1; - case 'multiFactor': + case 'multi_factor': return factor2Age === -1 ? isValidFactor1 : isValidFactor1 && isValidFactor2; } }; diff --git a/packages/types/src/sessionVerification.ts b/packages/types/src/sessionVerification.ts index 1ee1a6a63b..71a6b68fb7 100644 --- a/packages/types/src/sessionVerification.ts +++ b/packages/types/src/sessionVerification.ts @@ -15,7 +15,7 @@ export interface __experimental_SessionVerificationResource extends ClerkResourc export type __experimental_SessionVerificationStatus = 'needs_first_factor' | 'needs_second_factor' | 'complete'; -export type __experimental_SessionVerificationTypes = 'strictMfa' | 'strict' | 'moderate' | 'lax'; +export type __experimental_SessionVerificationTypes = 'strict_mfa' | 'strict' | 'moderate' | 'lax'; export type __experimental_ReverificationConfig = | __experimental_SessionVerificationTypes @@ -24,7 +24,7 @@ export type __experimental_ReverificationConfig = afterMinutes: __experimental_SessionVerificationAfterMinutes; }; -export type __experimental_SessionVerificationLevel = 'firstFactor' | 'secondFactor' | 'multiFactor'; +export type __experimental_SessionVerificationLevel = 'first_factor' | 'second_factor' | 'multi_factor'; export type __experimental_SessionVerificationAfterMinutes = number; export type __experimental_SessionVerificationFirstFactor = EmailCodeFactor | PhoneCodeFactor | PasswordFactor;