@1auth/authn-recovery-codes 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 (2) hide show
  1. package/index.js +86 -35
  2. package/package.json +6 -6
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/autn-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/autn-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/autn-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/autn-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/autn-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.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,9 +48,9 @@
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/authn": "0.0.0-alpha.32",
54
- "@1auth/crypto": "0.0.0-alpha.32"
53
+ "@1auth/authn": "0.0.0-alpha.34",
54
+ "@1auth/crypto": "0.0.0-alpha.34"
55
55
  }
56
56
  }