@carecard/jwt-read 3.0.3 → 3.0.4

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.
Files changed (3) hide show
  1. package/index.d.ts +22 -19
  2. package/lib/jwtLib.js +29 -29
  3. package/package.json +3 -2
package/index.d.ts CHANGED
@@ -2,60 +2,62 @@
2
2
  * Utility functions for reading and verifying JWTs.
3
3
  */
4
4
 
5
+ import { Request, Response, NextFunction } from 'express';
6
+
5
7
  /**
6
- * Verifies a JWT from the 'Authorization: Bearer <token>' header and extracts it into req.jwt.
7
- * Throws an error if invalid.
8
+ * Returns a middleware that verifies a JWT from the 'Authorization: Bearer <token>' header
9
+ * and extracts it into req.jwt. Throws an error if invalid.
8
10
  */
9
- export function verifyJwt(req: any, publicKey: string, customErrorFunction?: () => void): Promise<any>;
11
+ export function verifyJwt(publicKey: string, customErrorFunction?: () => void): (req: Request, res: Response, next: NextFunction) => void;
10
12
 
11
13
  /**
12
- * Verifies a JWT from a custom header and extracts it into req.jwt.
14
+ * Returns a middleware that verifies a JWT from a custom header and extracts it into req.jwt.
13
15
  * Throws an error if invalid.
14
16
  */
15
- export function verifyWebToken(req: any, publicKey: string, headerName: string, customErrorFunction?: () => void): Promise<any>;
17
+ export function verifyWebToken(publicKey: string, headerName: string, customErrorFunction?: () => void): (req: Request, res: Response, next: NextFunction) => void;
16
18
 
17
19
  /**
18
- * Verifies a JWT from the 'Authorization: Bearer <token>' header and extracts it into req.jwt.
19
- * Returns false instead of throwing if invalid.
20
+ * Returns a middleware that verifies a JWT from the 'Authorization: Bearer <token>' header
21
+ * and extracts it into req.jwt. Returns false instead of throwing if invalid.
20
22
  */
21
- export function verifyJwtNoThrow(req: any, publicKey: string): Promise<boolean>;
23
+ export function verifyJwtNoThrow(publicKey: string): (req: Request, res: Response, next: NextFunction) => void;
22
24
 
23
25
  /**
24
- * Verifies a JWT from a custom header and extracts it into req.jwt.
26
+ * Returns a middleware that verifies a JWT from a custom header and extracts it into req.jwt.
25
27
  * Returns false instead of throwing if invalid.
26
28
  */
27
- export function verifyWebTokenNoThrow(req: any, publicKey: string, headerName: string): Promise<boolean>;
29
+ export function verifyWebTokenNoThrow(publicKey: string, headerName: string): (req: Request, res: Response, next: NextFunction) => void;
28
30
 
29
31
  /**
30
- * Verifies a visitor token from the 'x-visitor-token' header and extracts it into req.visitor.
31
- * Returns false instead of throwing if invalid.
32
+ * Returns a middleware that verifies a visitor token from the 'Visitor' header
33
+ * and extracts it into req.visitor. Returns false instead of throwing if invalid.
32
34
  */
33
- export function verifyVisitorNoThrow(req: any, publicKey: string): Promise<boolean>;
35
+ export function verifyVisitorNoThrow(publicKey: string): (req: Request, res: Response, next: NextFunction) => void;
34
36
 
35
37
  /**
36
38
  * Returns the client_id from the extracted JWT in req.jwt.
37
39
  */
38
- export function jwtClientId(req: any): string | undefined;
40
+ export function jwtClientId(req?: any): string | undefined;
39
41
 
40
42
  /**
41
43
  * Returns the client_id from the extracted visitor token in req.visitor.
42
44
  */
43
- export function visitorClientId(req: any): string | undefined;
45
+ export function visitorClientId(req?: any): string | undefined;
44
46
 
45
47
  /**
46
48
  * Checks if the extracted JWT in req.jwt has expired.
47
49
  */
48
- export function isJwtExpired(req: any, jwtValiditySeconds?: number): boolean;
50
+ export function isJwtExpired(req?: any, jwtValiditySeconds?: number): boolean;
49
51
 
50
52
  /**
51
53
  * Returns the age of the extracted JWT in seconds.
52
54
  */
53
- export function jwtAgeInSeconds(req: any): number;
55
+ export function jwtAgeInSeconds(req?: any): number;
54
56
 
55
57
  /**
56
- * Verifies the JWT and checks if the user has the required role.
58
+ * Returns a middleware that verifies the JWT and checks if the user has the required role.
57
59
  */
58
- export function verifyJwtAndRole(req: any, publicKey: string, userRole: string, customErrorFunction?: () => void): Promise<any>;
60
+ export function verifyJwtAndRole(userRole: string, publicKey: string, customErrorFunction?: () => void): (req: Request, res: Response, next: NextFunction) => void;
59
61
 
60
62
  /**
61
63
  * Throws a Used_Token error.
@@ -66,6 +68,7 @@ export function throwUsedTokenError(): never;
66
68
  * Checks if the user in the extracted JWT has the specified role.
67
69
  */
68
70
  export function doesJwtUserHasRole(req: any, userRole: string): boolean;
71
+ export function doesJwtUserHasRole(userRole: string): boolean;
69
72
 
70
73
  /**
71
74
  * Gets the full name of a role from its code (e.g., 'ad' -> 'admin').
package/lib/jwtLib.js CHANGED
@@ -79,7 +79,7 @@ function jwtAgeInSeconds( req ) {
79
79
  }
80
80
  }
81
81
 
82
- async function validateAndExtractJwtObject( req, publicKey, customErrorFunction ) {
82
+ function validateAndExtractJwtObject( req, publicKey, customErrorFunction ) {
83
83
 
84
84
  const jwtString = _validateJwt( req, customErrorFunction );
85
85
 
@@ -92,10 +92,10 @@ async function validateAndExtractJwtObject( req, publicKey, customErrorFunction
92
92
  throwError( customErrorFunction );
93
93
  }
94
94
 
95
- return req;
95
+ return Promise.resolve( req );
96
96
  }
97
97
 
98
- async function validateAndExtractWebToken( req, publicKey, headerName, customErrorFunction ) {
98
+ function validateAndExtractWebToken( req, publicKey, headerName, customErrorFunction ) {
99
99
 
100
100
  const webTokenString = _validateWebToken( req, headerName, customErrorFunction );
101
101
 
@@ -108,10 +108,10 @@ async function validateAndExtractWebToken( req, publicKey, headerName, customErr
108
108
  throwError( customErrorFunction );
109
109
  }
110
110
 
111
- return req;
111
+ return Promise.resolve( req );
112
112
  }
113
113
 
114
- async function validateAndExtractJwtObjectNoThrow( req, publicKey ) {
114
+ function validateAndExtractJwtObjectNoThrow( req, publicKey ) {
115
115
 
116
116
  try {
117
117
 
@@ -131,10 +131,10 @@ async function validateAndExtractJwtObjectNoThrow( req, publicKey ) {
131
131
 
132
132
  }
133
133
 
134
- return req;
134
+ return Promise.resolve( req );
135
135
  }
136
136
 
137
- async function validateAndExtractWebTokenObjectNoThrow( req, publicKey, headerName ) {
137
+ function validateAndExtractWebTokenObjectNoThrow( req, publicKey, headerName ) {
138
138
 
139
139
  try {
140
140
 
@@ -154,10 +154,10 @@ async function validateAndExtractWebTokenObjectNoThrow( req, publicKey, headerNa
154
154
 
155
155
  }
156
156
 
157
- return req;
157
+ return Promise.resolve( req );
158
158
  }
159
159
 
160
- async function validateAndExtractVisitorObjectNoThrow( req, publicKey ) {
160
+ function validateAndExtractVisitorObjectNoThrow( req, publicKey ) {
161
161
 
162
162
  try {
163
163
 
@@ -177,12 +177,12 @@ async function validateAndExtractVisitorObjectNoThrow( req, publicKey ) {
177
177
 
178
178
  }
179
179
 
180
- return req;
180
+ return Promise.resolve( req );
181
181
  }
182
182
 
183
183
  function verifyJwtAndRole( role, publicKey, customErrorFunction ) {
184
- return async function ( req, res, next ) {
185
- await validateAndExtractJwtObject( req, publicKey, customErrorFunction )
184
+ return function ( req, res, next ) {
185
+ validateAndExtractJwtObject( req, publicKey, customErrorFunction )
186
186
  .then( req => doesJwtUserHasRole( req, role ) )
187
187
  .then( isRoleExist => _isLoginRequired( isRoleExist, customErrorFunction ) )
188
188
  .then( () => next() )
@@ -191,41 +191,41 @@ function verifyJwtAndRole( role, publicKey, customErrorFunction ) {
191
191
  }
192
192
 
193
193
  function verifyJwt( publicKey, customErrorFunction ) {
194
- return async function ( req, res, next ) {
195
- await validateAndExtractJwtObject( req, publicKey, customErrorFunction )
196
- .then( () => process.nextTick( next ) )
194
+ return function ( req, res, next ) {
195
+ validateAndExtractJwtObject( req, publicKey, customErrorFunction )
196
+ .then( () => next() )
197
197
  .catch( next );
198
198
  }
199
199
  }
200
200
 
201
201
  function verifyWebToken( publicKey, headerName, customErrorFunction ) {
202
- return async function ( req, res, next ) {
203
- await validateAndExtractWebToken( req, publicKey, headerName, customErrorFunction )
204
- .then( () => process.nextTick( next ) )
202
+ return function ( req, res, next ) {
203
+ validateAndExtractWebToken( req, publicKey, headerName, customErrorFunction )
204
+ .then( () => next() )
205
205
  .catch( next );
206
206
  }
207
207
  }
208
208
 
209
209
  function verifyJwtNoThrow( publicKey ) {
210
- return async function ( req, res, next ) {
211
- await validateAndExtractJwtObjectNoThrow( req, publicKey )
212
- .then( () => process.nextTick( next ) )
210
+ return function ( req, res, next ) {
211
+ validateAndExtractJwtObjectNoThrow( req, publicKey )
212
+ .then( () => next() )
213
213
  .catch( next );
214
214
  }
215
215
  }
216
216
 
217
217
  function verifyWebTokenNoThrow( publicKey, headerName ) {
218
- return async function ( req, res, next ) {
219
- await validateAndExtractWebTokenObjectNoThrow( req, publicKey, headerName )
220
- .then( () => process.nextTick( next ) )
218
+ return function ( req, res, next ) {
219
+ validateAndExtractWebTokenObjectNoThrow( req, publicKey, headerName )
220
+ .then( () => next() )
221
221
  .catch( next );
222
222
  }
223
223
  }
224
224
 
225
225
  function verifyVisitorNoThrow( publicKey ) {
226
- return async function ( req, res, next ) {
227
- await validateAndExtractVisitorObjectNoThrow( req, publicKey )
228
- .then( () => process.nextTick( next ) )
226
+ return function ( req, res, next ) {
227
+ validateAndExtractVisitorObjectNoThrow( req, publicKey )
228
+ .then( () => next() )
229
229
  .catch( next );
230
230
  }
231
231
  }
@@ -294,7 +294,7 @@ function _attachVisitorMethods( visitorObj ) {
294
294
  }
295
295
  }
296
296
 
297
- async function _isJwtSignatureValid( jwt, publicKey, customErrorFunction ) {
297
+ function _isJwtSignatureValid( jwt, publicKey, customErrorFunction ) {
298
298
 
299
299
  if ( jwt && typeof jwt === "string" ) {
300
300
  return jwtUtilAuth.verifyJwtSignature( jwt, publicKey )
@@ -303,7 +303,7 @@ async function _isJwtSignatureValid( jwt, publicKey, customErrorFunction ) {
303
303
  }
304
304
  }
305
305
 
306
- async function _isJwtSignatureValidNoThrow( jwt, publicKey ) {
306
+ function _isJwtSignatureValidNoThrow( jwt, publicKey ) {
307
307
 
308
308
  if ( jwt && typeof jwt === "string" ) {
309
309
  return jwtUtilAuth.verifyJwtSignature( jwt, publicKey )
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@carecard/jwt-read",
3
- "version": "3.0.3",
3
+ "version": "3.0.4",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "https://github.com/CareCard-ca/pkg-jwt-read.git"
@@ -9,7 +9,7 @@
9
9
  "main": "index.js",
10
10
  "types": "index.d.ts",
11
11
  "scripts": {
12
- "test": "export NODE_ENV=test && mocha --watch --recursive",
12
+ "test": "export NODE_ENV=test && mocha --recursive",
13
13
  "test:types": "tsc --noEmit && mocha -r ts-node/register test/types.test.ts"
14
14
  },
15
15
  "keywords": [
@@ -20,6 +20,7 @@
20
20
  "author": "CareCard team",
21
21
  "license": "ISC",
22
22
  "devDependencies": {
23
+ "@types/express": "5.0.6",
23
24
  "@types/mocha": "10.0.10",
24
25
  "@types/node": "25.5.0",
25
26
  "mocha": "11.7.5",