@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.
Files changed (70) hide show
  1. package/CHANGELOG.md +120 -0
  2. package/README.md +61 -0
  3. package/dist/data/index.cjs +282 -0
  4. package/dist/data/index.d.ts +7 -0
  5. package/dist/data/index.js +278 -0
  6. package/dist/most-used-free-email-domains-BiH62xhh.cjs +21 -0
  7. package/dist/most-used-free-email-domains-CtdkKumB.js +19 -0
  8. package/dist/security/index.cjs +8013 -0
  9. package/dist/security/index.d.ts +31 -0
  10. package/dist/security/index.js +7998 -0
  11. package/dist/services/email/index.cjs +19 -0
  12. package/dist/services/email/index.d.ts +12 -0
  13. package/dist/services/email/index.js +16 -0
  14. package/dist/services/oidc/index.cjs +10 -0
  15. package/dist/services/oidc/index.d.ts +19 -0
  16. package/dist/services/oidc/index.js +8 -0
  17. package/dist/services/suggestion/index.cjs +20 -0
  18. package/dist/services/suggestion/index.d.ts +3 -0
  19. package/dist/services/suggestion/index.js +18 -0
  20. package/package.json +92 -0
  21. package/src/data/diceware-wordlist-fr-alt.ts +7780 -0
  22. package/src/data/gouvfr-domains.ts +246 -0
  23. package/src/data/index.ts +3 -0
  24. package/src/data/most-used-free-email-domains.ts +18 -0
  25. package/src/data/other-gouv-domains.ts +41 -0
  26. package/src/security/generate-diceware-password.test.ts +26 -0
  27. package/src/security/generate-diceware-password.ts +25 -0
  28. package/src/security/generate-pin-token.test.ts +19 -0
  29. package/src/security/generate-pin-token.ts +13 -0
  30. package/src/security/generate-token.test.ts +14 -0
  31. package/src/security/generate-token.ts +9 -0
  32. package/src/security/get-trusted-referrer-path.test.ts +49 -0
  33. package/src/security/get-trusted-referrer-path.ts +34 -0
  34. package/src/security/hash-password.test.ts +16 -0
  35. package/src/security/hash-password.ts +10 -0
  36. package/src/security/index.ts +15 -0
  37. package/src/security/is-domain-valid.test.ts +28 -0
  38. package/src/security/is-domain-valid.ts +28 -0
  39. package/src/security/is-email-valid.test.ts +71 -0
  40. package/src/security/is-email-valid.ts +44 -0
  41. package/src/security/is-name-valid.test.ts +61 -0
  42. package/src/security/is-name-valid.ts +5 -0
  43. package/src/security/is-password-secure.test.ts +53 -0
  44. package/src/security/is-password-secure.ts +25 -0
  45. package/src/security/is-phone-number-valid.test.ts +44 -0
  46. package/src/security/is-phone-number-valid.ts +19 -0
  47. package/src/security/is-siret-valid.test.ts +36 -0
  48. package/src/security/is-siret-valid.ts +15 -0
  49. package/src/security/is-visible-string.test.ts +30 -0
  50. package/src/security/is-visible-string.ts +7 -0
  51. package/src/security/owasp-password-strength-tester.ts +149 -0
  52. package/src/security/validate-password.test.ts +27 -0
  53. package/src/security/validate-password.ts +16 -0
  54. package/src/services/email/get-email-domain.test.ts +30 -0
  55. package/src/services/email/get-email-domain.ts +20 -0
  56. package/src/services/email/index.ts +4 -0
  57. package/src/services/email/is-a-free-domain.test.ts +26 -0
  58. package/src/services/email/is-a-free-domain.ts +11 -0
  59. package/src/services/oidc/contains-essential-acrs.test.ts +63 -0
  60. package/src/services/oidc/contains-essential-acrs.ts +24 -0
  61. package/src/services/oidc/index.ts +2 -0
  62. package/src/services/oidc/prompt.ts +11 -0
  63. package/src/services/suggestion/did-you-mean.test.ts +29 -0
  64. package/src/services/suggestion/did-you-mean.ts +19 -0
  65. package/src/services/suggestion/index.ts +3 -0
  66. package/tsconfig.json +11 -0
  67. package/tsconfig.lib.json +9 -0
  68. package/types/index.d.ts +4 -0
  69. package/types/is-disposable-email-domain.d.ts +5 -0
  70. package/types/tld-extract.d.ts +12 -0
@@ -0,0 +1,246 @@
1
+ // This list is extracted from production database with the following query:
2
+ // select domain,
3
+ // count
4
+ // from (select unnest(verified_email_domains) as domain, count(*) as count
5
+ // from organizations
6
+ // group by domain
7
+ // order by domain) as sub
8
+ // where domain like '%.gouv.fr';
9
+ export default [
10
+ "academie.def.gouv.fr",
11
+ "afa.gouv.fr",
12
+ "aft.gouv.fr",
13
+ "agrasc.gouv.fr",
14
+ "agriculture.gouv.fr",
15
+ "ain.gouv.fr",
16
+ "aisne.gouv.fr",
17
+ "allier.gouv.fr",
18
+ "alpes-de-haute-provence.gouv.fr",
19
+ "alpes-maritimes.gouv.fr",
20
+ "anah.gouv.fr",
21
+ "anct.gouv.fr",
22
+ "anlci.gouv.fr",
23
+ "ape.gouv.fr",
24
+ "ardeche.gouv.fr",
25
+ "ardennes.gouv.fr",
26
+ "ariege.gouv.fr",
27
+ "aube.gouv.fr",
28
+ "aude.gouv.fr",
29
+ "auvergne-rhone-alpes.gouv.fr",
30
+ "aveyron.gouv.fr",
31
+ "aviation-civile.gouv.fr",
32
+ "bas-rhin.gouv.fr",
33
+ "beta.gouv.fr",
34
+ "bfc.gouv.fr",
35
+ "bmpm.gouv.fr",
36
+ "bouches-du-rhone.gouv.fr",
37
+ "bretagne.gouv.fr",
38
+ "cab.travail.gouv.fr",
39
+ "calvados.gouv.fr",
40
+ "cantal.gouv.fr",
41
+ "centre-val-de-loire.gouv.fr",
42
+ "charente.gouv.fr",
43
+ "charente-maritime.gouv.fr",
44
+ "cher.gouv.fr",
45
+ "civs.gouv.fr",
46
+ "code.gouv.fr",
47
+ "cofgc.gouv.fr",
48
+ "collectivites-territoriales.gouv.fr",
49
+ "correze.gouv.fr",
50
+ "corse-du-sud.gouv.fr",
51
+ "corse-du-sud.pref.gouv.fr",
52
+ "corse.gouv.fr",
53
+ "cote-dor.gouv.fr",
54
+ "cotes-darmor.gouv.fr",
55
+ "creps-dijon.sports.gouv.fr",
56
+ "creps-lorraine.sports.gouv.fr",
57
+ "creps-montpellier.sports.gouv.fr",
58
+ "creps-paca.sports.gouv.fr",
59
+ "creps-pdl.sports.gouv.fr",
60
+ "creps-poitiers.sports.gouv.fr",
61
+ "creps-reims.sports.gouv.fr",
62
+ "creps-rhonealpes.sports.gouv.fr",
63
+ "creps-toulouse.sports.gouv.fr",
64
+ "creps-vichy.sports.gouv.fr",
65
+ "creps-wattignies.sports.gouv.fr",
66
+ "creuse.gouv.fr",
67
+ "culture.gouv.fr",
68
+ "data.gouv.fr",
69
+ "dcstep.gouv.fr",
70
+ "ddc.social.gouv.fr",
71
+ "deets.gouv.fr",
72
+ "def.gouv.fr",
73
+ "deux-sevres.gouv.fr",
74
+ "developpement-durable.gouv.fr",
75
+ "dgccrf.finances.gouv.fr",
76
+ "dgcl.gouv.fr",
77
+ "dgfip.finances.gouv.fr",
78
+ "dgtresor.gouv.fr",
79
+ "dihal.gouv.fr",
80
+ "dila.gouv.fr",
81
+ "diplomatie.gouv.fr",
82
+ "dordogne.gouv.fr",
83
+ "douane.finances.gouv.fr",
84
+ "doubs.gouv.fr",
85
+ "dreets.gouv.fr",
86
+ "drieets.gouv.fr",
87
+ "drome.gouv.fr",
88
+ "ecologie-territoires.gouv.fr",
89
+ "education.gouv.fr",
90
+ "eleve.insp.gouv.fr",
91
+ "emploi.gouv.fr",
92
+ "enseignementsup.gouv.fr",
93
+ "ensm.sports.gouv.fr",
94
+ "entreprise.api.gouv.fr",
95
+ "envsn.sports.gouv.fr",
96
+ "equipement-agriculture.gouv.fr",
97
+ "esante.gouv.fr",
98
+ "essonne.gouv.fr",
99
+ "essonne.pref.gouv.fr",
100
+ "eure-et-loir.gouv.fr",
101
+ "eure-et-loir.pref.gouv.fr",
102
+ "eure.gouv.fr",
103
+ "finances.gouv.fr",
104
+ "finistere.gouv.fr",
105
+ "franceconnect.gouv.fr",
106
+ "france-services.gouv.fr",
107
+ "gard.gouv.fr",
108
+ "gendarmerie.defense.gouv.fr",
109
+ "gendarmerie.interieur.gouv.fr",
110
+ "gers.gouv.fr",
111
+ "gic.gouv.fr",
112
+ "gironde.gouv.fr",
113
+ "grand-est.gouv.fr",
114
+ "guadeloupe.gouv.fr",
115
+ "guyane.gouv.fr",
116
+ "guyane.pref.gouv.fr",
117
+ "haute-corse.gouv.fr",
118
+ "haute-garonne.gouv.fr",
119
+ "haute-loire.gouv.fr",
120
+ "haute-marne.gouv.fr",
121
+ "hautes-alpes.gouv.fr",
122
+ "haute-saone.gouv.fr",
123
+ "haute-savoie.gouv.fr",
124
+ "hautes-pyrenees.gouv.fr",
125
+ "haute-vienne.gouv.fr",
126
+ "haut-rhin.gouv.fr",
127
+ "hauts-de-france.gouv.fr",
128
+ "hauts-de-seine.gouv.fr",
129
+ "herault.gouv.fr",
130
+ "iga.interieur.gouv.fr",
131
+ "igas.gouv.fr",
132
+ "igesr.gouv.fr",
133
+ "igf.finances.gouv.fr",
134
+ "ille-et-vilaine.gouv.fr",
135
+ "inao.gouv.fr",
136
+ "inclusion.beta.gouv.fr",
137
+ "indre-et-loire.gouv.fr",
138
+ "indre-et-loire.pref.gouv.fr",
139
+ "indre.gouv.fr",
140
+ "insp.gouv.fr",
141
+ "interieur.gouv.fr",
142
+ "intradef.gouv.fr",
143
+ "ira-bastia.gouv.fr",
144
+ "ira-lyon.gouv.fr",
145
+ "ira-metz.gouv.fr",
146
+ "ira-nantes.gouv.fr",
147
+ "isere.gouv.fr",
148
+ "jeunesse-sports.gouv.fr",
149
+ "jura.gouv.fr",
150
+ "justice.gouv.fr",
151
+ "landes.gouv.fr",
152
+ "loire-atlantique.gouv.fr",
153
+ "loire.gouv.fr",
154
+ "loir-et-cher.gouv.fr",
155
+ "loiret.gouv.fr",
156
+ "lot-et-garonne.gouv.fr",
157
+ "lot.gouv.fr",
158
+ "lot.pref.gouv.fr",
159
+ "lozere.gouv.fr",
160
+ "mail.numerique.gouv.fr",
161
+ "maine-et-loire.gouv.fr",
162
+ "manche.gouv.fr",
163
+ "marne.gouv.fr",
164
+ "martinique.gouv.fr",
165
+ "martinique.pref.gouv.fr",
166
+ "mayenne.gouv.fr",
167
+ "mayotte.gouv.fr",
168
+ "mayotte.pref.gouv.fr",
169
+ "mer.gouv.fr",
170
+ "meurthe-et-moselle.gouv.fr",
171
+ "meurthe-et-moselle.pref.gouv.fr",
172
+ "meuse.gouv.fr",
173
+ "meuse.pref.gouv.fr",
174
+ "modernisation.gouv.fr",
175
+ "montsaintmichel.gouv.fr",
176
+ "morbihan.gouv.fr",
177
+ "moselle.gouv.fr",
178
+ "nievre.gouv.fr",
179
+ "nord.gouv.fr",
180
+ "normandie.gouv.fr",
181
+ "nouvelle-aquitaine.gouv.fr",
182
+ "nouvelle-caledonie.gouv.fr",
183
+ "numerique.gouv.fr",
184
+ "occitanie.gouv.fr",
185
+ "ofb.gouv.fr",
186
+ "ofpra.gouv.fr",
187
+ "oise.gouv.fr",
188
+ "orne.gouv.fr",
189
+ "outre-mer.gouv.fr",
190
+ "paca.gouv.fr",
191
+ "paris.gouv.fr",
192
+ "pas-de-calais.gouv.fr",
193
+ "pas-de-calais.pref.gouv.fr",
194
+ "pays-de-la-loire.gouv.fr",
195
+ "pays-de-la-loire.pref.gouv.fr",
196
+ "place-des-entreprises.beta.gouv.fr",
197
+ "pm.gouv.fr",
198
+ "polynesie-francaise.pref.gouv.fr",
199
+ "preau.education.gouv.fr",
200
+ "premar-atlantique.gouv.fr",
201
+ "puy-de-dome.gouv.fr",
202
+ "pyrenees-atlantiques.gouv.fr",
203
+ "pyrenees-orientales.gouv.fr",
204
+ "recherche.gouv.fr",
205
+ "reunion.gouv.fr",
206
+ "reunion.pref.gouv.fr",
207
+ "rhone.gouv.fr",
208
+ "saint-barth-saint-martin.gouv.fr",
209
+ "sante.gouv.fr",
210
+ "saone-et-loire.gouv.fr",
211
+ "sarthe.gouv.fr",
212
+ "savoie.gouv.fr",
213
+ "scl.finances.gouv.fr",
214
+ "seine-et-marne.gouv.fr",
215
+ "seine-maritime.gouv.fr",
216
+ "seine-saint-denis.gouv.fr",
217
+ "service-civique.gouv.fr",
218
+ "sgdsn.gouv.fr",
219
+ "sgg.pm.gouv.fr",
220
+ "sg.social.gouv.fr",
221
+ "snu.gouv.fr",
222
+ "social.gouv.fr",
223
+ "somme.gouv.fr",
224
+ "spm975.gouv.fr",
225
+ "sportsdenature.gouv.fr",
226
+ "sports.gouv.fr",
227
+ "ssi.gouv.fr",
228
+ "strategie.gouv.fr",
229
+ "tarn-et-garonne.gouv.fr",
230
+ "tarn-et-garonne.pref.gouv.fr",
231
+ "tarn.gouv.fr",
232
+ "territoire-de-belfort.gouv.fr",
233
+ "territoires-industrie.gouv.fr",
234
+ "transport.data.gouv.fr",
235
+ "travail.gouv.fr",
236
+ "val-de-marne.gouv.fr",
237
+ "val-doise.gouv.fr",
238
+ "var.gouv.fr",
239
+ "vaucluse.gouv.fr",
240
+ "vendee.gouv.fr",
241
+ "vienne.gouv.fr",
242
+ "vosges.gouv.fr",
243
+ "wallis-et-futuna.pref.gouv.fr",
244
+ "yonne.gouv.fr",
245
+ "yvelines.gouv.fr",
246
+ ];
@@ -0,0 +1,3 @@
1
+ export { default as gouvfrDomains } from "./gouvfr-domains.js";
2
+ export { default as mostUsedFreeEmailDomains } from "./most-used-free-email-domains.js";
3
+ export { default as otherGouvDomains } from "./other-gouv-domains.js";
@@ -0,0 +1,18 @@
1
+ // arbitrary selection of free email provider domains used by our users
2
+ export default [
3
+ "gmail.com",
4
+ "orange.fr",
5
+ "wanadoo.fr",
6
+ "hotmail.fr",
7
+ "outlook.fr",
8
+ "hotmail.com",
9
+ "yahoo.fr",
10
+ "laposte.net",
11
+ "free.fr",
12
+ "yahoo.com",
13
+ "live.fr",
14
+ "numericable.fr",
15
+ "sfr.fr",
16
+ "9business.fr",
17
+ "laposte.fr",
18
+ ];
@@ -0,0 +1,41 @@
1
+ // we try to register here all email domaines used within the administration
2
+ // and not ending with ".gouv.fr"
3
+ // we ran the following query in production database:
4
+ // select unnest(authorized_email_domains) as domain, count(*) from organizations group by domain having count(*) > 10 order by count desc;
5
+ // then we removed all .com and .gouv.fr from the list
6
+ // then we selected domains manually
7
+ export default [
8
+ "conseiller-numerique.fr",
9
+ "justice.fr",
10
+ "justice.fr",
11
+ "francetravail.fr",
12
+ "assurance-maladie.fr",
13
+ "pole-emploi.fr",
14
+ "urssaf.fr",
15
+ "ars.sante.fr",
16
+ "paris.fr",
17
+ "conseiller-numerique.fr",
18
+ "cnrs.fr",
19
+ "caf.fr",
20
+ "i-carre.net",
21
+ "ac-grenoble.fr",
22
+ "ac-nantes.fr",
23
+ "ac-lille.fr",
24
+ "aphp.fr",
25
+ "ac-versailles.fr",
26
+ "ac-rennes.fr",
27
+ "ac-normandie.fr",
28
+ "ac-lyon.fr",
29
+ "marseille.fr",
30
+ "ac-creteil.fr",
31
+ "ac-bordeaux.fr",
32
+ "ac-toulouse.fr",
33
+ "ac-nancy-metz.fr",
34
+ "ac-orleans-tours.fr",
35
+ "ac-clermont.fr",
36
+ "ac-amiens.fr",
37
+ "ac-aix-marseille.fr",
38
+ "ac-paris.fr",
39
+ "asp-public.fr",
40
+ "ac-montpellier.fr",
41
+ ];
@@ -0,0 +1,26 @@
1
+ //
2
+
3
+ import assert from "node:assert/strict";
4
+ import { describe, it } from "node:test";
5
+ import { GenerarateDicewarePassword } from "./generate-diceware-password.js";
6
+
7
+ //
8
+
9
+ describe("GenerarateDicewarePassword", () => {
10
+ it("should generate two words", () => {
11
+ const generatePassword = GenerarateDicewarePassword([
12
+ () => "11111",
13
+ () => "22222",
14
+ ]);
15
+ assert.equal(generatePassword(), "abandon-cible");
16
+ });
17
+
18
+ it("should generate three words", () => {
19
+ const generatePassword = GenerarateDicewarePassword([
20
+ () => "11111",
21
+ () => "22222",
22
+ () => "33333",
23
+ ]);
24
+ assert.equal(generatePassword(), "abandon-cible-gastrique");
25
+ });
26
+ });
@@ -0,0 +1,25 @@
1
+ //
2
+
3
+ import { customAlphabet } from "nanoid";
4
+ import dicewareWordlistFrAlt from "../data/diceware-wordlist-fr-alt.js";
5
+
6
+ //
7
+
8
+ type dice = "1" | "2" | "3" | "4" | "5" | "6";
9
+ type fiveDices = `${dice}${dice}${dice}${dice}${dice}`;
10
+ const nanoidFiveDices = customAlphabet("123456", 5);
11
+ const createFiveDices = () => nanoidFiveDices() as fiveDices;
12
+
13
+ export function GenerarateDicewarePassword(
14
+ generators: Array<typeof createFiveDices>,
15
+ ) {
16
+ return function generatePassword() {
17
+ return generators
18
+ .map((generator) => dicewareWordlistFrAlt[generator()])
19
+ .join("-");
20
+ };
21
+ }
22
+
23
+ export const generateDicewarePassword = GenerarateDicewarePassword(
24
+ Array(2).fill(createFiveDices),
25
+ );
@@ -0,0 +1,19 @@
1
+ //
2
+
3
+ import assert from "node:assert/strict";
4
+ import { describe, it } from "node:test";
5
+ import { generatePinToken } from "./generate-pin-token.js";
6
+
7
+ //
8
+
9
+ describe("generatePinToken", () => {
10
+ it("should use digits only", () => {
11
+ const token = generatePinToken();
12
+ assert.match(token, /^[0-9]{10}$/);
13
+ });
14
+
15
+ it("should be 10 characters long", () => {
16
+ const token = generatePinToken();
17
+ assert.equal(token.length, 10);
18
+ });
19
+ });
@@ -0,0 +1,13 @@
1
+ //
2
+
3
+ import { customAlphabet } from "nanoid";
4
+
5
+ //
6
+
7
+ const nanoidPin = customAlphabet("0123456789", 10);
8
+
9
+ //
10
+
11
+ export function generatePinToken() {
12
+ return nanoidPin();
13
+ }
@@ -0,0 +1,14 @@
1
+ //
2
+
3
+ import assert from "node:assert/strict";
4
+ import { describe, it } from "node:test";
5
+ import { generateToken } from "./generate-token.js";
6
+
7
+ //
8
+
9
+ describe("generateToken", () => {
10
+ it("should be 64 characters long", () => {
11
+ const token = generateToken();
12
+ assert.equal(token.length, 64);
13
+ });
14
+ });
@@ -0,0 +1,9 @@
1
+ //
2
+
3
+ import { nanoid } from "nanoid";
4
+
5
+ //
6
+
7
+ export function generateToken() {
8
+ return nanoid(64);
9
+ }
@@ -0,0 +1,49 @@
1
+ //
2
+
3
+ import assert from "node:assert/strict";
4
+ import { describe, it } from "node:test";
5
+ import { getTrustedReferrerPath } from "./get-trusted-referrer-path.js";
6
+
7
+ //
8
+
9
+ describe("getTrustedReferrerPath", () => {
10
+ it("should not trust undefined referrer", () => {
11
+ assert.equal(
12
+ getTrustedReferrerPath(undefined, "https://proconnect.gouv.fr"),
13
+ null,
14
+ );
15
+ });
16
+ it("should not trust empty url", () => {
17
+ assert.equal(
18
+ getTrustedReferrerPath("", "https://proconnect.gouv.fr"),
19
+ null,
20
+ );
21
+ });
22
+ it("should not trust external domain (over https)", () => {
23
+ assert.equal(
24
+ getTrustedReferrerPath(
25
+ "https://www.google.com",
26
+ "https://proconnect.gouv.fr",
27
+ ),
28
+ null,
29
+ );
30
+ });
31
+ it("should trust relative path", () => {
32
+ assert.equal(
33
+ getTrustedReferrerPath(
34
+ "/users/join-organization",
35
+ "https://proconnect.gouv.fr",
36
+ ),
37
+ "/users/join-organization",
38
+ );
39
+ });
40
+ it("should trust absolute path on same domain", () => {
41
+ assert.equal(
42
+ getTrustedReferrerPath(
43
+ "https://proconnect.gouv.fr/users/join-organization",
44
+ "https://proconnect.gouv.fr",
45
+ ),
46
+ "/users/join-organization",
47
+ );
48
+ });
49
+ });
@@ -0,0 +1,34 @@
1
+ //
2
+
3
+ import { isEmpty, isString } from "lodash-es";
4
+
5
+ //
6
+
7
+ export function getTrustedReferrerPath(
8
+ referrer: string | undefined,
9
+ base: string,
10
+ ) {
11
+ if (!isString(referrer) || isEmpty(referrer)) {
12
+ return null;
13
+ }
14
+
15
+ const isValidURL = URL.canParse(referrer);
16
+ const isValidRelativeURL = URL.canParse(referrer, base);
17
+ let parsedURL: URL;
18
+ if (isValidURL) {
19
+ parsedURL = new URL(referrer);
20
+ } else if (isValidRelativeURL) {
21
+ // referrer may be relative
22
+ parsedURL = new URL(referrer, base);
23
+ } else {
24
+ return null;
25
+ }
26
+
27
+ const baseURL = new URL(base);
28
+
29
+ if (baseURL.origin !== parsedURL.origin) {
30
+ return null;
31
+ }
32
+
33
+ return `${parsedURL.pathname}${parsedURL.search}`;
34
+ }
@@ -0,0 +1,16 @@
1
+ //
2
+
3
+ import bcrypt from "bcryptjs";
4
+ import assert from "node:assert/strict";
5
+ import { describe, it } from "node:test";
6
+ import { hashPassword } from "./hash-password.js";
7
+
8
+ //
9
+
10
+ describe("hashPassword", () => {
11
+ it("should hash a password", async () => {
12
+ const hashedPassword = await hashPassword("🔑");
13
+ const isSamePassword = await bcrypt.compare("🔑", hashedPassword);
14
+ assert.equal(isSamePassword, true);
15
+ });
16
+ });
@@ -0,0 +1,10 @@
1
+ //
2
+
3
+ import { hash } from "bcryptjs";
4
+
5
+ //
6
+
7
+ // TODO compare to https://github.com/anandundavia/manage-users/blob/master/src/api/utils/security.js
8
+ export function hashPassword(plainPassword: string): Promise<string> {
9
+ return hash(plainPassword, 10);
10
+ }
@@ -0,0 +1,15 @@
1
+ //
2
+
3
+ export * from "./generate-diceware-password.js";
4
+ export * from "./generate-pin-token.js";
5
+ export * from "./generate-token.js";
6
+ export * from "./get-trusted-referrer-path.js";
7
+ export * from "./hash-password.js";
8
+ export * from "./is-domain-valid.js";
9
+ export * from "./is-email-valid.js";
10
+ export * from "./is-name-valid.js";
11
+ export * from "./is-password-secure.js";
12
+ export * from "./is-phone-number-valid.js";
13
+ export * from "./is-siret-valid.js";
14
+ export * from "./is-visible-string.js";
15
+ export * from "./validate-password.js";
@@ -0,0 +1,28 @@
1
+ //
2
+
3
+ import assert from "node:assert/strict";
4
+ import { describe, it } from "node:test";
5
+ import { isDomainValid } from "./is-domain-valid.js";
6
+
7
+ //
8
+
9
+ describe("isDomainValid", () => {
10
+ it("should return false for undefined value", () => {
11
+ assert.equal(isDomainValid(undefined), false);
12
+ });
13
+
14
+ it("should return false for empty string", () => {
15
+ assert.equal(isDomainValid(""), false);
16
+ });
17
+
18
+ it("should return false if contains characters other than number and letters", () => {
19
+ assert.equal(isDomainValid("héééééé"), false);
20
+ });
21
+
22
+ it("should allow dot less tld", () => {
23
+ assert.equal(isDomainValid("co.uk"), true);
24
+ });
25
+ it("should allow gouv.fr", () => {
26
+ assert.equal(isDomainValid("gouv.fr"), true);
27
+ });
28
+ });
@@ -0,0 +1,28 @@
1
+ //
2
+
3
+ import { isEmpty, isString } from "lodash-es";
4
+ import { parse_host } from "tld-extract";
5
+
6
+ //
7
+
8
+ /*
9
+ * specifications of these functions can be found at
10
+ * https://cheatsheetseries.owasp.org/cheatsheets/Input_Validation_Cheat_Sheet.html#email-address-validation
11
+ */
12
+ export function isDomainValid(domain: unknown): domain is string {
13
+ if (!isString(domain) || isEmpty(domain)) {
14
+ return false;
15
+ }
16
+
17
+ if (domain.match(/^[a-zA-Z0-9.-]*$/) === null) {
18
+ return false;
19
+ }
20
+
21
+ try {
22
+ parse_host(domain, { allowDotlessTLD: true });
23
+ } catch {
24
+ return false;
25
+ }
26
+
27
+ return true;
28
+ }
@@ -0,0 +1,71 @@
1
+ import assert from "node:assert/strict";
2
+ import { describe, it } from "node:test";
3
+ import { isEmailValid } from "./is-email-valid.js";
4
+
5
+ describe("isEmailValid", () => {
6
+ it("should return false for undefined value", () => {
7
+ assert.equal(isEmailValid(undefined), false);
8
+ });
9
+
10
+ it("should return false for empty string", () => {
11
+ assert.equal(isEmailValid(""), false);
12
+ });
13
+
14
+ it("should return false if no @ is present", () => {
15
+ assert.equal(isEmailValid("test"), false);
16
+ });
17
+
18
+ it("should return false if no domain is present", () => {
19
+ assert.equal(isEmailValid("test@"), false);
20
+ });
21
+
22
+ it("should return false if two @ are present", () => {
23
+ assert.equal(isEmailValid("test@test@test"), false);
24
+ });
25
+
26
+ it("should return false if domains contain other than letters, numbers, hyphens (-) and periods (.)", () => {
27
+ assert.equal(isEmailValid("test@test_test"), false);
28
+ });
29
+
30
+ it("should return false if tld has the wrong case", () => {
31
+ assert.equal(isEmailValid("jean@wanadoo.Fr"), false);
32
+ });
33
+
34
+ it("should return false if local part is longer than 63 characters", () => {
35
+ assert.equal(
36
+ isEmailValid(
37
+ "1234567890123456789012345678901234567890123456789012345678901234@test",
38
+ ),
39
+ false,
40
+ );
41
+ });
42
+
43
+ it("should return false if total length is longer than 254 characters", () => {
44
+ assert.equal(
45
+ isEmailValid(
46
+ "test@1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890",
47
+ ),
48
+ false,
49
+ );
50
+ });
51
+
52
+ // this test cases have been taken from
53
+ // https://stackoverflow.com/questions/46155/how-to-validate-an-email-address-in-javascript/32686261#32686261
54
+ const validEmailAddresses = [
55
+ "prettyandsimple@example.com",
56
+ "very.common@example.com",
57
+ "disposable.style.email.with+symbol@example.com",
58
+ "other.email-with-dash@example.com",
59
+ "#!$%&'*+-/=?^_`{}|~@example.org",
60
+ '"()[]:,;\\"!#$%&\'*+-/=?^_`{}| ~.a"@example.org',
61
+ '" "@example.org', // space between the quotes
62
+ "üñîçøðé@example.com", // Unicode characters in local part
63
+ "Pelé@example.com", // Latin
64
+ ];
65
+
66
+ validEmailAddresses.forEach((validEmailAddress) => {
67
+ it(`should return true for valid email address: "${validEmailAddress}"`, () => {
68
+ assert.equal(isEmailValid(validEmailAddress), true);
69
+ });
70
+ });
71
+ });