@1auth/authn-recovery-codes 0.0.0-alpha.67 → 0.0.0-alpha.69

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 +132 -0
  2. package/package.json +3 -3
package/index.js ADDED
@@ -0,0 +1,132 @@
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,
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
+ };
70
+
71
+ export const authenticate = async (username, secret) => {
72
+ return await authnAuthenticate(options.secret, username, secret);
73
+ };
74
+
75
+ 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;
82
+ };
83
+
84
+ 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;
99
+ };
100
+
101
+ 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);
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
+ };
131
+
132
+ const nowInSeconds = () => Math.floor(Date.now() / 1000);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@1auth/authn-recovery-codes",
3
- "version": "0.0.0-alpha.67",
3
+ "version": "0.0.0-alpha.69",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "engines": {
@@ -50,7 +50,7 @@
50
50
  "homepage": "https://github.com/willfarrell/1auth",
51
51
  "gitHead": "7a6c0fbb8ab71d6a2171e678697de9f237568431",
52
52
  "dependencies": {
53
- "@1auth/authn": "0.0.0-alpha.67",
54
- "@1auth/crypto": "0.0.0-alpha.67"
53
+ "@1auth/authn": "0.0.0-alpha.69",
54
+ "@1auth/crypto": "0.0.0-alpha.69"
55
55
  }
56
56
  }