@1auth/authn-recovery-codes 0.0.0-alpha.4 → 0.0.0-alpha.40

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