@liuhlightning/jwt 0.1.0 → 0.1.1
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/error.d.ts +7 -0
- package/dist/error.js +13 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +27 -11
- package/package.json +1 -1
package/dist/error.d.ts
ADDED
package/dist/error.js
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.JWTError = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Error class for JWT
|
|
6
|
+
*/
|
|
7
|
+
class JWTError extends Error {
|
|
8
|
+
constructor(message) {
|
|
9
|
+
super(message);
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
exports.JWTError = JWTError;
|
|
13
|
+
//# sourceMappingURL=error.js.map
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -1,9 +1,24 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
2
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
17
|
exports.JWT = void 0;
|
|
4
18
|
const crypto_1 = require("crypto");
|
|
5
19
|
const decode_1 = require("./decode");
|
|
6
20
|
const encode_1 = require("./encode");
|
|
21
|
+
const error_1 = require("./error");
|
|
7
22
|
const time_1 = require("./time");
|
|
8
23
|
/**
|
|
9
24
|
* A class to create and verify JSON Web Tokens
|
|
@@ -16,7 +31,7 @@ class JWT {
|
|
|
16
31
|
get realAlgorithm() {
|
|
17
32
|
const a = this.algorithmMap[this.algorithm];
|
|
18
33
|
if (!a) {
|
|
19
|
-
throw new
|
|
34
|
+
throw new error_1.JWTError(`Algorithm ${this.algorithm} is not supported`);
|
|
20
35
|
}
|
|
21
36
|
return a;
|
|
22
37
|
}
|
|
@@ -91,14 +106,14 @@ class JWT {
|
|
|
91
106
|
case "HS384":
|
|
92
107
|
case "HS512": {
|
|
93
108
|
if (!this.secret) {
|
|
94
|
-
throw new
|
|
109
|
+
throw new error_1.JWTError("Secret is required for HS256, HS384, HS512");
|
|
95
110
|
}
|
|
96
111
|
const hmac = (0, crypto_1.createHmac)(this.realAlgorithm, this.secret);
|
|
97
112
|
hmac.update(input);
|
|
98
113
|
return hmac.digest("base64url");
|
|
99
114
|
}
|
|
100
115
|
default:
|
|
101
|
-
throw new
|
|
116
|
+
throw new error_1.JWTError(`Algorithm ${this.algorithm} is not supported`);
|
|
102
117
|
}
|
|
103
118
|
}
|
|
104
119
|
/**
|
|
@@ -118,7 +133,7 @@ class JWT {
|
|
|
118
133
|
*/
|
|
119
134
|
checkHeader(input) {
|
|
120
135
|
if (input.typ !== "JWT") {
|
|
121
|
-
throw new
|
|
136
|
+
throw new error_1.JWTError(`Invalid type ${input.typ}`);
|
|
122
137
|
}
|
|
123
138
|
}
|
|
124
139
|
/**
|
|
@@ -129,31 +144,31 @@ class JWT {
|
|
|
129
144
|
// check issuer
|
|
130
145
|
const iss = this.issuer ?? options.issuer;
|
|
131
146
|
if (iss && iss !== input.iss) {
|
|
132
|
-
throw new
|
|
147
|
+
throw new error_1.JWTError(`Invalid issuer ${input.iss}`);
|
|
133
148
|
}
|
|
134
149
|
// check subject
|
|
135
150
|
const sub = this.subject ?? options.subject;
|
|
136
151
|
if (sub && sub !== input.sub) {
|
|
137
|
-
throw new
|
|
152
|
+
throw new error_1.JWTError(`Invalid subject ${input.sub}`);
|
|
138
153
|
}
|
|
139
154
|
// check audience
|
|
140
155
|
const aud = this.audience ?? options.audience;
|
|
141
156
|
if (aud && aud !== input.aud) {
|
|
142
|
-
throw new
|
|
157
|
+
throw new error_1.JWTError(`Invalid audience ${input.aud}`);
|
|
143
158
|
}
|
|
144
159
|
const n = (0, time_1.buildTime)(options.currentTime ?? Date.now());
|
|
145
160
|
// check expiration time
|
|
146
161
|
if (input.exp && input.exp < n) {
|
|
147
|
-
throw new
|
|
162
|
+
throw new error_1.JWTError(`Token expired at ${new Date(input.exp * 1000).toISOString()}`);
|
|
148
163
|
}
|
|
149
164
|
// check not before
|
|
150
165
|
if (input.nbf && input.nbf > n) {
|
|
151
|
-
throw new
|
|
166
|
+
throw new error_1.JWTError(`Token not before ${new Date(input.nbf * 1000).toISOString()}`);
|
|
152
167
|
}
|
|
153
168
|
// check jwt id
|
|
154
169
|
const jti = this.jwtID ?? options.jwtID;
|
|
155
170
|
if (jti && jti !== input.jti) {
|
|
156
|
-
throw new
|
|
171
|
+
throw new error_1.JWTError(`Invalid JWT ID ${input.jti}`);
|
|
157
172
|
}
|
|
158
173
|
}
|
|
159
174
|
/**
|
|
@@ -166,7 +181,7 @@ class JWT {
|
|
|
166
181
|
checkSignature(signature, input, algorithm) {
|
|
167
182
|
const s = this.buildSignature(input, algorithm);
|
|
168
183
|
if (s !== signature) {
|
|
169
|
-
throw new
|
|
184
|
+
throw new error_1.JWTError("Invalid signature");
|
|
170
185
|
}
|
|
171
186
|
}
|
|
172
187
|
/**
|
|
@@ -188,5 +203,6 @@ class JWT {
|
|
|
188
203
|
}
|
|
189
204
|
}
|
|
190
205
|
exports.JWT = JWT;
|
|
206
|
+
__exportStar(require("./error"), exports);
|
|
191
207
|
exports.default = JWT;
|
|
192
208
|
//# sourceMappingURL=index.js.map
|