Skip to content

Commit

Permalink
Merge branch 'feat/mfa/tests' into feat/mfa/totp
Browse files Browse the repository at this point in the history
  • Loading branch information
porcellus committed Nov 21, 2023
2 parents 8f35657 + 3082073 commit 405c419
Show file tree
Hide file tree
Showing 14 changed files with 3,729 additions and 179 deletions.
59 changes: 46 additions & 13 deletions examples/for-tests/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,11 +172,38 @@ const formFields = [

const testContext = getTestContext();

let totpDevices = [];
let storedTOTPDevices = window.localStorage.getItem("totpDevices");
let totpDevices = storedTOTPDevices ? JSON.parse(storedTOTPDevices) : [];

function removeTOTPDevice(deviceName) {
const origLength = totpDevices.length;
totpDevices = totpDevices.filter((d) => d.deviceName !== deviceName);
window.localStorage.setItem("totpDevices", JSON.stringify(totpDevices));
return totpDevices.length !== origLength;
}

function addTOTPDevice(deviceName) {
totpDevices.push({
deviceName,
verified: false,
});
window.localStorage.setItem("totpDevices", JSON.stringify(totpDevices));
}

function verifyTOTPDevice(deviceName) {
totpDevices = totpDevices.filter((d) => d.deviceName !== deviceName);
totpDevices.push({
deviceName,
verified: true,
});
window.localStorage.setItem("totpDevices", JSON.stringify(totpDevices));
}
let tryCount = 0;
setInterval(() => (tryCount = tryCount > 0 ? tryCount - 1 : 0), 30);

setInterval(() => (tryCount = tryCount > 0 ? tryCount - 1 : 0), 30000);
window.resetTOTP = () => {
totpDevices = [];
window.localStorage.setItem("totpDevices", JSON.stringify(totpDevices));
tryCount = 0;
};
let recipeList = [
Expand All @@ -186,16 +213,11 @@ let recipeList = [
...oI,
listDevices: async () => ({ devices: totpDevices, status: "OK" }),
removeDevice: async ({ deviceName }) => {
const origLength = totpDevices.length;
totpDevices = totpDevices.filter((d) => d.deviceName !== deviceName);
return { status: "OK", didDeviceExist: origLength !== totpDevices.length };
return { status: "OK", didDeviceExist: removeTOTPDevice(deviceName) };
},
createDevice: async ({ deviceName }) => {
deviceName = deviceName ?? `totp-${Date.now()}`;
totpDevices.push({
deviceName,
verified: false,
});
addTOTPDevice(deviceName);
return {
status: "OK",
deviceName: deviceName,
Expand All @@ -207,6 +229,11 @@ let recipeList = [
verifyCode: async ({ totp }) => {
const dev = totpDevices.find((d) => d.deviceName.endsWith(totp) && d.verified);
if (dev) {
await fetch("http://localhost:8082/completeFactor", {
method: "POST",
body: JSON.stringify({ id: "totp" }),
headers: new Headers([["Content-Type", "application/json"]]),
});
return { status: "OK" };
}

Expand All @@ -222,10 +249,10 @@ let recipeList = [
}
if (deviceName.endsWith(totp)) {
const wasAlreadyVerified = dev.verified;
dev.verified = true;
await fetch("http://localhost:8082/mergeIntoAccessTokenPayload", {
verifyTOTPDevice(deviceName);
await fetch("http://localhost:8082/completeFactor", {
method: "POST",
body: JSON.stringify({ hasTOTP: true }),
body: JSON.stringify({ id: "totp" }),
headers: new Headers([["Content-Type", "application/json"]]),
});
return { status: "OK", wasAlreadyVerified };
Expand Down Expand Up @@ -551,7 +578,13 @@ export function DashboardHelper({ redirectOnLogout, ...props } = {}) {
</div>
<div className="session-context-userId">session context userID: {sessionContext.userId}</div>
<pre className="invalidClaims">{JSON.stringify(sessionContext.invalidClaims, undefined, 2)}</pre>
<a onClick={() => MultiFactorAuth.redirectToFactorChooser(true, props.history)}>MFA chooser</a>
<a
className="goToFactorChooser"
onClick={() => {
return MultiFactorAuth.redirectToFactorChooser(true, props.history);
}}>
MFA chooser
</a>
</div>
);
}
Expand Down
36 changes: 23 additions & 13 deletions examples/for-tests/src/testContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,16 @@ export function getTestContext() {
thirdPartyRedirectURL: localStorage.getItem("thirdPartyRedirectURL"),
authRecipe: window.localStorage.getItem("authRecipe") || "emailpassword",
usesDynamicLoginMethods: localStorage.getItem("usesDynamicLoginMethods") === "true",
enableAllRecipes: localStorage.getItem("enableAllRecipes") === "true",
clientRecipeListForDynamicLogin: localStorage.getItem("clientRecipeListForDynamicLogin"),
mockLoginMethodsForDynamicLogin: localStorage.getItem("mockLoginMethodsForDynamicLogin"),
staticProviderList: localStorage.getItem("staticProviderList"),
mockTenantId: localStorage.getItem("mockTenantId"),
clientType: localStorage.getItem("clientType") || undefined,
firstFactors: localStorage.getItem("firstFactors")?.split(", "),
firstFactors:
localStorage.getItem("firstFactors") !== null
? localStorage.getItem("firstFactors").split(", ")
: undefined,
};
return ret;
}
Expand All @@ -24,18 +28,24 @@ export function getEnabledRecipes() {

let enabledRecipes = [];

if (testContext.usesDynamicLoginMethods) {
if (testContext.clientRecipeListForDynamicLogin) {
enabledRecipes = JSON.parse(testContext.clientRecipeListForDynamicLogin);
} else {
enabledRecipes = [
"emailpassword",
"thirdparty",
"thirdpartyemailpassword",
"passwordless",
"thirdpartypasswordless",
];
}
if (testContext.enableAllRecipes) {
enabledRecipes = [
"emailpassword",
"thirdparty",
"thirdpartyemailpassword",
"passwordless",
"thirdpartypasswordless",
];
} else if (testContext.clientRecipeListForDynamicLogin) {
enabledRecipes = JSON.parse(testContext.clientRecipeListForDynamicLogin);
} else if (testContext.usesDynamicLoginMethods) {
enabledRecipes = [
"emailpassword",
"thirdparty",
"thirdpartyemailpassword",
"passwordless",
"thirdpartypasswordless",
];
} else {
if (testContext.authRecipe === "both") {
enabledRecipes.push("emailpassword", "thirdparty");
Expand Down
3 changes: 3 additions & 0 deletions test/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ export const RESET_PASSWORD_API = `${TEST_APPLICATION_SERVER_BASE_URL}/auth/user
export const SEND_VERIFY_EMAIL_API = `${TEST_APPLICATION_SERVER_BASE_URL}/auth/user/email/verify/token`;
export const VERIFY_EMAIL_API = `${TEST_APPLICATION_SERVER_BASE_URL}/auth/user/email/verify`;
export const SIGN_IN_UP_API = `${TEST_APPLICATION_SERVER_BASE_URL}/auth/signinup`;
export const CREATE_CODE_API = `${TEST_APPLICATION_SERVER_BASE_URL}/auth/signinup/code`;
export const CREATE_DEVICE_API = `${TEST_APPLICATION_SERVER_BASE_URL}/auth/signinup/code`;
export const GET_AUTH_URL_API = `${TEST_APPLICATION_SERVER_BASE_URL}/auth/authorisationurl`;
export const LOGIN_METHODS_API = `${TEST_APPLICATION_SERVER_BASE_URL}/auth/loginmethods`;
export const ST_ROOT_SELECTOR = `#${ST_ROOT_ID}`;

export const SOMETHING_WENT_WRONG_ERROR = "Something went wrong. Please try again.";
Loading

0 comments on commit 405c419

Please sign in to comment.