Skip to content

Commit

Permalink
chore(types,shared): Update reverification config values to snake_case (
Browse files Browse the repository at this point in the history
  • Loading branch information
panteliselef authored Nov 14, 2024
1 parent 3353935 commit 46faeb6
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 37 deletions.
14 changes: 14 additions & 0 deletions .changeset/mean-foxes-protect.md
Original file line number Diff line number Diff line change
@@ -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`
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
30 changes: 15 additions & 15 deletions packages/clerk-js/src/core/resources/__tests__/Session.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ describe('Session', () => {
const isAuthorized = session.checkAuthorization({
permission: 'org:sys_profile:delete',
__experimental_reverification: {
level: 'multiFactor',
level: 'multi_factor',
afterMinutes: 10,
},
});
Expand All @@ -339,7 +339,7 @@ describe('Session', () => {
const isAuthorized = session.checkAuthorization({
permission: 'org:sys_profile:delete',
__experimental_reverification: {
level: 'multiFactor',
level: 'multi_factor',
afterMinutes: 10,
},
});
Expand All @@ -363,7 +363,7 @@ describe('Session', () => {

const isAuthorized = session.checkAuthorization({
__experimental_reverification: {
level: 'multiFactor',
level: 'multi_factor',
afterMinutes: 10,
},
});
Expand All @@ -387,7 +387,7 @@ describe('Session', () => {

const isAuthorized = session.checkAuthorization({
__experimental_reverification: {
level: 'multiFactor',
level: 'multi_factor',
afterMinutes: 10,
},
});
Expand All @@ -411,7 +411,7 @@ describe('Session', () => {

const isAuthorized = session.checkAuthorization({
__experimental_reverification: {
level: 'multiFactor',
level: 'multi_factor',
afterMinutes: 10,
},
});
Expand All @@ -435,7 +435,7 @@ describe('Session', () => {

const isAuthorized = session.checkAuthorization({
__experimental_reverification: {
level: 'multiFactor',
level: 'multi_factor',
afterMinutes: 10,
},
});
Expand All @@ -459,7 +459,7 @@ describe('Session', () => {

const isAuthorized = session.checkAuthorization({
__experimental_reverification: {
level: 'multiFactor',
level: 'multi_factor',
afterMinutes: 10,
},
});
Expand Down Expand Up @@ -503,7 +503,7 @@ describe('Session', () => {
} as SessionJSON);

const isAuthorized = session.checkAuthorization({
__experimental_reverification: 'strictMfa',
__experimental_reverification: 'strict_mfa',
});

expect(isAuthorized).toBe(true);
Expand All @@ -525,7 +525,7 @@ describe('Session', () => {

const isAuthorized = session.checkAuthorization({
__experimental_reverification: {
level: 'multiFactor',
level: 'multi_factor',
afterMinutes: 10,
},
});
Expand All @@ -549,7 +549,7 @@ describe('Session', () => {

const isAuthorized = session.checkAuthorization({
__experimental_reverification: {
level: 'multiFactor',
level: 'multi_factor',
afterMinutes: 10,
},
});
Expand Down Expand Up @@ -645,7 +645,7 @@ describe('Session', () => {

const isAuthorized = session.checkAuthorization({
__experimental_reverification: {
level: 'firstFactor',
level: 'first_factor',
afterMinutes: 10,
},
});
Expand Down Expand Up @@ -786,7 +786,7 @@ describe('Session', () => {

const isAuthorized = session.checkAuthorization({
__experimental_reverification: {
level: 'firstFactor',
level: 'first_factor',
//@ts-expect-error
afterMinutes: '10',
},
Expand Down Expand Up @@ -814,15 +814,15 @@ describe('Session', () => {

const isAuthorized = session.checkAuthorization({
__experimental_reverification: {
level: 'multiFactor',
level: 'multi_factor',
afterMinutes: 10,
},
});

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',
Expand All @@ -838,7 +838,7 @@ describe('Session', () => {

const isAuthorized = session.checkAuthorization({
__experimental_reverification: {
level: 'firstFactor',
level: 'first_factor',
afterMinutes: 10,
},
});
Expand Down
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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],
);
};
Expand Down
10 changes: 5 additions & 5 deletions packages/react/src/hooks/__tests__/useAuth.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ describe('useAuth type tests', () => {
expectTypeOf({
role: 'org:admin',
__experimental_reverification: {
level: 'firstFactor',
level: 'first_factor',
afterMinutes: 10,
},
} as const).toMatchTypeOf<ParamsOfHas>();
Expand All @@ -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<ParamsOfHas>();
Expand All @@ -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<ParamsOfHas>();
Expand All @@ -57,15 +57,15 @@ 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<ParamsOfHas>();
});

it('veryStrict reverification is allowed', () => {
expectTypeOf({
__experimental_reverification: 'strictMfa',
__experimental_reverification: 'strict_mfa',
} as const).toMatchTypeOf<ParamsOfHas>();
});

Expand Down
24 changes: 14 additions & 10 deletions packages/shared/src/authorization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
};
Expand Down
4 changes: 2 additions & 2 deletions packages/types/src/sessionVerification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
Expand Down

0 comments on commit 46faeb6

Please sign in to comment.