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

Commit

Permalink
fix: password policy conversion in config (#113)
Browse files Browse the repository at this point in the history
* fix: passpolicy conversion in config

* chore: make unit test

* fix: type error

* fix: add wrong field to test

* fix: filter

Co-authored-by: KaWaite <flippindaisy@gmail.com>
  • Loading branch information
KaWaite and KaWaite committed Oct 28, 2021
1 parent 22e0acd commit 5d57c4f
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 10 deletions.
19 changes: 19 additions & 0 deletions src/config.test.ts
@@ -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}))",
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
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 },
);
}

0 comments on commit 5d57c4f

Please sign in to comment.