@1auth/authn-recovery-codes 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 (3) hide show
  1. package/index.js +90 -37
  2. package/package.json +6 -6
  3. package/LICENSE +0 -21
package/index.js CHANGED
@@ -1,64 +1,117 @@
1
- import { recoveryCode } from '@1auth/crypto'
2
1
  import {
3
- options as authnOptions,
4
- create as authnCreate,
5
- authenticate as authnVerifyAuthentication
2
+ charactersAlphaNumeric,
3
+ entropyToCharacterLength,
4
+ randomAlphaNumeric,
5
+ createSecretHash,
6
+ verifySecretHash
7
+ } from '@1auth/crypto'
8
+ import {
9
+ getOptions as authnGetOptions,
10
+ count as authnCount,
11
+ list as authnList,
12
+ // create as authnCreate,
13
+ createList as authnCreateList,
14
+ authenticate as authnAuthenticate,
15
+ remove as authnRemove
6
16
  } from '@1auth/authn'
7
17
 
8
- const options = {
9
- id: 'recovery',
18
+ // aka lookup secret
19
+ const id = 'recoveryCodes'
20
+
21
+ const secret = {
22
+ id,
23
+ type: 'secret',
24
+ minLength: entropyToCharacterLength(112, charactersAlphaNumeric.length),
25
+ otp: true,
26
+ create: async () => randomAlphaNumeric(secret.minLength),
27
+ encode: async (value) => createSecretHash(value),
28
+ decode: async (value) => value,
29
+ verify: async (value, hash) => verifySecretHash(hash, value)
30
+ }
31
+
32
+ const defaults = {
33
+ id,
34
+ secret,
10
35
  count: 5
11
36
  }
12
- export default (params) => {
13
- Object.assign(options, authnOptions, { secret: recoveryCode }, params)
37
+ const options = {}
38
+ export default (opt = {}) => {
39
+ Object.assign(options, authnGetOptions(), defaults, opt)
40
+ }
41
+
42
+ export const count = async (sub) => {
43
+ if (options.log) {
44
+ options.log('@1auth/authn-recovery-codes count(', sub, ')')
45
+ }
46
+ return await authnCount(options.secret, sub)
47
+ }
48
+
49
+ export const list = async (sub) => {
50
+ if (options.log) {
51
+ options.log('@1auth/authn-recovery-codes list(', sub, ')')
52
+ }
53
+ return await authnList(options.secret, sub)
14
54
  }
15
55
 
16
56
  export const authenticate = async (username, secret) => {
17
- const { sub } = await authnVerifyAuthentication(username, secret, options)
18
- return sub
57
+ return await authnAuthenticate(options.secret, username, secret)
19
58
  }
20
59
 
21
60
  export const create = async (sub) => {
22
- const secrets = await createSecrets(sub)
61
+ if (options.log) {
62
+ options.log('@1auth/authn-recovery-codes create(', sub, ')')
63
+ }
64
+ const secrets = await createSecrets(sub, options.count)
65
+ await options.notify.trigger('authn-recovery-codes-create', sub)
23
66
  return secrets
24
67
  }
25
68
 
26
69
  export const update = async (sub) => {
27
- const secrets = await createSecrets(sub)
70
+ if (options.log) {
71
+ options.log('@1auth/authn-recovery-codes update(', sub, ')')
72
+ }
73
+ const existingSecrets = await options.store.selectList(options.table, {
74
+ sub,
75
+ type: options.secret.id + '-' + options.secret.type
76
+ })
77
+ const secrets = await createSecrets(sub, options.count)
78
+
79
+ const id = existingSecrets.map((item) => item.id)
80
+ await authnRemove(options.secret, sub, id)
81
+
28
82
  await options.notify.trigger('authn-recovery-codes-update', sub)
29
83
  return secrets
30
84
  }
31
85
 
32
- /* export const verifySecret = async (sub, ids) => {
33
- if (!Array.isArray(ids)) ids = [ids]
34
- for (const id of ids) {
35
- await authnVerifySecret(sub, id, options)
86
+ export const remove = async (sub, id) => {
87
+ if (options.log) {
88
+ options.log('@1auth/authn-recovery-codes remove(', sub, id, ')')
36
89
  }
37
- } */
38
90
 
39
- const createSecrets = async (sub) => {
40
- const existingSecrets = await options.store.selectList(options.table, {
41
- sub,
42
- type: options.id + '-' + options.secret.type
43
- })
91
+ id ??= await options.store
92
+ .selectList(options.table, {
93
+ sub,
94
+ type: options.id + '-' + options.secret.type
95
+ })
96
+ .then((res) => res.map((item) => item.id))
97
+
98
+ await authnRemove(options.secret, sub, id)
99
+
100
+ await options.notify.trigger('authn-recovery-codes-remove', sub)
101
+ }
102
+
103
+ const createSecrets = async (sub, count = options.count) => {
44
104
  const secrets = []
45
- for (let i = options.count; i--;) {
105
+ const now = nowInSeconds()
106
+ for (let i = count; i--;) {
46
107
  const secret = await options.secret.create()
47
- const id = await authnCreate(
48
- options.secret.type,
49
- { sub, value: secret },
50
- options
51
- )
52
- secrets.push({ id, secret })
108
+ secrets.push({
109
+ value: secret,
110
+ verify: now
111
+ })
53
112
  }
54
-
55
- for (const item of existingSecrets) {
56
- options.store.remove(options.table, { id: item.id, sub })
57
- }
58
-
113
+ await authnCreateList(options.secret, sub, secrets)
59
114
  return secrets
60
115
  }
61
116
 
62
- export const list = async (sub, type = options.id + '-secret') => {
63
- return options.store.selectList(options.table, { sub, type })
64
- }
117
+ const nowInSeconds = () => Math.floor(Date.now() / 1000)
package/package.json CHANGED
@@ -1,10 +1,10 @@
1
1
  {
2
2
  "name": "@1auth/authn-recovery-codes",
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,9 +48,9 @@
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/authn": "0.0.0-alpha.6",
54
- "@1auth/crypto": "0.0.0-alpha.6"
53
+ "@1auth/authn": "0.0.0-alpha.60",
54
+ "@1auth/crypto": "0.0.0-alpha.60"
55
55
  }
56
56
  }
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.