Skip to content
This repository has been archived by the owner on Apr 25, 2023. It is now read-only.

fix: password policy conversion in config #113

Merged
merged 5 commits into from
Oct 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/config.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { convertPasswordPolicy } from "./config";

test("convert password policy to regular expression", () => {
const passwordPolicy = {
whiteSpace: "(?=.*\\s)",
highSecurity: "^(?=.*[a-z])(?=.*[A-Z])((?=(.*\\d){2}))",
KaWaite marked this conversation as resolved.
Show resolved Hide resolved
wrong: "@[",
};
const actual = convertPasswordPolicy(passwordPolicy);
expect(actual).toStrictEqual({
whiteSpace: /(?=.*\s)/,
highSecurity: /^(?=.*[a-z])(?=.*[A-Z])((?=(.*\d){2}))/,
});
});

test("return undefined if no password policy", () => {
const actual = convertPasswordPolicy(undefined);
expect(actual).toBeUndefined();
});
31 changes: 21 additions & 10 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,24 @@ export const defaultConfig: Config = {
published: window.origin + "/p/{}/",
};

export function convertPasswordPolicy(passwordPolicy?: {
[key: string]: string;
}): { [key: string]: RegExp | undefined } | undefined {
if (!passwordPolicy) return;
return Object.fromEntries(
Object.entries(passwordPolicy)
.map(([k, v]) => {
if (typeof v !== "string") return [k, undefined];
try {
return [k, new RegExp(v)];
} catch {
return [k, undefined];
}
})
.filter(i => !!i[1]),
);
}

export default async function loadConfig() {
if (window.REEARTH_CONFIG) return;
window.REEARTH_CONFIG = defaultConfig;
Expand All @@ -41,14 +59,7 @@ export default async function loadConfig() {

if (!window.REEARTH_CONFIG?.passwordPolicy) return;

window.REEARTH_CONFIG.passwordPolicy = Object.entries(
Object.values(window.REEARTH_CONFIG.passwordPolicy).map((k, v) => {
if (typeof v !== "string") return undefined;
try {
return [k, new RegExp(v)];
} catch {
return undefined;
}
}),
) as Config["passwordPolicy"];
window.REEARTH_CONFIG.passwordPolicy = convertPasswordPolicy(
window.REEARTH_CONFIG.passwordPolicy as { [key: string]: string },
);
}