@proconnect-gouv/proconnect.core 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +120 -0
- package/README.md +61 -0
- package/dist/data/index.cjs +282 -0
- package/dist/data/index.d.ts +7 -0
- package/dist/data/index.js +278 -0
- package/dist/most-used-free-email-domains-BiH62xhh.cjs +21 -0
- package/dist/most-used-free-email-domains-CtdkKumB.js +19 -0
- package/dist/security/index.cjs +8013 -0
- package/dist/security/index.d.ts +31 -0
- package/dist/security/index.js +7998 -0
- package/dist/services/email/index.cjs +19 -0
- package/dist/services/email/index.d.ts +12 -0
- package/dist/services/email/index.js +16 -0
- package/dist/services/oidc/index.cjs +10 -0
- package/dist/services/oidc/index.d.ts +19 -0
- package/dist/services/oidc/index.js +8 -0
- package/dist/services/suggestion/index.cjs +20 -0
- package/dist/services/suggestion/index.d.ts +3 -0
- package/dist/services/suggestion/index.js +18 -0
- package/package.json +92 -0
- package/src/data/diceware-wordlist-fr-alt.ts +7780 -0
- package/src/data/gouvfr-domains.ts +246 -0
- package/src/data/index.ts +3 -0
- package/src/data/most-used-free-email-domains.ts +18 -0
- package/src/data/other-gouv-domains.ts +41 -0
- package/src/security/generate-diceware-password.test.ts +26 -0
- package/src/security/generate-diceware-password.ts +25 -0
- package/src/security/generate-pin-token.test.ts +19 -0
- package/src/security/generate-pin-token.ts +13 -0
- package/src/security/generate-token.test.ts +14 -0
- package/src/security/generate-token.ts +9 -0
- package/src/security/get-trusted-referrer-path.test.ts +49 -0
- package/src/security/get-trusted-referrer-path.ts +34 -0
- package/src/security/hash-password.test.ts +16 -0
- package/src/security/hash-password.ts +10 -0
- package/src/security/index.ts +15 -0
- package/src/security/is-domain-valid.test.ts +28 -0
- package/src/security/is-domain-valid.ts +28 -0
- package/src/security/is-email-valid.test.ts +71 -0
- package/src/security/is-email-valid.ts +44 -0
- package/src/security/is-name-valid.test.ts +61 -0
- package/src/security/is-name-valid.ts +5 -0
- package/src/security/is-password-secure.test.ts +53 -0
- package/src/security/is-password-secure.ts +25 -0
- package/src/security/is-phone-number-valid.test.ts +44 -0
- package/src/security/is-phone-number-valid.ts +19 -0
- package/src/security/is-siret-valid.test.ts +36 -0
- package/src/security/is-siret-valid.ts +15 -0
- package/src/security/is-visible-string.test.ts +30 -0
- package/src/security/is-visible-string.ts +7 -0
- package/src/security/owasp-password-strength-tester.ts +149 -0
- package/src/security/validate-password.test.ts +27 -0
- package/src/security/validate-password.ts +16 -0
- package/src/services/email/get-email-domain.test.ts +30 -0
- package/src/services/email/get-email-domain.ts +20 -0
- package/src/services/email/index.ts +4 -0
- package/src/services/email/is-a-free-domain.test.ts +26 -0
- package/src/services/email/is-a-free-domain.ts +11 -0
- package/src/services/oidc/contains-essential-acrs.test.ts +63 -0
- package/src/services/oidc/contains-essential-acrs.ts +24 -0
- package/src/services/oidc/index.ts +2 -0
- package/src/services/oidc/prompt.ts +11 -0
- package/src/services/suggestion/did-you-mean.test.ts +29 -0
- package/src/services/suggestion/did-you-mean.ts +19 -0
- package/src/services/suggestion/index.ts +3 -0
- package/tsconfig.json +11 -0
- package/tsconfig.lib.json +9 -0
- package/types/index.d.ts +4 -0
- package/types/is-disposable-email-domain.d.ts +5 -0
- package/types/tld-extract.d.ts +12 -0
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
//
|
|
2
|
+
|
|
3
|
+
import { isEmpty, isString } from "lodash-es";
|
|
4
|
+
import { Buffer } from "node:buffer";
|
|
5
|
+
import { isDomainValid } from "./is-domain-valid.js";
|
|
6
|
+
|
|
7
|
+
//
|
|
8
|
+
|
|
9
|
+
export function isEmailValid(email: unknown): email is string {
|
|
10
|
+
if (!isString(email) || isEmpty(email)) {
|
|
11
|
+
return false;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const parts = email.split("@").filter((part) => part);
|
|
15
|
+
|
|
16
|
+
// The email address contains two parts, separated with an @ symbol.
|
|
17
|
+
// => these parts are non-empty strings
|
|
18
|
+
// => there are two and only two parts
|
|
19
|
+
if (parts.length !== 2) {
|
|
20
|
+
return false;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// The email address does not contain dangerous characters
|
|
24
|
+
// => the postgres connector is taking care of this
|
|
25
|
+
|
|
26
|
+
// The domain part contains only letters, numbers, hyphens (-) and periods (.)
|
|
27
|
+
const domain = parts[1];
|
|
28
|
+
if (!isDomainValid(domain)) {
|
|
29
|
+
return false;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// The local part (before the @) should be no more than 63 characters.
|
|
33
|
+
const localPart = parts[0];
|
|
34
|
+
if (Buffer.from(localPart).length > 63) {
|
|
35
|
+
return false;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// The total length should be no more than 254 characters.
|
|
39
|
+
if (Buffer.from(email).length > 254) {
|
|
40
|
+
return false;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
return true;
|
|
44
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
//
|
|
2
|
+
|
|
3
|
+
import assert from "node:assert/strict";
|
|
4
|
+
import { describe, it } from "node:test";
|
|
5
|
+
import { isNameValid } from "./is-name-valid.js";
|
|
6
|
+
|
|
7
|
+
//
|
|
8
|
+
|
|
9
|
+
describe("isNameValid", () => {
|
|
10
|
+
const invalidNames = [
|
|
11
|
+
"jean@domaine.fr",
|
|
12
|
+
"dsi_etudes_applications",
|
|
13
|
+
"R2 - Sebastien",
|
|
14
|
+
"0000",
|
|
15
|
+
"CCTV70",
|
|
16
|
+
"0623456789",
|
|
17
|
+
";GOUZE",
|
|
18
|
+
"Agathe/Carine",
|
|
19
|
+
`<script>alert("hello")</script>`,
|
|
20
|
+
"SG/PAFF/DDTM06",
|
|
21
|
+
"Jean*Robert",
|
|
22
|
+
"Jose_luis",
|
|
23
|
+
"MME.",
|
|
24
|
+
"Sabrina.b",
|
|
25
|
+
"M.Christine",
|
|
26
|
+
"Bousbecque59098*",
|
|
27
|
+
"vAL2RIE",
|
|
28
|
+
"Ch.",
|
|
29
|
+
"YOANNI TH.",
|
|
30
|
+
"M. le Président",
|
|
31
|
+
];
|
|
32
|
+
|
|
33
|
+
invalidNames.forEach((invalidName) => {
|
|
34
|
+
it(`should return false for invalid names: ${invalidName}`, () => {
|
|
35
|
+
assert.equal(isNameValid(invalidName), false);
|
|
36
|
+
});
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
const validNames = [
|
|
40
|
+
"Jean",
|
|
41
|
+
"Jean-Jean",
|
|
42
|
+
"TAREK WAJDI",
|
|
43
|
+
" Tania",
|
|
44
|
+
"Надежда",
|
|
45
|
+
"沃德天·",
|
|
46
|
+
"อาทิตย์ นาถมทอง",
|
|
47
|
+
"俊宇",
|
|
48
|
+
"Doğan",
|
|
49
|
+
"Hanåğğne",
|
|
50
|
+
"سليمان خالد",
|
|
51
|
+
"marcn bh",
|
|
52
|
+
"THỊ PHƯƠNG HỒNG",
|
|
53
|
+
"Yamina⁵",
|
|
54
|
+
];
|
|
55
|
+
|
|
56
|
+
validNames.forEach((validName) => {
|
|
57
|
+
it(`should return true for valid names: ${validName}`, () => {
|
|
58
|
+
assert.equal(isNameValid(validName), true);
|
|
59
|
+
});
|
|
60
|
+
});
|
|
61
|
+
});
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
//
|
|
2
|
+
|
|
3
|
+
// heavily inspired from https://github.com/nowsecure/owasp-password-strength-test/blob/1.3.0/test.js
|
|
4
|
+
|
|
5
|
+
import assert from "node:assert/strict";
|
|
6
|
+
import { describe, it } from "node:test";
|
|
7
|
+
import { isPasswordSecure } from "./is-password-secure.js";
|
|
8
|
+
|
|
9
|
+
//
|
|
10
|
+
|
|
11
|
+
describe("isPasswordSecure", () => {
|
|
12
|
+
it("should return false for too short password", () => {
|
|
13
|
+
assert.equal(
|
|
14
|
+
isPasswordSecure("password", "user1234567890@test.com"),
|
|
15
|
+
false,
|
|
16
|
+
);
|
|
17
|
+
});
|
|
18
|
+
it("should return false for password with only 2 types of character", () => {
|
|
19
|
+
assert.equal(
|
|
20
|
+
isPasswordSecure("mon mot de passe", "user1234567890@test.com"),
|
|
21
|
+
false,
|
|
22
|
+
);
|
|
23
|
+
});
|
|
24
|
+
it("should return true for long enough password with 4 types of character", () => {
|
|
25
|
+
assert.equal(
|
|
26
|
+
isPasswordSecure("Password123!", "user1234567890@test.com"),
|
|
27
|
+
true,
|
|
28
|
+
);
|
|
29
|
+
});
|
|
30
|
+
it("should return true for passphrase", () => {
|
|
31
|
+
assert.equal(
|
|
32
|
+
isPasswordSecure(
|
|
33
|
+
"agrafe correcte de la batterie du cheval",
|
|
34
|
+
"user1234567890@test.com",
|
|
35
|
+
),
|
|
36
|
+
true,
|
|
37
|
+
);
|
|
38
|
+
});
|
|
39
|
+
["Pro Connect forever", "MonComptePro-2023!"].forEach((blacklistedWord) => {
|
|
40
|
+
it("should not contains blacklisted word", () => {
|
|
41
|
+
assert.equal(
|
|
42
|
+
isPasswordSecure(blacklistedWord, "user1234567890@test.com"),
|
|
43
|
+
false,
|
|
44
|
+
);
|
|
45
|
+
});
|
|
46
|
+
});
|
|
47
|
+
it("should not contains users email address", () => {
|
|
48
|
+
assert.equal(
|
|
49
|
+
isPasswordSecure("User1234567890@test.com!", "user1234567890@test.com"),
|
|
50
|
+
false,
|
|
51
|
+
);
|
|
52
|
+
});
|
|
53
|
+
});
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
//
|
|
2
|
+
|
|
3
|
+
import { owaspPasswordStrengthTest } from "./owasp-password-strength-tester.js";
|
|
4
|
+
|
|
5
|
+
//
|
|
6
|
+
|
|
7
|
+
export function isPasswordSecure(plainPassword: string, email: string) {
|
|
8
|
+
const { strong } = owaspPasswordStrengthTest(plainPassword);
|
|
9
|
+
|
|
10
|
+
const lowerCasedBlacklistedWords = [
|
|
11
|
+
email.toLowerCase(),
|
|
12
|
+
"moncomptepro",
|
|
13
|
+
"mon compte pro",
|
|
14
|
+
"agentconnect",
|
|
15
|
+
"agent connect",
|
|
16
|
+
"proconnect",
|
|
17
|
+
"pro connect",
|
|
18
|
+
];
|
|
19
|
+
|
|
20
|
+
const containsBlacklistedWord = lowerCasedBlacklistedWords.some((word) =>
|
|
21
|
+
plainPassword.toLowerCase().includes(word),
|
|
22
|
+
);
|
|
23
|
+
|
|
24
|
+
return !containsBlacklistedWord && strong;
|
|
25
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
//
|
|
2
|
+
|
|
3
|
+
import assert from "node:assert/strict";
|
|
4
|
+
import { describe, it } from "node:test";
|
|
5
|
+
import { isPhoneNumberValid } from "./is-phone-number-valid.js";
|
|
6
|
+
|
|
7
|
+
//
|
|
8
|
+
|
|
9
|
+
describe("isPhoneNumberValid", () => {
|
|
10
|
+
[
|
|
11
|
+
undefined,
|
|
12
|
+
null,
|
|
13
|
+
0,
|
|
14
|
+
true,
|
|
15
|
+
"📞",
|
|
16
|
+
"FR",
|
|
17
|
+
"Jean Michel",
|
|
18
|
+
"$",
|
|
19
|
+
"&",
|
|
20
|
+
"(",
|
|
21
|
+
")",
|
|
22
|
+
"+33210",
|
|
23
|
+
].forEach((name) => {
|
|
24
|
+
it(`should return false for "${name}"`, () => {
|
|
25
|
+
assert.equal(isPhoneNumberValid(name), false);
|
|
26
|
+
});
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
it("should return true for '0123456789'", () => {
|
|
30
|
+
assert.equal(isPhoneNumberValid("0123456789"), true);
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
it("should return true for '0-1-2-3-4-5-6-7-8-9'", () => {
|
|
34
|
+
assert.equal(isPhoneNumberValid("0-1-2-3-4-5-6-7-8-9"), true);
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
it("should return true for '+00123456'", () => {
|
|
38
|
+
assert.equal(isPhoneNumberValid("+00123456"), true);
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it("should return true for '+33123456789'", () => {
|
|
42
|
+
assert.equal(isPhoneNumberValid("+33123456789"), true);
|
|
43
|
+
});
|
|
44
|
+
});
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
//
|
|
2
|
+
|
|
3
|
+
import { isEmpty, isString } from "lodash-es";
|
|
4
|
+
|
|
5
|
+
//
|
|
6
|
+
|
|
7
|
+
export function isPhoneNumberValid(
|
|
8
|
+
phoneNumber: unknown,
|
|
9
|
+
): phoneNumber is string {
|
|
10
|
+
if (!isString(phoneNumber) || isEmpty(phoneNumber)) {
|
|
11
|
+
return false;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
if (!phoneNumber.match(/^\+?(?:[0-9][ -]?){6,14}[0-9]$/)) {
|
|
15
|
+
return false;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
return true;
|
|
19
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
//
|
|
2
|
+
|
|
3
|
+
import assert from "node:assert/strict";
|
|
4
|
+
import { describe, it } from "node:test";
|
|
5
|
+
import { isSiretValid } from "./is-siret-valid.js";
|
|
6
|
+
|
|
7
|
+
//
|
|
8
|
+
|
|
9
|
+
describe("isSiretValid", () => {
|
|
10
|
+
it("should return false for undefined value", () => {
|
|
11
|
+
assert.equal(isSiretValid(undefined), false);
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
it("should return false for empty string", () => {
|
|
15
|
+
assert.equal(isSiretValid(""), false);
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
it("should return false if it contains characters other than number", () => {
|
|
19
|
+
assert.equal(isSiretValid("a2345678901234"), false);
|
|
20
|
+
});
|
|
21
|
+
it("should return false if it contains more that 14 numbers", () => {
|
|
22
|
+
assert.equal(isSiretValid("123456789012345"), false);
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
it("should return false if it contains less that 14 numbers", () => {
|
|
26
|
+
assert.equal(isSiretValid("1234567890123"), false);
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
it("should return true if it contains exactly 14 numbers", () => {
|
|
30
|
+
assert.equal(isSiretValid("12345678901234"), true);
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
it("should return true if it contains exactly 14 numbers with spaces", () => {
|
|
34
|
+
assert.equal(isSiretValid(" 123 456 789\n\r01234 \n"), true);
|
|
35
|
+
});
|
|
36
|
+
});
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
//
|
|
2
|
+
|
|
3
|
+
import { isEmpty, isString } from "lodash-es";
|
|
4
|
+
|
|
5
|
+
//
|
|
6
|
+
|
|
7
|
+
export function isSiretValid(siret: unknown): siret is string {
|
|
8
|
+
if (!isString(siret) || isEmpty(siret)) {
|
|
9
|
+
return false;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
const siretNoSpaces = siret.replace(/\s/g, "");
|
|
13
|
+
|
|
14
|
+
return /^\d{14}$/.test(siretNoSpaces);
|
|
15
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
//
|
|
2
|
+
|
|
3
|
+
import assert from "node:assert/strict";
|
|
4
|
+
import { describe, it } from "node:test";
|
|
5
|
+
import { isVisibleString } from "./is-visible-string.js";
|
|
6
|
+
|
|
7
|
+
describe("isVisibleString", () => {
|
|
8
|
+
const nonVisibleStrings = [
|
|
9
|
+
"",
|
|
10
|
+
"\n",
|
|
11
|
+
" ",
|
|
12
|
+
"", // zero width space character
|
|
13
|
+
"", // left to right mark character
|
|
14
|
+
" ",
|
|
15
|
+
];
|
|
16
|
+
|
|
17
|
+
nonVisibleStrings.forEach((nonVisibleString) => {
|
|
18
|
+
it(`should return false for non-visible string: "${nonVisibleString}"`, () => {
|
|
19
|
+
assert.equal(isVisibleString(nonVisibleString), false);
|
|
20
|
+
});
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
const visibleStrings = ["a", "a"];
|
|
24
|
+
|
|
25
|
+
visibleStrings.forEach((visibleString) => {
|
|
26
|
+
it(`should return true for visible string: "${visibleString}"`, () => {
|
|
27
|
+
assert.equal(isVisibleString(visibleString), true);
|
|
28
|
+
});
|
|
29
|
+
});
|
|
30
|
+
});
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
// heavily inspired from https://github.com/nowsecure/owasp-password-strength-test
|
|
2
|
+
|
|
3
|
+
type errorCode =
|
|
4
|
+
| "failedMinLength"
|
|
5
|
+
| "failedMaxLength"
|
|
6
|
+
| "failedThreeRepeatedChars"
|
|
7
|
+
| "optionalLowercaseRequired"
|
|
8
|
+
| "optionalUppercaseRequired"
|
|
9
|
+
| "optionalNumberRequired"
|
|
10
|
+
| "optionalSpecialCharRequired";
|
|
11
|
+
|
|
12
|
+
interface Result {
|
|
13
|
+
errors: errorCode[];
|
|
14
|
+
failedTests: errorCode[];
|
|
15
|
+
requiredTestErrors: errorCode[];
|
|
16
|
+
optionalTestErrors: errorCode[];
|
|
17
|
+
isPassphrase: boolean;
|
|
18
|
+
strong: boolean;
|
|
19
|
+
optionalTestsPassed: number;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
interface Config {
|
|
23
|
+
allowPassphrases: boolean;
|
|
24
|
+
maxLength: number;
|
|
25
|
+
minLength: number;
|
|
26
|
+
minPhraseLength: number;
|
|
27
|
+
minOptionalTestsToPass: number;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// This default config ensure a minimum entropy of 66.
|
|
31
|
+
// CNIL recommandation are 50 with access restriction (which is our case) or 80 without.
|
|
32
|
+
// More info at https://www.cnil.fr/fr/mots-de-passe-une-nouvelle-recommandation-pour-maitriser-sa-securite
|
|
33
|
+
const defaultConfig: Config = {
|
|
34
|
+
allowPassphrases: true,
|
|
35
|
+
maxLength: 128,
|
|
36
|
+
minLength: 10,
|
|
37
|
+
minPhraseLength: 20,
|
|
38
|
+
minOptionalTestsToPass: 4,
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
// This is an object containing the tests to run against all passwords.
|
|
42
|
+
const tests = {
|
|
43
|
+
// An array of required tests. A password *must* pass these tests in order
|
|
44
|
+
// to be considered strong.
|
|
45
|
+
required: [
|
|
46
|
+
// enforce a minimum length
|
|
47
|
+
(password: string, config: Config): errorCode | null =>
|
|
48
|
+
password.length < config.minLength ? "failedMinLength" : null,
|
|
49
|
+
|
|
50
|
+
// enforce a maximum length
|
|
51
|
+
(password: string, config: Config): errorCode | null =>
|
|
52
|
+
password.length > config.maxLength ? "failedMaxLength" : null,
|
|
53
|
+
|
|
54
|
+
// forbid repeating characters
|
|
55
|
+
(password: string, _config: Config): errorCode | null =>
|
|
56
|
+
/(.)\1{2,}/.test(password) ? "failedThreeRepeatedChars" : null,
|
|
57
|
+
],
|
|
58
|
+
|
|
59
|
+
// An array of optional tests. These tests are "optional" in two senses:
|
|
60
|
+
//
|
|
61
|
+
// 1. Passphrases (passwords whose length exceeds
|
|
62
|
+
// this.config.minPhraseLength) are not obligated to pass these tests
|
|
63
|
+
// provided that this.config.allowPassphrases is set to Boolean true
|
|
64
|
+
// (which it is by default).
|
|
65
|
+
//
|
|
66
|
+
// 2. A password need only to pass this.config.minOptionalTestsToPass
|
|
67
|
+
// number of these optional tests in order to be considered strong.
|
|
68
|
+
optional: [
|
|
69
|
+
// require at least one lowercase letter
|
|
70
|
+
(password: string, _config: Config): errorCode | null =>
|
|
71
|
+
!/[a-z]/.test(password) ? "optionalLowercaseRequired" : null,
|
|
72
|
+
|
|
73
|
+
// require at least one uppercase letter
|
|
74
|
+
(password: string, _config: Config): errorCode | null =>
|
|
75
|
+
!/[A-Z]/.test(password) ? "optionalUppercaseRequired" : null,
|
|
76
|
+
|
|
77
|
+
// require at least one number
|
|
78
|
+
(password: string, _config: Config): errorCode | null =>
|
|
79
|
+
!/[0-9]/.test(password) ? "optionalNumberRequired" : null,
|
|
80
|
+
|
|
81
|
+
// require at least one special character
|
|
82
|
+
(password: string, _config: Config): errorCode | null =>
|
|
83
|
+
!/[^A-Za-z0-9]/.test(password) ? "optionalSpecialCharRequired" : null,
|
|
84
|
+
],
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
// This method tests password strength
|
|
88
|
+
export const owaspPasswordStrengthTest = (
|
|
89
|
+
password: string,
|
|
90
|
+
options: Partial<Config> = {},
|
|
91
|
+
): Result => {
|
|
92
|
+
const config = { ...defaultConfig, ...options };
|
|
93
|
+
// create an object to store the test results
|
|
94
|
+
const result: Result = {
|
|
95
|
+
errors: [],
|
|
96
|
+
failedTests: [],
|
|
97
|
+
requiredTestErrors: [],
|
|
98
|
+
optionalTestErrors: [],
|
|
99
|
+
isPassphrase: false,
|
|
100
|
+
strong: true,
|
|
101
|
+
optionalTestsPassed: 0,
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
// Always submit the password/passphrase to the required tests
|
|
105
|
+
let i = 0;
|
|
106
|
+
tests.required.forEach(function (test) {
|
|
107
|
+
const err = test(password, config);
|
|
108
|
+
if (!!err) {
|
|
109
|
+
result.strong = false;
|
|
110
|
+
result.errors.push(err);
|
|
111
|
+
result.requiredTestErrors.push(err);
|
|
112
|
+
result.failedTests.push(err);
|
|
113
|
+
}
|
|
114
|
+
i++;
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
// If configured to allow passphrases, and if the password is of a
|
|
118
|
+
// sufficient length to consider it a passphrase, exempt it from the
|
|
119
|
+
// optional tests.
|
|
120
|
+
if (config.allowPassphrases && password.length >= config.minPhraseLength) {
|
|
121
|
+
result.isPassphrase = true;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
if (!result.isPassphrase) {
|
|
125
|
+
let j = tests.required.length;
|
|
126
|
+
tests.optional.forEach(function (test) {
|
|
127
|
+
const err = test(password, config);
|
|
128
|
+
if (!!err) {
|
|
129
|
+
result.errors.push(err);
|
|
130
|
+
result.optionalTestErrors.push(err);
|
|
131
|
+
result.failedTests.push(err);
|
|
132
|
+
} else {
|
|
133
|
+
result.optionalTestsPassed++;
|
|
134
|
+
}
|
|
135
|
+
j++;
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
// If the password is not a passphrase, assert that it has passed a
|
|
140
|
+
// sufficient number of the optional tests, per the configuration
|
|
141
|
+
if (
|
|
142
|
+
!result.isPassphrase &&
|
|
143
|
+
result.optionalTestsPassed < config.minOptionalTestsToPass
|
|
144
|
+
) {
|
|
145
|
+
result.strong = false;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
return result;
|
|
149
|
+
};
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
//
|
|
2
|
+
|
|
3
|
+
import bcrypt from "bcryptjs";
|
|
4
|
+
import assert from "node:assert/strict";
|
|
5
|
+
import { describe, it } from "node:test";
|
|
6
|
+
import { validatePassword } from "./validate-password.js";
|
|
7
|
+
|
|
8
|
+
//
|
|
9
|
+
|
|
10
|
+
describe("validatePassword", () => {
|
|
11
|
+
it("should verify a password", async () => {
|
|
12
|
+
const hash = bcrypt.hashSync("🔑", bcrypt.genSaltSync(10));
|
|
13
|
+
const isSamePassword = await validatePassword("🔑", hash);
|
|
14
|
+
assert.equal(isSamePassword, true);
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
it("should return false if the password is empty", async () => {
|
|
18
|
+
const hash = bcrypt.hashSync("🔑", bcrypt.genSaltSync(10));
|
|
19
|
+
const isSamePassword = await validatePassword("", hash);
|
|
20
|
+
assert.equal(isSamePassword, false);
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
it("should return false if the hash is null", async () => {
|
|
24
|
+
const isSamePassword = await validatePassword("🔑", null);
|
|
25
|
+
assert.equal(isSamePassword, false);
|
|
26
|
+
});
|
|
27
|
+
});
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
//
|
|
2
|
+
|
|
3
|
+
import bcrypt from "bcryptjs";
|
|
4
|
+
|
|
5
|
+
//
|
|
6
|
+
|
|
7
|
+
export function validatePassword(
|
|
8
|
+
plainPassword: string,
|
|
9
|
+
storedHash: string | null,
|
|
10
|
+
) {
|
|
11
|
+
if (!plainPassword || !storedHash) {
|
|
12
|
+
return false;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
return bcrypt.compare(plainPassword || "", storedHash);
|
|
16
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
//
|
|
2
|
+
|
|
3
|
+
import assert from "node:assert/strict";
|
|
4
|
+
import { describe, it } from "node:test";
|
|
5
|
+
import { getEmailDomain } from "./get-email-domain.js";
|
|
6
|
+
|
|
7
|
+
//
|
|
8
|
+
|
|
9
|
+
describe("getEmailDomain", () => {
|
|
10
|
+
const data = [
|
|
11
|
+
{
|
|
12
|
+
email: "user@beta.gouv.fr",
|
|
13
|
+
domain: "beta.gouv.fr",
|
|
14
|
+
},
|
|
15
|
+
{
|
|
16
|
+
email: "user@notaires.fr",
|
|
17
|
+
domain: "notaires.fr",
|
|
18
|
+
},
|
|
19
|
+
{
|
|
20
|
+
email: "user@subdomain.domain.org",
|
|
21
|
+
domain: "subdomain.domain.org",
|
|
22
|
+
},
|
|
23
|
+
];
|
|
24
|
+
|
|
25
|
+
data.forEach(({ email, domain }) => {
|
|
26
|
+
it("should return email domain", () => {
|
|
27
|
+
assert.equal(getEmailDomain(email), domain);
|
|
28
|
+
});
|
|
29
|
+
});
|
|
30
|
+
});
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
//
|
|
2
|
+
|
|
3
|
+
import { parse_host } from "tld-extract";
|
|
4
|
+
|
|
5
|
+
//
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Get the domain of an email address
|
|
9
|
+
* @example
|
|
10
|
+
* getEmailDomain("lion.eljonson@darkangels.world") // darkangels.world
|
|
11
|
+
* @param email - the email address
|
|
12
|
+
* @returns the domain of the email address
|
|
13
|
+
*/
|
|
14
|
+
export function getEmailDomain(email: string) {
|
|
15
|
+
const parts = email.split("@");
|
|
16
|
+
const host = parts[parts.length - 1];
|
|
17
|
+
const { sub, domain } = parse_host(host, { allowDotlessTLD: true });
|
|
18
|
+
|
|
19
|
+
return [sub, domain].filter(Boolean).join(".");
|
|
20
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
//
|
|
2
|
+
|
|
3
|
+
import assert from "node:assert/strict";
|
|
4
|
+
import { describe, it } from "node:test";
|
|
5
|
+
import { isAFreeDomain } from "./is-a-free-domain.js";
|
|
6
|
+
|
|
7
|
+
//
|
|
8
|
+
|
|
9
|
+
describe("isAFreeDomain", () => {
|
|
10
|
+
[
|
|
11
|
+
"gmail.com",
|
|
12
|
+
"hotmail.com",
|
|
13
|
+
"yahoo.com",
|
|
14
|
+
"outlook.com",
|
|
15
|
+
"aol.com",
|
|
16
|
+
"yopmail.com",
|
|
17
|
+
].forEach((domain) => {
|
|
18
|
+
it(`should return true for free domains "${domain}"`, () => {
|
|
19
|
+
assert.equal(isAFreeDomain(domain), true);
|
|
20
|
+
});
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
it("should return false for non-free domains", () => {
|
|
24
|
+
assert.equal(isAFreeDomain("beta.gouv.fr"), false);
|
|
25
|
+
});
|
|
26
|
+
});
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
//
|
|
2
|
+
|
|
3
|
+
import mostUsedFreeEmailDomains from "#src/data/most-used-free-email-domains";
|
|
4
|
+
import { isFree } from "is-disposable-email-domain";
|
|
5
|
+
|
|
6
|
+
//
|
|
7
|
+
|
|
8
|
+
export function isAFreeDomain(domain: string) {
|
|
9
|
+
// heavily inspired from https://stackoverflow.com/questions/71232973/check-email-domain-type-personal-email-or-company-email#answer-72640757
|
|
10
|
+
return isFree(domain) || mostUsedFreeEmailDomains.includes(domain);
|
|
11
|
+
}
|