kron-otp 0.0.0-stage → 2.0.0

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Vib Tools (vibtoolshq)
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.
package/README.md CHANGED
@@ -1,3 +1,42 @@
1
- # Temporary Holding Version
1
+ # kron-otp (Node.js)
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ A small CommonJS TOTP engine for Node.js, maintained in the [KronOTP monorepo](https://github.com/vibtoolshq/KronOTP).
4
+
5
+ ## Install
6
+
7
+ ```sh
8
+ npm install kron-otp@2.0.0
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ ```js
14
+ const KronOTP = require('kron-otp');
15
+
16
+ const secret = process.env.TOTP_SECRET; // Base32 provisioning secret, not an OTP
17
+ if (!secret) throw new Error('TOTP_SECRET is required');
18
+
19
+ const token = KronOTP.generate(secret); // six-digit string
20
+ const valid = KronOTP.verify(secret, token);
21
+ console.log('Verification:', valid);
22
+ ```
23
+
24
+ - `generate(secret)` returns a six-digit string, or throws `TypeError` for a malformed Base32 secret.
25
+ - **v2.0.0 breaking change:** `verify(secret, token)` returns `true`/`false`, including `false` for expected invalid secrets. Previous v1.0.0 threw `TypeError` on invalid secrets. Update any consumer error-handling logic before upgrading.
26
+ - Configuration remains SHA-1, 6 digits and 30 seconds. **v2.0.0:** canonical ASCII Base32, >=14 decoded bytes (112 bits), <=4096 raw chars and <=1024 cleaned chars, only ASCII space/tab/CR/LF/FF/VT removed; `generate` rejects invalid keys with sanitized `TypeError`. Recommend cryptographically random >=20-byte secrets for provisioning.
27
+ - Supported Node.js: 20.19+ (dependency requirement).
28
+
29
+ ## Local tests
30
+
31
+ ```sh
32
+ npm ci
33
+ npm test
34
+ ```
35
+
36
+ Only use provisioning secrets for accounts you own or are authorized to automate. Keep secrets in a secret manager, do not log them, and enforce replay prevention and rate limits on the server.
37
+
38
+ See the [integration and publishing guide](../docs/AUTOMATION_AND_PUBLISHING.md).
39
+
40
+ Licensed under the [MIT License](./LICENSE).
41
+
42
+ **Migration from v1.0.0:** Short legacy keys with fewer than 14 decoded bytes now fail validation; re-provision weak credentials securely. The legacy v1.0.0 version is not overwritten. v2.0.0 includes the P01 input/contract updates. This library has no built-in rate limits or replay protection.
package/package.json CHANGED
@@ -1,6 +1,41 @@
1
1
  {
2
2
  "name": "kron-otp",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "2.0.0",
4
+ "description": "Lightweight TOTP generation and verification for Node.js.",
5
+ "license": "MIT",
6
+ "type": "commonjs",
7
+ "main": "src/kron-otp.js",
8
+ "files": [
9
+ "src/",
10
+ "README.md",
11
+ "LICENSE"
12
+ ],
13
+ "repository": {
14
+ "type": "git",
15
+ "url": "git+https://github.com/vibtoolshq/KronOTP.git",
16
+ "directory": "javascript"
17
+ },
18
+ "homepage": "https://github.com/vibtoolshq/KronOTP#readme",
19
+ "bugs": {
20
+ "url": "https://github.com/vibtoolshq/KronOTP/issues"
21
+ },
22
+ "keywords": [
23
+ "totp",
24
+ "2fa",
25
+ "otp",
26
+ "authentication",
27
+ "automation"
28
+ ],
29
+ "engines": {
30
+ "node": ">=20.19.0"
31
+ },
32
+ "scripts": {
33
+ "test": "node tests/test.js"
34
+ },
35
+ "dependencies": {
36
+ "otpauth": "^9.6.0"
37
+ },
38
+ "publishConfig": {
39
+ "access": "public"
40
+ }
41
+ }
@@ -0,0 +1,86 @@
1
+ 'use strict';
2
+
3
+ const OTPAuth = require('otpauth');
4
+
5
+ // Bound attacker-controlled input before normalization/decoding.
6
+ // 14 bytes matches PyOTP >=2.12; use random 20+ byte keys when provisioning.
7
+ const MIN_SECRET_BYTES = 14;
8
+ const MAX_RAW_SECRET_LENGTH = 4096;
9
+ const MAX_BASE32_LENGTH = 1024;
10
+ const INVALID_SECRET = 'Invalid TOTP secret: expected valid Base32 of an accepted size.';
11
+
12
+ class KronOTP {
13
+ static #createTOTP(secret) {
14
+ if (typeof secret !== 'string') {
15
+ throw new TypeError('TOTP secret must be a Base32 string.');
16
+ }
17
+ if (secret.length > MAX_RAW_SECRET_LENGTH) {
18
+ throw new TypeError(INVALID_SECRET);
19
+ }
20
+
21
+ // Only ASCII whitespace is ignored, identically in Python and Node.
22
+ // Check ASCII characters BEFORE uppercasing to avoid Unicode case folding.
23
+ const compact = secret.replace(/[ \t\n\r\f\v]+/g, '');
24
+ if (compact.length > MAX_BASE32_LENGTH || !/^[A-Za-z2-7=]+$/.test(compact)) {
25
+ throw new TypeError(INVALID_SECRET);
26
+ }
27
+
28
+ const cleaned = compact.toUpperCase();
29
+ const unpadded = cleaned.replace(/=+$/, '');
30
+ const padding = cleaned.length - unpadded.length;
31
+ const remainder = unpadded.length % 8;
32
+
33
+ const validBase32 =
34
+ /^[A-Z2-7]+$/.test(unpadded) &&
35
+ [0, 2, 4, 5, 7].includes(remainder) &&
36
+ (padding === 0 || (remainder !== 0 && padding === 8 - remainder));
37
+
38
+ if (!validBase32) {
39
+ throw new TypeError(INVALID_SECRET);
40
+ }
41
+
42
+ try {
43
+ const decoded = OTPAuth.Secret.fromBase32(unpadded);
44
+
45
+ // Enforce PyOTP's minimum and reject non-canonical unused padding bits.
46
+ if (decoded.bytes.length < MIN_SECRET_BYTES || decoded.base32 !== unpadded) {
47
+ throw new TypeError(INVALID_SECRET);
48
+ }
49
+
50
+ return new OTPAuth.TOTP({
51
+ algorithm: 'SHA1',
52
+ digits: 6,
53
+ period: 30,
54
+ secret: decoded,
55
+ });
56
+ } catch {
57
+ // Never include secret values or dependency internals in public errors.
58
+ throw new TypeError(INVALID_SECRET);
59
+ }
60
+ }
61
+
62
+ static generate(secret) {
63
+ return KronOTP.#createTOTP(secret).generate();
64
+ }
65
+
66
+ static verify(secret, token) {
67
+ // Invalid OTPs or expected invalid secrets are nonmatches in both runtimes.
68
+ if (typeof token !== 'string' || !/^[0-9]{6}$/.test(token)) {
69
+ return false;
70
+ }
71
+
72
+ let totp;
73
+ try {
74
+ totp = KronOTP.#createTOTP(secret);
75
+ } catch (error) {
76
+ if (error instanceof TypeError) return false;
77
+ throw error;
78
+ }
79
+
80
+ // Preserve existing SHA-1, 6 digits, 30-second window and stateless design.
81
+ return totp.validate({ token, window: 1 }) !== null;
82
+ }
83
+ }
84
+
85
+ module.exports = KronOTP;
86
+ module.exports.KronOTP = KronOTP;