@1auth/account-username 0.0.0-alpha.32 → 0.0.0-alpha.34

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/README.md +38 -0
  2. package/index.js +72 -35
  3. package/package.json +5 -5
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,83 +1,120 @@
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
+ import { createEncryptedDigest, symetricDecryptFields } from '@1auth/crypto'
7
8
 
8
9
  // Only allow characters that are safe to encode
9
10
  // . 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
-
11
+ let usernameBlacklistRegExp
18
12
  const options = {
19
13
  id: 'username',
20
- blacklist: []
21
- // minLength: 1,
22
- // maxLength: 32
14
+ allowedCharRegExp: /^[a-z0-9_-]*$/,
15
+ usernameBlacklist: [],
16
+ maxLength: 32
23
17
  }
24
18
  export default (params) => {
25
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
+ }
26
25
  }
27
26
 
28
27
  export const exists = async (username) => {
29
- const usernameSanitized = __sanitize(username)
28
+ const usernameSanitized = sanitize(username)
29
+ const usernameDigest = createEncryptedDigest(usernameSanitized)
30
30
  return options.store.exists(options.table, {
31
- digest: await createDigest(usernameSanitized)
31
+ digest: usernameDigest
32
32
  })
33
33
  }
34
34
 
35
35
  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)
36
+ const usernameSanitized = sanitize(username)
37
+ const usernameDigest = createEncryptedDigest(usernameSanitized)
38
+
39
+ let item = await options.store.select(options.table, {
40
+ digest: usernameDigest
40
41
  })
42
+ if (!item) return
43
+ // must match @1auth/account
44
+ item = symetricDecryptFields(
45
+ item,
46
+ { encryptedKey: item.encryptionKey, sub: item.sub },
47
+ options.encryptedFields
48
+ )
49
+ delete item.encryptionKey
50
+ delete item.privateKey
51
+ return item
41
52
  }
42
53
 
43
54
  export const create = async (sub, username) => {
44
- const usernameSanitized = __sanitize(username)
45
- if (!__validate(usernameSanitized)) {
46
- 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} invalid username`)
47
59
  }
48
- if (!__blacklist(usernameSanitized) || (await exists(usernameSanitized))) {
60
+ const usernameDigest = createEncryptedDigest(usernameSanitized)
61
+ const usernameExists = await options.store.exists(options.table, {
62
+ digest: usernameDigest
63
+ })
64
+ if (usernameExists) {
49
65
  throw new Error('409 Conflict')
50
66
  }
67
+
51
68
  await accountUpdate(sub, {
52
69
  username,
53
- digest: await createDigest(usernameSanitized)
70
+ digest: usernameDigest
54
71
  })
55
72
  }
56
73
 
57
74
  export const update = async (sub, username) => {
58
75
  await create(sub, username)
59
- await options.notify.trigger('account-username-change')
76
+ await options.notify.trigger('account-username-change', sub)
60
77
  }
61
78
 
62
79
  export const recover = async (sub) => {
63
- const { username } = await options.store.select(options.table, { sub })
64
- await options.notify.trigger('account-username-recover', { username })
80
+ const { username } = await accountLookup(sub)
81
+ await options.notify.trigger('account-username-recover', sub, { username })
82
+ }
83
+
84
+ export const sanitize = (value) => {
85
+ return value
86
+ .trim()
87
+ .toLocaleLowerCase()
88
+ .normalize('NFKD')
89
+ .replace(/\p{Diacritic}/gu, '')
65
90
  }
66
91
 
67
- export const __sanitize = (value) => {
68
- return value.trim().toLocaleLowerCase()
92
+ export const validate = (value) => {
93
+ let valid = true
94
+ if (valid === true) valid = validateLength(value)
95
+ if (valid === true) valid = validateAllowedChar(value)
96
+ if (valid === true) valid = validateBlacklist(value)
97
+ return valid
98
+ }
99
+
100
+ export const validateLength = (value) => {
101
+ if (value.length < 1 && options.maxLength < value.length) {
102
+ return '400'
103
+ }
104
+ return true
69
105
  }
70
106
 
71
- export const __validate = (value) => {
72
- if (!regexp.test(value)) {
73
- return false
107
+ export const validateAllowedChar = (value) => {
108
+ // TODO URL encode compare
109
+ if (!options.allowedCharRegExp.test(value)) {
110
+ return '400'
74
111
  }
75
112
  return true
76
113
  }
77
114
 
78
- export const __blacklist = (value) => {
79
- if (options.blacklist.includes(value)) {
80
- return false
115
+ export const validateBlacklist = (value) => {
116
+ if (usernameBlacklistRegExp?.test(value)) {
117
+ return '409'
81
118
  }
82
119
  return true
83
120
  }
package/package.json CHANGED
@@ -1,10 +1,10 @@
1
1
  {
2
2
  "name": "@1auth/account-username",
3
- "version": "0.0.0-alpha.32",
3
+ "version": "0.0.0-alpha.34",
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": "3750bef3d7e376c48f7d680e5f2181ee809213b9",
51
+ "gitHead": "c88105a99efd7f3de80795736d6194e52ef465b4",
52
52
  "dependencies": {
53
- "@1auth/account": "0.0.0-alpha.32"
53
+ "@1auth/account": "0.0.0-alpha.34"
54
54
  }
55
55
  }