@carecard/jwt-read 3.0.1 → 3.0.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/lib/jwtLib.js CHANGED
@@ -44,6 +44,12 @@ function isJwtExpired( req, jwtValiditySeconds ) {
44
44
  req = undefined;
45
45
  }
46
46
 
47
+ const jwtObj = req?.jwt || this;
48
+
49
+ if ( jwtObj?.payload?.exp ) {
50
+ return ( Math.floor( Date.now() / 1000 ) ) >= jwtObj.payload.exp;
51
+ }
52
+
47
53
  if ( jwtValiditySeconds && typeof jwtValiditySeconds === "number" ) {
48
54
  return ( parseInt( jwtValiditySeconds ) ) < jwtAgeInSeconds.call( this, req );
49
55
  } else {
@@ -75,12 +81,12 @@ function jwtAgeInSeconds( req ) {
75
81
 
76
82
  async function validateAndExtractJwtObject( req, publicKey, customErrorFunction ) {
77
83
 
78
- const jwtString = await _validateJwt( req, customErrorFunction );
84
+ const jwtString = _validateJwt( req, customErrorFunction );
79
85
 
80
- const isJwtSignatureValid = await _isJwtSignatureValid( jwtString, publicKey, customErrorFunction );
86
+ const isJwtSignatureValid = jwtUtilAuth.verifyJwtSignature( jwtString, publicKey );
81
87
 
82
88
  if ( jwtString && isJwtSignatureValid ) {
83
- await _extractJwtObject( req, jwtString, customErrorFunction );
89
+ _extractJwtObject( req, jwtString, customErrorFunction );
84
90
  } else {
85
91
  req[ "jwt" ] = null;
86
92
  throwError( customErrorFunction );
@@ -91,12 +97,12 @@ async function validateAndExtractJwtObject( req, publicKey, customErrorFunction
91
97
 
92
98
  async function validateAndExtractWebToken( req, publicKey, headerName, customErrorFunction ) {
93
99
 
94
- const webTokenString = await _validateWebToken( req, headerName, customErrorFunction );
100
+ const webTokenString = _validateWebToken( req, headerName, customErrorFunction );
95
101
 
96
- const isJwtSignatureValid = await _isJwtSignatureValid( webTokenString, publicKey, customErrorFunction );
102
+ const isJwtSignatureValid = jwtUtilAuth.verifyJwtSignature( webTokenString, publicKey );
97
103
 
98
104
  if ( webTokenString && isJwtSignatureValid ) {
99
- await _extractJwtObject( req, webTokenString, customErrorFunction );
105
+ _extractJwtObject( req, webTokenString, customErrorFunction );
100
106
  } else {
101
107
  req[ "jwt" ] = null;
102
108
  throwError( customErrorFunction );
@@ -109,12 +115,12 @@ async function validateAndExtractJwtObjectNoThrow( req, publicKey ) {
109
115
 
110
116
  try {
111
117
 
112
- const jwtString = await _validateJwtNoThrow( req );
118
+ const jwtString = _validateJwtNoThrow( req );
113
119
 
114
- const isJwtSignatureValid = await _isJwtSignatureValidNoThrow( jwtString, publicKey );
120
+ const isJwtSignatureValid = jwtUtilAuth.verifyJwtSignature( jwtString, publicKey );
115
121
 
116
122
  if ( jwtString && isJwtSignatureValid ) {
117
- await _extractJwtObjectNoThrow( req, jwtString );
123
+ _extractJwtObjectNoThrow( req, jwtString );
118
124
  } else {
119
125
  req[ "jwt" ] = null;
120
126
  }
@@ -134,10 +140,10 @@ async function validateAndExtractWebTokenObjectNoThrow( req, publicKey, headerNa
134
140
 
135
141
  const jwtString = _validateWebTokenNoThrow( req, headerName );
136
142
 
137
- const isJwtSignatureValid = await _isJwtSignatureValidNoThrow( jwtString, publicKey );
143
+ const isJwtSignatureValid = jwtUtilAuth.verifyJwtSignature( jwtString, publicKey );
138
144
 
139
145
  if ( jwtString && isJwtSignatureValid ) {
140
- await _extractJwtObjectNoThrow( req, jwtString );
146
+ _extractJwtObjectNoThrow( req, jwtString );
141
147
  } else {
142
148
  req[ "jwt" ] = null;
143
149
  }
@@ -155,12 +161,12 @@ async function validateAndExtractVisitorObjectNoThrow( req, publicKey ) {
155
161
 
156
162
  try {
157
163
 
158
- const jwtString = await _validateVisitorNoThrow( req );
164
+ const jwtString = _validateVisitorNoThrow( req );
159
165
 
160
- const isJwtSignatureValid = await _isJwtSignatureValidNoThrow( jwtString, publicKey );
166
+ const isJwtSignatureValid = jwtUtilAuth.verifyJwtSignature( jwtString, publicKey );
161
167
 
162
168
  if ( jwtString && isJwtSignatureValid ) {
163
- await _extractVisitorObjectNoThrow( req, jwtString );
169
+ _extractVisitorObjectNoThrow( req, jwtString );
164
170
  } else {
165
171
  req[ "visitor" ] = null;
166
172
  }
@@ -239,7 +245,7 @@ function _isLoginRequired( hasRequiredRole, customErrorFunction ) {
239
245
 
240
246
  }
241
247
 
242
- async function _extractJwtObject( req, jwt, customErrorFunction ) {
248
+ function _extractJwtObject( req, jwt, customErrorFunction ) {
243
249
  if ( jwt && typeof jwt === "string" ) {
244
250
  const jwtObj = jwtUtilAuth.getHeaderPayloadFromJwt( jwt );
245
251
  _attachJwtMethods( jwtObj );
@@ -249,7 +255,7 @@ async function _extractJwtObject( req, jwt, customErrorFunction ) {
249
255
  }
250
256
  }
251
257
 
252
- async function _extractJwtObjectNoThrow( req, jwt ) {
258
+ function _extractJwtObjectNoThrow( req, jwt ) {
253
259
  if ( jwt && typeof jwt === "string" ) {
254
260
  const jwtObj = jwtUtilAuth.getHeaderPayloadFromJwt( jwt );
255
261
  _attachJwtMethods( jwtObj );
@@ -259,7 +265,7 @@ async function _extractJwtObjectNoThrow( req, jwt ) {
259
265
  }
260
266
  }
261
267
 
262
- async function _extractVisitorObjectNoThrow( req, jwt ) {
268
+ function _extractVisitorObjectNoThrow( req, jwt ) {
263
269
  if ( jwt && typeof jwt === "string" ) {
264
270
  const visitorObj = jwtUtilAuth.getHeaderPayloadFromJwt( jwt );
265
271
  _attachVisitorMethods( visitorObj );
@@ -306,7 +312,7 @@ async function _isJwtSignatureValidNoThrow( jwt, publicKey ) {
306
312
  }
307
313
  }
308
314
 
309
- async function _validateJwt( req, customErrorFunction ) {
315
+ function _validateJwt( req, customErrorFunction ) {
310
316
 
311
317
  // Get from http header.
312
318
  let jwtRaw = req.get( "Authorization" );
@@ -322,7 +328,7 @@ async function _validateJwt( req, customErrorFunction ) {
322
328
  }
323
329
  }
324
330
 
325
- async function _validateWebToken( req, headerName, customErrorFunction ) {
331
+ function _validateWebToken( req, headerName, customErrorFunction ) {
326
332
 
327
333
  // Get from http header.
328
334
  let jwtRaw = req.get( headerName );
package/lib/jwtRoles.js CHANGED
@@ -7,14 +7,14 @@ function getNameOfRoleFromCode( roleCode ) {
7
7
  const codesToCategory = {
8
8
  "ad": "admin",
9
9
  "su": "super_admin",
10
- }
10
+ };
11
11
  return codesToCategory[ roleCode ] || "";
12
12
  }
13
13
 
14
- function getCodeFromNameOfRole( roleCode ) {
15
- const codesToCategory = {
14
+ function getCodeFromNameOfRole( roleName ) {
15
+ const namesToCode = {
16
16
  "admin": "ad",
17
17
  "super_admin": "su",
18
- }
19
- return codesToCategory[ roleCode ] || "";
18
+ };
19
+ return namesToCode[ roleName ] || "";
20
20
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@carecard/jwt-read",
3
- "version": "3.0.1",
3
+ "version": "3.0.2",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "https://github.com/CareCard-ca/pkg-jwt-read.git"
@@ -21,7 +21,7 @@
21
21
  "mocha": "11.7.5"
22
22
  },
23
23
  "dependencies": {
24
- "@carecard/auth-util": "3.0.1",
24
+ "@carecard/auth-util": "3.0.2",
25
25
  "@carecard/common-util": "2.0.2",
26
26
  "@carecard/validate": "2.2.12"
27
27
  }