@carecard/auth-util 3.9.0 → 3.11.0

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.
@@ -7,6 +7,10 @@ description: 'Follow the shared SO_CareCardCa/CareCard workspace coding, testing
7
7
 
8
8
  Non-negotiable TDD rule: Always write the failing test first, run it to confirm it fails for the intended reason, then implement the code and rerun the test until it passes. Test Driven Development is required for all coding work and must not be skipped. For documentation- or skill-only edits, add or update the relevant validation check before changing the prose.
9
9
 
10
+ Non-negotiable repository isolation rule: Every repository must run its Husky hooks and tests using only files, code, fixtures, dependencies, and services contained within that repository. Tests and Husky scripts must not import, require, read, execute, or otherwise depend on sibling repositories or paths outside the repository root. app-e2e-tests is the only exception because cross-repository end-to-end testing is its explicit responsibility.
11
+
12
+ Non-negotiable error and warning rule: Never suppress, silence, hide, downgrade, filter, ignore, skip, or bypass errors or warnings from code, tests, tools, compilers, linters, or validation. Fix the root cause, then rerun the affected check and require a clean result. Expected error-path tests may assert errors, but must not conceal unexpected failures.
13
+
10
14
  Non-negotiable code organization rule: Functions with the same or equivalent behavior must use the same or clearly corresponding descriptive names across CareCard repositories, and equivalent functionality must live in files with the same names within each repository's established architecture. No backward compatibility names, aliases, or duplicate locations are allowed.
11
15
 
12
16
  ## Purpose
@@ -83,7 +87,7 @@ config.
83
87
  - Never suppress errors, TypeScript errors, linter warnings, authorization
84
88
  failures, RLS failures, build failures, hydration issues, or failing tests.
85
89
  Do not add `eslint-disable`, `@ts-ignore`, broad catches, empty catches, or
86
- similar suppression unless explicitly requested.
90
+ similar suppression. Fix the root cause.
87
91
  - Do not add dependencies unless clearly necessary. If one might be needed, ask
88
92
  first with the reason, tradeoff, and why existing code cannot solve it.
89
93
  - Before finalizing repository work, run the affected repository's relevant
@@ -212,8 +216,8 @@ build artifacts, logs, or `.DS_Store`.
212
216
 
213
217
  Most JavaScript `ms-*` services use CommonJS, Mocha, Supertest, Docker Compose
214
218
  database tests, `@carecard/*` packages, and `sub-apps`
215
- controller/router/model patterns. TypeScript services such as `ms-contact-us`
216
- and `ms-template-ts` use Jest or TypeScript tooling and should keep their
219
+ controller/router/model patterns. TypeScript services such as `ms-messages`
220
+ use Jest or TypeScript tooling and should keep their
217
221
  existing TypeScript style.
218
222
 
219
223
  - Keep environment-specific files explicit: `.env.development`, `.env.test`,
@@ -311,7 +315,7 @@ existing TypeScript style.
311
315
 
312
316
  `app-dashboard` is a Next.js App Router TypeScript app using MUI, React Query,
313
317
  `next-intl`, and shared CareCard utilities. It consumes `ms-auth`,
314
- `ms-institutions`, `ms-contact-us`, and `ms-user-profiles` through service
318
+ `ms-institutions`, `ms-messages`, and `ms-user-profiles` through service
315
319
  modules.
316
320
 
317
321
  - Keep backend URL definitions centralized in `src/services/api.routes.ts`.
@@ -378,7 +382,7 @@ the authenticated dashboard.
378
382
 
379
383
  ## Auth Service RLS Contract
380
384
 
381
- - `ms-auth` follows the shared PostgreSQL/RLS pattern from `ms-template-js`: auth tables live in the `carecard` schema, RLS is enabled and forced on every auth table, and application runtime queries use the unprivileged database role.
385
+ - `ms-auth` follows the shared PostgreSQL/RLS pattern: auth tables live in the `carecard` schema, RLS is enabled and forced on every auth table, and application runtime queries use the unprivileged database role.
382
386
  - Auth table policies allow normal JWT users to access only self-owned rows. Do not add redundant `user_id = <jwt sub>` SQL predicates to duplicate self-row checks when RLS owns the authorization decision.
383
387
  - A JWT payload containing `roles: ["ad"]` is the auth-service super-admin signal and can perform any action on auth tables. Dashboard code may map that role to `super_admin`, but backend auth RLS must not require a separate database role row for that bypass.
384
388
  - Public auth flows such as registration, login, confirmation, recovery, visitor creation, and service user lookup must use narrow system contexts (`system_create`, `system_login`, `system_confirm`, `system_recovery`, `system_visitor`, `system_service`) instead of privileged runtime queries.
@@ -7,6 +7,10 @@ description: 'Use only when the user explicitly asks for remote Git or GitHub PR
7
7
 
8
8
  Non-negotiable TDD rule: Always write the failing test first, run it to confirm it fails for the intended reason, then implement the code and rerun the test until it passes. Test Driven Development is required for all coding work and must not be skipped. For documentation- or skill-only edits, add or update the relevant validation check before changing the prose.
9
9
 
10
+ Non-negotiable repository isolation rule: Every repository must run its Husky hooks and tests using only files, code, fixtures, dependencies, and services contained within that repository. Tests and Husky scripts must not import, require, read, execute, or otherwise depend on sibling repositories or paths outside the repository root. app-e2e-tests is the only exception because cross-repository end-to-end testing is its explicit responsibility.
11
+
12
+ Non-negotiable error and warning rule: Never suppress, silence, hide, downgrade, filter, ignore, skip, or bypass errors or warnings from code, tests, tools, compilers, linters, or validation. Fix the root cause, then rerun the affected check and require a clean result. Expected error-path tests may assert errors, but must not conceal unexpected failures.
13
+
10
14
  Non-negotiable code organization rule: Functions with the same or equivalent behavior must use the same or clearly corresponding descriptive names across CareCard repositories, and equivalent functionality must live in files with the same names within each repository's established architecture. No backward compatibility names, aliases, or duplicate locations are allowed.
11
15
 
12
16
  ## Purpose
@@ -75,14 +79,20 @@ Do not continue automatically when:
75
79
  when `origin/development` is absent:
76
80
 
77
81
  ```sh
78
- if git ls-remote --exit-code --heads origin development >/dev/null 2>&1; then
79
- base="development"
80
- elif git ls-remote --exit-code --heads origin main >/dev/null 2>&1; then
81
- base="main"
82
- else
83
- echo "No origin/development or origin/main branch exists."
84
- exit 1
82
+ remote_base_refs="$(git ls-remote --heads origin development main)"
83
+ remote_query_status=$?
84
+ if [ "$remote_query_status" -ne 0 ]; then
85
+ printf 'Unable to inspect origin base branches (exit %s).\n' "$remote_query_status" >&2
86
+ exit "$remote_query_status"
85
87
  fi
88
+ case "$remote_base_refs" in
89
+ *"refs/heads/development") base="development" ;;
90
+ *"refs/heads/main") base="main" ;;
91
+ *)
92
+ printf '%s\n' "No origin/development or origin/main branch exists." >&2
93
+ exit 1
94
+ ;;
95
+ esac
86
96
  git fetch origin "$base" --prune
87
97
  ```
88
98
 
@@ -7,6 +7,10 @@ description: 'Use only when the user explicitly asks for remote Git or GitHub PR
7
7
 
8
8
  Non-negotiable TDD rule: Always write the failing test first, run it to confirm it fails for the intended reason, then implement the code and rerun the test until it passes. Test Driven Development is required for all coding work and must not be skipped. For documentation- or skill-only edits, add or update the relevant validation check before changing the prose.
9
9
 
10
+ Non-negotiable repository isolation rule: Every repository must run its Husky hooks and tests using only files, code, fixtures, dependencies, and services contained within that repository. Tests and Husky scripts must not import, require, read, execute, or otherwise depend on sibling repositories or paths outside the repository root. app-e2e-tests is the only exception because cross-repository end-to-end testing is its explicit responsibility.
11
+
12
+ Non-negotiable error and warning rule: Never suppress, silence, hide, downgrade, filter, ignore, skip, or bypass errors or warnings from code, tests, tools, compilers, linters, or validation. Fix the root cause, then rerun the affected check and require a clean result. Expected error-path tests may assert errors, but must not conceal unexpected failures.
13
+
10
14
  Non-negotiable code organization rule: Functions with the same or equivalent behavior must use the same or clearly corresponding descriptive names across CareCard repositories, and equivalent functionality must live in files with the same names within each repository's established architecture. No backward compatibility names, aliases, or duplicate locations are allowed.
11
15
 
12
16
  ## Purpose
@@ -72,14 +76,20 @@ completion.
72
76
 
73
77
  ```sh
74
78
  gh auth status
75
- if git ls-remote --exit-code --heads origin development >/dev/null 2>&1; then
76
- base="development"
77
- elif git ls-remote --exit-code --heads origin main >/dev/null 2>&1; then
78
- base="main"
79
- else
80
- echo "No origin/development or origin/main branch exists."
81
- exit 1
79
+ remote_base_refs="$(git ls-remote --heads origin development main)"
80
+ remote_query_status=$?
81
+ if [ "$remote_query_status" -ne 0 ]; then
82
+ printf 'Unable to inspect origin base branches (exit %s).\n' "$remote_query_status" >&2
83
+ exit "$remote_query_status"
82
84
  fi
85
+ case "$remote_base_refs" in
86
+ *"refs/heads/development") base="development" ;;
87
+ *"refs/heads/main") base="main" ;;
88
+ *)
89
+ printf '%s\n' "No origin/development or origin/main branch exists." >&2
90
+ exit 1
91
+ ;;
92
+ esac
83
93
  target_branch="$(git branch --show-current)"
84
94
  test -n "$target_branch"
85
95
  test "$target_branch" != "$base"
@@ -169,7 +179,19 @@ completion.
169
179
  not protected:
170
180
 
171
181
  ```sh
172
- protected="$(gh api "repos/{owner}/{repo}/branches/$target_branch" --jq '.protected' 2>/dev/null || echo false)"
182
+ protected="$(gh api "repos/{owner}/{repo}/branches/$target_branch" --jq '.protected')"
183
+ protection_query_status=$?
184
+ if [ "$protection_query_status" -ne 0 ]; then
185
+ printf 'Unable to inspect branch protection (exit %s).\n' "$protection_query_status" >&2
186
+ exit "$protection_query_status"
187
+ fi
188
+ case "$protected" in
189
+ true|false) ;;
190
+ *)
191
+ printf 'Unexpected branch protection value: %s\n' "$protected" >&2
192
+ exit 1
193
+ ;;
194
+ esac
173
195
  if [ "$protected" = true ]; then
174
196
  gh pr merge "$pr_number" --squash --admin
175
197
  else
@@ -181,8 +203,16 @@ completion.
181
203
  delete it explicitly:
182
204
 
183
205
  ```sh
184
- if [ "$protected" != true ] && git ls-remote --exit-code --heads origin "$target_branch" >/dev/null 2>&1; then
185
- git push origin --delete "$target_branch"
206
+ if [ "$protected" != true ]; then
207
+ remote_target_ref="$(git ls-remote --heads origin "$target_branch")"
208
+ remote_query_status=$?
209
+ if [ "$remote_query_status" -ne 0 ]; then
210
+ printf 'Unable to inspect the remote target branch (exit %s).\n' "$remote_query_status" >&2
211
+ exit "$remote_query_status"
212
+ fi
213
+ if [ -n "$remote_target_ref" ]; then
214
+ git push origin --delete "$target_branch"
215
+ fi
186
216
  fi
187
217
  ```
188
218
 
@@ -7,6 +7,10 @@ description: 'Use when changing pkg-auth-util auth, password, JWT, crypto, key,
7
7
 
8
8
  Non-negotiable TDD rule: Always write the failing test first, run it to confirm it fails for the intended reason, then implement the code and rerun the test until it passes. Test Driven Development is required for all coding work and must not be skipped. For documentation- or skill-only edits, add or update the relevant validation check before changing the prose.
9
9
 
10
+ Non-negotiable repository isolation rule: Every repository must run its Husky hooks and tests using only files, code, fixtures, dependencies, and services contained within that repository. Tests and Husky scripts must not import, require, read, execute, or otherwise depend on sibling repositories or paths outside the repository root. app-e2e-tests is the only exception because cross-repository end-to-end testing is its explicit responsibility.
11
+
12
+ Non-negotiable error and warning rule: Never suppress, silence, hide, downgrade, filter, ignore, skip, or bypass errors or warnings from code, tests, tools, compilers, linters, or validation. Fix the root cause, then rerun the affected check and require a clean result. Expected error-path tests may assert errors, but must not conceal unexpected failures.
13
+
10
14
  Non-negotiable code organization rule: Functions with the same or equivalent behavior must use the same or clearly corresponding descriptive names across CareCard repositories, and equivalent functionality must live in files with the same names within each repository's established architecture. No backward compatibility names, aliases, or duplicate locations are allowed.
11
15
 
12
16
  ## Purpose
@@ -7,6 +7,10 @@ description: 'Use when any pkg-* repository has non-Markdown package changes, in
7
7
 
8
8
  Non-negotiable TDD rule: Always write the failing test first, run it to confirm it fails for the intended reason, then implement the code and rerun the test until it passes. Test Driven Development is required for all coding work and must not be skipped. For documentation- or skill-only edits, add or update the relevant validation check before changing the prose.
9
9
 
10
+ Non-negotiable repository isolation rule: Every repository must run its Husky hooks and tests using only files, code, fixtures, dependencies, and services contained within that repository. Tests and Husky scripts must not import, require, read, execute, or otherwise depend on sibling repositories or paths outside the repository root. app-e2e-tests is the only exception because cross-repository end-to-end testing is its explicit responsibility.
11
+
12
+ Non-negotiable error and warning rule: Never suppress, silence, hide, downgrade, filter, ignore, skip, or bypass errors or warnings from code, tests, tools, compilers, linters, or validation. Fix the root cause, then rerun the affected check and require a clean result. Expected error-path tests may assert errors, but must not conceal unexpected failures.
13
+
10
14
  Non-negotiable code organization rule: Functions with the same or equivalent behavior must use the same or clearly corresponding descriptive names across CareCard repositories, and equivalent functionality must live in files with the same names within each repository's established architecture. No backward compatibility names, aliases, or duplicate locations are allowed.
11
15
 
12
16
  ## Trigger
@@ -7,6 +7,10 @@ description: 'Use every time before coding, refactoring, debugging, or reviewing
7
7
 
8
8
  Non-negotiable TDD rule: Always write the failing test first, run it to confirm it fails for the intended reason, then implement the code and rerun the test until it passes. Test Driven Development is required for all coding work and must not be skipped. For documentation- or skill-only edits, add or update the relevant validation check before changing the prose.
9
9
 
10
+ Non-negotiable repository isolation rule: Every repository must run its Husky hooks and tests using only files, code, fixtures, dependencies, and services contained within that repository. Tests and Husky scripts must not import, require, read, execute, or otherwise depend on sibling repositories or paths outside the repository root. app-e2e-tests is the only exception because cross-repository end-to-end testing is its explicit responsibility.
11
+
12
+ Non-negotiable error and warning rule: Never suppress, silence, hide, downgrade, filter, ignore, skip, or bypass errors or warnings from code, tests, tools, compilers, linters, or validation. Fix the root cause, then rerun the affected check and require a clean result. Expected error-path tests may assert errors, but must not conceal unexpected failures.
13
+
10
14
  Non-negotiable code organization rule: Functions with the same or equivalent behavior must use the same or clearly corresponding descriptive names across CareCard repositories, and equivalent functionality must live in files with the same names within each repository's established architecture. No backward compatibility names, aliases, or duplicate locations are allowed.
11
15
 
12
16
  ## Purpose
package/index.d.ts CHANGED
@@ -148,14 +148,14 @@ export const jwtUtilAuth: {
148
148
  * @param headerObject - Header data for the JWT.
149
149
  * @param payloadObject - Payload data for the JWT.
150
150
  * @param privateKey - PEM formatted private key to sign the token.
151
- * @returns Signed JWT string or null if an error occurs.
151
+ * @returns Signed JWT string, or null when the private key is missing. Unexpected failures throw.
152
152
  */
153
153
  createSignedJwtFromObject: (headerObject: JwtHeader, payloadObject: JwtPayload, privateKey: string) => string | null;
154
154
  /**
155
155
  * Verifies the signature of a JWT using a public key.
156
156
  * @param jwt - The JWT string to verify.
157
157
  * @param publicKey - PEM formatted public key.
158
- * @returns True if the signature is valid, false otherwise.
158
+ * @returns True if the signature is valid, false for malformed or invalid tokens. Unexpected failures throw.
159
159
  */
160
160
  verifyJwtSignature: (jwt: string, publicKey: string) => boolean;
161
161
  /**
@@ -77,25 +77,21 @@ const normalizeSeconds = value => {
77
77
  * @return {string|null}
78
78
  */
79
79
  const createSignedJwtFromObject = (headerObject, payloadObject, privateKey) => {
80
- try {
81
- if (!privateKey) return null;
80
+ if (!privateKey) return null;
82
81
 
83
- const header = { ...headerObject };
84
- header.alg = header.alg || 'EdDSA';
85
- header.typ = 'JWT';
82
+ const header = { ...headerObject };
83
+ header.alg = header.alg || 'EdDSA';
84
+ header.typ = 'JWT';
86
85
 
87
- const payload = _normalizePayload(payloadObject);
86
+ const payload = _normalizePayload(payloadObject);
88
87
 
89
- const headerBase64UrlSafe = _encode(header);
90
- const payloadBase64UrlSafe = _encode(payload);
91
- const token = headerBase64UrlSafe + '.' + payloadBase64UrlSafe;
88
+ const headerBase64UrlSafe = _encode(header);
89
+ const payloadBase64UrlSafe = _encode(payload);
90
+ const token = headerBase64UrlSafe + '.' + payloadBase64UrlSafe;
92
91
 
93
- const signature = _sign(token, header.alg, privateKey);
92
+ const signature = _sign(token, header.alg, privateKey);
94
93
 
95
- return token + '.' + signature;
96
- } catch (error) {
97
- return null;
98
- }
94
+ return token + '.' + signature;
99
95
  };
100
96
 
101
97
  function createServiceJwt({
@@ -161,6 +157,7 @@ const verifyJwtSignature = (jwt, publicKey) => {
161
157
 
162
158
  return _verify(token, signature, headerObject.alg, publicKey);
163
159
  } catch (error) {
160
+ if (!(error instanceof SyntaxError)) throw error;
164
161
  return false;
165
162
  }
166
163
  };
@@ -180,7 +177,8 @@ const getHeaderPayloadFromJwt = jwt => {
180
177
  const payloadObject = _decode(splitJWT[1]);
181
178
 
182
179
  return { header: headerObject, payload: payloadObject };
183
- } catch (e) {
180
+ } catch (error) {
181
+ if (!(error instanceof SyntaxError)) throw error;
184
182
  return null;
185
183
  }
186
184
  };
@@ -8,14 +8,10 @@ const crypto = require('crypto');
8
8
  * @return {string}
9
9
  */
10
10
  const createPasswordHashWithRandomSalt = (password, secret, algorithm) => {
11
- try {
12
- const salt = crypto.randomBytes(32).toString('base64');
13
- const algorithmBase64 = Buffer.from(algorithm).toString('base64');
14
- const hashBase64 = crypto.createHmac(algorithm, secret).update(password).digest('base64');
15
- return '$1$' + algorithmBase64 + '$' + hashBase64 + '$' + salt + '$';
16
- } catch (e) {
17
- return null;
18
- }
11
+ const salt = crypto.randomBytes(32).toString('base64');
12
+ const algorithmBase64 = Buffer.from(algorithm).toString('base64');
13
+ const hashBase64 = crypto.createHmac(algorithm, secret).update(password).digest('base64');
14
+ return '$1$' + algorithmBase64 + '$' + hashBase64 + '$' + salt + '$';
19
15
  };
20
16
 
21
17
  /**
@@ -26,19 +22,17 @@ const createPasswordHashWithRandomSalt = (password, secret, algorithm) => {
26
22
  * @return {string}
27
23
  */
28
24
  const createPasswordHashBasedOnSavedAlgorithmSalt = (password, savedPasswordHash, secret) => {
29
- try {
30
- const splitStringArray = savedPasswordHash.split('$');
31
- if (splitStringArray.length !== 6) return null;
25
+ if (typeof savedPasswordHash !== 'string') return null;
32
26
 
33
- const algBase64 = splitStringArray[2];
34
- const salt = splitStringArray[4];
35
- const algorithm = Buffer.from(algBase64, 'base64').toString('utf8');
27
+ const splitStringArray = savedPasswordHash.split('$');
28
+ if (splitStringArray.length !== 6) return null;
36
29
 
37
- const hashBase64 = crypto.createHmac(algorithm, secret).update(password).digest('base64');
38
- return '$1$' + algBase64 + '$' + hashBase64 + '$' + salt + '$';
39
- } catch (e) {
40
- return null;
41
- }
30
+ const algBase64 = splitStringArray[2];
31
+ const salt = splitStringArray[4];
32
+ const algorithm = Buffer.from(algBase64, 'base64').toString('utf8');
33
+
34
+ const hashBase64 = crypto.createHmac(algorithm, secret).update(password).digest('base64');
35
+ return '$1$' + algBase64 + '$' + hashBase64 + '$' + salt + '$';
42
36
  };
43
37
 
44
38
  module.exports = {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@carecard/auth-util",
3
- "version": "3.9.0",
3
+ "version": "3.11.0",
4
4
  "repository": "https://github.com/CareCard-ca/pkg-auth-util.git",
5
5
  "description": "Auth utility functions",
6
6
  "main": "index.js",
@@ -26,7 +26,6 @@
26
26
  "license": "ISC",
27
27
  "devDependencies": {
28
28
  "@istanbuljs/nyc-config-typescript": "1.0.2",
29
- "@types/express": "5.0.6",
30
29
  "@types/mocha": "10.0.10",
31
30
  "@types/node": "25.9.3",
32
31
  "eslint": "9.39.4",
@@ -42,11 +41,13 @@
42
41
  "typescript": "6.0.3"
43
42
  },
44
43
  "dependencies": {
45
- "@carecard/common-util": "3.9.0",
46
- "@carecard/validate": "3.9.0"
44
+ "@carecard/common-util": "3.11.0",
45
+ "@carecard/validate": "3.11.0",
46
+ "@types/express": "5.0.6"
47
47
  },
48
48
  "overrides": {
49
49
  "diff": "8.0.4",
50
+ "glob": "13.0.6",
50
51
  "serialize-javascript": "7.0.5",
51
52
  "js-yaml": "4.2.0"
52
53
  }
package/readme.md CHANGED
@@ -9,6 +9,10 @@ Utility package for authentication and authorization in the CareCard ecosystem.
9
9
 
10
10
  Non-negotiable TDD rule: Always write the failing test first, run it to confirm it fails for the intended reason, then implement the code and rerun the test until it passes. Test Driven Development is required for all coding work and must not be skipped. For documentation- or skill-only edits, add or update the relevant validation check before changing the prose.
11
11
 
12
+ Non-negotiable repository isolation rule: Every repository must run its Husky hooks and tests using only files, code, fixtures, dependencies, and services contained within that repository. Tests and Husky scripts must not import, require, read, execute, or otherwise depend on sibling repositories or paths outside the repository root. app-e2e-tests is the only exception because cross-repository end-to-end testing is its explicit responsibility.
13
+
14
+ Non-negotiable error and warning rule: Never suppress, silence, hide, downgrade, filter, ignore, skip, or bypass errors or warnings from code, tests, tools, compilers, linters, or validation. Fix the root cause, then rerun the affected check and require a clean result. Expected error-path tests may assert errors, but must not conceal unexpected failures.
15
+
12
16
  Non-negotiable code organization rule: Functions with the same or equivalent behavior must use the same or clearly corresponding descriptive names across CareCard repositories, and equivalent functionality must live in files with the same names within each repository's established architecture. No backward compatibility names, aliases, or duplicate locations are allowed.
13
17
 
14
18
  ## Features