@carecard/jwt-read 3.0.2 → 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.
- package/index.d.ts +81 -0
- package/lib/jwtLib.js +29 -29
- package/package.json +13 -6
package/index.d.ts
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Utility functions for reading and verifying JWTs.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { Request, Response, NextFunction } from 'express';
|
|
6
|
+
|
|
7
|
+
/**
|
|
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.
|
|
10
|
+
*/
|
|
11
|
+
export function verifyJwt(publicKey: string, customErrorFunction?: () => void): (req: Request, res: Response, next: NextFunction) => void;
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Returns a middleware that verifies a JWT from a custom header and extracts it into req.jwt.
|
|
15
|
+
* Throws an error if invalid.
|
|
16
|
+
*/
|
|
17
|
+
export function verifyWebToken(publicKey: string, headerName: string, customErrorFunction?: () => void): (req: Request, res: Response, next: NextFunction) => void;
|
|
18
|
+
|
|
19
|
+
/**
|
|
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.
|
|
22
|
+
*/
|
|
23
|
+
export function verifyJwtNoThrow(publicKey: string): (req: Request, res: Response, next: NextFunction) => void;
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Returns a middleware that verifies a JWT from a custom header and extracts it into req.jwt.
|
|
27
|
+
* Returns false instead of throwing if invalid.
|
|
28
|
+
*/
|
|
29
|
+
export function verifyWebTokenNoThrow(publicKey: string, headerName: string): (req: Request, res: Response, next: NextFunction) => void;
|
|
30
|
+
|
|
31
|
+
/**
|
|
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.
|
|
34
|
+
*/
|
|
35
|
+
export function verifyVisitorNoThrow(publicKey: string): (req: Request, res: Response, next: NextFunction) => void;
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Returns the client_id from the extracted JWT in req.jwt.
|
|
39
|
+
*/
|
|
40
|
+
export function jwtClientId(req?: any): string | undefined;
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Returns the client_id from the extracted visitor token in req.visitor.
|
|
44
|
+
*/
|
|
45
|
+
export function visitorClientId(req?: any): string | undefined;
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Checks if the extracted JWT in req.jwt has expired.
|
|
49
|
+
*/
|
|
50
|
+
export function isJwtExpired(req?: any, jwtValiditySeconds?: number): boolean;
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Returns the age of the extracted JWT in seconds.
|
|
54
|
+
*/
|
|
55
|
+
export function jwtAgeInSeconds(req?: any): number;
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Returns a middleware that verifies the JWT and checks if the user has the required role.
|
|
59
|
+
*/
|
|
60
|
+
export function verifyJwtAndRole(userRole: string, publicKey: string, customErrorFunction?: () => void): (req: Request, res: Response, next: NextFunction) => void;
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Throws a Used_Token error.
|
|
64
|
+
*/
|
|
65
|
+
export function throwUsedTokenError(): never;
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Checks if the user in the extracted JWT has the specified role.
|
|
69
|
+
*/
|
|
70
|
+
export function doesJwtUserHasRole(req: any, userRole: string): boolean;
|
|
71
|
+
export function doesJwtUserHasRole(userRole: string): boolean;
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Gets the full name of a role from its code (e.g., 'ad' -> 'admin').
|
|
75
|
+
*/
|
|
76
|
+
export function getNameOfRole(roleCode: string): string;
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Gets the code of a role from its name (e.g., 'admin' -> 'ad').
|
|
80
|
+
*/
|
|
81
|
+
export function getCodeOfRole(roleName: string): string;
|
package/lib/jwtLib.js
CHANGED
|
@@ -79,7 +79,7 @@ function jwtAgeInSeconds( req ) {
|
|
|
79
79
|
}
|
|
80
80
|
}
|
|
81
81
|
|
|
82
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
|
185
|
-
|
|
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
|
|
195
|
-
|
|
196
|
-
.then( () =>
|
|
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
|
|
203
|
-
|
|
204
|
-
.then( () =>
|
|
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
|
|
211
|
-
|
|
212
|
-
.then( () =>
|
|
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
|
|
219
|
-
|
|
220
|
-
.then( () =>
|
|
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
|
|
227
|
-
|
|
228
|
-
.then( () =>
|
|
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
|
-
|
|
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
|
-
|
|
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,14 +1,16 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@carecard/jwt-read",
|
|
3
|
-
"version": "3.0.
|
|
3
|
+
"version": "3.0.4",
|
|
4
4
|
"repository": {
|
|
5
5
|
"type": "git",
|
|
6
6
|
"url": "https://github.com/CareCard-ca/pkg-jwt-read.git"
|
|
7
7
|
},
|
|
8
8
|
"description": "Jwt read functions",
|
|
9
9
|
"main": "index.js",
|
|
10
|
+
"types": "index.d.ts",
|
|
10
11
|
"scripts": {
|
|
11
|
-
"test": "export NODE_ENV=test && mocha --
|
|
12
|
+
"test": "export NODE_ENV=test && mocha --recursive",
|
|
13
|
+
"test:types": "tsc --noEmit && mocha -r ts-node/register test/types.test.ts"
|
|
12
14
|
},
|
|
13
15
|
"keywords": [
|
|
14
16
|
"auth",
|
|
@@ -18,11 +20,16 @@
|
|
|
18
20
|
"author": "CareCard team",
|
|
19
21
|
"license": "ISC",
|
|
20
22
|
"devDependencies": {
|
|
21
|
-
"
|
|
23
|
+
"@types/express": "5.0.6",
|
|
24
|
+
"@types/mocha": "10.0.10",
|
|
25
|
+
"@types/node": "25.5.0",
|
|
26
|
+
"mocha": "11.7.5",
|
|
27
|
+
"ts-node": "10.9.2",
|
|
28
|
+
"typescript": "5.9.3"
|
|
22
29
|
},
|
|
23
30
|
"dependencies": {
|
|
24
|
-
"@carecard/auth-util": "3.0.
|
|
25
|
-
"@carecard/common-util": "
|
|
26
|
-
"@carecard/validate": "
|
|
31
|
+
"@carecard/auth-util": "3.0.3",
|
|
32
|
+
"@carecard/common-util": "3.0.3",
|
|
33
|
+
"@carecard/validate": "3.0.3"
|
|
27
34
|
}
|
|
28
35
|
}
|