Skip to content

Commit

Permalink
feat: stricter type for first factor/mfa requirement
Browse files Browse the repository at this point in the history
  • Loading branch information
porcellus committed Oct 13, 2023
1 parent 5806fa1 commit 107bed5
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 47 deletions.
39 changes: 14 additions & 25 deletions lib/build/recipe/multifactorauth/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,18 @@ import OverrideableBuilder from "supertokens-js-override";
import { GeneralErrorResponse, User } from "../../types";
import { SessionContainer } from "../session";
import { SessionContainerInterface } from "../session/types";
export declare type MFARequirement = string;
export declare type FirstFactor =
| "emailpassword"
| "thirdparty"
| "otp-email"
| "otp-phone"
| "link-email"
| "link-phone"
| {
type: "custom";
id: string;
};
export declare type MFARequirement = FirstFactor | "totp";
export declare type MFARequirementList = (
| {
oneOf: MFARequirement[];
Expand All @@ -19,18 +30,7 @@ export declare type MFAClaimValue = {
n: string[];
};
export declare type TypeInput = {
firstFactors?: (
| "emailpassword"
| "thirdparty"
| "otp-email"
| "otp-phone"
| "link-email"
| "link-phone"
| {
type: "custom";
id: string;
}
)[];
firstFactors?: FirstFactor[];
override?: {
functions?: (
originalImplementation: RecipeInterface,
Expand All @@ -40,18 +40,7 @@ export declare type TypeInput = {
};
};
export declare type TypeNormalisedInput = {
firstFactors?: (
| "emailpassword"
| "thirdparty"
| "otp-email"
| "otp-phone"
| "link-email"
| "link-phone"
| {
type: "custom";
id: string;
}
)[];
firstFactors?: FirstFactor[];
override: {
functions: (
originalImplementation: RecipeInterface,
Expand Down
31 changes: 12 additions & 19 deletions lib/ts/recipe/multifactorauth/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,16 @@ import { GeneralErrorResponse, User } from "../../types";
import { SessionContainer } from "../session";
import { SessionContainerInterface } from "../session/types";

export type MFARequirement = string;
export type FirstFactor =
| "emailpassword"
| "thirdparty"
| "otp-email"
| "otp-phone"
| "link-email"
| "link-phone"
| { type: "custom"; id: string };

export type MFARequirement = FirstFactor | "totp";

export type MFARequirementList = (
| {
Expand All @@ -37,15 +46,7 @@ export type MFAClaimValue = {
};

export type TypeInput = {
firstFactors?: (
| "emailpassword"
| "thirdparty"
| "otp-email"
| "otp-phone"
| "link-email"
| "link-phone"
| { type: "custom"; id: string }
)[];
firstFactors?: FirstFactor[];

override?: {
functions?: (
Expand All @@ -57,15 +58,7 @@ export type TypeInput = {
};

export type TypeNormalisedInput = {
firstFactors?: (
| "emailpassword"
| "thirdparty"
| "otp-email"
| "otp-phone"
| "link-email"
| "link-phone"
| { type: "custom"; id: string }
)[];
firstFactors?: FirstFactor[];

override: {
functions: (
Expand Down
14 changes: 11 additions & 3 deletions test/with-typescript/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1871,14 +1871,22 @@ Supertokens.init({
...oI,

getMFARequirementsForAuth: ({ completedFactors }) => {
const factors = ["otp-email", "totp", "biometric"];
const completedFromList = factors.filter((id) => completedFactors[id] !== undefined);
const factors = ["otp-email", "totp", { type: "custom", id: "biometric" }] as const;
const completedFromList = factors.filter(
(fact) => completedFactors[typeof fact === "string" ? fact : fact.id] !== undefined
);
if (completedFromList.length >= 2) {
// We have completed two factors
return [];
}
// Otherwise the next step is completing something from the rest of the list
return [{ oneOf: factors.filter((id) => completedFactors[id] === undefined) }];
return [
{
oneOf: factors.filter(
(fact) => completedFactors[typeof fact === "string" ? fact : fact.id] === undefined
),
},
];
},
}),
},
Expand Down

0 comments on commit 107bed5

Please sign in to comment.