dff-util 1.1.1 → 1.1.2
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/dist/main/jwt-util.d.ts +3 -3
- package/dist/main/jwt-util.js +40 -35
- package/package.json +4 -2
package/dist/main/jwt-util.d.ts
CHANGED
|
@@ -5,20 +5,20 @@
|
|
|
5
5
|
* @param expiresIn
|
|
6
6
|
* @returns
|
|
7
7
|
*/
|
|
8
|
-
export declare const JwtEncode: (data: any, secret: string, expiresIn?: number) =>
|
|
8
|
+
export declare const JwtEncode: (data: any, secret: string, expiresIn?: number) => string;
|
|
9
9
|
/**
|
|
10
10
|
* Verify JWT token with secret
|
|
11
11
|
* @param token
|
|
12
12
|
* @param secret
|
|
13
13
|
* @returns
|
|
14
14
|
*/
|
|
15
|
-
export declare const JwtVerify: (token: string, secret: string) =>
|
|
15
|
+
export declare const JwtVerify: (token: string, secret: string) => any;
|
|
16
16
|
/**
|
|
17
17
|
* Decode JWT Token without verification
|
|
18
18
|
* @param token
|
|
19
19
|
* @returns
|
|
20
20
|
*/
|
|
21
|
-
export declare const JwtDecode: (token: string) => import("
|
|
21
|
+
export declare const JwtDecode: (token: string) => import("jwt-decode").JwtPayload;
|
|
22
22
|
/**
|
|
23
23
|
* Check if token is expired or not
|
|
24
24
|
* @param token
|
package/dist/main/jwt-util.js
CHANGED
|
@@ -1,16 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var
|
|
3
|
-
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
10
4
|
};
|
|
11
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
6
|
exports.JwtValid = exports.JwtDecode = exports.JwtVerify = exports.JwtEncode = void 0;
|
|
13
|
-
const
|
|
7
|
+
const jwt_encode_1 = __importDefault(require("jwt-encode"));
|
|
8
|
+
const jwt_decode_1 = require("jwt-decode");
|
|
14
9
|
/**
|
|
15
10
|
* Encode JWT Token with secret key, expiresIn in seconds (86400 = 1 day) default
|
|
16
11
|
* @param data
|
|
@@ -18,14 +13,18 @@ const jose_1 = require("jose");
|
|
|
18
13
|
* @param expiresIn
|
|
19
14
|
* @returns
|
|
20
15
|
*/
|
|
21
|
-
const JwtEncode = (
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
.
|
|
25
|
-
.
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
16
|
+
const JwtEncode = (data, secret, expiresIn = 86400) => {
|
|
17
|
+
try {
|
|
18
|
+
const exp = Math.floor(Date.now() / 1000) + expiresIn;
|
|
19
|
+
const payload = Object.assign(Object.assign({}, data), { exp });
|
|
20
|
+
const token = (0, jwt_encode_1.default)(payload, secret);
|
|
21
|
+
return token;
|
|
22
|
+
}
|
|
23
|
+
catch (err) {
|
|
24
|
+
console.error('Error encoding JWT:', err);
|
|
25
|
+
throw new Error('Error encoding JWT');
|
|
26
|
+
}
|
|
27
|
+
};
|
|
29
28
|
exports.JwtEncode = JwtEncode;
|
|
30
29
|
/**
|
|
31
30
|
* Verify JWT token with secret
|
|
@@ -33,24 +32,32 @@ exports.JwtEncode = JwtEncode;
|
|
|
33
32
|
* @param secret
|
|
34
33
|
* @returns
|
|
35
34
|
*/
|
|
36
|
-
const JwtVerify = (token, secret) =>
|
|
35
|
+
const JwtVerify = (token, secret) => {
|
|
37
36
|
if (token) {
|
|
38
37
|
try {
|
|
39
38
|
token = token.replace(/jwt |JWT |Bearer |bearer |BEARER /g, '');
|
|
40
|
-
const
|
|
41
|
-
const
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
39
|
+
const decoded = (0, jwt_decode_1.jwtDecode)(token);
|
|
40
|
+
const currentTime = Math.floor(Date.now() / 1000);
|
|
41
|
+
// Check if token is expired
|
|
42
|
+
if (decoded.exp && decoded.exp < currentTime) {
|
|
43
|
+
throw new Error('Token has expired');
|
|
44
|
+
}
|
|
45
|
+
// Since `jwt-encode` doesn't provide signature verification, manually check if the payload matches.
|
|
46
|
+
const regeneratedToken = (0, jwt_encode_1.default)(decoded, secret);
|
|
47
|
+
if (token !== regeneratedToken) {
|
|
48
|
+
throw new Error('Token is not valid');
|
|
49
|
+
}
|
|
50
|
+
return decoded;
|
|
45
51
|
}
|
|
46
52
|
catch (err) {
|
|
47
|
-
|
|
53
|
+
console.error('Invalid JWT:', err);
|
|
54
|
+
throw new Error('Token is not valid');
|
|
48
55
|
}
|
|
49
56
|
}
|
|
50
57
|
else {
|
|
51
|
-
throw
|
|
58
|
+
throw new Error('Auth token is not supplied');
|
|
52
59
|
}
|
|
53
|
-
}
|
|
60
|
+
};
|
|
54
61
|
exports.JwtVerify = JwtVerify;
|
|
55
62
|
/**
|
|
56
63
|
* Decode JWT Token without verification
|
|
@@ -61,15 +68,16 @@ const JwtDecode = (token) => {
|
|
|
61
68
|
if (token) {
|
|
62
69
|
try {
|
|
63
70
|
token = token.replace(/jwt |JWT |Bearer |bearer |BEARER /g, '');
|
|
64
|
-
|
|
71
|
+
const decoded = (0, jwt_decode_1.jwtDecode)(token);
|
|
72
|
+
return decoded;
|
|
65
73
|
}
|
|
66
74
|
catch (err) {
|
|
67
|
-
console.error(err);
|
|
68
|
-
throw
|
|
75
|
+
console.error('Error decoding JWT:', err);
|
|
76
|
+
throw new Error('Token is not valid');
|
|
69
77
|
}
|
|
70
78
|
}
|
|
71
79
|
else {
|
|
72
|
-
throw
|
|
80
|
+
throw new Error('Auth token is not supplied');
|
|
73
81
|
}
|
|
74
82
|
};
|
|
75
83
|
exports.JwtDecode = JwtDecode;
|
|
@@ -83,12 +91,9 @@ const JwtValid = (token) => {
|
|
|
83
91
|
if (!token) {
|
|
84
92
|
return false;
|
|
85
93
|
}
|
|
86
|
-
const
|
|
94
|
+
const decoded = (0, jwt_decode_1.jwtDecode)(token);
|
|
87
95
|
const currentTime = Math.floor(Date.now() / 1000);
|
|
88
|
-
|
|
89
|
-
return false;
|
|
90
|
-
}
|
|
91
|
-
return true;
|
|
96
|
+
return !(decoded.exp && decoded.exp < currentTime);
|
|
92
97
|
}
|
|
93
98
|
catch (error) {
|
|
94
99
|
console.error('Invalid JWT:', error);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dff-util",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.2",
|
|
4
4
|
"main": "dist/index.js",
|
|
5
5
|
"types": "dist/index.d.ts",
|
|
6
6
|
"scripts": {
|
|
@@ -17,13 +17,15 @@
|
|
|
17
17
|
"license": "ISC",
|
|
18
18
|
"description": "DesignForFeature Utilities",
|
|
19
19
|
"devDependencies": {
|
|
20
|
+
"@types/jwt-encode": "^1.0.3",
|
|
20
21
|
"@types/node": "^22.10.5",
|
|
21
22
|
"@types/randomstring": "^1.3.0",
|
|
22
23
|
"ts-node": "^10.9.2",
|
|
23
24
|
"typescript": "^5.7.2"
|
|
24
25
|
},
|
|
25
26
|
"dependencies": {
|
|
26
|
-
"
|
|
27
|
+
"jwt-decode": "^4.0.0",
|
|
28
|
+
"jwt-encode": "^1.0.1",
|
|
27
29
|
"randomstring": "1.3.0"
|
|
28
30
|
}
|
|
29
31
|
}
|