Skip to content

Commit 436c8c8

Browse files
committed
feat: add numberOfSecretBytes parameter to Payload and adjust related functionality
Added optional numberOfSecretBytes parameter to the Payload interface to specify the number of authenticator secret bytes (default: 20). Added the numberOfSecretBytes parameter in the config to use the value from the Payload or fallback to the default. Adjusted and added corresponding test cases to validate the new parameter and ensure compatibility with existing behavior. This change provides greater flexibility in configuring the number of secret bytes for the authenticator while maintaining backward compatibility.
1 parent c27d565 commit 436c8c8

File tree

5 files changed

+10
-5
lines changed

5 files changed

+10
-5
lines changed

lib/interfaces.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ export interface Payload {
22
name: string;
33
account: string;
44
counter: number | undefined;
5+
numberOfSecretBytes: number | undefined;
56
}

lib/secret.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import type { Strategy } from "./types.js";
1010
* @param {string} payload.name - The name of the application or service (displayed in the authenticator app).
1111
* @param {string} payload.account - The account identifier (e.g., email or username) associated with the user.
1212
* @param {number} [payload.counter] - The counter value, required only for HOTP. Defaults to `0` if not provided and the type is "HOTP".
13+
* @param {number} [payload.numberOfSecretBytes] - Number of authenticator secret bytes default is 20.
1314
* @param {Strategy} [strategy="TOTP"] - The type of OTP to generate. Defaults to "TOTP" (Time-based OTP). Use "HOTP" for counter-based OTP.
1415
*
1516
* @returns {Promise<{ secret: string, uri: string, qr: string }>} - A Promise that resolves to an object containing:
@@ -47,10 +48,11 @@ export async function generateSecret(
4748
const config = {
4849
name: encodeURIComponent(payload?.name ?? "App"),
4950
account: payload.account ? encodeURIComponent(`:${payload.account}`) : "",
50-
count: strategy === "HOTP" ? (payload.counter ?? 0).toString() : undefined,
51+
counter: strategy === "HOTP" ? (payload.counter ?? 0).toString() : undefined,
52+
numberOfBytes: payload.numberOfSecretBytes ?? 20
5153
} as const;
5254

53-
const secret = authenticator.generateSecret(20);
55+
const secret = authenticator.generateSecret(config.numberOfBytes);
5456

5557
const uri =
5658
strategy === "TOTP"
@@ -62,8 +64,8 @@ export async function generateSecret(
6264
name: config.name,
6365
};
6466

65-
if (strategy === "HOTP" && config.count) {
66-
params.counter = config.count;
67+
if (strategy === "HOTP" && config.counter) {
68+
params.counter = config.counter;
6769
}
6870

6971
uri.search = new URLSearchParams(params).toString();

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "2fa-node",
3-
"version": "0.0.5",
3+
"version": "0.0.6",
44
"main": "dist/index.js",
55
"types": "dist/index.d.ts",
66
"files": [

tests/hotp.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const { secret } = await generateSecret({
66
name: "App",
77
account: "exemple@exemple.com",
88
counter: 0,
9+
numberOfSecretBytes: 20
910
});
1011

1112
const counter = 0;

tests/totp.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const { secret } = await generateSecret(
77
name: "App",
88
account: "exemple@exemple.com",
99
counter: 0,
10+
numberOfSecretBytes: 20
1011
},
1112
"HOTP",
1213
);

0 commit comments

Comments
 (0)