auth-verify 1.2.7 → 1.3.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/index.js +3 -1
- package/package.json +5 -3
- package/readme.md +17 -2
- package/src/jwt/index.js +4 -2
- package/tests/jwa.test.js +45 -0
- package/tests/jwtmanager.test.js +1 -1
package/index.js
CHANGED
|
@@ -9,9 +9,10 @@ class AuthVerify {
|
|
|
9
9
|
jwtSecret = "jwt_secret",
|
|
10
10
|
cookieName = "jwt_token",
|
|
11
11
|
otpExpiry = 300,
|
|
12
|
-
storeTokens = "
|
|
12
|
+
storeTokens = "memory",
|
|
13
13
|
otpHash = "sha256",
|
|
14
14
|
redisUrl,
|
|
15
|
+
useAlg
|
|
15
16
|
} = options;
|
|
16
17
|
|
|
17
18
|
// ✅ Ensure cookieName and secret always exist
|
|
@@ -22,6 +23,7 @@ class AuthVerify {
|
|
|
22
23
|
this.jwt = new JWTManager(jwtSecret, {
|
|
23
24
|
storeTokens,
|
|
24
25
|
cookieName,
|
|
26
|
+
useAlg
|
|
25
27
|
});
|
|
26
28
|
|
|
27
29
|
this.otp = new OTPManager({
|
package/package.json
CHANGED
|
@@ -11,8 +11,8 @@
|
|
|
11
11
|
"uuid": "^9.0.1"
|
|
12
12
|
},
|
|
13
13
|
"name": "auth-verify",
|
|
14
|
-
"version": "1.
|
|
15
|
-
"description": "A simple Node.js library for sending and verifying OTP via email, SMS and Telegram bot",
|
|
14
|
+
"version": "1.3.0",
|
|
15
|
+
"description": "A simple Node.js library for sending and verifying OTP via email, SMS and Telegram bot. And handling JWT with Cookies",
|
|
16
16
|
"main": "index.js",
|
|
17
17
|
"scripts": {
|
|
18
18
|
"test": "jest --runInBand"
|
|
@@ -39,7 +39,9 @@
|
|
|
39
39
|
"jwt",
|
|
40
40
|
"oauth",
|
|
41
41
|
"redis",
|
|
42
|
-
"cookie"
|
|
42
|
+
"cookie",
|
|
43
|
+
"jwa",
|
|
44
|
+
"jsonwebtoken"
|
|
43
45
|
],
|
|
44
46
|
"author": "Jahongir Sobirov",
|
|
45
47
|
"license": "MIT",
|
package/readme.md
CHANGED
|
@@ -48,6 +48,20 @@ const auth = new AuthVerify({
|
|
|
48
48
|
|
|
49
49
|
## 🔐 JWT Usage
|
|
50
50
|
|
|
51
|
+
### JWA Handling (New in v1.3.0)
|
|
52
|
+
|
|
53
|
+
You can choose json web algorithm for signing jwt
|
|
54
|
+
```js
|
|
55
|
+
const AuthVerify = require('auth-verify');
|
|
56
|
+
const auth = new AuthVerify({ useAlg: 'HS512' }); // or 'HS256'
|
|
57
|
+
|
|
58
|
+
(async ()=>{
|
|
59
|
+
const token = await auth.jwt.sign({userId: 123}, '30m');
|
|
60
|
+
console.log('token', token);
|
|
61
|
+
})();
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
|
|
51
65
|
```js
|
|
52
66
|
// create JWT
|
|
53
67
|
const token = await auth.jwt.sign({ userId: 123 }, '1h'); // expiry string or number (ms) (and also you can add '1m' (minute), '5s' (second) and '7d' (day))
|
|
@@ -202,7 +216,7 @@ auth.otp.verify({ check: 'user@example.com', code: '123456' }, (err, isValid)=>{
|
|
|
202
216
|
`resend` returns the new code (promise style) or calls callback.
|
|
203
217
|
|
|
204
218
|
---
|
|
205
|
-
## 🌍 OAuth 2.0 Integration (
|
|
219
|
+
## 🌍 OAuth 2.0 Integration (v1.2.0+)
|
|
206
220
|
`auth.oauth` supports login via Google, Facebook, GitHub, X (Twitter) and Linkedin.
|
|
207
221
|
### Example (Google Login with Express)
|
|
208
222
|
```js
|
|
@@ -368,7 +382,7 @@ auth.register.sender('consoleOtp', async ({ to, code }) => {
|
|
|
368
382
|
});
|
|
369
383
|
|
|
370
384
|
// use it later (chainable)
|
|
371
|
-
await auth.use('consoleOtp').send({ to: '+998901234567', code: await auth.otp.generate(5) });
|
|
385
|
+
await auth.use('consoleOtp').send({ to: '+998901234567', code: await auth.otp.generate(5).code });
|
|
372
386
|
```
|
|
373
387
|
|
|
374
388
|
---
|
|
@@ -434,6 +448,7 @@ auth-verify/
|
|
|
434
448
|
| ├─ /oauth/index.js
|
|
435
449
|
│ └─ helpers/helper.js
|
|
436
450
|
├─ test/
|
|
451
|
+
│ ├─ jwa.test.js
|
|
437
452
|
│ ├─ jwtmanager.multitab.test.js
|
|
438
453
|
│ ├─ jwtmanager.test.js
|
|
439
454
|
│ ├─ otpmanager.test.js
|
package/src/jwt/index.js
CHANGED
|
@@ -251,7 +251,8 @@ class JWTManager {
|
|
|
251
251
|
// if (!secret) throw new Error("JWT secret is required");
|
|
252
252
|
this.secret = secret || "jwt_secret";
|
|
253
253
|
this.cookieName = options.cookieName || "jwt_token";
|
|
254
|
-
this.storeType = options.storeTokens || "
|
|
254
|
+
this.storeType = options.storeTokens || "memory";
|
|
255
|
+
this.jwtAlgorithm = options.useAlg || "HS256";
|
|
255
256
|
|
|
256
257
|
if (this.storeType === "memory") {
|
|
257
258
|
this.tokenStore = new Map();
|
|
@@ -290,8 +291,9 @@ class JWTManager {
|
|
|
290
291
|
|
|
291
292
|
const createToken = () =>
|
|
292
293
|
new Promise((resolve, reject) => {
|
|
293
|
-
jwt.sign(payload, this.secret, { expiresIn: expiryJwt }, (err, token) => {
|
|
294
|
+
jwt.sign(payload, this.secret, { expiresIn: expiryJwt, algorithm: this.jwtAlgorithm}, (err, token) => {
|
|
294
295
|
if (err) return reject(err);
|
|
296
|
+
// console.log(this.jwtAlgorithm);
|
|
295
297
|
resolve(token);
|
|
296
298
|
});
|
|
297
299
|
});
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
const AuthVerify = require('../index');
|
|
2
|
+
|
|
3
|
+
describe('JWA / JWT tests', () => {
|
|
4
|
+
|
|
5
|
+
let auth;
|
|
6
|
+
|
|
7
|
+
beforeAll(() => {
|
|
8
|
+
auth = new AuthVerify({
|
|
9
|
+
useAlg: 'HS512'
|
|
10
|
+
});
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
test('should sign token with HS512', async () => {
|
|
14
|
+
const token = await auth.jwt.sign({ id: 1 }, '1h');
|
|
15
|
+
|
|
16
|
+
expect(typeof token).toBe('string');
|
|
17
|
+
|
|
18
|
+
// decode headers — NOT verify
|
|
19
|
+
const parts = token.split('.');
|
|
20
|
+
const header = JSON.parse(Buffer.from(parts[0], 'base64url').toString());
|
|
21
|
+
|
|
22
|
+
expect(header.alg).toBe('HS512');
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
test('should verify token payload', async () => {
|
|
26
|
+
const token = await auth.jwt.sign({ id: 123 }, '1h');
|
|
27
|
+
|
|
28
|
+
const payload = await auth.jwt.verify(token);
|
|
29
|
+
|
|
30
|
+
expect(payload.id).toBe(123);
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
test('should reject tampered token', async () => {
|
|
34
|
+
const token = await auth.jwt.sign({ id: 1 }, '1h');
|
|
35
|
+
|
|
36
|
+
// break signature
|
|
37
|
+
const parts = token.split('.');
|
|
38
|
+
const bad = parts[0] + '.' + parts[1] + '.xxxx';
|
|
39
|
+
|
|
40
|
+
await expect(auth.jwt.verify(bad))
|
|
41
|
+
.rejects
|
|
42
|
+
.toThrow();
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
});
|
package/tests/jwtmanager.test.js
CHANGED
|
@@ -7,7 +7,7 @@ describe('JWTManager', () => {
|
|
|
7
7
|
let auth;
|
|
8
8
|
|
|
9
9
|
beforeAll(() => {
|
|
10
|
-
auth = new AuthVerify({jwtSecret: 'test_secret', storeTokens: 'memory'});
|
|
10
|
+
auth = new AuthVerify({jwtSecret: 'test_secret', storeTokens: 'memory', useAlg: "HS512"});
|
|
11
11
|
});
|
|
12
12
|
|
|
13
13
|
test('should sign and verify a JWT', async () => {
|