@carecard/jwt-read 3.1.27 → 3.1.28
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 +5 -0
- package/lib/jwtRoles.js +19 -7
- package/package.json +1 -1
- package/readme.md +6 -0
package/index.d.ts
CHANGED
|
@@ -281,11 +281,16 @@ export interface JwtContext {
|
|
|
281
281
|
user_id: string | undefined;
|
|
282
282
|
/** The role of the user. Only set to 'super_admin' when roles contains 'ad'. */
|
|
283
283
|
role?: string;
|
|
284
|
+
/** Decoded scoped authorization claims from X-Authorization-Context when verified and attached. */
|
|
285
|
+
authorizationContext?: UserAuthorizationPayload;
|
|
286
|
+
/** The verified scoped authorization object attached to the request. */
|
|
287
|
+
userAuthorization?: UserAuthorizationRequestObject;
|
|
284
288
|
}
|
|
285
289
|
|
|
286
290
|
/**
|
|
287
291
|
* Returns the context derived from the JWT in req.jwt.
|
|
288
292
|
* Always returns user_id. If the roles array contains 'ad', also returns role: 'super_admin'.
|
|
293
|
+
* If req.userAuthorization is present, also returns authorizationContext and userAuthorization.
|
|
289
294
|
*/
|
|
290
295
|
export function jwtGetContext(req: any): JwtContext;
|
|
291
296
|
|
package/lib/jwtRoles.js
CHANGED
|
@@ -25,15 +25,27 @@ function getCodeFromNameOfRole(roleName) {
|
|
|
25
25
|
function getContext(req) {
|
|
26
26
|
const userId = jwtClientId(req);
|
|
27
27
|
const roles = req.jwt?.payload?.roles;
|
|
28
|
+
const userAuthorization = getUserAuthorization(req);
|
|
29
|
+
const context = {
|
|
30
|
+
user_id: userId,
|
|
31
|
+
};
|
|
28
32
|
|
|
29
33
|
if (Array.isArray(roles) && roles.includes('ad')) {
|
|
30
|
-
|
|
31
|
-
user_id: userId,
|
|
32
|
-
role: 'super_admin',
|
|
33
|
-
};
|
|
34
|
+
context.role = 'super_admin';
|
|
34
35
|
}
|
|
35
36
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
37
|
+
if (userAuthorization) {
|
|
38
|
+
context.authorizationContext = userAuthorization.payload;
|
|
39
|
+
context.userAuthorization = userAuthorization;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
return context;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function getUserAuthorization(req) {
|
|
46
|
+
const userAuthorization = req?.userAuthorization;
|
|
47
|
+
if (!userAuthorization || typeof userAuthorization !== 'object') return null;
|
|
48
|
+
if (!userAuthorization.payload || typeof userAuthorization.payload !== 'object') return null;
|
|
49
|
+
|
|
50
|
+
return userAuthorization;
|
|
39
51
|
}
|
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -174,6 +174,12 @@ leaves `req.userAuthorization` as `null`. If the header is present but invalid,
|
|
|
174
174
|
throwing middleware fails closed. No-throw middleware clears
|
|
175
175
|
`req.userAuthorization` and continues.
|
|
176
176
|
|
|
177
|
+
`jwtGetContext(req)` preserves the normal database caller shape of `user_id`
|
|
178
|
+
and optional `role`. When a verified `req.userAuthorization.payload` is present,
|
|
179
|
+
it also returns `authorizationContext` with those compact claims and the original
|
|
180
|
+
`userAuthorization` object so service models can set `app.authz_context` for
|
|
181
|
+
RLS without rebuilding the full authorization graph.
|
|
182
|
+
|
|
177
183
|
## Testing
|
|
178
184
|
|
|
179
185
|
Run tests using:
|