@carecard/jwt-read 1.0.3 → 2.0.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.
package/lib/jwtLib.js ADDED
@@ -0,0 +1,434 @@
1
+ const validate = require( '@carecard/validate' ).validate;
2
+ const { jwtUtilAuth } = require( '@carecard/auth-util' );
3
+ const {
4
+ throwLoginRequiredError,
5
+ throwNotAuthorizedError
6
+ } = require( "@carecard/common-util" ).error;
7
+
8
+ function jwtClientId( req ) {
9
+
10
+ return req?.jwt?.payload?.client_id;
11
+
12
+ }
13
+
14
+ function visitorClientId( req ) {
15
+
16
+ return req?.visitor?.payload?.client_id;
17
+
18
+ }
19
+
20
+ function doesJwtUserHasRole( req, userRole ) {
21
+
22
+ if ( userRole && typeof userRole === "string" ) {
23
+ return req?.jwt?.payload?.roles?.includes( userRole );
24
+ } else {
25
+ throwNotAuthorizedError();
26
+ }
27
+ }
28
+
29
+ function throwError( customErrorFunction ) {
30
+ ( typeof customErrorFunction === "function" ) ?
31
+ customErrorFunction() :
32
+ throwLoginRequiredError()
33
+ }
34
+
35
+ function isJwtExpired( req, jwtValiditySeconds ) {
36
+
37
+ if ( jwtValiditySeconds && typeof jwtValiditySeconds === "number" ) {
38
+ return ( parseInt( jwtValiditySeconds ) * 1000 ) < jwtAgeInMilliseconds( req );
39
+ } else {
40
+ return true;
41
+ }
42
+
43
+ }
44
+
45
+ function jwtAgeInMilliseconds( req ) {
46
+
47
+ if ( req?.jwt?.payload?.iat ) {
48
+
49
+ req.jwt[ "age" ] = Date.now() - req.jwt.payload.iat;
50
+ return req.jwt.age;
51
+
52
+ } else {
53
+
54
+ req.jwt[ "age" ] = Infinity
55
+ return req.jwt.age;
56
+
57
+ }
58
+ }
59
+
60
+ async function validateAndExtractJwtObject( req, publicKey, customErrorFunction ) {
61
+
62
+ const jwtString = await _validateJwt( req, customErrorFunction );
63
+
64
+ const isJwtSignatureValid = await _isJwtSignatureValid( jwtString, publicKey, customErrorFunction );
65
+
66
+ if ( jwtString && isJwtSignatureValid ) {
67
+ await _extractJwtObject( req, jwtString, customErrorFunction );
68
+ } else {
69
+ req[ "jwt" ] = null;
70
+ throwError( customErrorFunction );
71
+ }
72
+
73
+ return req;
74
+ }
75
+
76
+ async function validateAndExtractWebToken( req, publicKey, headerName, customErrorFunction ) {
77
+
78
+ const webTokenString = await _validateWebToken( req, headerName, customErrorFunction );
79
+
80
+ const isJwtSignatureValid = await _isJwtSignatureValid( webTokenString, publicKey, customErrorFunction );
81
+
82
+ if ( webTokenString && isJwtSignatureValid ) {
83
+ await _extractJwtObject( req, webTokenString, customErrorFunction );
84
+ } else {
85
+ req[ "jwt" ] = null;
86
+ throwError( customErrorFunction );
87
+ }
88
+
89
+ return req;
90
+ }
91
+
92
+ async function validateAndExtractJwtObjectNoThrow( req, publicKey ) {
93
+
94
+ try {
95
+
96
+ const jwtString = await _validateJwtNoThrow( req );
97
+
98
+ const isJwtSignatureValid = await _isJwtSignatureValidNoThrow( jwtString, publicKey );
99
+
100
+ if ( jwtString && isJwtSignatureValid ) {
101
+ await _extractJwtObjectNoThrow( req, jwtString );
102
+ } else {
103
+ req[ "jwt" ] = null;
104
+ }
105
+
106
+ } catch ( err ) {
107
+
108
+ req[ "jwt" ] = null;
109
+
110
+ }
111
+
112
+ return req;
113
+ }
114
+
115
+ async function validateAndExtractWebTokenObjectNoThrow( req, publicKey, headerName ) {
116
+
117
+ try {
118
+
119
+ const jwtString = _validateWebTokenNoThrow( req, headerName );
120
+
121
+ const isJwtSignatureValid = await _isJwtSignatureValidNoThrow( jwtString, publicKey );
122
+
123
+ if ( jwtString && isJwtSignatureValid ) {
124
+ await _extractJwtObjectNoThrow( req, jwtString );
125
+ } else {
126
+ req[ "jwt" ] = null;
127
+ }
128
+
129
+ } catch ( err ) {
130
+
131
+ req[ "jwt" ] = null;
132
+
133
+ }
134
+
135
+ return req;
136
+ }
137
+
138
+ async function validateAndExtractVisitorObjectNoThrow( req, publicKey ) {
139
+
140
+ try {
141
+
142
+ const jwtString = await _validateVisitorNoThrow( req );
143
+
144
+ const isJwtSignatureValid = await _isJwtSignatureValidNoThrow( jwtString, publicKey );
145
+
146
+ if ( jwtString && isJwtSignatureValid ) {
147
+ await _extractVisitorObjectNoThrow( req, jwtString );
148
+ } else {
149
+ req[ "visitor" ] = null;
150
+ }
151
+
152
+ } catch ( err ) {
153
+
154
+ req[ "visitor" ] = null;
155
+
156
+ }
157
+
158
+ return req;
159
+ }
160
+
161
+ function verifyJwtAndRole( role, publicKey, customErrorFunction ) {
162
+ return async function ( req, res, next ) {
163
+ await validateAndExtractJwtObject( req, publicKey, customErrorFunction )
164
+ .then( req => doesJwtUserHasRole( req, role ) )
165
+ .then( isRoleExist => _isLoginRequired( isRoleExist, customErrorFunction ) )
166
+ .then( next )
167
+ .catch( next );
168
+ }
169
+ }
170
+
171
+ function verifyJwt( publicKey, customErrorFunction ) {
172
+ return async function ( req, res, next ) {
173
+ await validateAndExtractJwtObject( req, publicKey, customErrorFunction )
174
+ .then( () => process.nextTick( next ) )
175
+ .catch( next );
176
+ }
177
+ }
178
+
179
+ function verifyWebToken( publicKey, headerName, customErrorFunction ) {
180
+ return async function ( req, res, next ) {
181
+ await validateAndExtractWebToken( req, publicKey, headerName, customErrorFunction )
182
+ .then( () => process.nextTick( next ) )
183
+ .catch( next );
184
+ }
185
+ }
186
+
187
+ function verifyJwtNoThrow( publicKey ) {
188
+ return async function ( req, res, next ) {
189
+ await validateAndExtractJwtObjectNoThrow( req, publicKey )
190
+ .then( () => process.nextTick( next ) )
191
+ .catch( next );
192
+ }
193
+ }
194
+
195
+ function verifyWebTokenNoThrow( publicKey, headerName ) {
196
+ return async function ( req, res, next ) {
197
+ await validateAndExtractWebTokenObjectNoThrow( req, publicKey, headerName )
198
+ .then( () => process.nextTick( next ) )
199
+ .catch( next );
200
+ }
201
+ }
202
+
203
+ function verifyVisitorNoThrow( publicKey ) {
204
+ return async function ( req, res, next ) {
205
+ await validateAndExtractVisitorObjectNoThrow( req, publicKey )
206
+ .then( () => process.nextTick( next ) )
207
+ .catch( next );
208
+ }
209
+ }
210
+
211
+ function throwUsedTokenError() {
212
+ throw new Error( "Used_Token" );
213
+ }
214
+
215
+ /*********************
216
+ * Private functions *
217
+ *********************/
218
+ function _isLoginRequired( hasRequiredRole, customErrorFunction ) {
219
+
220
+ if ( !hasRequiredRole ) {
221
+ throwError( customErrorFunction );
222
+ }
223
+
224
+ }
225
+
226
+ async function _extractJwtObject( req, jwt, customErrorFunction ) {
227
+ if ( jwt && typeof jwt === "string" ) {
228
+ req[ "jwt" ] = jwtUtilAuth.getHeaderPayloadFromJwt( jwt );
229
+ } else {
230
+ throwError( customErrorFunction );
231
+ }
232
+ }
233
+
234
+ async function _extractJwtObjectNoThrow( req, jwt ) {
235
+ if ( jwt && typeof jwt === "string" ) {
236
+ req[ "jwt" ] = jwtUtilAuth.getHeaderPayloadFromJwt( jwt );
237
+ } else {
238
+ req[ "jwt" ] = null;
239
+ }
240
+ }
241
+
242
+ async function _extractVisitorObjectNoThrow( req, jwt ) {
243
+ if ( jwt && typeof jwt === "string" ) {
244
+ req[ "visitor" ] = jwtUtilAuth.getHeaderPayloadFromJwt( jwt );
245
+ } else {
246
+ req[ "visitor" ] = null;
247
+ }
248
+ }
249
+
250
+ async function _isJwtSignatureValid( jwt, publicKey, customErrorFunction ) {
251
+
252
+ if ( jwt && typeof jwt === "string" ) {
253
+ return jwtUtilAuth.verifyJwtSignature( jwt, publicKey )
254
+ } else {
255
+ throwError( customErrorFunction );
256
+ }
257
+ }
258
+
259
+ async function _isJwtSignatureValidNoThrow( jwt, publicKey ) {
260
+
261
+ if ( jwt && typeof jwt === "string" ) {
262
+ return jwtUtilAuth.verifyJwtSignature( jwt, publicKey )
263
+ } else {
264
+ return false;
265
+ }
266
+ }
267
+
268
+ async function _validateJwt( req, customErrorFunction ) {
269
+
270
+ // Get from http header.
271
+ let jwtRaw = req.get( "Authorization" );
272
+
273
+ // Extract jwt from bearer schema.
274
+ let jwt = _extractJwt( jwtRaw, customErrorFunction );
275
+
276
+ // Validate characters string of jwt.
277
+ if ( validate.isJwtString( jwt ) ) {
278
+ return jwt;
279
+ } else {
280
+ throwError( customErrorFunction );
281
+ }
282
+ }
283
+
284
+ async function _validateWebToken( req, headerName, customErrorFunction ) {
285
+
286
+ // Get from http header.
287
+ let jwtRaw = req.get( headerName );
288
+
289
+ // Extract jwt from bearer schema.
290
+ let webToken = _extractWebToken( jwtRaw, customErrorFunction );
291
+
292
+ // Validate characters string of jwt.
293
+ if ( validate.isJwtString( webToken ) ) {
294
+ return webToken;
295
+ } else {
296
+ throwError( customErrorFunction );
297
+ }
298
+ }
299
+
300
+ function _validateJwtNoThrow( req ) {
301
+
302
+ // Get from http header.
303
+ let jwtRaw = req.get( "Authorization" );
304
+
305
+ if ( !jwtRaw ) {
306
+ return null;
307
+ }
308
+
309
+ // Extract jwt from bearer schema.
310
+ let jwt = _extractJwtNoThrow( jwtRaw );
311
+
312
+ // Validate characters string of jwt.
313
+ if ( validate.isJwtString( jwt ) ) {
314
+ return jwt;
315
+ }
316
+
317
+ return null;
318
+ }
319
+
320
+ function _validateWebTokenNoThrow( req, headerName ) {
321
+
322
+ // Get from http header.
323
+ let jwtRaw = req.get( headerName );
324
+
325
+ if ( !jwtRaw ) {
326
+ return null;
327
+ }
328
+
329
+ // Extract jwt from bearer schema.
330
+ let webToken = _extractWebTokenNoThrow( jwtRaw );
331
+
332
+ // Validate characters string of jwt.
333
+ if ( validate.isJwtString( webToken ) ) {
334
+ return webToken;
335
+ }
336
+
337
+ return null;
338
+ }
339
+
340
+ function _validateVisitorNoThrow( req ) {
341
+
342
+ // Get from http header.
343
+ let jwtRaw = req.get( "Visitor" );
344
+
345
+ if ( !jwtRaw ) {
346
+ return null;
347
+ }
348
+
349
+ // Extract jwt from bearer schema.
350
+ let jwt = _extractJwtNoThrow( jwtRaw );
351
+
352
+ // Validate characters string of jwt.
353
+ if ( validate.isJwtString( jwt ) ) {
354
+ return jwt;
355
+ }
356
+
357
+ return null;
358
+ }
359
+
360
+ function _extractJwt( jwtRaw, customErrorFunction ) {
361
+ let jwtSplit = null;
362
+
363
+ if ( jwtRaw && typeof jwtRaw === "string" ) {
364
+ jwtSplit = jwtRaw.split( " " );
365
+ } else {
366
+ throwError( customErrorFunction );
367
+ }
368
+
369
+ if ( jwtSplit[ 0 ]?.toLowerCase() === "bearer" ) {
370
+ return jwtSplit[ 1 ];
371
+ } else {
372
+ throwError( customErrorFunction );
373
+ }
374
+
375
+ return null;
376
+ }
377
+
378
+ function _extractWebToken( jwtRaw, customErrorFunction ) {
379
+
380
+ if ( jwtRaw && typeof jwtRaw === "string" ) {
381
+ return jwtRaw;
382
+ } else {
383
+ throwError( customErrorFunction );
384
+ }
385
+
386
+ return null;
387
+ }
388
+
389
+ function _extractJwtNoThrow( jwtRaw ) {
390
+ let jwtSplit = null;
391
+
392
+ if ( jwtRaw && typeof jwtRaw === "string" ) {
393
+ jwtSplit = jwtRaw.split( " " );
394
+ } else {
395
+ return null;
396
+ }
397
+
398
+ if ( jwtSplit[ 0 ]?.toLowerCase() === "bearer" ) {
399
+ return jwtSplit[ 1 ];
400
+ }
401
+
402
+ return null;
403
+ }
404
+
405
+ function _extractWebTokenNoThrow( jwtRaw ) {
406
+
407
+ if ( jwtRaw && typeof jwtRaw === "string" ) {
408
+ return jwtRaw;
409
+ }
410
+ return null;
411
+ }
412
+
413
+ module.exports = {
414
+ _validateJwt,
415
+ _validateJwtNoThrow,
416
+ _isJwtSignatureValid,
417
+ _isJwtSignatureValidNoThrow,
418
+ _extractJwtObject,
419
+ _extractJwtObjectNoThrow,
420
+ validateAndExtractJwtObject,
421
+ jwtAgeInMilliseconds,
422
+ isJwtExpired,
423
+ doesJwtUserHasRole,
424
+ jwtClientId,
425
+ visitorClientId,
426
+ verifyJwtAndRole,
427
+ verifyJwt,
428
+ verifyWebTokenNoThrow,
429
+ verifyWebToken,
430
+ verifyJwtNoThrow,
431
+ throwUsedTokenError,
432
+ throwError,
433
+ verifyVisitorNoThrow
434
+ }
@@ -0,0 +1,20 @@
1
+ module.exports = {
2
+ getNameOfRoleFromCode,
3
+ getCodeFromNameOfRole,
4
+ }
5
+
6
+ function getNameOfRoleFromCode( roleCode ) {
7
+ const codesToCategory = {
8
+ "ad": "admin",
9
+ "su": "super_admin",
10
+ }
11
+ return codesToCategory[ roleCode ] || "";
12
+ }
13
+
14
+ function getCodeFromNameOfRole( roleCode ) {
15
+ const codesToCategory = {
16
+ "admin": "ad",
17
+ "super_admin": "su",
18
+ }
19
+ return codesToCategory[ roleCode ] || "";
20
+ }
package/package.json CHANGED
@@ -1,61 +1,28 @@
1
1
  {
2
2
  "name": "@carecard/jwt-read",
3
- "version": "1.0.3",
4
- "private": false,
5
- "description": "Jwt read functions",
6
- "license": "ISC",
7
- "author": "PK Singh",
3
+ "version": "2.0.1",
8
4
  "repository": {
9
5
  "type": "git",
10
- "url": "git+https:github.com/CareCard-ca/pkg-jwt-read.git"
6
+ "url": "https://github.com/CareCard-ca/pkg-jwt-read.git"
7
+ },
8
+ "description": "Jwt read functions",
9
+ "main": "index.js",
10
+ "scripts": {
11
+ "test": "export NODE_ENV=test && mocha --watch --recursive"
11
12
  },
12
13
  "keywords": [
13
14
  "auth",
14
15
  "utility",
15
16
  "cryptology"
16
17
  ],
17
- "type": "module",
18
- "publishConfig": {
19
- "access": "public"
20
- },
21
- "main": "./dist/cjs/index.cjs",
22
- "module": "./dist/esm/index.js",
23
- "types": "./dist/esm/index.d.ts",
24
- "exports": {
25
- ".": {
26
- "import": "./dist/esm/index.js",
27
- "require": "./dist/cjs/index.cjs",
28
- "types": "./dist/esm/index.d.ts"
29
- }
30
- },
31
- "files": [
32
- "dist"
33
- ],
34
- "scripts": {
35
- "build": "npm run build:esm && npm run build:cjs",
36
- "build:esm": "tsc -p tsconfig.esm.json",
37
- "build:cjs": "tsc -p tsconfig.cjs.json && node ./scripts/rename-cjs.js",
38
- "test": "NODE_NO_WARNINGS=1 jest --coverage",
39
- "format": "prettier --write .",
40
- "format:check": "prettier --check .",
41
- "lint": "eslint",
42
- "prepare": "husky"
43
- },
18
+ "author": "CareCard team",
19
+ "license": "ISC",
44
20
  "devDependencies": {
45
- "@types/express": "5.0.6",
46
- "@types/jest": "30.0.0",
47
- "eslint": "9.39.2",
48
- "husky": "9.1.7",
49
- "jest": "30.2.0",
50
- "prettier": "3.7.4",
51
- "ts-jest": "29.4.6",
52
- "typescript": "5.9.3",
53
- "typescript-eslint": "8.50.1"
21
+ "mocha": "11.7.4"
54
22
  },
55
23
  "dependencies": {
56
- "@carecard/auth-util": "1.0.1",
57
- "@carecard/common-util": "1.0.1",
58
- "@carecard/validate": "1.0.1",
59
- "express": "5.2.1"
24
+ "@carecard/auth-util": "2.0.1",
25
+ "@carecard/common-util": "2.0.1",
26
+ "@carecard/validate": "2.0.1"
60
27
  }
61
28
  }
package/readme.md CHANGED
@@ -7,7 +7,7 @@ This is a collection of jwt reading functions.
7
7
  ### Main functions
8
8
 
9
9
  ```js
10
- const jwtReader = require( '@carecard/jwt-read' );
10
+ const jwtReader = require( '@chatpta/jwt-read' );
11
11
  ```
12
12
 
13
13
  Read jwt
@@ -22,9 +22,11 @@ Following the above ```req.jwt``` contains
22
22
  ```js
23
23
  {
24
24
  header: {
25
- alg: ''
25
+ alg:...
26
26
  }
27
+ ,
27
28
  payload: {
29
+ ...
28
30
  }
29
31
  }
30
32
  ```
File without changes
File without changes
File without changes
File without changes