@1auth/account-username 0.0.0-alpha.9 → 0.0.0-beta.1

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 +111 -64
  2. package/package.json +53 -53
  3. package/LICENSE +0 -21
package/index.js CHANGED
@@ -1,83 +1,130 @@
1
+ // Copyright 2003 - 2026 will Farrell, and 1Auth contributors.
2
+ // SPDX-License-Identifier: MIT
1
3
  import {
2
- getOptions as accountOptions,
3
- update as accountUpdate
4
- } from '@1auth/account'
4
+ lookup as accountLookup,
5
+ getOptions as accountOptions,
6
+ update as accountUpdate,
7
+ } from "@1auth/account";
5
8
 
6
- import { createDigest } from '@1auth/crypto'
9
+ import { createSeasonedDigest, symmetricDecryptFields } from "@1auth/crypto";
7
10
 
8
11
  // Only allow characters that are safe to encode
9
- // . not allowed because it can be used to declare and extension
10
- export const regexp = /^[a-z0-9_-]*$/
11
- export const jsonSchema = {
12
- type: 'string',
13
- pattern: '^[a-z0-9_-]*$',
14
- minLength: 1,
15
- maxLength: 32
16
- }
17
-
12
+ // not allowed because it can be used to declare and extension
13
+ let usernameBlacklistRegExp;
18
14
  const options = {
19
- id: 'username',
20
- blacklist: []
21
- //minLength: 1,
22
- //maxLength: 32
23
- }
15
+ id: "username",
16
+ allowedCharRegExp: /^[a-z0-9_-]*$/,
17
+ usernameBlacklist: [],
18
+ minLength: 1,
19
+ maxLength: 32,
20
+ };
24
21
  export default (params) => {
25
- Object.assign(options, accountOptions(), params)
26
- }
22
+ Object.assign(options, accountOptions(), params);
23
+ if (options.usernameBlacklist.length) {
24
+ usernameBlacklistRegExp = new RegExp(
25
+ `(${options.usernameBlacklist.map((value) => value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")).join("|")})`,
26
+ );
27
+ }
28
+ };
27
29
 
28
30
  export const exists = async (username) => {
29
- const usernameSanitized = __sanitize(username)
30
- return options.store.exists(options.table, {
31
- digest: await createDigest(usernameSanitized)
32
- })
33
- }
31
+ const usernameSanitized = sanitize(username);
32
+ const usernameDigest = createSeasonedDigest(usernameSanitized);
33
+ return options.store.exists(options.table, {
34
+ digest: usernameDigest,
35
+ });
36
+ };
34
37
 
35
38
  export const lookup = async (username) => {
36
- if (!username) return {}
37
- const usernameSanitized = __sanitize(username)
38
- return await options.store.select(options.table, {
39
- digest: await createDigest(usernameSanitized)
40
- })
41
- }
39
+ const usernameSanitized = sanitize(username);
40
+ const usernameDigest = createSeasonedDigest(usernameSanitized);
41
+
42
+ let item = await options.store.select(options.table, {
43
+ digest: usernameDigest,
44
+ });
45
+ if (!item) return;
46
+ // must match @1auth/account
47
+ item = symmetricDecryptFields(
48
+ item,
49
+ { encryptedKey: item.encryptionKey, sub: item.sub },
50
+ options.encryptedFields,
51
+ );
52
+ item.encryptionKey = undefined;
53
+ return item;
54
+ };
42
55
 
43
56
  export const create = async (sub, username) => {
44
- const usernameSanitized = __sanitize(username)
45
- if (!__validate(usernameSanitized)) {
46
- throw new Error('400 invalid characters')
47
- }
48
- if (!__blacklist(usernameSanitized) || (await exists(usernameSanitized))) {
49
- throw new Error('409 Conflict')
50
- }
51
- await accountUpdate(sub, {
52
- username,
53
- digest: await createDigest(usernameSanitized)
54
- })
55
- }
57
+ if (!sub || typeof sub !== "string") {
58
+ throw new Error("401 Unauthorized", { cause: { sub } });
59
+ }
60
+ const usernameSanitized = sanitize(username);
61
+ const usernameValidate = validate(usernameSanitized);
62
+ if (usernameValidate !== true) {
63
+ throw new Error(usernameValidate, {
64
+ cause: { username, usernameSanitized },
65
+ });
66
+ }
67
+ const usernameDigest = createSeasonedDigest(usernameSanitized);
68
+ const usernameExists = await options.store.exists(options.table, {
69
+ digest: usernameDigest,
70
+ });
71
+ if (usernameExists) {
72
+ throw new Error("409 Conflict", { cause: { username, usernameSanitized } });
73
+ }
74
+
75
+ await accountUpdate(sub, {
76
+ value: username,
77
+ digest: usernameDigest,
78
+ });
79
+ };
56
80
 
57
81
  export const update = async (sub, username) => {
58
- await create(sub, username)
59
- await options.notify.trigger('account-username-change')
60
- }
82
+ await create(sub, username);
83
+ await options.notify.trigger("account-username-change", sub);
84
+ };
61
85
 
62
86
  export const recover = async (sub) => {
63
- const { username } = await options.store.select(options.table, { sub })
64
- await options.notify.trigger('account-username-recover', { username })
65
- }
87
+ const { value: username } = await accountLookup(sub);
88
+ await options.notify.trigger("account-username-recover", sub, { username });
89
+ };
90
+
91
+ export const sanitize = (value) => {
92
+ if (!value || typeof value !== "string") {
93
+ throw new Error("400 Bad Request", { cause: { value } });
94
+ }
95
+ return value
96
+ .trim()
97
+ .toLocaleLowerCase()
98
+ .normalize("NFKD")
99
+ .replace(/\p{Diacritic}/gu, "");
100
+ };
101
+
102
+ export const validate = (value) => {
103
+ let valid = true;
104
+ if (valid === true) valid = validateLength(value);
105
+ if (valid === true) valid = validateAllowedChar(value);
106
+ if (valid === true) valid = validateBlacklist(value);
107
+ return valid;
108
+ };
66
109
 
67
- export const __sanitize = (value) => {
68
- return value.trim().toLocaleLowerCase()
69
- }
110
+ export const validateLength = (value) => {
111
+ if (value.length < options.minLength || options.maxLength < value.length) {
112
+ return "400 Bad Request";
113
+ }
114
+ return true;
115
+ };
70
116
 
71
- export const __validate = (value) => {
72
- if (!regexp.test(value)) {
73
- return false
74
- }
75
- return true
76
- }
117
+ export const validateAllowedChar = (value) => {
118
+ // TODO URL encode compare
119
+ if (!options.allowedCharRegExp.test(value)) {
120
+ return "400 Bad Request";
121
+ }
122
+ return true;
123
+ };
77
124
 
78
- export const __blacklist = (value) => {
79
- if (options.blacklist.includes(value)) {
80
- return false
81
- }
82
- return true
83
- }
125
+ export const validateBlacklist = (value) => {
126
+ if (usernameBlacklistRegExp?.test(value)) {
127
+ return "409 Conflict";
128
+ }
129
+ return true;
130
+ };
package/package.json CHANGED
@@ -1,55 +1,55 @@
1
1
  {
2
- "name": "@1auth/account-username",
3
- "version": "0.0.0-alpha.9",
4
- "description": "",
5
- "type": "module",
6
- "engines": {
7
- "node": ">=16"
8
- },
9
- "engineStrict": true,
10
- "publishConfig": {
11
- "access": "public"
12
- },
13
- "main": "./index.js",
14
- "module": "./index.js",
15
- "exports": {
16
- ".": {
17
- "import": {
18
- "types": "./index.d.ts",
19
- "default": "./index.js"
20
- }
21
- }
22
- },
23
- "types": "index.d.ts",
24
- "files": [
25
- "index.js",
26
- "index.d.ts"
27
- ],
28
- "scripts": {
29
- "test": "npm run test:unit",
30
- "test:unit": "ava"
31
- },
32
- "license": "MIT",
33
- "funding": {
34
- "type": "github",
35
- "url": "https://github.com/sponsors/willfarrell"
36
- },
37
- "keywords": [],
38
- "author": {
39
- "name": "1auth contributors",
40
- "url": "https://github.com/willfarrell/1auth/graphs/contributors"
41
- },
42
- "repository": {
43
- "type": "git",
44
- "url": "github:willfarrell/1auth",
45
- "directory": "packages/account-username"
46
- },
47
- "bugs": {
48
- "url": "https://github.com/willfarrell/1auth/issues"
49
- },
50
- "homepage": "https://github.com/willfarrell/1auth",
51
- "gitHead": "d21e1013f55ed05af4daf980bc4dfdfd52538792",
52
- "dependencies": {
53
- "@1auth/account": "0.0.0-alpha.9"
54
- }
2
+ "name": "@1auth/account-username",
3
+ "version": "0.0.0-beta.1",
4
+ "description": "",
5
+ "type": "module",
6
+ "engines": {
7
+ "node": ">=24"
8
+ },
9
+ "engineStrict": true,
10
+ "publishConfig": {
11
+ "access": "public"
12
+ },
13
+ "main": "./index.js",
14
+ "module": "./index.js",
15
+ "exports": {
16
+ ".": {
17
+ "import": {
18
+ "types": "./index.d.ts",
19
+ "default": "./index.js"
20
+ }
21
+ }
22
+ },
23
+ "types": "index.d.ts",
24
+ "files": [
25
+ "index.js",
26
+ "index.d.ts"
27
+ ],
28
+ "scripts": {
29
+ "test": "npm run test:unit",
30
+ "test:unit": "node --test"
31
+ },
32
+ "license": "MIT",
33
+ "funding": {
34
+ "type": "github",
35
+ "url": "https://github.com/sponsors/willfarrell"
36
+ },
37
+ "keywords": [],
38
+ "author": {
39
+ "name": "1auth contributors",
40
+ "url": "https://github.com/willfarrell/1auth/graphs/contributors"
41
+ },
42
+ "repository": {
43
+ "type": "git",
44
+ "url": "git+https://github.com/willfarrell/1auth.git",
45
+ "directory": "packages/account-username"
46
+ },
47
+ "bugs": {
48
+ "url": "https://github.com/willfarrell/1auth/issues"
49
+ },
50
+ "homepage": "https://github.com/willfarrell/1auth",
51
+ "gitHead": "7a6c0fbb8ab71d6a2171e678697de9f237568431",
52
+ "dependencies": {
53
+ "@1auth/account": "0.0.0-beta.1"
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.