@1auth/authn-recovery-codes 0.0.0-alpha.7 → 0.0.0-alpha.70

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