@dwtechs/toker-express 0.1.1 → 0.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/README.md CHANGED
@@ -2,6 +2,7 @@
2
2
  [![License: MIT](https://img.shields.io/npm/l/@dwtechs/toker-express.svg?color=brightgreen)](https://opensource.org/licenses/MIT)
3
3
  [![npm version](https://badge.fury.io/js/%40dwtechs%2Ftoker-express.svg)](https://www.npmjs.com/package/@dwtechs/toker-express)
4
4
  [![last version release date](https://img.shields.io/github/release-date/DWTechs/Toker-express.js)](https://www.npmjs.com/package/@dwtechs/toker-express)
5
+ ![Jest:coverage](https://img.shields.io/badge/Jest:coverage-83%25-brightgreen.svg)
5
6
 
6
7
 
7
8
  - [Synopsis](#synopsis)
@@ -124,13 +125,13 @@ const refreshDuration = isNumber(REFRESH_TOKEN_DURATION, false) ? REFRESH_TOKEN_
124
125
  /**
125
126
  * Refreshes the JWT tokens for a user.
126
127
  *
127
- * This function generates new access and refresh tokens for a user based on the provided
128
+ * This function generates new access and refresh tokens for a consumer based on the provided
128
129
  * decoded access token or user ID in the request body. It validates the issuer (iss) and
129
130
  * creates new tokens if the validation is successful. The new tokens are then added to the
130
- * response object.
131
+ * response local and the request body objects.
131
132
  *
132
- * @param {Request} req - The request object containing the decoded access token or user ID.
133
- * @param {MyResponse} res - The response object where the new tokens will be added.
133
+ * @param {Request} req - The request object containing the decoded access token or user ID. Where the new tokens will be added
134
+ * @param {Response} res - The response object where the new tokens will be added.
134
135
  * @param {NextFunction} next - The next middleware function in the Express.js request-response cycle.
135
136
  *
136
137
  * @returns {Promise<void>} Calls the next middleware function with an error if the issuer is invalid,
@@ -142,9 +143,8 @@ const refreshDuration = isNumber(REFRESH_TOKEN_DURATION, false) ? REFRESH_TOKEN_
142
143
  * @throws {InvalidBase64Secret} If the secret cannot be decoded from base64 (HTTP 500)
143
144
  * @throws {Object} Will call next() with error object containing:
144
145
  * - statusCode: 400 - When iss (issuer) is missing or invalid
145
- * - statusCode: 400 - When iss is not a valid number between 1 and 999999999
146
146
  */
147
- function refresh(req: Request, res: MyResponse, next: NextFunction): void {}
147
+ function refresh(req: Request, res: Response, next: NextFunction): void {}
148
148
 
149
149
  /**
150
150
  * Express middleware function to decode and verify an access token from the Authorization header.
@@ -213,10 +213,13 @@ This function will look for an ISS in the client request body :
213
213
  const iss = req.body.decodedAccessToken?.iss || req.body?.id?.toString();
214
214
  ```
215
215
 
216
- It will then send both new refresh and access tokens in the res object.
216
+ It will then send both new refresh and access tokens in the res.locals and req.body objects.
217
217
 
218
218
  ```Javascript
219
- res.rows = [{ accessToken, refreshToken }];
219
+ res.locals.accessToken = accessToken;
220
+ res.locals.refreshToken = refreshToken;
221
+ req.body.accessToken = accessToken;
222
+ req.body.refreshToken = refreshToken;
220
223
  ```
221
224
 
222
225
  ### JWT Decoding
@@ -25,7 +25,6 @@ https://github.com/DWTechs/Toker-express.js
25
25
  */
26
26
 
27
27
  import type { Request, Response, NextFunction } from 'express';
28
- import type { MyResponse } from './interfaces';
29
28
 
30
29
  // Extend Express Request interface globally
31
30
  declare global {
@@ -38,7 +37,7 @@ declare global {
38
37
  }
39
38
  }
40
39
 
41
- declare function refresh(req: Request, res: MyResponse, next: NextFunction): Promise<void>;
40
+ declare function refresh(req: Request, res: Response, next: NextFunction): Promise<void>;
42
41
  declare function decodeAccess(req: Request, _res: Response, next: NextFunction): void;
43
42
  declare function decodeRefresh(req: Request, _res: Response, next: NextFunction): Promise<void>;
44
43
 
@@ -38,21 +38,21 @@ var __awaiter = (undefined && undefined.__awaiter) || function (thisArg, _argume
38
38
  });
39
39
  };
40
40
  const { TOKEN_SECRET, ACCESS_TOKEN_DURATION, REFRESH_TOKEN_DURATION } = process.env;
41
- const TE_PREFIX = "Toker-express: ";
41
+ const LOGS_PREFIX = "Toker-express: ";
42
42
  if (!TOKEN_SECRET)
43
- throw new Error(`${TE_PREFIX}Missing TOKEN_SECRET environment variable`);
43
+ throw new Error(`${LOGS_PREFIX}Missing TOKEN_SECRET environment variable`);
44
44
  if (!isString(TOKEN_SECRET, "!0"))
45
- throw new Error(`${TE_PREFIX}Invalid TOKEN_SECRET environment variable`);
45
+ throw new Error(`${LOGS_PREFIX}Invalid TOKEN_SECRET environment variable`);
46
46
  const secrets = [TOKEN_SECRET];
47
- const accessDuration = isNumber(ACCESS_TOKEN_DURATION, false) ? ACCESS_TOKEN_DURATION : 600;
48
- const refreshDuration = isNumber(REFRESH_TOKEN_DURATION, false) ? REFRESH_TOKEN_DURATION : 86400;
47
+ const accessDuration = isNumber(ACCESS_TOKEN_DURATION, false) ? Number(ACCESS_TOKEN_DURATION) : 600;
48
+ const refreshDuration = isNumber(REFRESH_TOKEN_DURATION, false) ? Number(REFRESH_TOKEN_DURATION) : 86400;
49
49
  function refresh(req, res, next) {
50
50
  return __awaiter(this, void 0, void 0, function* () {
51
51
  var _a, _b, _c;
52
52
  const iss = ((_a = req.decodedAccessToken) === null || _a === void 0 ? void 0 : _a.iss) || ((_c = (_b = req.body) === null || _b === void 0 ? void 0 : _b.id) === null || _c === void 0 ? void 0 : _c.toString());
53
53
  if (!isValidNumber(iss, 1, 999999999, false))
54
- return next({ statusCode: 400, message: `${TE_PREFIX}Missing iss` });
55
- log.debug(`${TE_PREFIX}Create tokens for user ${iss}`);
54
+ return next({ statusCode: 400, message: `${LOGS_PREFIX}Missing iss` });
55
+ log.debug(`${LOGS_PREFIX}Create tokens for user ${iss}`);
56
56
  let accessToken;
57
57
  let refreshToken;
58
58
  try {
@@ -63,12 +63,15 @@ function refresh(req, res, next) {
63
63
  return next(err);
64
64
  }
65
65
  log.debug(`refreshToken='${refreshToken}', accessToken='${accessToken}'`);
66
- res.rows = [{ accessToken, refreshToken }];
66
+ res.locals.accessToken = accessToken;
67
+ res.locals.refreshToken = refreshToken;
68
+ req.body.accessToken = accessToken;
69
+ req.body.refreshToken = refreshToken;
67
70
  next();
68
71
  });
69
72
  }
70
73
  function decodeAccess(req, _res, next) {
71
- log.debug(`${TE_PREFIX}decode access token`);
74
+ log.debug(`${LOGS_PREFIX}decode access token`);
72
75
  if (!req.isProtected)
73
76
  return next();
74
77
  let t;
@@ -78,9 +81,9 @@ function decodeAccess(req, _res, next) {
78
81
  catch (e) {
79
82
  return next(e);
80
83
  }
81
- log.debug(`${TE_PREFIX}accessToken : ${t}`);
84
+ log.debug(`${LOGS_PREFIX}accessToken : ${t}`);
82
85
  if (!isJWT(t))
83
- return next({ statusCode: 401, message: `${TE_PREFIX}Invalid access token` });
86
+ return next({ statusCode: 401, message: `${LOGS_PREFIX}Invalid access token` });
84
87
  let decodedToken = null;
85
88
  try {
86
89
  decodedToken = verify(t, secrets, true);
@@ -89,17 +92,17 @@ function decodeAccess(req, _res, next) {
89
92
  return next(e);
90
93
  }
91
94
  if (!isValidNumber(decodedToken.iss, 1, 999999999, false))
92
- return next({ statusCode: 400, message: `${TE_PREFIX}Missing iss` });
93
- log.debug(`${TE_PREFIX}Decoded access token : ${JSON.stringify(decodedToken)}`);
95
+ return next({ statusCode: 400, message: `${LOGS_PREFIX}Missing iss` });
96
+ log.debug(`${LOGS_PREFIX}Decoded access token : ${JSON.stringify(decodedToken)}`);
94
97
  req.decodedAccessToken = decodedToken;
95
98
  next();
96
99
  }
97
100
  function decodeRefresh(req, _res, next) {
98
101
  return __awaiter(this, void 0, void 0, function* () {
99
102
  const token = req.body.refreshToken;
100
- log.debug(`${TE_PREFIX}decodeRefresh(token=${token})`);
103
+ log.debug(`${LOGS_PREFIX}decodeRefresh(token=${token})`);
101
104
  if (!isJWT(token))
102
- return next({ statusCode: 401, message: `${TE_PREFIX}Invalid refresh token` });
105
+ return next({ statusCode: 401, message: `${LOGS_PREFIX}Invalid refresh token` });
103
106
  let decodedToken = null;
104
107
  try {
105
108
  decodedToken = verify(token, secrets, false);
@@ -108,8 +111,8 @@ function decodeRefresh(req, _res, next) {
108
111
  return next(e);
109
112
  }
110
113
  if (!isValidNumber(decodedToken.iss, 1, 999999999, false))
111
- return next({ statusCode: 400, message: `${TE_PREFIX}Missing iss` });
112
- log.debug(`${TE_PREFIX}Decoded refresh token : ${JSON.stringify(req.decodedRefreshToken)}`);
114
+ return next({ statusCode: 400, message: `${LOGS_PREFIX}Missing iss` });
115
+ log.debug(`${LOGS_PREFIX}Decoded refresh token : ${JSON.stringify(req.decodedRefreshToken)}`);
113
116
  req.decodedRefreshToken = decodedToken;
114
117
  next();
115
118
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dwtechs/toker-express",
3
- "version": "0.1.1",
3
+ "version": "0.1.2",
4
4
  "description": "Open source JWT management library for Express.js to refresh and decode tokens safely.",
5
5
  "keywords": [
6
6
  "JWT",
@@ -31,7 +31,7 @@
31
31
  "rollup:mjs": "rollup --config rollup.config.mjs",
32
32
  "rollup:cjs": "rollup --config rollup.config.cjs.mjs",
33
33
  "rollup": "npm run rollup:mjs",
34
- "test": ""
34
+ "test": "jest --coverage"
35
35
  },
36
36
  "files": [
37
37
  "dist/"
@@ -42,10 +42,13 @@
42
42
  "@dwtechs/winstan": "0.4.0"
43
43
  },
44
44
  "devDependencies": {
45
- "@types/express": "5.0.0",
45
+ "@babel/preset-env": "7.26.0",
46
46
  "@rollup/plugin-node-resolve": "15.3.0",
47
+ "@types/express": "5.0.3",
48
+ "babel-jest": "29.7.0",
47
49
  "core-js": "3.38.1",
50
+ "jest": "29.7.0",
48
51
  "rollup": "4.24.0",
49
- "typescript": "5.6.3"
52
+ "typescript": "5.9.2"
50
53
  }
51
54
  }