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

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 (2) hide show
  1. package/index.js +84 -79
  2. package/package.json +53 -53
package/index.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import {
2
- getOptions as accountOptions,
3
- update as accountUpdate,
4
- lookup as accountLookup,
2
+ lookup as accountLookup,
3
+ getOptions as accountOptions,
4
+ update as accountUpdate,
5
5
  } from "@1auth/account";
6
6
 
7
7
  import { createSeasonedDigest, symmetricDecryptFields } from "@1auth/crypto";
@@ -10,114 +10,119 @@ import { createSeasonedDigest, symmetricDecryptFields } from "@1auth/crypto";
10
10
  // not allowed because it can be used to declare and extension
11
11
  let usernameBlacklistRegExp;
12
12
  const options = {
13
- id: "username",
14
- allowedCharRegExp: /^[a-z0-9_-]*$/,
15
- usernameBlacklist: [],
16
- minLength: 1,
17
- maxLength: 32,
13
+ id: "username",
14
+ allowedCharRegExp: /^[a-z0-9_-]*$/,
15
+ usernameBlacklist: [],
16
+ minLength: 1,
17
+ maxLength: 32,
18
18
  };
19
19
  export default (params) => {
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
- }
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
26
  };
27
27
 
28
28
  export const exists = async (username) => {
29
- const usernameSanitized = sanitize(username);
30
- const usernameDigest = createSeasonedDigest(usernameSanitized);
31
- return options.store.exists(options.table, {
32
- digest: usernameDigest,
33
- });
29
+ const usernameSanitized = sanitize(username);
30
+ const usernameDigest = createSeasonedDigest(usernameSanitized);
31
+ return options.store.exists(options.table, {
32
+ digest: usernameDigest,
33
+ });
34
34
  };
35
35
 
36
36
  export const lookup = async (username) => {
37
- const usernameSanitized = sanitize(username);
38
- const usernameDigest = createSeasonedDigest(usernameSanitized);
37
+ const usernameSanitized = sanitize(username);
38
+ const usernameDigest = createSeasonedDigest(usernameSanitized);
39
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;
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
+ item.encryptionKey = undefined;
51
+ return item;
53
52
  };
54
53
 
55
54
  export const create = async (sub, username) => {
56
- const usernameSanitized = sanitize(username);
57
- const usernameValidate = validate(usernameSanitized);
58
- if (usernameValidate !== true) {
59
- throw new Error(usernameValidate, {
60
- cause: { username, usernameSanitized },
61
- });
62
- }
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 } });
69
- }
55
+ if (!sub || typeof sub !== "string") {
56
+ throw new Error("401 Unauthorized", { cause: { sub } });
57
+ }
58
+ const usernameSanitized = sanitize(username);
59
+ const usernameValidate = validate(usernameSanitized);
60
+ if (usernameValidate !== true) {
61
+ throw new Error(usernameValidate, {
62
+ cause: { username, usernameSanitized },
63
+ });
64
+ }
65
+ const usernameDigest = createSeasonedDigest(usernameSanitized);
66
+ const usernameExists = await options.store.exists(options.table, {
67
+ digest: usernameDigest,
68
+ });
69
+ if (usernameExists) {
70
+ throw new Error("409 Conflict", { cause: { username, usernameSanitized } });
71
+ }
70
72
 
71
- await accountUpdate(sub, {
72
- username,
73
- digest: usernameDigest,
74
- });
73
+ await accountUpdate(sub, {
74
+ value: username,
75
+ digest: usernameDigest,
76
+ });
75
77
  };
76
78
 
77
79
  export const update = async (sub, username) => {
78
- await create(sub, username);
79
- await options.notify.trigger("account-username-change", sub);
80
+ await create(sub, username);
81
+ await options.notify.trigger("account-username-change", sub);
80
82
  };
81
83
 
82
84
  export const recover = async (sub) => {
83
- const { username } = await accountLookup(sub);
84
- await options.notify.trigger("account-username-recover", sub, { username });
85
+ const { value: username } = await accountLookup(sub);
86
+ await options.notify.trigger("account-username-recover", sub, { username });
85
87
  };
86
88
 
87
89
  export const sanitize = (value) => {
88
- return value
89
- .trim()
90
- .toLocaleLowerCase()
91
- .normalize("NFKD")
92
- .replace(/\p{Diacritic}/gu, "");
90
+ if (!value || typeof value !== "string") {
91
+ throw new Error("400 Bad Request", { cause: { value } });
92
+ }
93
+ return value
94
+ .trim()
95
+ .toLocaleLowerCase()
96
+ .normalize("NFKD")
97
+ .replace(/\p{Diacritic}/gu, "");
93
98
  };
94
99
 
95
100
  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
+ let valid = true;
102
+ if (valid === true) valid = validateLength(value);
103
+ if (valid === true) valid = validateAllowedChar(value);
104
+ if (valid === true) valid = validateBlacklist(value);
105
+ return valid;
101
106
  };
102
107
 
103
108
  export const validateLength = (value) => {
104
- if (value.length < options.minLength || options.maxLength < value.length) {
105
- return "400 Bad Request";
106
- }
107
- return true;
109
+ if (value.length < options.minLength || options.maxLength < value.length) {
110
+ return "400 Bad Request";
111
+ }
112
+ return true;
108
113
  };
109
114
 
110
115
  export const validateAllowedChar = (value) => {
111
- // TODO URL encode compare
112
- if (!options.allowedCharRegExp.test(value)) {
113
- return "400 Bad Request";
114
- }
115
- return true;
116
+ // TODO URL encode compare
117
+ if (!options.allowedCharRegExp.test(value)) {
118
+ return "400 Bad Request";
119
+ }
120
+ return true;
116
121
  };
117
122
 
118
123
  export const validateBlacklist = (value) => {
119
- if (usernameBlacklistRegExp?.test(value)) {
120
- return "409 Conflict";
121
- }
122
- return true;
124
+ if (usernameBlacklistRegExp?.test(value)) {
125
+ return "409 Conflict";
126
+ }
127
+ return true;
123
128
  };
package/package.json CHANGED
@@ -1,55 +1,55 @@
1
1
  {
2
- "name": "@1auth/account-username",
3
- "version": "0.0.0-alpha.70",
4
- "description": "",
5
- "type": "module",
6
- "engines": {
7
- "node": ">=20"
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-alpha.70"
54
- }
2
+ "name": "@1auth/account-username",
3
+ "version": "0.0.0-alpha.72",
4
+ "description": "",
5
+ "type": "module",
6
+ "engines": {
7
+ "node": ">=22"
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-alpha.72"
54
+ }
55
55
  }