@1auth/authn-recovery-codes 0.0.0-alpha.9 → 0.0.0-beta.1

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 +112 -51
  2. package/package.json +54 -54
  3. package/LICENSE +0 -21
package/index.js CHANGED
@@ -1,64 +1,125 @@
1
- import { recoveryCode } from '@1auth/crypto'
1
+ // Copyright 2003 - 2026 will Farrell, and 1Auth contributors.
2
+ // SPDX-License-Identifier: MIT
2
3
  import {
3
- options as authnOptions,
4
- create as authnCreate,
5
- authenticate as authnVerifyAuthentication
6
- } from '@1auth/authn'
4
+ authenticate as authnAuthenticate,
5
+ count as authnCount,
6
+ // create as authnCreate,
7
+ createList as authnCreateList,
8
+ getOptions as authnGetOptions,
9
+ //select as authnSelect,
10
+ list as authnList,
11
+ remove as authnRemove,
12
+ removeList as authnRemoveList,
13
+ } from "@1auth/authn";
14
+ import {
15
+ createSecretHash,
16
+ makeRandomConfigObject,
17
+ verifySecretHash,
18
+ } from "@1auth/crypto";
19
+
20
+ // aka lookup secret
21
+ const id = "recoveryCodes";
7
22
 
8
- const options = {
9
- id: 'recovery',
10
- count: 5
11
- }
12
- export default (params) => {
13
- Object.assign(options, authnOptions, { secret: recoveryCode }, params)
14
- }
23
+ export const secret = ({
24
+ type = "secret",
25
+ entropy = 112,
26
+ otp = true,
27
+ encode = (value) => createSecretHash(value),
28
+ decode = (value) => value,
29
+ verify = (value, hash) => verifySecretHash(hash, value),
30
+ ...params
31
+ } = {}) =>
32
+ makeRandomConfigObject({
33
+ id,
34
+ type,
35
+ entropy,
36
+ otp,
37
+ encode,
38
+ decode,
39
+ verify,
40
+ ...params,
41
+ });
42
+
43
+ const defaults = {
44
+ id,
45
+ secret: secret(),
46
+ count: 5,
47
+ };
48
+ const options = {};
49
+ export default (opt = {}) => {
50
+ Object.assign(options, authnGetOptions(), defaults, opt);
51
+ };
15
52
 
16
53
  export const authenticate = async (username, secret) => {
17
- const { sub } = await authnVerifyAuthentication(username, secret, options)
18
- return sub
19
- }
54
+ return await authnAuthenticate(options.secret, username, secret);
55
+ };
56
+
57
+ export const count = async (sub) => {
58
+ return await authnCount(options.secret, sub);
59
+ };
60
+
61
+ // export const select = async (sub, id) => {
62
+ // return await authnSelect(options.secret, sub, id);
63
+ // };
64
+
65
+ export const list = async (sub) => {
66
+ return await authnList(options.secret, sub);
67
+ };
20
68
 
21
69
  export const create = async (sub) => {
22
- const secrets = await createSecrets(sub)
23
- return secrets
24
- }
70
+ const secrets = await createSecrets(sub, options.count);
71
+ await options.notify.trigger("authn-recovery-codes-create", sub);
72
+ return secrets;
73
+ };
25
74
 
26
75
  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
- }
76
+ if (!sub || typeof sub !== "string") {
77
+ throw new Error("401 Unauthorized", { cause: { sub } });
78
+ }
79
+ const existingSecrets = await options.store.selectList(options.table, {
80
+ sub,
81
+ type: `${options.secret.id}-${options.secret.type}`,
82
+ });
83
+ const secrets = await createSecrets(sub, options.count);
84
+
85
+ const id = existingSecrets.map((item) => item.id);
86
+ await authnRemoveList(options.secret, sub, id);
31
87
 
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)
36
- }
37
- } */
88
+ await options.notify.trigger("authn-recovery-codes-update", sub);
89
+ return secrets;
90
+ };
38
91
 
39
- const createSecrets = async (sub) => {
40
- const existingSecrets = await options.store.selectList(options.table, {
41
- 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
- }
92
+ export const remove = async (sub, id) => {
93
+ if (id) {
94
+ await authnRemove(options.secret, sub, id);
95
+ } else {
96
+ if (!sub || typeof sub !== "string") {
97
+ throw new Error("401 Unauthorized", { cause: { sub } });
98
+ }
99
+ const ids = await options.store
100
+ .selectList(options.table, {
101
+ sub,
102
+ type: `${options.id}-${options.secret.type}`,
103
+ })
104
+ .then((res) => res.map((item) => item.id));
105
+ await authnRemoveList(options.secret, sub, ids);
106
+ }
54
107
 
55
- for (const item of existingSecrets) {
56
- options.store.remove(options.table, { id: item.id, sub })
57
- }
108
+ await options.notify.trigger("authn-recovery-codes-remove", sub);
109
+ };
58
110
 
59
- return secrets
60
- }
111
+ const createSecrets = async (sub, count = options.count) => {
112
+ const secrets = [];
113
+ const now = nowInSeconds();
114
+ for (let i = count; i--; ) {
115
+ const secret = await options.secret.create();
116
+ secrets.push({
117
+ value: secret,
118
+ verify: now,
119
+ });
120
+ }
121
+ await authnCreateList(options.secret, sub, secrets);
122
+ return secrets;
123
+ };
61
124
 
62
- export const list = async (sub, type = options.id + '-secret') => {
63
- return options.store.selectList(options.table, { sub, type })
64
- }
125
+ const nowInSeconds = () => Math.floor(Date.now() / 1000);
package/package.json CHANGED
@@ -1,56 +1,56 @@
1
1
  {
2
- "name": "@1auth/authn-recovery-codes",
3
- "version": "0.0.0-alpha.9",
4
- "description": "",
5
- "type": "module",
6
- "engines": {
7
- "node": ">=16"
8
- },
9
- "engineStrict": true,
10
- "publishConfig": {
11
- "access": "public"
12
- },
13
- "main": "./index.js",
14
- "module": "./index.js",
15
- "exports": {
16
- ".": {
17
- "import": {
18
- "types": "./index.d.ts",
19
- "default": "./index.js"
20
- }
21
- }
22
- },
23
- "types": "index.d.ts",
24
- "files": [
25
- "index.js",
26
- "index.d.ts"
27
- ],
28
- "scripts": {
29
- "test": "npm run test:unit",
30
- "test:unit": "ava"
31
- },
32
- "license": "MIT",
33
- "funding": {
34
- "type": "github",
35
- "url": "https://github.com/sponsors/willfarrell"
36
- },
37
- "keywords": [],
38
- "author": {
39
- "name": "1auth contributors",
40
- "url": "https://github.com/willfarrell/1auth/graphs/contributors"
41
- },
42
- "repository": {
43
- "type": "git",
44
- "url": "github:willfarrell/1auth",
45
- "directory": "packages/authn-recovery-codes"
46
- },
47
- "bugs": {
48
- "url": "https://github.com/willfarrell/1auth/issues"
49
- },
50
- "homepage": "https://github.com/willfarrell/1auth",
51
- "gitHead": "d21e1013f55ed05af4daf980bc4dfdfd52538792",
52
- "dependencies": {
53
- "@1auth/authn": "0.0.0-alpha.9",
54
- "@1auth/crypto": "0.0.0-alpha.9"
55
- }
2
+ "name": "@1auth/authn-recovery-codes",
3
+ "version": "0.0.0-beta.1",
4
+ "description": "",
5
+ "type": "module",
6
+ "engines": {
7
+ "node": ">=24"
8
+ },
9
+ "engineStrict": true,
10
+ "publishConfig": {
11
+ "access": "public"
12
+ },
13
+ "main": "./index.js",
14
+ "module": "./index.js",
15
+ "exports": {
16
+ ".": {
17
+ "import": {
18
+ "types": "./index.d.ts",
19
+ "default": "./index.js"
20
+ }
21
+ }
22
+ },
23
+ "types": "index.d.ts",
24
+ "files": [
25
+ "index.js",
26
+ "index.d.ts"
27
+ ],
28
+ "scripts": {
29
+ "test": "npm run test:unit",
30
+ "test:unit": "node --test"
31
+ },
32
+ "license": "MIT",
33
+ "funding": {
34
+ "type": "github",
35
+ "url": "https://github.com/sponsors/willfarrell"
36
+ },
37
+ "keywords": [],
38
+ "author": {
39
+ "name": "1auth contributors",
40
+ "url": "https://github.com/willfarrell/1auth/graphs/contributors"
41
+ },
42
+ "repository": {
43
+ "type": "git",
44
+ "url": "git+https://github.com/willfarrell/1auth.git",
45
+ "directory": "packages/authn-recovery-codes"
46
+ },
47
+ "bugs": {
48
+ "url": "https://github.com/willfarrell/1auth/issues"
49
+ },
50
+ "homepage": "https://github.com/willfarrell/1auth",
51
+ "gitHead": "7a6c0fbb8ab71d6a2171e678697de9f237568431",
52
+ "dependencies": {
53
+ "@1auth/authn": "0.0.0-beta.1",
54
+ "@1auth/crypto": "0.0.0-beta.1"
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.