@dwtechs/toker-express 0.6.2 → 0.7.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/README.md CHANGED
@@ -218,14 +218,16 @@ function parseBearer(req: Request, res: Response, next: NextFunction): void {}
218
218
  * subsequent middleware. It only processes requests that have `res.locals.route.isProtected`
219
219
  * set to true. For non-protected routes, it simply passes control to the next middleware.
220
220
  *
221
- * Note: This middleware IGNORES token expiration (exp claim) by design, allowing
222
- * expired access tokens to be decoded. This is useful for token refresh flows where
223
- * you need to identify the user even after their access token has expired.
221
+ * Note: By default, this middleware checks token expiration (exp claim) and will reject
222
+ * expired tokens. For token refresh flows where you need to identify the user even after
223
+ * their access token has expired, set `res.locals.tokens.ignoreExpiration` to true before
224
+ * calling decodeAccess function.
224
225
  *
225
226
  * @param {Request} _req - The Express request object (unused)
226
227
  * @param {Response} res - The Express response object. Should contain:
227
228
  * - `res.locals.route.isProtected`: Boolean flag to determine if route requires JWT protection
228
229
  * - `res.locals.tokens.access`: The JWT token to decode (from parseBearer middleware)
230
+ * - `res.locals.tokens.ignoreExpiration`: Optional boolean to skip expiration checking (default: false)
229
231
  * Decoded token will be added to `res.locals.tokens.decodedAccess`
230
232
  * @param {NextFunction} next - The next middleware function to be called
231
233
  *
@@ -234,6 +236,7 @@ function parseBearer(req: Request, res: Response, next: NextFunction): void {}
234
236
  * @throws Will call next() with error when:
235
237
  * - Token is not a valid JWT format (HTTP 401)
236
238
  * - Token is malformed or has invalid structure (HTTP 401)
239
+ * - Token has expired (exp claim) - unless ignoreExpiration is true (HTTP 401)
237
240
  * - Token cannot be used yet (nbf claim) (HTTP 401)
238
241
  * - Token signature is invalid (HTTP 401)
239
242
  * - Issuer (iss) is missing or invalid - not a number between 1-999999999 (HTTP 400)
@@ -241,8 +244,15 @@ function parseBearer(req: Request, res: Response, next: NextFunction): void {}
241
244
  * - Secret cannot be decoded from base64 (HTTP 500)
242
245
  *
243
246
  * @example
244
- * // Use in protected route chain for token refresh
245
- * app.post('/refresh', parseBearer, decodeAccess, refreshTokens, ...);
247
+ * // Use in protected route chain (checks expiration by default)
248
+ * app.get('/protected', parseBearer, decodeAccess, ...);
249
+ *
250
+ * @example
251
+ * // Use in token refresh flow (ignores expiration)
252
+ * app.post('/refresh', (req, res, next) => {
253
+ * res.locals.tokens = { ignoreExpiration: true };
254
+ * next();
255
+ * }, parseBearer, decodeAccess, refreshTokens, ...);
246
256
  */
247
257
  function decodeAccess(_req: Request, res: Response, next: NextFunction): void {}
248
258
 
@@ -91,16 +91,17 @@ function parseBearer(req, res, next) {
91
91
  next();
92
92
  }
93
93
  function decodeAccess(_req, res, next) {
94
- var _a, _b, _c, _d;
94
+ var _a, _b, _c, _d, _e, _f, _g;
95
95
  log.debug(`${LOGS_PREFIX}decode access token`);
96
96
  if (!((_b = (_a = res.locals) === null || _a === void 0 ? void 0 : _a.route) === null || _b === void 0 ? void 0 : _b.isProtected))
97
97
  return next();
98
98
  const t = (_d = (_c = res.locals) === null || _c === void 0 ? void 0 : _c.tokens) === null || _d === void 0 ? void 0 : _d.access;
99
+ const ignoreExpiration = (_g = (_f = (_e = res.locals) === null || _e === void 0 ? void 0 : _e.tokens) === null || _f === void 0 ? void 0 : _f.ignoreExpiration) !== null && _g !== void 0 ? _g : false;
99
100
  if (!isJWT(t))
100
101
  return next({ statusCode: 401, message: `${LOGS_PREFIX}Invalid access token` });
101
102
  let dt = null;
102
103
  try {
103
- dt = verify(t, secrets, true);
104
+ dt = verify(t, secrets, ignoreExpiration);
104
105
  }
105
106
  catch (e) {
106
107
  return next(e);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dwtechs/toker-express",
3
- "version": "0.6.2",
3
+ "version": "0.7.0",
4
4
  "description": "Open source JWT management library for Express.js to refresh and decode tokens safely.",
5
5
  "keywords": [
6
6
  "JWT",