@1auth/account-username 0.0.0-alpha.67 → 0.0.0-alpha.69

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 +123 -0
  2. package/package.json +2 -2
package/index.js ADDED
@@ -0,0 +1,123 @@
1
+ import {
2
+ getOptions as accountOptions,
3
+ update as accountUpdate,
4
+ lookup as accountLookup,
5
+ } from "@1auth/account";
6
+
7
+ import { createSeasonedDigest, symmetricDecryptFields } from "@1auth/crypto";
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;
12
+ const options = {
13
+ id: "username",
14
+ allowedCharRegExp: /^[a-z0-9_-]*$/,
15
+ usernameBlacklist: [],
16
+ minLength: 1,
17
+ maxLength: 32,
18
+ };
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
+ }
26
+ };
27
+
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
+ });
34
+ };
35
+
36
+ export const lookup = async (username) => {
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
+ };
54
+
55
+ 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
+ }
70
+
71
+ await accountUpdate(sub, {
72
+ username,
73
+ digest: usernameDigest,
74
+ });
75
+ };
76
+
77
+ export const update = async (sub, username) => {
78
+ await create(sub, username);
79
+ await options.notify.trigger("account-username-change", sub);
80
+ };
81
+
82
+ export const recover = async (sub) => {
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
+ };
94
+
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
+ };
109
+
110
+ 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
+ };
117
+
118
+ export const validateBlacklist = (value) => {
119
+ if (usernameBlacklistRegExp?.test(value)) {
120
+ return "409 Conflict";
121
+ }
122
+ return true;
123
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@1auth/account-username",
3
- "version": "0.0.0-alpha.67",
3
+ "version": "0.0.0-alpha.69",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "engines": {
@@ -50,6 +50,6 @@
50
50
  "homepage": "https://github.com/willfarrell/1auth",
51
51
  "gitHead": "7a6c0fbb8ab71d6a2171e678697de9f237568431",
52
52
  "dependencies": {
53
- "@1auth/account": "0.0.0-alpha.67"
53
+ "@1auth/account": "0.0.0-alpha.69"
54
54
  }
55
55
  }