@carecard/auth-util 3.10.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.
@@ -9,6 +9,8 @@ Non-negotiable TDD rule: Always write the failing test first, run it to confirm
9
9
 
10
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
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
+
12
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.
13
15
 
14
16
  ## Purpose
@@ -85,7 +87,7 @@ config.
85
87
  - Never suppress errors, TypeScript errors, linter warnings, authorization
86
88
  failures, RLS failures, build failures, hydration issues, or failing tests.
87
89
  Do not add `eslint-disable`, `@ts-ignore`, broad catches, empty catches, or
88
- similar suppression unless explicitly requested.
90
+ similar suppression. Fix the root cause.
89
91
  - Do not add dependencies unless clearly necessary. If one might be needed, ask
90
92
  first with the reason, tradeoff, and why existing code cannot solve it.
91
93
  - Before finalizing repository work, run the affected repository's relevant
@@ -215,7 +217,7 @@ build artifacts, logs, or `.DS_Store`.
215
217
  Most JavaScript `ms-*` services use CommonJS, Mocha, Supertest, Docker Compose
216
218
  database tests, `@carecard/*` packages, and `sub-apps`
217
219
  controller/router/model patterns. TypeScript services such as `ms-messages`
218
- and `ms-template-ts` use Jest or TypeScript tooling and should keep their
220
+ use Jest or TypeScript tooling and should keep their
219
221
  existing TypeScript style.
220
222
 
221
223
  - Keep environment-specific files explicit: `.env.development`, `.env.test`,
@@ -380,7 +382,7 @@ the authenticated dashboard.
380
382
 
381
383
  ## Auth Service RLS Contract
382
384
 
383
- - `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.
384
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.
385
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.
386
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.
@@ -9,6 +9,8 @@ Non-negotiable TDD rule: Always write the failing test first, run it to confirm
9
9
 
10
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
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
+
12
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.
13
15
 
14
16
  ## Purpose
@@ -77,14 +79,20 @@ Do not continue automatically when:
77
79
  when `origin/development` is absent:
78
80
 
79
81
  ```sh
80
- if git ls-remote --exit-code --heads origin development >/dev/null 2>&1; then
81
- base="development"
82
- elif git ls-remote --exit-code --heads origin main >/dev/null 2>&1; then
83
- base="main"
84
- else
85
- echo "No origin/development or origin/main branch exists."
86
- 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"
87
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
88
96
  git fetch origin "$base" --prune
89
97
  ```
90
98
 
@@ -9,6 +9,8 @@ Non-negotiable TDD rule: Always write the failing test first, run it to confirm
9
9
 
10
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
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
+
12
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.
13
15
 
14
16
  ## Purpose
@@ -74,14 +76,20 @@ completion.
74
76
 
75
77
  ```sh
76
78
  gh auth status
77
- if git ls-remote --exit-code --heads origin development >/dev/null 2>&1; then
78
- base="development"
79
- elif git ls-remote --exit-code --heads origin main >/dev/null 2>&1; then
80
- base="main"
81
- else
82
- echo "No origin/development or origin/main branch exists."
83
- 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"
84
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
85
93
  target_branch="$(git branch --show-current)"
86
94
  test -n "$target_branch"
87
95
  test "$target_branch" != "$base"
@@ -171,7 +179,19 @@ completion.
171
179
  not protected:
172
180
 
173
181
  ```sh
174
- 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
175
195
  if [ "$protected" = true ]; then
176
196
  gh pr merge "$pr_number" --squash --admin
177
197
  else
@@ -183,8 +203,16 @@ completion.
183
203
  delete it explicitly:
184
204
 
185
205
  ```sh
186
- if [ "$protected" != true ] && git ls-remote --exit-code --heads origin "$target_branch" >/dev/null 2>&1; then
187
- 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
188
216
  fi
189
217
  ```
190
218
 
@@ -9,6 +9,8 @@ Non-negotiable TDD rule: Always write the failing test first, run it to confirm
9
9
 
10
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
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
+
12
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.
13
15
 
14
16
  ## Purpose
@@ -9,6 +9,8 @@ Non-negotiable TDD rule: Always write the failing test first, run it to confirm
9
9
 
10
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
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
+
12
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.
13
15
 
14
16
  ## Trigger
@@ -9,6 +9,8 @@ Non-negotiable TDD rule: Always write the failing test first, run it to confirm
9
9
 
10
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
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
+
12
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.
13
15
 
14
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.10.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.10.0",
46
- "@carecard/validate": "3.10.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
@@ -11,6 +11,8 @@ Non-negotiable TDD rule: Always write the failing test first, run it to confirm
11
11
 
12
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
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
+
14
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.
15
17
 
16
18
  ## Features