@1auth/account-username 0.0.0-alpha.7 → 0.0.0-alpha.70

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 (3) hide show
  1. package/index.js +98 -54
  2. package/package.json +6 -6
  3. package/LICENSE +0 -21
package/index.js CHANGED
@@ -1,79 +1,123 @@
1
1
  import {
2
2
  getOptions as accountOptions,
3
- update as accountUpdate
4
- } from '@1auth/account'
3
+ update as accountUpdate,
4
+ lookup as accountLookup,
5
+ } from "@1auth/account";
5
6
 
6
- import { createDigest } from '@1auth/crypto'
7
-
8
- export const regexp = /^[a-z0-9-]*$/
9
- export const jsonSchema = {
10
- type: 'string',
11
- pattern: '^[a-z0-9-]*$',
12
- minLength: 1,
13
- maxLength: 32
14
- }
7
+ import { createSeasonedDigest, symmetricDecryptFields } from "@1auth/crypto";
15
8
 
9
+ // Only allow characters that are safe to encode
10
+ // not allowed because it can be used to declare and extension
11
+ let usernameBlacklistRegExp;
16
12
  const options = {
17
- id: 'username',
18
- blacklist: ['admin', 'security']
19
- }
13
+ id: "username",
14
+ allowedCharRegExp: /^[a-z0-9_-]*$/,
15
+ usernameBlacklist: [],
16
+ minLength: 1,
17
+ maxLength: 32,
18
+ };
20
19
  export default (params) => {
21
- Object.assign(options, accountOptions(), params)
22
- }
20
+ Object.assign(options, accountOptions(), params);
21
+ if (options.usernameBlacklist.length) {
22
+ usernameBlacklistRegExp = new RegExp(
23
+ `(${options.usernameBlacklist.map((value) => value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")).join("|")})`,
24
+ );
25
+ }
26
+ };
23
27
 
24
28
  export const exists = async (username) => {
25
- const usernameSanitized = __sanitize(username)
29
+ const usernameSanitized = sanitize(username);
30
+ const usernameDigest = createSeasonedDigest(usernameSanitized);
26
31
  return options.store.exists(options.table, {
27
- digest: await createDigest(usernameSanitized)
28
- })
29
- }
32
+ digest: usernameDigest,
33
+ });
34
+ };
30
35
 
31
36
  export const lookup = async (username) => {
32
- if (!username) return {}
33
- const usernameSanitized = __sanitize(username)
34
- return await options.store.select(options.table, {
35
- digest: await createDigest(usernameSanitized)
36
- })
37
- }
37
+ const usernameSanitized = sanitize(username);
38
+ const usernameDigest = createSeasonedDigest(usernameSanitized);
39
+
40
+ let item = await options.store.select(options.table, {
41
+ digest: usernameDigest,
42
+ });
43
+ if (!item) return;
44
+ // must match @1auth/account
45
+ item = symmetricDecryptFields(
46
+ item,
47
+ { encryptedKey: item.encryptionKey, sub: item.sub },
48
+ options.encryptedFields,
49
+ );
50
+ delete item.encryptionKey;
51
+ delete item.privateKey;
52
+ return item;
53
+ };
38
54
 
39
55
  export const create = async (sub, username) => {
40
- if (!__validate(username)) {
41
- throw new Error('400 invalid characters')
56
+ const usernameSanitized = sanitize(username);
57
+ const usernameValidate = validate(usernameSanitized);
58
+ if (usernameValidate !== true) {
59
+ throw new Error(usernameValidate, {
60
+ cause: { username, usernameSanitized },
61
+ });
42
62
  }
43
- if (!__blacklist(username) || (await exists(username))) {
44
- throw new Error('409 Conflict')
63
+ const usernameDigest = createSeasonedDigest(usernameSanitized);
64
+ const usernameExists = await options.store.exists(options.table, {
65
+ digest: usernameDigest,
66
+ });
67
+ if (usernameExists) {
68
+ throw new Error("409 Conflict", { cause: { username, usernameSanitized } });
45
69
  }
46
- const usernameSanitized = __sanitize(username)
70
+
47
71
  await accountUpdate(sub, {
48
- username: usernameSanitized,
49
- digest: await createDigest(usernameSanitized)
50
- })
51
- }
72
+ username,
73
+ digest: usernameDigest,
74
+ });
75
+ };
52
76
 
53
77
  export const update = async (sub, username) => {
54
- await create(sub, username)
55
- await options.notify.trigger('account-username-change')
56
- }
78
+ await create(sub, username);
79
+ await options.notify.trigger("account-username-change", sub);
80
+ };
57
81
 
58
82
  export const recover = async (sub) => {
59
- const { username } = await options.store.select(options.table, { sub })
60
- await options.notify.trigger('account-username-recover', { username })
61
- }
83
+ const { username } = await accountLookup(sub);
84
+ await options.notify.trigger("account-username-recover", sub, { username });
85
+ };
86
+
87
+ export const sanitize = (value) => {
88
+ return value
89
+ .trim()
90
+ .toLocaleLowerCase()
91
+ .normalize("NFKD")
92
+ .replace(/\p{Diacritic}/gu, "");
93
+ };
62
94
 
63
- export const __sanitize = (value) => {
64
- return value.trim()
65
- }
95
+ export const validate = (value) => {
96
+ let valid = true;
97
+ if (valid === true) valid = validateLength(value);
98
+ if (valid === true) valid = validateAllowedChar(value);
99
+ if (valid === true) valid = validateBlacklist(value);
100
+ return valid;
101
+ };
102
+
103
+ export const validateLength = (value) => {
104
+ if (value.length < options.minLength || options.maxLength < value.length) {
105
+ return "400 Bad Request";
106
+ }
107
+ return true;
108
+ };
66
109
 
67
- export const __validate = (value) => {
68
- if (!regexp.test(value)) {
69
- return false
110
+ export const validateAllowedChar = (value) => {
111
+ // TODO URL encode compare
112
+ if (!options.allowedCharRegExp.test(value)) {
113
+ return "400 Bad Request";
70
114
  }
71
- return true
72
- }
115
+ return true;
116
+ };
73
117
 
74
- export const __blacklist = (value) => {
75
- if (options.blacklist.includes(value)) {
76
- return false
118
+ export const validateBlacklist = (value) => {
119
+ if (usernameBlacklistRegExp?.test(value)) {
120
+ return "409 Conflict";
77
121
  }
78
- return true
79
- }
122
+ return true;
123
+ };
package/package.json CHANGED
@@ -1,10 +1,10 @@
1
1
  {
2
2
  "name": "@1auth/account-username",
3
- "version": "0.0.0-alpha.7",
3
+ "version": "0.0.0-alpha.70",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "engines": {
7
- "node": ">=16"
7
+ "node": ">=20"
8
8
  },
9
9
  "engineStrict": true,
10
10
  "publishConfig": {
@@ -27,7 +27,7 @@
27
27
  ],
28
28
  "scripts": {
29
29
  "test": "npm run test:unit",
30
- "test:unit": "ava"
30
+ "test:unit": "node --test"
31
31
  },
32
32
  "license": "MIT",
33
33
  "funding": {
@@ -41,15 +41,15 @@
41
41
  },
42
42
  "repository": {
43
43
  "type": "git",
44
- "url": "github:willfarrell/1auth",
44
+ "url": "git+https://github.com/willfarrell/1auth.git",
45
45
  "directory": "packages/account-username"
46
46
  },
47
47
  "bugs": {
48
48
  "url": "https://github.com/willfarrell/1auth/issues"
49
49
  },
50
50
  "homepage": "https://github.com/willfarrell/1auth",
51
- "gitHead": "39c704fcab5798f6bee8d00b2bcf574ba250a4cf",
51
+ "gitHead": "7a6c0fbb8ab71d6a2171e678697de9f237568431",
52
52
  "dependencies": {
53
- "@1auth/account": "0.0.0-alpha.7"
53
+ "@1auth/account": "0.0.0-alpha.70"
54
54
  }
55
55
  }
package/LICENSE DELETED
@@ -1,21 +0,0 @@
1
- MIT License
2
-
3
- Copyright (c) 2023 will Farrell
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.