@carecard/jwt-read 3.1.13 → 3.1.15

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 CHANGED
@@ -2,7 +2,7 @@
2
2
  * Utility functions for authentication and authorization in the CareCard ecosystem.
3
3
  */
4
4
 
5
- import { Request, Response, NextFunction } from 'express';
5
+ import { NextFunction, Request, Response } from 'express';
6
6
 
7
7
  /**
8
8
  * Represents the standard JWT header structure.
@@ -50,44 +50,50 @@ export interface JwtParts {
50
50
  * Structure of the JWT object attached to the request.
51
51
  */
52
52
  export interface JwtRequestObject {
53
- header: JwtHeader;
54
- payload: JwtPayload;
55
- age?: number;
56
- jwtClientId: (req?: any) => string | undefined;
57
- doesJwtUserHasRole: (role: string) => boolean;
58
- isJwtExpired: (jwtValiditySeconds?: number) => boolean;
59
- jwtAgeInSeconds: (req?: any) => number;
53
+ header: JwtHeader;
54
+ payload: JwtPayload;
55
+ age?: number;
56
+ jwtClientId: (req?: any) => string | undefined;
57
+ doesJwtUserHasRole: (role: string) => boolean;
58
+ isJwtExpired: (jwtValiditySeconds?: number) => boolean;
59
+ jwtAgeInSeconds: (req?: any) => number;
60
60
  }
61
61
 
62
62
  /**
63
63
  * Structure of the visitor object attached to the request.
64
64
  */
65
65
  export interface VisitorRequestObject {
66
- header: JwtHeader;
67
- payload: JwtPayload;
68
- visitorClientId: (req?: any) => string | undefined;
66
+ header: JwtHeader;
67
+ payload: JwtPayload;
68
+ visitorClientId: (req?: any) => string | undefined;
69
69
  }
70
70
 
71
71
  /**
72
72
  * Extended Express Request to include jwt and visitor objects.
73
73
  */
74
74
  export interface AuthenticatedRequest extends Request {
75
- jwt?: JwtRequestObject | null;
76
- visitor?: VisitorRequestObject | null;
75
+ jwt?: JwtRequestObject | null;
76
+ visitor?: VisitorRequestObject | null;
77
77
  }
78
78
 
79
-
80
79
  /**
81
80
  * Returns a middleware that verifies a JWT from the 'Authorization: Bearer <token>' header
82
81
  * and extracts it into req.jwt. Throws an error if invalid.
83
82
  */
84
- export function jwtVerify(publicKey: string, customErrorFunction?: () => void): (req: AuthenticatedRequest, res: Response, next: NextFunction) => void;
83
+ export function jwtVerify(
84
+ publicKey: string,
85
+ customErrorFunction?: () => void,
86
+ ): (req: AuthenticatedRequest, res: Response, next: NextFunction) => void;
85
87
 
86
88
  /**
87
89
  * Returns a middleware that verifies a JWT from a custom header and extracts it into req.jwt.
88
90
  * Throws an error if invalid.
89
91
  */
90
- export function jwtVerifyWebToken(publicKey: string, headerName: string, customErrorFunction?: () => void): (req: AuthenticatedRequest, res: Response, next: NextFunction) => void;
92
+ export function jwtVerifyWebToken(
93
+ publicKey: string,
94
+ headerName: string,
95
+ customErrorFunction?: () => void,
96
+ ): (req: AuthenticatedRequest, res: Response, next: NextFunction) => void;
91
97
 
92
98
  /**
93
99
  * Returns a middleware that verifies a JWT from the 'Authorization: Bearer <token>' header
@@ -99,7 +105,10 @@ export function jwtVerifyNoThrow(publicKey: string): (req: AuthenticatedRequest,
99
105
  * Returns a middleware that verifies a JWT from a custom header and extracts it into req.jwt.
100
106
  * Returns false instead of throwing if invalid.
101
107
  */
102
- export function jwtVerifyWebTokenNoThrow(publicKey: string, headerName: string): (req: AuthenticatedRequest, res: Response, next: NextFunction) => void;
108
+ export function jwtVerifyWebTokenNoThrow(
109
+ publicKey: string,
110
+ headerName: string,
111
+ ): (req: AuthenticatedRequest, res: Response, next: NextFunction) => void;
103
112
 
104
113
  /**
105
114
  * Returns a middleware that verifies a visitor token from the 'Visitor' header
@@ -132,7 +141,11 @@ export function jwtGetAgeInSeconds(req?: any): number;
132
141
  /**
133
142
  * Returns a middleware that verifies the JWT and checks if the user has the required role.
134
143
  */
135
- export function jwtVerifyAndHasRole(userRole: string, publicKey: string, customErrorFunction?: () => void): (req: AuthenticatedRequest, res: Response, next: NextFunction) => void;
144
+ export function jwtVerifyAndHasRole(
145
+ userRole: string,
146
+ publicKey: string,
147
+ customErrorFunction?: () => void,
148
+ ): (req: AuthenticatedRequest, res: Response, next: NextFunction) => void;
136
149
 
137
150
  /**
138
151
  * Gets the full name of a role from its code (e.g., 'ad' -> 'admin').
@@ -144,15 +157,47 @@ export function jwtGetRoleName(roleCode: string): string;
144
157
  */
145
158
  export function jwtGetRoleCode(roleName: string): string;
146
159
 
160
+ /**
161
+ * Represents the context derived from the JWT request.
162
+ */
163
+ export interface JwtContext {
164
+ /** The user ID (sub) from the JWT. Always returned. */
165
+ user_id: string | undefined;
166
+ /** The role of the user. Only set to 'super_admin' when roles contains 'ad'. */
167
+ role?: string;
168
+ }
169
+
170
+ /**
171
+ * Returns the context derived from the JWT in req.jwt.
172
+ * Always returns user_id. If the roles array contains 'ad', also returns role: 'super_admin'.
173
+ */
174
+ export function jwtGetContext(req: any): JwtContext;
175
+
147
176
  /**
148
177
  * Validates the JWT from the Authorization header and extracts it into req.jwt.
149
178
  */
150
179
  export function jwtValidateAndExtract(req: AuthenticatedRequest, publicKey: string, customErrorFunction?: () => void): void;
151
180
 
181
+ /**
182
+ * Validates a service-to-service JWT from the Authorization header and extracts it into req.jwt.
183
+ */
184
+ export function jwtValidateAndExtractService(
185
+ req: AuthenticatedRequest,
186
+ publicKey: string,
187
+ expectedIssuer: string,
188
+ expectedAudience: string,
189
+ customErrorFunction?: () => void,
190
+ ): void;
191
+
152
192
  /**
153
193
  * Validates the JWT from a custom header and extracts it into req.jwt.
154
194
  */
155
- export function jwtValidateAndExtractWebToken(req: AuthenticatedRequest, publicKey: string, headerName: string, customErrorFunction?: () => void): void;
195
+ export function jwtValidateAndExtractWebToken(
196
+ req: AuthenticatedRequest,
197
+ publicKey: string,
198
+ headerName: string,
199
+ customErrorFunction?: () => void,
200
+ ): void;
156
201
 
157
202
  /**
158
203
  * Validates the JWT from the Authorization header and extracts it into req.jwt (no-throw).
@@ -169,20 +214,36 @@ export function jwtValidateAndExtractWebTokenNoThrow(req: AuthenticatedRequest,
169
214
  */
170
215
  export function jwtValidateAndExtractVisitorNoThrow(req: AuthenticatedRequest, publicKey: string): void;
171
216
 
217
+ /**
218
+ * Returns middleware that verifies a service-to-service JWT from one expected sender.
219
+ */
220
+ export function jwtVerifyService(
221
+ publicKey: string,
222
+ expectedIssuer: string,
223
+ expectedAudience: string,
224
+ customErrorFunction?: () => void,
225
+ ): (req: AuthenticatedRequest, res: Response, next: NextFunction) => void;
172
226
 
173
227
  /**
174
228
  * Returns a middleware that verifies a JWT from the 'Authorization: Bearer <token>' header
175
229
  * and extracts it into req.jwt. Throws an error if invalid.
176
230
  * @deprecated use jwtVerify
177
231
  */
178
- export function verifyJwt(publicKey: string, customErrorFunction?: () => void): (req: AuthenticatedRequest, res: Response, next: NextFunction) => void;
232
+ export function verifyJwt(
233
+ publicKey: string,
234
+ customErrorFunction?: () => void,
235
+ ): (req: AuthenticatedRequest, res: Response, next: NextFunction) => void;
179
236
 
180
237
  /**
181
238
  * Returns a middleware that verifies a JWT from a custom header and extracts it into req.jwt.
182
239
  * Throws an error if invalid.
183
240
  * @deprecated use jwtVerifyWebToken
184
241
  */
185
- export function verifyWebToken(publicKey: string, headerName: string, customErrorFunction?: () => void): (req: AuthenticatedRequest, res: Response, next: NextFunction) => void;
242
+ export function verifyWebToken(
243
+ publicKey: string,
244
+ headerName: string,
245
+ customErrorFunction?: () => void,
246
+ ): (req: AuthenticatedRequest, res: Response, next: NextFunction) => void;
186
247
 
187
248
  /**
188
249
  * Returns a middleware that verifies a JWT from the 'Authorization: Bearer <token>' header
@@ -196,7 +257,10 @@ export function verifyJwtNoThrow(publicKey: string): (req: AuthenticatedRequest,
196
257
  * Returns false instead of throwing if invalid.
197
258
  * @deprecated use jwtVerifyWebTokenNoThrow
198
259
  */
199
- export function verifyWebTokenNoThrow(publicKey: string, headerName: string): (req: AuthenticatedRequest, res: Response, next: NextFunction) => void;
260
+ export function verifyWebTokenNoThrow(
261
+ publicKey: string,
262
+ headerName: string,
263
+ ): (req: AuthenticatedRequest, res: Response, next: NextFunction) => void;
200
264
 
201
265
  /**
202
266
  * Returns a middleware that verifies a visitor token from the 'Visitor' header
@@ -237,7 +301,11 @@ export function jwtAgeInSeconds(req?: any): number;
237
301
  * Returns a middleware that verifies the JWT and checks if the user has the required role.
238
302
  * @deprecated use jwtVerifyAndHasRole
239
303
  */
240
- export function verifyJwtAndRole(userRole: string, publicKey: string, customErrorFunction?: () => void): (req: AuthenticatedRequest, res: Response, next: NextFunction) => void;
304
+ export function verifyJwtAndRole(
305
+ userRole: string,
306
+ publicKey: string,
307
+ customErrorFunction?: () => void,
308
+ ): (req: AuthenticatedRequest, res: Response, next: NextFunction) => void;
241
309
 
242
310
  /**
243
311
  * Throws a Used_Token error.
@@ -245,7 +313,6 @@ export function verifyJwtAndRole(userRole: string, publicKey: string, customErro
245
313
  */
246
314
  export function throwUsedTokenError(): never;
247
315
 
248
-
249
316
  /**
250
317
  * Checks if the user in the extracted JWT has the specified role.
251
318
  * @deprecated use jwtDoesJwtUserHasRole
@@ -260,10 +327,8 @@ export function doesJwtUserHasRole(userRole: string): boolean;
260
327
  */
261
328
  export function getNameOfRole(roleCode: string): string;
262
329
 
263
-
264
330
  /**
265
331
  * Gets the code of a role from its name (e.g., 'admin' -> 'ad').
266
332
  * @deprecated use jwtGetRoleCode
267
333
  */
268
334
  export function getCodeOfRole(roleName: string): string;
269
-
package/index.js CHANGED
@@ -2,50 +2,53 @@ const jwtLib = require('./lib/jwtLib');
2
2
  const jwtRoles = require('./lib/jwtRoles');
3
3
 
4
4
  module.exports = {
5
- jwtVerify: jwtLib.verifyJwt,
6
- jwtVerifyWebToken: jwtLib.verifyWebToken,
7
- jwtVerifyNoThrow: jwtLib.verifyJwtNoThrow,
8
- jwtVerifyWebTokenNoThrow: jwtLib.verifyWebTokenNoThrow,
9
- jwtVerifyVisitorNoThrow: jwtLib.verifyVisitorNoThrow,
10
- jwtGetClientId: jwtLib.jwtClientId,
11
- jwtGetVisitorClientId: jwtLib.visitorClientId,
12
- jwtIsExpired: jwtLib.isJwtExpired,
13
- jwtGetAgeInSeconds: jwtLib.jwtAgeInSeconds,
14
- jwtVerifyAndHasRole: jwtLib.verifyJwtAndRole,
15
- jwtGetRoleName: jwtRoles.getNameOfRoleFromCode,
16
- jwtGetRoleCode: jwtRoles.getCodeFromNameOfRole,
17
- jwtValidateAndExtract: jwtLib.validateAndExtractJwtObject,
18
- jwtValidateAndExtractWebToken: jwtLib.validateAndExtractWebToken,
19
- jwtValidateAndExtractNoThrow: jwtLib.validateAndExtractJwtObjectNoThrow,
20
- jwtValidateAndExtractWebTokenNoThrow: jwtLib.validateAndExtractWebTokenObjectNoThrow,
21
- jwtValidateAndExtractVisitorNoThrow: jwtLib.validateAndExtractVisitorObjectNoThrow,
5
+ jwtVerify: jwtLib.verifyJwt,
6
+ jwtVerifyWebToken: jwtLib.verifyWebToken,
7
+ jwtVerifyNoThrow: jwtLib.verifyJwtNoThrow,
8
+ jwtVerifyWebTokenNoThrow: jwtLib.verifyWebTokenNoThrow,
9
+ jwtVerifyVisitorNoThrow: jwtLib.verifyVisitorNoThrow,
10
+ jwtGetClientId: jwtLib.jwtClientId,
11
+ jwtGetVisitorClientId: jwtLib.visitorClientId,
12
+ jwtIsExpired: jwtLib.isJwtExpired,
13
+ jwtGetAgeInSeconds: jwtLib.jwtAgeInSeconds,
14
+ jwtVerifyAndHasRole: jwtLib.verifyJwtAndRole,
15
+ jwtGetRoleName: jwtRoles.getNameOfRoleFromCode,
16
+ jwtGetRoleCode: jwtRoles.getCodeFromNameOfRole,
17
+ jwtGetContext: jwtRoles.getContext,
18
+ jwtValidateAndExtract: jwtLib.validateAndExtractJwtObject,
19
+ jwtValidateAndExtractService: jwtLib.validateAndExtractServiceJwtObject,
20
+ jwtValidateAndExtractWebToken: jwtLib.validateAndExtractWebToken,
21
+ jwtValidateAndExtractNoThrow: jwtLib.validateAndExtractJwtObjectNoThrow,
22
+ jwtValidateAndExtractWebTokenNoThrow: jwtLib.validateAndExtractWebTokenObjectNoThrow,
23
+ jwtValidateAndExtractVisitorNoThrow: jwtLib.validateAndExtractVisitorObjectNoThrow,
24
+ jwtVerifyService: jwtLib.verifyServiceJwt,
22
25
 
23
- /** @deprecated use jwtVerify */
24
- verifyJwt: jwtLib.verifyJwt,
25
- /** @deprecated use jwtVerifyWebToken */
26
- verifyWebToken: jwtLib.verifyWebToken,
27
- /** @deprecated use jwtVerifyNoThrow */
28
- verifyJwtNoThrow: jwtLib.verifyJwtNoThrow,
29
- /** @deprecated use jwtVerifyWebTokenNoThrow */
30
- verifyWebTokenNoThrow: jwtLib.verifyWebTokenNoThrow,
31
- /** @deprecated use jwtVerifyVisitorNoThrow */
32
- verifyVisitorNoThrow: jwtLib.verifyVisitorNoThrow,
33
- /** @deprecated use jwtGetClientId */
34
- jwtClientId: jwtLib.jwtClientId,
35
- /** @deprecated use jwtGetVisitorClientId */
36
- visitorClientId: jwtLib.visitorClientId,
37
- /** @deprecated use jwtIsExpired */
38
- isJwtExpired: jwtLib.isJwtExpired,
39
- /** @deprecated use jwtGetAgeInSeconds */
40
- jwtAgeInSeconds: jwtLib.jwtAgeInSeconds,
41
- /** @deprecated use jwtVerifyAndHasRole */
42
- verifyJwtAndRole: jwtLib.verifyJwtAndRole,
43
- /** @deprecated use jwtThrowUsedTokenError */
44
- throwUsedTokenError: jwtLib.throwUsedTokenError,
45
- /** @deprecated use jwtDoesJwtUserHasRole */
46
- doesJwtUserHasRole: jwtLib.doesJwtUserHasRole,
47
- /** @deprecated use jwtGetRoleName */
48
- getNameOfRole: jwtRoles.getNameOfRoleFromCode,
49
- /** @deprecated use jwtGetRoleCode */
50
- getCodeOfRole: jwtRoles.getCodeFromNameOfRole
26
+ /** @deprecated use jwtVerify */
27
+ verifyJwt: jwtLib.verifyJwt,
28
+ /** @deprecated use jwtVerifyWebToken */
29
+ verifyWebToken: jwtLib.verifyWebToken,
30
+ /** @deprecated use jwtVerifyNoThrow */
31
+ verifyJwtNoThrow: jwtLib.verifyJwtNoThrow,
32
+ /** @deprecated use jwtVerifyWebTokenNoThrow */
33
+ verifyWebTokenNoThrow: jwtLib.verifyWebTokenNoThrow,
34
+ /** @deprecated use jwtVerifyVisitorNoThrow */
35
+ verifyVisitorNoThrow: jwtLib.verifyVisitorNoThrow,
36
+ /** @deprecated use jwtGetClientId */
37
+ jwtClientId: jwtLib.jwtClientId,
38
+ /** @deprecated use jwtGetVisitorClientId */
39
+ visitorClientId: jwtLib.visitorClientId,
40
+ /** @deprecated use jwtIsExpired */
41
+ isJwtExpired: jwtLib.isJwtExpired,
42
+ /** @deprecated use jwtGetAgeInSeconds */
43
+ jwtAgeInSeconds: jwtLib.jwtAgeInSeconds,
44
+ /** @deprecated use jwtVerifyAndHasRole */
45
+ verifyJwtAndRole: jwtLib.verifyJwtAndRole,
46
+ /** @deprecated use jwtThrowUsedTokenError */
47
+ throwUsedTokenError: jwtLib.throwUsedTokenError,
48
+ /** @deprecated use jwtDoesJwtUserHasRole */
49
+ doesJwtUserHasRole: jwtLib.doesJwtUserHasRole,
50
+ /** @deprecated use jwtGetRoleName */
51
+ getNameOfRole: jwtRoles.getNameOfRoleFromCode,
52
+ /** @deprecated use jwtGetRoleCode */
53
+ getCodeOfRole: jwtRoles.getCodeFromNameOfRole,
51
54
  };