@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.
- package/README.md +38 -0
- package/index.js +72 -35
- 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 {
|
|
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
|
-
|
|
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
|
-
|
|
21
|
-
|
|
22
|
-
|
|
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 =
|
|
28
|
+
const usernameSanitized = sanitize(username)
|
|
29
|
+
const usernameDigest = createEncryptedDigest(usernameSanitized)
|
|
30
30
|
return options.store.exists(options.table, {
|
|
31
|
-
digest:
|
|
31
|
+
digest: usernameDigest
|
|
32
32
|
})
|
|
33
33
|
}
|
|
34
34
|
|
|
35
35
|
export const lookup = async (username) => {
|
|
36
|
-
|
|
37
|
-
const
|
|
38
|
-
|
|
39
|
-
|
|
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 =
|
|
45
|
-
|
|
46
|
-
|
|
55
|
+
const usernameSanitized = sanitize(username)
|
|
56
|
+
const usernameValidate = validate(usernameSanitized)
|
|
57
|
+
if (usernameValidate !== true) {
|
|
58
|
+
throw new Error(`${usernameValidate} invalid username`)
|
|
47
59
|
}
|
|
48
|
-
|
|
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:
|
|
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
|
|
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
|
|
68
|
-
|
|
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
|
|
72
|
-
|
|
73
|
-
|
|
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
|
|
79
|
-
if (
|
|
80
|
-
return
|
|
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.
|
|
3
|
+
"version": "0.0.0-alpha.34",
|
|
4
4
|
"description": "",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"engines": {
|
|
7
|
-
"node": ">=
|
|
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": "
|
|
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": "
|
|
51
|
+
"gitHead": "c88105a99efd7f3de80795736d6194e52ef465b4",
|
|
52
52
|
"dependencies": {
|
|
53
|
-
"@1auth/account": "0.0.0-alpha.
|
|
53
|
+
"@1auth/account": "0.0.0-alpha.34"
|
|
54
54
|
}
|
|
55
55
|
}
|