Skip to content

Commit c27d565

Browse files
committed
run format
1 parent 0d23979 commit c27d565

16 files changed

Lines changed: 226 additions & 108 deletions

File tree

.github/lock.yml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
---
2+
ignoreUnless: { { STALE_BOT } }
3+
---
4+
# Configuration for Lock Threads - https://github.com/dessant/lock-threads-app
5+
6+
# Number of days of inactivity before a closed issue or pull request is locked
7+
daysUntilLock: 60
8+
9+
# Skip issues and pull requests created before a given timestamp. Timestamp must
10+
# follow ISO 8601 (`YYYY-MM-DD`). Set to `false` to disable
11+
skipCreatedBefore: false
12+
13+
# Issues and pull requests with these labels will be ignored. Set to `[]` to disable
14+
exemptLabels: ["Type: Security"]
15+
16+
# Label to add before locking, such as `outdated`. Set to `false` to disable
17+
lockLabel: false
18+
19+
# Comment to post before locking. Set to `false` to disable
20+
lockComment: >
21+
This thread has been automatically locked since there has not been
22+
any recent activity after it was closed. Please open a new issue for
23+
related bugs.
24+
25+
# Assign `resolved` as the reason for locking. Set to `false` to disable
26+
setLockReason: false

.github/stale.yml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
---
2+
ignoreUnless: { { STALE_BOT } }
3+
---
4+
# Number of days of inactivity before an issue becomes stale
5+
daysUntilStale: 60
6+
7+
# Number of days of inactivity before a stale issue is closed
8+
daysUntilClose: 7
9+
10+
# Issues with these labels will never be considered stale
11+
exemptLabels:
12+
- "Type: Security"
13+
14+
# Label to use when marking an issue as stale
15+
staleLabel: "Status: Abandoned"
16+
17+
# Comment to post when marking an issue as stale. Set to `false` to disable
18+
markComment: >
19+
This issue has been automatically marked as stale because it has not had
20+
recent activity. It will be closed if no further activity occurs. Thank you
21+
for your contributions.
22+
23+
# Comment to post when closing a stale issue. Set to `false` to disable
24+
closeComment: false

.github/workflows/test.yml

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
name: test
2+
3+
on:
4+
- push
5+
- pull_request
6+
7+
jobs:
8+
lint:
9+
runs-on: ubuntu-latest
10+
steps:
11+
- uses: actions/checkout@v4
12+
- name: Install
13+
run: npm install
14+
- name: Run lint
15+
run: npm run lint
16+
17+
typecheck:
18+
runs-on: ubuntu-latest
19+
steps:
20+
- uses: actions/checkout@v4
21+
- name: Install
22+
run: npm install
23+
- name: Run typecheck
24+
run: npm run typecheck
25+
26+
tests:
27+
runs-on: ${{ matrix.os }}
28+
strategy:
29+
matrix:
30+
os: [ubuntu-latest, windows-latest]
31+
node-version:
32+
- 20.10.0
33+
- 21.x
34+
steps:
35+
- uses: actions/checkout@v4
36+
- name: Use Node.js ${{ matrix.node-version }}
37+
uses: actions/setup-node@v1
38+
with:
39+
node-version: ${{ matrix.node-version }}
40+
- name: Install
41+
run: npm install
42+
- name: Run tests
43+
run: npm test
44+
windows:
45+
runs-on: windows-latest
46+
strategy:
47+
matrix:
48+
node-version:
49+
- 20.10.0
50+
- 21.x
51+
steps:
52+
- uses: actions/checkout@v2
53+
- name: Use Node.js ${{ matrix.node-version }}
54+
uses: actions/setup-node@v1
55+
with:
56+
node-version: ${{ matrix.node-version }}
57+
- name: Install
58+
run: npm install
59+
- name: Run tests
60+
run: npm test

README.md

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -27,42 +27,42 @@ This function generates a secret key, a URI, and a QR code for 2FA integration.
2727
### Generate TOTP Secret
2828

2929
```typescript
30-
import { generateSecret } from '2fa-node';
30+
import { generateSecret } from "2fa-node";
3131

3232
const payload = {
33-
name: 'MyApp', // Application name
34-
account: '[email protected]', // User account (email or username)
33+
name: "MyApp", // Application name
34+
account: "[email protected]", // User account (email or username)
3535
};
3636

37-
const secret = await generateSecret(payload)
37+
const secret = await generateSecret(payload);
3838

39-
console.log(secret)
39+
console.log(secret);
4040
```
4141

4242
### Generate HOTP Secret
4343

4444
```typescript
45-
import { generateSecret } from '2fa-node';
45+
import { generateSecret } from "2fa-node";
4646

4747
const options = {
48-
name: 'MyApp', // Application name
49-
account: '[email protected]', // User account (email or username)
50-
counter: 0 // ONLY WITH HOTP
48+
name: "MyApp", // Application name
49+
account: "[email protected]", // User account (email or username)
50+
counter: 0, // ONLY WITH HOTP
5151
};
5252

53-
const secret = await generateSecret(option, "HOTP")
53+
const secret = await generateSecret(option, "HOTP");
5454

55-
console.log(secret)
55+
console.log(secret);
5656
```
5757

5858
### Generate TOTP Token
5959

6060
This function generates a time-based one-time password (TOTP) using the provided secret key.
6161

6262
```typescript
63-
import { generateToken } from '2fa-node';
63+
import { generateToken } from "2fa-node";
6464

65-
const secret = 'JBSWY3DPEHPK3PXP'; // Your pre-generated secret
65+
const secret = "JBSWY3DPEHPK3PXP"; // Your pre-generated secret
6666
const result = generateToken(secret);
6767

6868
console.log(result.token); // The generated TOTP token
@@ -73,10 +73,10 @@ console.log(result.token); // The generated TOTP token
7373
This function verifies if a provided TOTP token is valid for the given secret key.
7474

7575
```typescript
76-
import { verifyToken } from '2fa-node';
76+
import { verifyToken } from "2fa-node";
7777

78-
const secret = 'JBSWY3DPEHPK3PXP'; // The secret key used for verification
79-
const token = '123456'; // The token to verify
78+
const secret = "JBSWY3DPEHPK3PXP"; // The secret key used for verification
79+
const token = "123456"; // The token to verify
8080

8181
const isValid = verifyToken(secret, token);
8282

@@ -88,9 +88,9 @@ console.log(isValid); // true if valid, false if invalid
8888
This function generates a HMAC-based one-time password (HOTP) token based on the provided secret key and counter value.
8989

9090
```typescript
91-
import { generateHOTPToken } from '2fa-node';
91+
import { generateHOTPToken } from "2fa-node";
9292

93-
const secret = 'JBSWY3DPEHPK3PXP'; // Your pre-generated secret
93+
const secret = "JBSWY3DPEHPK3PXP"; // Your pre-generated secret
9494
const counter = 1; // Optional counter (defaults to 0)
9595

9696
const result = generateHOTPToken(secret, counter);
@@ -103,10 +103,10 @@ console.log(result.token); // The generated HOTP token
103103
This function verifies if a provided HOTP token is valid for the given secret key and counter value.
104104

105105
```typescript
106-
import { verifyHOTPToken } from '2fa-node';
106+
import { verifyHOTPToken } from "2fa-node";
107107

108-
const secret = 'JBSWY3DPEHPK3PXP'; // The secret key used for verification
109-
const token = '123456'; // The token to verify
108+
const secret = "JBSWY3DPEHPK3PXP"; // The secret key used for verification
109+
const token = "123456"; // The token to verify
110110
const counter = 1; // The counter associated with the token
111111

112112
const isValid = verifyHOTPToken(secret, token, counter);
@@ -117,6 +117,7 @@ console.log(isValid); // true if valid, false if invalid
117117
## API Documentation
118118

119119
### `generateSecret(options: Options): Promise<{ secret: string, uri: string, qr: string }>`
120+
120121
Generates a secret key, URI, and QR code for 2FA integration.
121122

122123
- **Parameters**:
@@ -129,13 +130,15 @@ Generates a secret key, URI, and QR code for 2FA integration.
129130
- `qr`: The Data URL for the QR code.
130131

131132
### `generateToken(secret: string): { token: string } | null`
133+
132134
Generates a TOTP token.
133135

134136
- **Parameters**:
135137
- `secret`: The secret key used for generating the TOTP token.
136138
- **Returns**: An object containing the generated token, or `null` if the secret is invalid or missing.
137139

138140
### `verifyToken(secret: string, token?: string): boolean | null`
141+
139142
Verifies the validity of a TOTP token.
140143

141144
- **Parameters**:
@@ -144,6 +147,7 @@ Verifies the validity of a TOTP token.
144147
- **Returns**: `true` if the token is valid, `false` if it's invalid, or `null` if the token is missing.
145148

146149
### `generateHOTPToken(secret: string, counter: number = 0): { token: string } | null`
150+
147151
Generates an HOTP token.
148152

149153
- **Parameters**:
@@ -152,6 +156,7 @@ Generates an HOTP token.
152156
- **Returns**: An object containing the generated token, or `null` if the secret is invalid or missing.
153157

154158
### `verifyHOTPToken(secret: string, token?: string, counter: number = 0): boolean | null`
159+
155160
Verifies the validity of an HOTP token.
156161

157162
- **Parameters**:

bin/test.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import { configure, processCLIArgs, run } from '@japa/runner'
2-
import { assert } from '@japa/assert'
3-
import { expectTypeOf } from '@japa/expect-type'
4-
import { expect } from '@japa/expect'
1+
import { configure, processCLIArgs, run } from "@japa/runner";
2+
import { assert } from "@japa/assert";
3+
import { expectTypeOf } from "@japa/expect-type";
4+
import { expect } from "@japa/expect";
55

6-
processCLIArgs(process.argv.splice(2))
6+
processCLIArgs(process.argv.splice(2));
77
configure({
8-
files: ['tests/**/*.spec.ts'],
8+
files: ["tests/**/*.spec.ts"],
99
plugins: [assert(), expect(), expectTypeOf()],
10-
})
10+
});
1111

12-
run()
12+
run();

eslint.config.mjs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
import globals from 'globals';
2-
import pluginJs from '@eslint/js';
3-
import tseslint from 'typescript-eslint';
4-
1+
import globals from "globals";
2+
import pluginJs from "@eslint/js";
3+
import tseslint from "typescript-eslint";
54

65
/** @type {import('eslint').Linter.Config[]} */
76
export default [
8-
{files: ['**/*.{js,mjs,cjs,ts}']},
9-
{languageOptions: { globals: globals.browser }},
7+
{ files: ["**/*.{js,mjs,cjs,ts}"] },
8+
{ languageOptions: { globals: globals.browser } },
109
pluginJs.configs.recommended,
1110
...tseslint.configs.recommended,
12-
];
11+
];

lib/hotp.ts

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { hotp } from 'otplib'
1+
import { hotp } from "otplib";
22

33
/**
44
* Generates a HMAC-based one-time password (HOTP) token based on the provided secret and counter.
@@ -16,10 +16,13 @@ import { hotp } from 'otplib'
1616
* const result = generateHOTPToken(secret, counter);
1717
* console.log(result.token); // The generated HOTP token
1818
*/
19-
export function generateHOTPToken(secret: string, counter: number = 0): { token: string } | null {
20-
if (!secret || !secret.length) return null
21-
const token = hotp.generate(secret, counter!)
22-
return { token: token }
19+
export function generateHOTPToken(
20+
secret: string,
21+
counter: number = 0,
22+
): { token: string } | null {
23+
if (!secret || !secret.length) return null;
24+
const token = hotp.generate(secret, counter!);
25+
return { token: token };
2326
}
2427

2528
/**
@@ -44,13 +47,13 @@ export function verifyHOTPToken(
4447
secret: string,
4548
token?: string,
4649
counter: number = 0,
47-
window: number | [number, number] = 4
50+
window: number | [number, number] = 4,
4851
): boolean | null {
49-
if (!token || !token.length) return null
52+
if (!token || !token.length) return null;
5053

5154
hotp.options = {
5255
window: window,
53-
}
56+
};
5457

55-
return hotp.verify({ token, secret, counter: counter! })
58+
return hotp.verify({ token, secret, counter: counter! });
5659
}

lib/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { generateToken, verifyToken } from './totp.js'
2-
import { generateHOTPToken, verifyHOTPToken } from './hotp.js'
3-
import { generateSecret } from './secret.js'
1+
import { generateToken, verifyToken } from "./totp.js";
2+
import { generateHOTPToken, verifyHOTPToken } from "./hotp.js";
3+
import { generateSecret } from "./secret.js";
44

55
export {
66
generateToken,

lib/interfaces.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
export interface Payload {
2-
name: string
3-
account: string,
4-
counter: number | undefined
2+
name: string;
3+
account: string;
4+
counter: number | undefined;
55
}
6-

0 commit comments

Comments
 (0)