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

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/package.json +3 -3
  2. package/README.md +0 -38
  3. package/index.js +0 -122
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@1auth/account-username",
3
- "version": "0.0.0-alpha.60",
3
+ "version": "0.0.0-alpha.63",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "engines": {
@@ -41,7 +41,7 @@
41
41
  },
42
42
  "repository": {
43
43
  "type": "git",
44
- "url": "github:willfarrell/1auth",
44
+ "url": "git+https://github.com/willfarrell/1auth.git",
45
45
  "directory": "packages/account-username"
46
46
  },
47
47
  "bugs": {
@@ -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.60"
53
+ "@1auth/account": "0.0.0-alpha.63"
54
54
  }
55
55
  }
package/README.md DELETED
@@ -1,38 +0,0 @@
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 DELETED
@@ -1,122 +0,0 @@
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
- maxLength: 32
17
- }
18
- export default (params) => {
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
- }
25
- }
26
-
27
- export const exists = async (username) => {
28
- const usernameSanitized = sanitize(username)
29
- const usernameDigest = createSeasonedDigest(usernameSanitized)
30
- return options.store.exists(options.table, {
31
- digest: usernameDigest
32
- })
33
- }
34
-
35
- export const lookup = async (username) => {
36
- const usernameSanitized = sanitize(username)
37
- const usernameDigest = createSeasonedDigest(usernameSanitized)
38
-
39
- let item = await options.store.select(options.table, {
40
- digest: usernameDigest
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
52
- }
53
-
54
- export const create = async (sub, username) => {
55
- const usernameSanitized = sanitize(username)
56
- const usernameValidate = validate(usernameSanitized)
57
- if (usernameValidate !== true) {
58
- throw new Error(usernameValidate, {
59
- cause: { username, usernameSanitized }
60
- })
61
- }
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 } })
68
- }
69
-
70
- await accountUpdate(sub, {
71
- username,
72
- digest: usernameDigest
73
- })
74
- }
75
-
76
- export const update = async (sub, username) => {
77
- await create(sub, username)
78
- await options.notify.trigger('account-username-change', sub)
79
- }
80
-
81
- export const recover = async (sub) => {
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, '')
92
- }
93
-
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
107
- }
108
-
109
- export const validateAllowedChar = (value) => {
110
- // TODO URL encode compare
111
- if (!options.allowedCharRegExp.test(value)) {
112
- return '400 Bad Request'
113
- }
114
- return true
115
- }
116
-
117
- export const validateBlacklist = (value) => {
118
- if (usernameBlacklistRegExp?.test(value)) {
119
- return '409 Conflict'
120
- }
121
- return true
122
- }