@1auth/authn-recovery-codes 0.0.0-alpha.71 → 0.0.0-alpha.73

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