@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.
- package/index.js +85 -94
- package/package.json +54 -54
package/index.js
CHANGED
|
@@ -1,132 +1,123 @@
|
|
|
1
1
|
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
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
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
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
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
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
|
-
|
|
42
|
-
|
|
43
|
-
|
|
42
|
+
id,
|
|
43
|
+
secret: secret(),
|
|
44
|
+
count: 5,
|
|
44
45
|
};
|
|
45
46
|
const options = {};
|
|
46
47
|
export default (opt = {}) => {
|
|
47
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
77
|
-
|
|
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
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
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
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
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
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
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
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
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
|
}
|