kron-otp 0.0.0-stage → 1.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,40 @@
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
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
+ - `verify(secret, token)` returns `true` or `false` for a six-digit string, with ±one 30-second step tolerated. Invalid secrets throw `TypeError`.
26
+ - Configuration: SHA-1, 6 digits, 30-second period. Secrets are normalized by removing whitespace and uppercasing.
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).
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": "1.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,64 @@
1
+ 'use strict';
2
+
3
+ const OTPAuth = require('otpauth');
4
+
5
+ class KronOTP {
6
+ static #createTOTP(secret) {
7
+ if (typeof secret !== 'string') {
8
+ throw new TypeError('TOTP secret must be a Base32 string.');
9
+ }
10
+
11
+ // Authenticator setup keys may contain spaces, mixed case, or Base32 padding.
12
+ const cleaned = secret.replace(/\s+/g, '').toUpperCase();
13
+ const unpadded = cleaned.replace(/=+$/, '');
14
+ const padding = cleaned.length - unpadded.length;
15
+ const remainder = unpadded.length % 8;
16
+
17
+ const validBase32 =
18
+ /^[A-Z2-7]+$/.test(unpadded) &&
19
+ [0, 2, 4, 5, 7].includes(remainder) &&
20
+ (padding === 0 || (remainder !== 0 && padding === 8 - remainder));
21
+
22
+ if (!validBase32) {
23
+ throw new TypeError('Invalid TOTP secret: expected a valid Base32 string.');
24
+ }
25
+
26
+ try {
27
+ const decoded = OTPAuth.Secret.fromBase32(unpadded);
28
+
29
+ // Reject malformed encodings instead of silently accepting altered keys.
30
+ if (decoded.bytes.length === 0 || decoded.base32 !== unpadded) {
31
+ throw new Error('Non-canonical Base32 input.');
32
+ }
33
+
34
+ return new OTPAuth.TOTP({
35
+ algorithm: 'SHA1',
36
+ digits: 6,
37
+ period: 30,
38
+ secret: decoded,
39
+ });
40
+ } catch {
41
+ // Never include the secret or dependency error details in an exception.
42
+ throw new TypeError('Invalid TOTP secret: expected a valid Base32 string.');
43
+ }
44
+ }
45
+
46
+ static generate(secret) {
47
+ return KronOTP.#createTOTP(secret).generate();
48
+ }
49
+
50
+ static verify(secret, token) {
51
+ const totp = KronOTP.#createTOTP(secret);
52
+
53
+ // Preserve leading zeros: OTP tokens must be six-character strings.
54
+ if (typeof token !== 'string' || !/^\d{6}$/.test(token)) {
55
+ return false;
56
+ }
57
+
58
+ // Allow one 30-second time step of clock drift on either side.
59
+ return totp.validate({ token, window: 1 }) !== null;
60
+ }
61
+ }
62
+
63
+ module.exports = KronOTP;
64
+ module.exports.KronOTP = KronOTP;