@1auth/account-username 0.0.0-alpha.6 → 0.0.0-alpha.60

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 (4) hide show
  1. package/README.md +38 -0
  2. package/index.js +78 -35
  3. package/package.json +5 -5
  4. package/LICENSE +0 -21
package/README.md ADDED
@@ -0,0 +1,38 @@
1
+ # @1auth/account-username
2
+ Extends `@1auth/account`
3
+
4
+ ## Getting started
5
+
6
+ **auth.js**
7
+ ```javascript
8
+ import accountUsername from "@1auth/account";
9
+
10
+ export {
11
+ create as accountUsernameCreate,
12
+ exists as accountUsernameExists,
13
+ lookup as accountUsernameLookup,
14
+ update as accountUsernameUpdate,
15
+ recover as accountUsernameRecover,
16
+ } from "@1auth/account-username";
17
+
18
+ account({ store, notify, encryptedFields: ["username", "privateKey"] });
19
+ accountUsername({
20
+ usernameBlacklist: ["admin"]
21
+ })
22
+ ```
23
+
24
+ ```javascript
25
+ import {accountUsernameCreate} from './auth.js'
26
+ await accountUsernameCreate(sub, username)
27
+ ```
28
+
29
+ ## Options
30
+
31
+ - `maxLength`: Max length of username. Defaults to `32`
32
+ - `usernameBlacklist`: Array of blacklisted words.
33
+
34
+ ## Database table
35
+
36
+
37
+ - `username`
38
+ - `digest`: Digest of `username`
package/index.js CHANGED
@@ -1,79 +1,122 @@
1
1
  import {
2
2
  getOptions as accountOptions,
3
- update as accountUpdate
3
+ update as accountUpdate,
4
+ lookup as accountLookup
4
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
13
  id: 'username',
18
- blacklist: ['admin', 'security']
14
+ allowedCharRegExp: /^[a-z0-9_-]*$/,
15
+ usernameBlacklist: [],
16
+ maxLength: 32
19
17
  }
20
18
  export default (params) => {
21
19
  Object.assign(options, accountOptions(), params)
20
+ if (options.usernameBlacklist.length) {
21
+ usernameBlacklistRegExp = new RegExp(
22
+ `(${options.usernameBlacklist.map((value) => value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')).join('|')})`
23
+ )
24
+ }
22
25
  }
23
26
 
24
27
  export const exists = async (username) => {
25
- const usernameSanitized = __sanitize(username)
28
+ const usernameSanitized = sanitize(username)
29
+ const usernameDigest = createSeasonedDigest(usernameSanitized)
26
30
  return options.store.exists(options.table, {
27
- digest: await createDigest(usernameSanitized)
31
+ digest: usernameDigest
28
32
  })
29
33
  }
30
34
 
31
35
  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
+ const usernameSanitized = sanitize(username)
37
+ const usernameDigest = createSeasonedDigest(usernameSanitized)
38
+
39
+ let item = await options.store.select(options.table, {
40
+ digest: usernameDigest
36
41
  })
42
+ if (!item) return
43
+ // must match @1auth/account
44
+ item = symmetricDecryptFields(
45
+ item,
46
+ { encryptedKey: item.encryptionKey, sub: item.sub },
47
+ options.encryptedFields
48
+ )
49
+ delete item.encryptionKey
50
+ delete item.privateKey
51
+ return item
37
52
  }
38
53
 
39
54
  export const create = async (sub, username) => {
40
- if (!__validate(username)) {
41
- throw new Error('400 invalid characters')
55
+ const usernameSanitized = sanitize(username)
56
+ const usernameValidate = validate(usernameSanitized)
57
+ if (usernameValidate !== true) {
58
+ throw new Error(usernameValidate, {
59
+ cause: { username, usernameSanitized }
60
+ })
42
61
  }
43
- if (!__blacklist(username) || (await exists(username))) {
44
- throw new Error('409 Conflict')
62
+ const usernameDigest = createSeasonedDigest(usernameSanitized)
63
+ const usernameExists = await options.store.exists(options.table, {
64
+ digest: usernameDigest
65
+ })
66
+ if (usernameExists) {
67
+ throw new Error('409 Conflict', { cause: { username, usernameSanitized } })
45
68
  }
46
- const usernameSanitized = __sanitize(username)
69
+
47
70
  await accountUpdate(sub, {
48
- username: usernameSanitized,
49
- digest: await createDigest(usernameSanitized)
71
+ username,
72
+ digest: usernameDigest
50
73
  })
51
74
  }
52
75
 
53
76
  export const update = async (sub, username) => {
54
77
  await create(sub, username)
55
- await options.notify.trigger('account-username-change')
78
+ await options.notify.trigger('account-username-change', sub)
56
79
  }
57
80
 
58
81
  export const recover = async (sub) => {
59
- const { username } = await options.store.select(options.table, { sub })
60
- await options.notify.trigger('account-username-recover', { username })
82
+ const { username } = await accountLookup(sub)
83
+ await options.notify.trigger('account-username-recover', sub, { username })
84
+ }
85
+
86
+ export const sanitize = (value) => {
87
+ return value
88
+ .trim()
89
+ .toLocaleLowerCase()
90
+ .normalize('NFKD')
91
+ .replace(/\p{Diacritic}/gu, '')
61
92
  }
62
93
 
63
- export const __sanitize = (value) => {
64
- return value.trim()
94
+ export const validate = (value) => {
95
+ let valid = true
96
+ if (valid === true) valid = validateLength(value)
97
+ if (valid === true) valid = validateAllowedChar(value)
98
+ if (valid === true) valid = validateBlacklist(value)
99
+ return valid
100
+ }
101
+
102
+ export const validateLength = (value) => {
103
+ if (value.length < 1 && options.maxLength < value.length) {
104
+ return '400 Bad Request'
105
+ }
106
+ return true
65
107
  }
66
108
 
67
- export const __validate = (value) => {
68
- if (!regexp.test(value)) {
69
- return false
109
+ export const validateAllowedChar = (value) => {
110
+ // TODO URL encode compare
111
+ if (!options.allowedCharRegExp.test(value)) {
112
+ return '400 Bad Request'
70
113
  }
71
114
  return true
72
115
  }
73
116
 
74
- export const __blacklist = (value) => {
75
- if (options.blacklist.includes(value)) {
76
- return false
117
+ export const validateBlacklist = (value) => {
118
+ if (usernameBlacklistRegExp?.test(value)) {
119
+ return '409 Conflict'
77
120
  }
78
121
  return true
79
122
  }
package/package.json CHANGED
@@ -1,10 +1,10 @@
1
1
  {
2
2
  "name": "@1auth/account-username",
3
- "version": "0.0.0-alpha.6",
3
+ "version": "0.0.0-alpha.60",
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": {
@@ -48,8 +48,8 @@
48
48
  "url": "https://github.com/willfarrell/1auth/issues"
49
49
  },
50
50
  "homepage": "https://github.com/willfarrell/1auth",
51
- "gitHead": "be4a52c6e52443e7b21d0f67cb6062ae9f6f069c",
51
+ "gitHead": "7a6c0fbb8ab71d6a2171e678697de9f237568431",
52
52
  "dependencies": {
53
- "@1auth/account": "0.0.0-alpha.6"
53
+ "@1auth/account": "0.0.0-alpha.60"
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.