@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/lib/jwtRoles.js CHANGED
@@ -1,20 +1,39 @@
1
+ const { jwtClientId } = require('./jwtLib');
2
+
1
3
  module.exports = {
2
- getNameOfRoleFromCode,
3
- getCodeFromNameOfRole,
4
+ getNameOfRoleFromCode,
5
+ getCodeFromNameOfRole,
6
+ getContext,
7
+ };
8
+
9
+ function getNameOfRoleFromCode(roleCode) {
10
+ const codesToCategory = {
11
+ ad: 'admin',
12
+ su: 'super_admin',
13
+ };
14
+ return codesToCategory[roleCode] || '';
4
15
  }
5
16
 
6
- function getNameOfRoleFromCode( roleCode ) {
7
- const codesToCategory = {
8
- "ad": "admin",
9
- "su": "super_admin",
10
- };
11
- return codesToCategory[ roleCode ] || "";
17
+ function getCodeFromNameOfRole(roleName) {
18
+ const namesToCode = {
19
+ admin: 'ad',
20
+ super_admin: 'su',
21
+ };
22
+ return namesToCode[roleName] || '';
12
23
  }
13
24
 
14
- function getCodeFromNameOfRole( roleName ) {
15
- const namesToCode = {
16
- "admin": "ad",
17
- "super_admin": "su",
25
+ function getContext(req) {
26
+ const userId = jwtClientId(req);
27
+ const roles = req.jwt?.payload?.roles;
28
+
29
+ if (Array.isArray(roles) && roles.includes('ad')) {
30
+ return {
31
+ user_id: userId,
32
+ role: 'super_admin',
18
33
  };
19
- return namesToCode[ roleName ] || "";
34
+ }
35
+
36
+ return {
37
+ user_id: userId,
38
+ };
20
39
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@carecard/jwt-read",
3
- "version": "3.1.13",
3
+ "version": "3.1.15",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "https://github.com/CareCard-ca/pkg-jwt-read.git"
@@ -9,10 +9,15 @@
9
9
  "main": "index.js",
10
10
  "types": "index.d.ts",
11
11
  "scripts": {
12
- "test": "export NODE_ENV=test && mocha --recursive",
12
+ "test": "mocha --recursive",
13
13
  "test:types": "tsc --noEmit && mocha -r ts-node/register test/types.test.ts",
14
- "test:coverage": "tsc --noEmit && export NODE_ENV=test && nyc mocha --recursive -r ts-node/register 'test/**/*.{js,ts}'",
15
- "prepare": "husky"
14
+ "test:coverage": "tsc --noEmit && nyc mocha --recursive -r ts-node/register 'test/**/*.{js,ts}'",
15
+ "test:All": "npm run test && npm run test:types",
16
+ "format": "prettier --write .",
17
+ "format:check": "prettier --check .",
18
+ "prepare": "husky",
19
+ "lint:fix": "eslint --fix",
20
+ "lint": "eslint"
16
21
  },
17
22
  "keywords": [
18
23
  "auth",
@@ -25,16 +30,19 @@
25
30
  "devDependencies": {
26
31
  "@types/express": "5.0.6",
27
32
  "@types/mocha": "10.0.10",
28
- "@types/node": "25.5.0",
29
- "husky": "^9.1.7",
30
- "mocha": "11.7.5",
31
- "nyc": "^18.0.0",
33
+ "@types/node": "25.9.1",
34
+ "eslint": "9.39.4",
35
+ "husky": "9.1.7",
36
+ "lint-staged": "17.0.5",
37
+ "mocha": "11.7.6",
38
+ "nyc": "18.0.0",
39
+ "prettier": "3.8.3",
32
40
  "ts-node": "10.9.2",
33
- "typescript": "5.9.3"
41
+ "typescript": "6.0.3"
34
42
  },
35
43
  "dependencies": {
36
- "@carecard/common-util": "3.1.11",
37
- "@carecard/auth-util": "3.1.12",
38
- "@carecard/validate": "3.0.10"
44
+ "@carecard/common-util": "3.1.15",
45
+ "@carecard/auth-util": "3.1.15",
46
+ "@carecard/validate": "3.1.24"
39
47
  }
40
48
  }
package/readme.md CHANGED
@@ -12,6 +12,7 @@ Utility package for reading, parsing, and verifying JWTs in the CareCard ecosyst
12
12
  - **Role Mapping**: Simple utility for translating internal role codes to human-readable names.
13
13
  - **Claims Extraction**: Easy extraction of `sub` (clientId) and other JWT payload claims.
14
14
  - **Expiration Management**: Helpers to check if a JWT is expired and calculate its remaining TTL.
15
+ - **Service JWTs**: Helpers for verifying and extracting microservice-to-microservice JWTs with standard `iss`, `sub`, `aud`, `iat`, and `exp` claims.
15
16
 
16
17
  ## Installation
17
18
 
@@ -31,11 +32,11 @@ const verifyAdmin = verifyJwtAndRole('admin', publicKey, throwUsedTokenError);
31
32
 
32
33
  // In an Express controller/middleware
33
34
  try {
34
- await verifyAdmin(req, res, next);
35
- // If successful, req.jwt contains { header, payload }
36
- console.log(req.jwt.payload.sub);
35
+ await verifyAdmin(req, res, next);
36
+ // If successful, req.jwt contains { header, payload }
37
+ console.log(req.jwt.payload.sub);
37
38
  } catch (error) {
38
- // Handle verification error
39
+ // Handle verification error
39
40
  }
40
41
  ```
41
42
 
@@ -46,7 +47,7 @@ const { verifyJwt, isJwtExpired } = require('@carecard/jwt-read');
46
47
 
47
48
  const result = verifyJwt(rawJwt, publicKey);
48
49
  if (result && !isJwtExpired(result)) {
49
- console.log('JWT is valid and not expired:', result.payload);
50
+ console.log('JWT is valid and not expired:', result.payload);
50
51
  }
51
52
  ```
52
53
 
@@ -59,6 +60,34 @@ console.log(getNameOfRole('ad')); // Result: 'admin'
59
60
  console.log(getCodeOfRole('super_admin')); // Result: 'su'
60
61
  ```
61
62
 
63
+ ### Service-To-Service JWTs
64
+
65
+ Use service JWT verification helpers for backend service calls. The sending
66
+ service signs the token with `@carecard/auth-util`. The receiving service uses
67
+ this package to verify the token with the sending service public key and check
68
+ the expected issuer and audience.
69
+
70
+ ```javascript
71
+ const { jwtCreateServiceAuthorizationHeader } = require('@carecard/auth-util');
72
+ const { jwtVerifyService } = require('@carecard/jwt-read');
73
+
74
+ const authorization = jwtCreateServiceAuthorizationHeader({
75
+ issuer: 'ms-institutions',
76
+ audience: 'ms-auth',
77
+ privateKey: institutionsPrivateKey,
78
+ });
79
+
80
+ app.use(jwtVerifyService(institutionsPublicKey, 'ms-institutions', 'ms-auth', throwNotAuthorizedError));
81
+ ```
82
+
83
+ Service JWT payloads follow standard JWT semantics:
84
+
85
+ - `iss`: sending service
86
+ - `sub`: sending service identity
87
+ - `aud`: receiving service
88
+ - `iat`: issued-at NumericDate
89
+ - `exp`: expiration NumericDate
90
+
62
91
  ## Testing
63
92
 
64
93
  Run tests using:
@@ -82,6 +111,7 @@ npm run test:types
82
111
  ## Architecture
83
112
 
84
113
  The package is organized into several modules:
114
+
85
115
  - `jwtLib`: Main logic for JWT verification, extraction, and Express integration.
86
116
  - `jwtRoles`: Role mapping between internal codes and names.
87
117