@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.
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Error class for JWT
3
+ */
4
+ export declare class JWTError extends Error {
5
+ constructor(message: string);
6
+ }
7
+ //# sourceMappingURL=error.d.ts.map
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
@@ -285,5 +285,6 @@ export declare class JWT {
285
285
  */
286
286
  verify<T extends JWTPayload = JWTPayload>(input: string, options?: JWTVerifyOptions): T;
287
287
  }
288
+ export * from "./error";
288
289
  export default JWT;
289
290
  //# sourceMappingURL=index.d.ts.map
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 Error(`Algorithm ${this.algorithm} is not supported`);
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 Error("Secret is required for HS256, HS384, HS512");
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 Error(`Algorithm ${this.algorithm} is not supported`);
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 Error(`Invalid type ${input.typ}`);
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 Error(`Invalid issuer ${input.iss}`);
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 Error(`Invalid subject ${input.sub}`);
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 Error(`Invalid audience ${input.aud}`);
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 Error(`Token expired at ${new Date(input.exp * 1000).toISOString()}`);
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 Error(`Token not before ${new Date(input.nbf * 1000).toISOString()}`);
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 Error(`Invalid JWT ID ${input.jti}`);
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 Error("Invalid signature");
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
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@liuhlightning/jwt",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "description": "json web token in full typescript.",
5
5
  "keywords": [
6
6
  "jwt",