@carecard/auth-util 3.0.2 → 3.0.4
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 +174 -0
- package/package.json +9 -3
package/index.d.ts
ADDED
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Represents the standard JWT header structure.
|
|
3
|
+
*/
|
|
4
|
+
export interface JwtHeader {
|
|
5
|
+
/** The cryptographic algorithm used to secure the JWT. */
|
|
6
|
+
alg?: string;
|
|
7
|
+
/** The media type of the JWT. Defaults to 'JWT'. */
|
|
8
|
+
typ?: string;
|
|
9
|
+
/** Any other custom header fields. */
|
|
10
|
+
[key: string]: any;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Represents the standard JWT payload (claims) structure.
|
|
15
|
+
*/
|
|
16
|
+
export interface JwtPayload {
|
|
17
|
+
/** Issued at time, in seconds since the epoch. */
|
|
18
|
+
iat?: number;
|
|
19
|
+
/** Expiration time, in seconds since the epoch. */
|
|
20
|
+
exp?: number;
|
|
21
|
+
/** Not before time, in seconds since the epoch. */
|
|
22
|
+
nbf?: number;
|
|
23
|
+
/** Authentication time, in seconds since the epoch. */
|
|
24
|
+
auth_time?: number;
|
|
25
|
+
/** Any other custom payload fields. */
|
|
26
|
+
[key: string]: any;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Container for the decoded header and payload of a JWT.
|
|
31
|
+
*/
|
|
32
|
+
export interface JwtParts {
|
|
33
|
+
/** Decoded JWT header. */
|
|
34
|
+
header: JwtHeader;
|
|
35
|
+
/** Decoded JWT payload. */
|
|
36
|
+
payload: JwtPayload;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Represents the decomposed parts of a stored password hash.
|
|
41
|
+
*/
|
|
42
|
+
export interface PasswordParts {
|
|
43
|
+
/** Version indicator of the hashing format. */
|
|
44
|
+
version: string;
|
|
45
|
+
/** Base64 encoded algorithm name. */
|
|
46
|
+
alg: string;
|
|
47
|
+
/** Base64 encoded password hash. */
|
|
48
|
+
hash: string;
|
|
49
|
+
/** Base64 encoded random salt. */
|
|
50
|
+
salt: string;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Contains a pair of public and private cryptographic keys.
|
|
55
|
+
*/
|
|
56
|
+
export interface KeyPair {
|
|
57
|
+
/** PEM formatted public key string. */
|
|
58
|
+
publicKey: string;
|
|
59
|
+
/** PEM formatted private key string. */
|
|
60
|
+
privateKey: string;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Utility functions for creating, verifying, and parsing JSON Web Tokens (JWT).
|
|
65
|
+
*/
|
|
66
|
+
export const jwtUtilAuth: {
|
|
67
|
+
/**
|
|
68
|
+
* Creates a signed JWT from header and payload objects.
|
|
69
|
+
* Automatically normalizes header (sets alg/typ) and payload (sets iat/exp/etc. in seconds).
|
|
70
|
+
* @param headerObject - Header data for the JWT.
|
|
71
|
+
* @param payloadObject - Payload data for the JWT.
|
|
72
|
+
* @param privateKey - PEM formatted private key to sign the token.
|
|
73
|
+
* @returns Signed JWT string or null if an error occurs.
|
|
74
|
+
*/
|
|
75
|
+
createSignedJwtFromObject: (headerObject: JwtHeader, payloadObject: JwtPayload, privateKey: string) => string | null;
|
|
76
|
+
/**
|
|
77
|
+
* Verifies the signature of a JWT using a public key.
|
|
78
|
+
* @param jwt - The JWT string to verify.
|
|
79
|
+
* @param publicKey - PEM formatted public key.
|
|
80
|
+
* @returns True if the signature is valid, false otherwise.
|
|
81
|
+
*/
|
|
82
|
+
verifyJwtSignature: (jwt: string, publicKey: string) => boolean;
|
|
83
|
+
/**
|
|
84
|
+
* Decodes a JWT and returns its header and payload as objects.
|
|
85
|
+
* Note: This does NOT verify the signature.
|
|
86
|
+
* @param jwt - The JWT string to parse.
|
|
87
|
+
* @returns An object containing the header and payload, or null if parsing fails.
|
|
88
|
+
*/
|
|
89
|
+
getHeaderPayloadFromJwt: (jwt: string) => JwtParts | null;
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Utility functions for password hashing and verification.
|
|
94
|
+
*/
|
|
95
|
+
export const pwdUtilAuth: {
|
|
96
|
+
/**
|
|
97
|
+
* Generates a password hash using a random salt and specified algorithm.
|
|
98
|
+
* @param password - The plain-text password to hash.
|
|
99
|
+
* @param secret - A pepper/secret key to combine with the password.
|
|
100
|
+
* @param algorithm - The hashing algorithm to use (e.g., 'sha256').
|
|
101
|
+
* @returns A string containing the formatted hash with metadata ($1$alg$hash$salt$).
|
|
102
|
+
*/
|
|
103
|
+
createPasswordHashWithRandomSalt: (password: string, secret: string, algorithm: string) => string;
|
|
104
|
+
/**
|
|
105
|
+
* Generates a password hash using the same algorithm and salt from a previously saved hash.
|
|
106
|
+
* Useful for verifying a password against a stored hash.
|
|
107
|
+
* @param password - The plain-text password to verify.
|
|
108
|
+
* @param savedPasswordHash - The full stored hash string (including salt and metadata).
|
|
109
|
+
* @param secret - The pepper/secret key used for hashing.
|
|
110
|
+
* @returns A hash string that should match the saved hash if the password is correct.
|
|
111
|
+
*/
|
|
112
|
+
createPasswordHashBasedOnSavedAlgorithmSalt: (password: string, savedPasswordHash: string, secret: string) => string;
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Generates a public/private key pair for JWT signing.
|
|
117
|
+
* @param algorithm - The algorithm to use ('ed25519' or 'rsa'). Defaults to 'ed25519'.
|
|
118
|
+
* @returns A KeyPair object containing PEM formatted keys.
|
|
119
|
+
*/
|
|
120
|
+
export const createKeys: (algorithm?: 'ed25519' | 'rsa' | string) => KeyPair;
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Utility functions for string manipulation, base64 encoding, and parsing auth-related strings.
|
|
124
|
+
*/
|
|
125
|
+
export const stringUtilAuth: {
|
|
126
|
+
/**
|
|
127
|
+
* Converts a base64 string to be URL-safe (replaces + with -, / with _, and removes =).
|
|
128
|
+
* @param urlUnsafeString - The string to convert.
|
|
129
|
+
* @returns URL-safe string.
|
|
130
|
+
*/
|
|
131
|
+
makeStringUrlSafe: (urlUnsafeString?: string) => string;
|
|
132
|
+
/**
|
|
133
|
+
* Reverses URL-safe conversion and restores standard base64 characters and padding.
|
|
134
|
+
* @param urlSafeString - The URL-safe string to restore.
|
|
135
|
+
* @returns Standard base64 string.
|
|
136
|
+
*/
|
|
137
|
+
reverseStringUrlSafe: (urlSafeString?: string) => string;
|
|
138
|
+
/**
|
|
139
|
+
* Encodes a plain-text string to base64.
|
|
140
|
+
* @param unCodedString - Plain-text string.
|
|
141
|
+
* @returns Base64 encoded string.
|
|
142
|
+
*/
|
|
143
|
+
asciiToBase64: (unCodedString: string) => string;
|
|
144
|
+
/**
|
|
145
|
+
* Decodes a base64 string to UTF-8 plain-text.
|
|
146
|
+
* @param codedString - Base64 encoded string.
|
|
147
|
+
* @returns Decoded plain-text string.
|
|
148
|
+
*/
|
|
149
|
+
base64ToAscii: (codedString: string) => string;
|
|
150
|
+
/**
|
|
151
|
+
* Parses a stored password hash string into its constituent parts.
|
|
152
|
+
* @param passwordHash - The formatted hash string ($1$alg$hash$salt$).
|
|
153
|
+
* @returns A PasswordParts object or null if the format is invalid.
|
|
154
|
+
*/
|
|
155
|
+
dollarSignConnectedStringToAlgorithmHashSalt: (passwordHash: string) => PasswordParts | null;
|
|
156
|
+
/**
|
|
157
|
+
* Splits a JWT into its three base64-encoded string parts (header, payload, signature).
|
|
158
|
+
* @param jwt - The full JWT string.
|
|
159
|
+
* @returns An object with the three raw parts, or null if the format is invalid.
|
|
160
|
+
*/
|
|
161
|
+
dotConnectedStringToHeaderPayloadSignature: (jwt: string) => { header: string, payload: string, signature: string } | null;
|
|
162
|
+
/**
|
|
163
|
+
* Serializes an object into a URL-safe base64 string.
|
|
164
|
+
* @param object - The object to serialize.
|
|
165
|
+
* @returns URL-safe base64 string.
|
|
166
|
+
*/
|
|
167
|
+
objectToBase64UrlSafeString: (object: any) => string;
|
|
168
|
+
/**
|
|
169
|
+
* Parses a URL-safe base64 string into an object.
|
|
170
|
+
* @param urlSafeBase64String - URL-safe base64 string.
|
|
171
|
+
* @returns The parsed object.
|
|
172
|
+
*/
|
|
173
|
+
urlSafeBase64ToObject: (urlSafeBase64String: string) => any;
|
|
174
|
+
};
|
package/package.json
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@carecard/auth-util",
|
|
3
|
-
"version": "3.0.
|
|
3
|
+
"version": "3.0.4",
|
|
4
4
|
"repository": "https://github.com/CareCard-ca/pkg-auth-util.git",
|
|
5
5
|
"description": "Auth utility functions",
|
|
6
6
|
"main": "index.js",
|
|
7
|
+
"types": "index.d.ts",
|
|
7
8
|
"scripts": {
|
|
8
|
-
"test": "export NODE_ENV=test && mocha --
|
|
9
|
+
"test": "export NODE_ENV=test && mocha --recursive",
|
|
10
|
+
"test:types": "tsc --noEmit && mocha -r ts-node/register test/types.test.ts"
|
|
9
11
|
},
|
|
10
12
|
"keywords": [
|
|
11
13
|
"auth",
|
|
@@ -15,6 +17,10 @@
|
|
|
15
17
|
"author": "CareCard team",
|
|
16
18
|
"license": "ISC",
|
|
17
19
|
"devDependencies": {
|
|
18
|
-
"mocha": "
|
|
20
|
+
"@types/mocha": "10.0.10",
|
|
21
|
+
"@types/node": "25.5.0",
|
|
22
|
+
"mocha": "11.7.5",
|
|
23
|
+
"ts-node": "10.9.2",
|
|
24
|
+
"typescript": "5.9.3"
|
|
19
25
|
}
|
|
20
26
|
}
|