@carecard/auth-util 3.1.13 → 3.1.16
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/.agents/config.toml +2 -0
- package/.agents/skills/carecard-workspace-standards/SKILL.md +401 -0
- package/.agents/skills/carecard-workspace-standards/agents/openai.yaml +4 -0
- package/.agents/skills/github-pr-create-update/SKILL.md +188 -0
- package/.agents/skills/github-pr-create-update/agents/openai.yaml +5 -0
- package/.agents/skills/github-pr-merge-cleanup/SKILL.md +175 -0
- package/.agents/skills/github-pr-merge-cleanup/agents/openai.yaml +5 -0
- package/.agents/skills/pkg-auth-util-auth-crypto-library/SKILL.md +239 -0
- package/.agents/skills/pkg-auth-util-auth-crypto-library/agents/openai.yaml +4 -0
- package/.agents/skills/software-design-patterns-and-clean-code/SKILL.md +73 -0
- package/.codex/config.toml +2 -0
- package/.prettierrc.js +12 -0
- package/eslint.config.mjs +14 -0
- package/index.d.ts +197 -151
- package/index.js +29 -28
- package/lib/jwtUtilAuth.js +152 -86
- package/lib/keyGen.js +18 -22
- package/lib/pwdUtilAuth.js +25 -25
- package/lib/stringUtilAuth.js +43 -49
- package/package.json +23 -11
- package/readme.md +37 -5
package/index.d.ts
CHANGED
|
@@ -2,80 +2,114 @@
|
|
|
2
2
|
* Utility functions for authentication and authorization in the CareCard ecosystem.
|
|
3
3
|
*/
|
|
4
4
|
|
|
5
|
-
import {Request} from 'express';
|
|
5
|
+
import { Request } from 'express';
|
|
6
6
|
|
|
7
7
|
/**
|
|
8
8
|
* Represents the standard JWT header structure.
|
|
9
9
|
*/
|
|
10
10
|
export interface JwtHeader {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
11
|
+
/** The cryptographic algorithm used to secure the JWT. */
|
|
12
|
+
alg?: string;
|
|
13
|
+
/** The media type of the JWT. Defaults to 'JWT'. */
|
|
14
|
+
typ?: string;
|
|
15
15
|
|
|
16
|
-
|
|
17
|
-
|
|
16
|
+
/** Any other custom header fields. */
|
|
17
|
+
[key: string]: any;
|
|
18
18
|
}
|
|
19
19
|
|
|
20
20
|
/**
|
|
21
21
|
* Represents the standard JWT payload (claims) structure.
|
|
22
22
|
*/
|
|
23
23
|
export interface JwtPayload {
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
24
|
+
/** Issued at time, in seconds since the epoch. */
|
|
25
|
+
iat?: number;
|
|
26
|
+
/** Expiration time, in seconds since the epoch. */
|
|
27
|
+
exp?: number;
|
|
28
|
+
/** Not before time, in seconds since the epoch. */
|
|
29
|
+
nbf?: number;
|
|
30
|
+
/** Authentication time, in seconds since the epoch. */
|
|
31
|
+
auth_time?: number;
|
|
32
|
+
/** Subject (usually the client ID). */
|
|
33
|
+
sub?: string;
|
|
34
|
+
/** Roles assigned to the user. */
|
|
35
|
+
roles?: string[];
|
|
36
36
|
|
|
37
|
-
|
|
38
|
-
|
|
37
|
+
/** Any other custom payload fields. */
|
|
38
|
+
[key: string]: any;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Claims used by service-to-service JWTs.
|
|
43
|
+
*/
|
|
44
|
+
export interface ServiceJwtPayload extends JwtPayload {
|
|
45
|
+
/** Issuing microservice name, for example 'ms-institutions'. */
|
|
46
|
+
iss: string;
|
|
47
|
+
/** Receiving microservice name or names. */
|
|
48
|
+
aud: string | string[];
|
|
49
|
+
/** Subject. Defaults to the issuing microservice name. */
|
|
50
|
+
sub: string;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Options for creating a service-to-service JWT.
|
|
55
|
+
*/
|
|
56
|
+
export interface ServiceJwtOptions {
|
|
57
|
+
/** Issuing microservice name, for example 'ms-institutions'. */
|
|
58
|
+
issuer: string;
|
|
59
|
+
/** Receiving microservice name or names. */
|
|
60
|
+
audience: string | string[];
|
|
61
|
+
/** Private key owned by the issuing microservice. */
|
|
62
|
+
privateKey: string;
|
|
63
|
+
/** Subject claim. Defaults to issuer. */
|
|
64
|
+
subject?: string;
|
|
65
|
+
/** Issued-at timestamp in seconds or milliseconds. Defaults to now. */
|
|
66
|
+
issuedAt?: number;
|
|
67
|
+
/** Token lifetime in seconds. Defaults to 60. */
|
|
68
|
+
expiresInSeconds?: number;
|
|
69
|
+
/** JWT signing algorithm. Defaults to EdDSA. */
|
|
70
|
+
algorithm?: string;
|
|
71
|
+
/** Additional non-sensitive JWT claims. */
|
|
72
|
+
claims?: Record<string, unknown>;
|
|
39
73
|
}
|
|
40
74
|
|
|
41
75
|
/**
|
|
42
76
|
* Container for the decoded header and payload of a JWT.
|
|
43
77
|
*/
|
|
44
78
|
export interface JwtParts {
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
79
|
+
/** Decoded JWT header. */
|
|
80
|
+
header: JwtHeader;
|
|
81
|
+
/** Decoded JWT payload. */
|
|
82
|
+
payload: JwtPayload;
|
|
49
83
|
}
|
|
50
84
|
|
|
51
85
|
/**
|
|
52
86
|
* Structure of the JWT object attached to the request.
|
|
53
87
|
*/
|
|
54
88
|
export interface JwtRequestObject {
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
89
|
+
header: JwtHeader;
|
|
90
|
+
payload: JwtPayload;
|
|
91
|
+
age?: number;
|
|
92
|
+
jwtClientId: (req?: any) => string | undefined;
|
|
93
|
+
doesJwtUserHasRole: (role: string) => boolean;
|
|
94
|
+
isJwtExpired: (jwtValiditySeconds?: number) => boolean;
|
|
95
|
+
jwtAgeInSeconds: (req?: any) => number;
|
|
62
96
|
}
|
|
63
97
|
|
|
64
98
|
/**
|
|
65
99
|
* Structure of the visitor object attached to the request.
|
|
66
100
|
*/
|
|
67
101
|
export interface VisitorRequestObject {
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
102
|
+
header: JwtHeader;
|
|
103
|
+
payload: JwtPayload;
|
|
104
|
+
visitorClientId: (req?: any) => string | undefined;
|
|
71
105
|
}
|
|
72
106
|
|
|
73
107
|
/**
|
|
74
108
|
* Extended Express Request to include jwt and visitor objects.
|
|
75
109
|
*/
|
|
76
110
|
export interface AuthenticatedRequest extends Request {
|
|
77
|
-
|
|
78
|
-
|
|
111
|
+
jwt?: JwtRequestObject | null;
|
|
112
|
+
visitor?: VisitorRequestObject | null;
|
|
79
113
|
}
|
|
80
114
|
|
|
81
115
|
/**
|
|
@@ -83,24 +117,24 @@ export interface AuthenticatedRequest extends Request {
|
|
|
83
117
|
* @deprecated Use native Buffer methods or other modern alternatives.
|
|
84
118
|
*/
|
|
85
119
|
export interface PasswordParts {
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
120
|
+
/** Version indicator of the hashing format. */
|
|
121
|
+
version: string;
|
|
122
|
+
/** Base64 encoded algorithm name. */
|
|
123
|
+
alg: string;
|
|
124
|
+
/** Base64 encoded password hash. */
|
|
125
|
+
hash: string;
|
|
126
|
+
/** Base64 encoded random salt. */
|
|
127
|
+
salt: string;
|
|
94
128
|
}
|
|
95
129
|
|
|
96
130
|
/**
|
|
97
131
|
* Contains a pair of public and private cryptographic keys.
|
|
98
132
|
*/
|
|
99
133
|
export interface KeyPair {
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
134
|
+
/** PEM formatted public key string. */
|
|
135
|
+
publicKey: string;
|
|
136
|
+
/** PEM formatted private key string. */
|
|
137
|
+
privateKey: string;
|
|
104
138
|
}
|
|
105
139
|
|
|
106
140
|
/**
|
|
@@ -108,29 +142,29 @@ export interface KeyPair {
|
|
|
108
142
|
* @deprecated use direct import of the new functions.
|
|
109
143
|
*/
|
|
110
144
|
export const jwtUtilAuth: {
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
145
|
+
/**
|
|
146
|
+
* Creates a signed JWT from header and payload objects.
|
|
147
|
+
* Automatically normalizes header (sets alg/typ) and payload (sets iat/exp/etc. in seconds).
|
|
148
|
+
* @param headerObject - Header data for the JWT.
|
|
149
|
+
* @param payloadObject - Payload data for the JWT.
|
|
150
|
+
* @param privateKey - PEM formatted private key to sign the token.
|
|
151
|
+
* @returns Signed JWT string or null if an error occurs.
|
|
152
|
+
*/
|
|
153
|
+
createSignedJwtFromObject: (headerObject: JwtHeader, payloadObject: JwtPayload, privateKey: string) => string | null;
|
|
154
|
+
/**
|
|
155
|
+
* Verifies the signature of a JWT using a public key.
|
|
156
|
+
* @param jwt - The JWT string to verify.
|
|
157
|
+
* @param publicKey - PEM formatted public key.
|
|
158
|
+
* @returns True if the signature is valid, false otherwise.
|
|
159
|
+
*/
|
|
160
|
+
verifyJwtSignature: (jwt: string, publicKey: string) => boolean;
|
|
161
|
+
/**
|
|
162
|
+
* Decodes a JWT and returns its header and payload as objects.
|
|
163
|
+
* Note: This does NOT verify the signature.
|
|
164
|
+
* @param jwt - The JWT string to parse.
|
|
165
|
+
* @returns An object containing the header and payload, or null if parsing fails.
|
|
166
|
+
*/
|
|
167
|
+
getHeaderPayloadFromJwt: (jwt: string) => JwtParts | null;
|
|
134
168
|
};
|
|
135
169
|
|
|
136
170
|
/**
|
|
@@ -138,94 +172,92 @@ export const jwtUtilAuth: {
|
|
|
138
172
|
* @deprecated use direct imports of the new functions.
|
|
139
173
|
*/
|
|
140
174
|
export const pwdUtilAuth: {
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
175
|
+
/**
|
|
176
|
+
* Generates a password hash using a random salt and specified algorithm.
|
|
177
|
+
* @param password - The plain-text password to hash.
|
|
178
|
+
* @param secret - A pepper/secret key to combine with the password.
|
|
179
|
+
* @param algorithm - The hashing algorithm to use (e.g., 'sha256').
|
|
180
|
+
* @returns A string containing the formatted hash with metadata ($1$alg$hash$salt$) or null if an error occurs.
|
|
181
|
+
*/
|
|
182
|
+
createPasswordHashWithRandomSalt: (password: string, secret: string, algorithm: string) => string | null;
|
|
183
|
+
/**
|
|
184
|
+
* Generates a password hash using the same algorithm and salt from a previously saved hash.
|
|
185
|
+
* Useful for verifying a password against a stored hash.
|
|
186
|
+
* @param password - The plain-text password to verify.
|
|
187
|
+
* @param savedPasswordHash - The full stored hash string (including salt and metadata).
|
|
188
|
+
* @param secret - The pepper/secret key used for hashing.
|
|
189
|
+
* @returns A hash string that should match the saved hash if the password is correct, or null if an error occurs.
|
|
190
|
+
*/
|
|
191
|
+
createPasswordHashBasedOnSavedAlgorithmSalt: (password: string, savedPasswordHash: string, secret: string) => string | null;
|
|
158
192
|
};
|
|
159
193
|
|
|
160
|
-
|
|
161
194
|
/**
|
|
162
195
|
* Utility functions for string manipulation, base64 encoding, and parsing auth-related strings.
|
|
163
196
|
* @deprecated Use native Buffer methods or other modern alternatives.
|
|
164
197
|
*/
|
|
165
198
|
export const stringUtilAuth: {
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
199
|
+
/**
|
|
200
|
+
* Converts a base64 string to be URL-safe (replaces + with -, / with _, and removes =).
|
|
201
|
+
* @deprecated Use native Buffer methods or other modern alternatives.
|
|
202
|
+
* @param urlUnsafeString - The string to convert.
|
|
203
|
+
* @returns URL-safe string.
|
|
204
|
+
*/
|
|
205
|
+
makeStringUrlSafe: (urlUnsafeString?: string) => string;
|
|
206
|
+
/**
|
|
207
|
+
* Reverses URL-safe conversion and restores standard base64 characters and padding.
|
|
208
|
+
* @deprecated Use native Buffer methods or other modern alternatives.
|
|
209
|
+
* @param urlSafeString - The URL-safe string to restore.
|
|
210
|
+
* @returns Standard base64 string.
|
|
211
|
+
*/
|
|
212
|
+
reverseStringUrlSafe: (urlSafeString?: string) => string;
|
|
213
|
+
/**
|
|
214
|
+
* Encodes a plain-text string to base64.
|
|
215
|
+
* @deprecated Use native Buffer methods or other modern alternatives.
|
|
216
|
+
* @param unCodedString - Plain-text string.
|
|
217
|
+
* @returns Base64 encoded string.
|
|
218
|
+
*/
|
|
219
|
+
asciiToBase64: (unCodedString: string) => string;
|
|
220
|
+
/**
|
|
221
|
+
* Decodes a base64 string to UTF-8 plain-text.
|
|
222
|
+
* @deprecated Use native Buffer methods or other modern alternatives.
|
|
223
|
+
* @param codedString - Base64 encoded string.
|
|
224
|
+
* @returns Decoded plain-text string.
|
|
225
|
+
*/
|
|
226
|
+
base64ToAscii: (codedString: string) => string;
|
|
227
|
+
/**
|
|
228
|
+
* Parses a stored password hash string into its constituent parts.
|
|
229
|
+
* @deprecated Use native Buffer methods or other modern alternatives.
|
|
230
|
+
* @param passwordHash - The formatted hash string ($1$alg$hash$salt$).
|
|
231
|
+
* @returns A PasswordParts object or null if the format is invalid.
|
|
232
|
+
*/
|
|
233
|
+
dollarSignConnectedStringToAlgorithmHashSalt: (passwordHash: string) => PasswordParts | null;
|
|
234
|
+
/**
|
|
235
|
+
* Splits a JWT into its three base64-encoded string parts (header, payload, signature).
|
|
236
|
+
* @deprecated Use native Buffer methods or other modern alternatives.
|
|
237
|
+
* @param jwt - The full JWT string.
|
|
238
|
+
* @returns An object with the three raw parts, or null if the format is invalid.
|
|
239
|
+
*/
|
|
240
|
+
dotConnectedStringToHeaderPayloadSignature: (jwt: string) => {
|
|
241
|
+
header: string;
|
|
242
|
+
payload: string;
|
|
243
|
+
signature: string;
|
|
244
|
+
} | null;
|
|
245
|
+
/**
|
|
246
|
+
* Serializes an object into a URL-safe base64 string.
|
|
247
|
+
* @deprecated Use native Buffer methods or other modern alternatives.
|
|
248
|
+
* @param object - The object to serialize.
|
|
249
|
+
* @returns URL-safe base64 string.
|
|
250
|
+
*/
|
|
251
|
+
objectToBase64UrlSafeString: (object: any) => string;
|
|
252
|
+
/**
|
|
253
|
+
* Parses a URL-safe base64 string into an object.
|
|
254
|
+
* @deprecated Use native Buffer methods or other modern alternatives.
|
|
255
|
+
* @param urlSafeBase64String - URL-safe base64 string.
|
|
256
|
+
* @returns The parsed object.
|
|
257
|
+
*/
|
|
258
|
+
urlSafeBase64ToObject: (urlSafeBase64String: string) => any;
|
|
226
259
|
};
|
|
227
260
|
|
|
228
|
-
|
|
229
261
|
/**
|
|
230
262
|
* Generates a public/private key pair for JWT signing.
|
|
231
263
|
* @param algorithm - The algorithm to use ('ed25519' or 'rsa'). Defaults to 'ed25519'.
|
|
@@ -243,6 +275,20 @@ export function generateKeyPair(algorithm?: 'ed25519' | 'rsa' | string): KeyPair
|
|
|
243
275
|
*/
|
|
244
276
|
export function jwtCreateSignedToken(headerObject: JwtHeader, payloadObject: JwtPayload, privateKey: string): string | null;
|
|
245
277
|
|
|
278
|
+
/**
|
|
279
|
+
* Creates a signed service-to-service JWT using the issuing service's private key.
|
|
280
|
+
* @param options - Service JWT creation options.
|
|
281
|
+
* @returns Signed JWT string or null if validation or signing fails.
|
|
282
|
+
*/
|
|
283
|
+
export function jwtCreateServiceToken(options: ServiceJwtOptions): string | null;
|
|
284
|
+
|
|
285
|
+
/**
|
|
286
|
+
* Creates a Bearer Authorization header containing a signed service-to-service JWT.
|
|
287
|
+
* @param options - Service JWT creation options.
|
|
288
|
+
* @returns Bearer Authorization header or null if validation or signing fails.
|
|
289
|
+
*/
|
|
290
|
+
export function jwtCreateServiceAuthorizationHeader(options: ServiceJwtOptions): string | null;
|
|
291
|
+
|
|
246
292
|
/**
|
|
247
293
|
* Verifies the signature of a JWT using a public key.
|
|
248
294
|
* @param jwt - The JWT string to verify.
|
package/index.js
CHANGED
|
@@ -3,33 +3,34 @@ const jwtUtilAuth = require('./lib/jwtUtilAuth');
|
|
|
3
3
|
const keyGen = require('./lib/keyGen');
|
|
4
4
|
|
|
5
5
|
module.exports = {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
6
|
+
// New
|
|
7
|
+
generateKeyPair: keyGen.generateKeyPair,
|
|
8
|
+
jwtCreateSignedToken: jwtUtilAuth.createSignedJwtFromObject,
|
|
9
|
+
jwtCreateServiceToken: jwtUtilAuth.createServiceJwt,
|
|
10
|
+
jwtCreateServiceAuthorizationHeader: jwtUtilAuth.createServiceAuthorizationHeader,
|
|
11
|
+
jwtVerifySignedToken: jwtUtilAuth.verifyJwtSignature,
|
|
12
|
+
jwtGetHeaderPayload: jwtUtilAuth.getHeaderPayloadFromJwt,
|
|
13
|
+
passwordCreateHashWithRandomSalt: pwdUtilAuth.createPasswordHashWithRandomSalt,
|
|
14
|
+
passwordCreateHashFromSavedHash: pwdUtilAuth.createPasswordHashBasedOnSavedAlgorithmSalt,
|
|
13
15
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
16
|
+
// Deprecated
|
|
17
|
+
/**
|
|
18
|
+
* @deprecated use direct import of the new functions.
|
|
19
|
+
*/
|
|
20
|
+
jwtUtilAuth: {
|
|
21
|
+
createSignedJwtFromObject: jwtUtilAuth.createSignedJwtFromObject,
|
|
22
|
+
verifyJwtSignature: jwtUtilAuth.verifyJwtSignature,
|
|
23
|
+
getHeaderPayloadFromJwt: jwtUtilAuth.getHeaderPayloadFromJwt,
|
|
24
|
+
},
|
|
25
|
+
/**
|
|
26
|
+
* @deprecated use direct import of the new functions.
|
|
27
|
+
*/
|
|
28
|
+
pwdUtilAuth: {
|
|
29
|
+
createPasswordHashWithRandomSalt: pwdUtilAuth.createPasswordHashWithRandomSalt,
|
|
30
|
+
createPasswordHashBasedOnSavedAlgorithmSalt: pwdUtilAuth.createPasswordHashBasedOnSavedAlgorithmSalt,
|
|
31
|
+
},
|
|
32
|
+
/**
|
|
33
|
+
* @deprecated Use native Buffer methods or other modern alternatives.
|
|
34
|
+
*/
|
|
35
|
+
stringUtilAuth: require('./lib/stringUtilAuth'),
|
|
34
36
|
};
|
|
35
|
-
|