@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.
- package/index.js +84 -79
- package/package.json +53 -53
package/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
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
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
13
|
+
id: "username",
|
|
14
|
+
allowedCharRegExp: /^[a-z0-9_-]*$/,
|
|
15
|
+
usernameBlacklist: [],
|
|
16
|
+
minLength: 1,
|
|
17
|
+
maxLength: 32,
|
|
18
18
|
};
|
|
19
19
|
export default (params) => {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
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
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
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
|
-
|
|
38
|
-
|
|
37
|
+
const usernameSanitized = sanitize(username);
|
|
38
|
+
const usernameDigest = createSeasonedDigest(usernameSanitized);
|
|
39
39
|
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
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
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
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
|
-
|
|
72
|
-
|
|
73
|
-
|
|
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
|
-
|
|
79
|
-
|
|
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
|
-
|
|
84
|
-
|
|
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
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
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
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
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
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
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
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
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
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
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
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
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
|
}
|