@hawcx/oauth-client 0.1.0 → 4.0.1

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.
Files changed (71) hide show
  1. package/README.md +475 -204
  2. package/dist/delegation/credentials.d.ts +92 -0
  3. package/dist/delegation/credentials.d.ts.map +1 -0
  4. package/dist/delegation/credentials.js +215 -0
  5. package/dist/delegation/credentials.js.map +1 -0
  6. package/dist/delegation/crypto.d.ts +2 -2
  7. package/dist/delegation/crypto.d.ts.map +1 -1
  8. package/dist/delegation/crypto.js +41 -32
  9. package/dist/delegation/crypto.js.map +1 -1
  10. package/dist/delegation/exceptions.d.ts +12 -5
  11. package/dist/delegation/exceptions.d.ts.map +1 -1
  12. package/dist/delegation/exceptions.js +12 -14
  13. package/dist/delegation/exceptions.js.map +1 -1
  14. package/dist/delegation/factory.d.ts +20 -0
  15. package/dist/delegation/factory.d.ts.map +1 -0
  16. package/dist/delegation/factory.js +49 -0
  17. package/dist/delegation/factory.js.map +1 -0
  18. package/dist/delegation/hawcx.d.ts +67 -224
  19. package/dist/delegation/hawcx.d.ts.map +1 -1
  20. package/dist/delegation/hawcx.js +116 -299
  21. package/dist/delegation/hawcx.js.map +1 -1
  22. package/dist/delegation/index.d.ts +4 -9
  23. package/dist/delegation/index.d.ts.map +1 -1
  24. package/dist/delegation/index.js +5 -17
  25. package/dist/delegation/index.js.map +1 -1
  26. package/dist/delegation/transport.d.ts +40 -0
  27. package/dist/delegation/transport.d.ts.map +1 -0
  28. package/dist/delegation/transport.js +120 -0
  29. package/dist/delegation/transport.js.map +1 -0
  30. package/dist/errors.d.ts +14 -0
  31. package/dist/errors.d.ts.map +1 -0
  32. package/dist/errors.js +29 -0
  33. package/dist/errors.js.map +1 -0
  34. package/dist/index.d.ts +10 -15
  35. package/dist/index.d.ts.map +1 -1
  36. package/dist/index.js +15 -27
  37. package/dist/index.js.map +1 -1
  38. package/dist/oauth.d.ts +98 -0
  39. package/dist/oauth.d.ts.map +1 -0
  40. package/dist/oauth.js +150 -0
  41. package/dist/oauth.js.map +1 -0
  42. package/dist/stepup/client.d.ts +26 -0
  43. package/dist/stepup/client.d.ts.map +1 -0
  44. package/dist/stepup/client.js +84 -0
  45. package/dist/stepup/client.js.map +1 -0
  46. package/dist/stepup/index.d.ts +3 -0
  47. package/dist/stepup/index.d.ts.map +1 -0
  48. package/dist/stepup/index.js +19 -0
  49. package/dist/stepup/index.js.map +1 -0
  50. package/dist/stepup/types.d.ts +36 -0
  51. package/dist/stepup/types.d.ts.map +1 -0
  52. package/dist/stepup/types.js +3 -0
  53. package/dist/stepup/types.js.map +1 -0
  54. package/package.json +16 -17
  55. package/LICENSE +0 -22
  56. package/dist/client.d.ts +0 -134
  57. package/dist/client.d.ts.map +0 -1
  58. package/dist/client.js +0 -301
  59. package/dist/client.js.map +0 -1
  60. package/dist/delegation/client.d.ts +0 -116
  61. package/dist/delegation/client.d.ts.map +0 -1
  62. package/dist/delegation/client.js +0 -227
  63. package/dist/delegation/client.js.map +0 -1
  64. package/dist/exceptions.d.ts +0 -52
  65. package/dist/exceptions.d.ts.map +0 -1
  66. package/dist/exceptions.js +0 -81
  67. package/dist/exceptions.js.map +0 -1
  68. package/dist/utils.d.ts +0 -61
  69. package/dist/utils.d.ts.map +0 -1
  70. package/dist/utils.js +0 -263
  71. package/dist/utils.js.map +0 -1
@@ -1,52 +0,0 @@
1
- /**
2
- * Custom exceptions for the Hawcx OAuth Client SDK
3
- */
4
- /**
5
- * Base exception for all hawcx_oauth_client errors
6
- */
7
- export declare class HawcxOAuthError extends Error {
8
- constructor(message: string);
9
- }
10
- /**
11
- * Raised when the OAuth code exchange fails.
12
- *
13
- * This can happen due to:
14
- * - Invalid or expired authorization code
15
- * - Network connectivity issues
16
- * - OAuth server errors
17
- * - Malformed responses
18
- */
19
- export declare class OAuthExchangeError extends HawcxOAuthError {
20
- statusCode?: number;
21
- responseBody?: any;
22
- constructor(message: string, statusCode?: number, responseBody?: any);
23
- toString(): string;
24
- }
25
- /**
26
- * Raised when JWT signature or claims verification fails.
27
- *
28
- * This can happen due to:
29
- * - Invalid JWT signature (tampering detected)
30
- * - Expired token
31
- * - Wrong audience or issuer
32
- * - Token not yet valid (nbf claim)
33
- * - Malformed JWT structure
34
- */
35
- export declare class JWTVerificationError extends HawcxOAuthError {
36
- originalError?: Error;
37
- constructor(message: string, originalError?: Error);
38
- }
39
- /**
40
- * Raised when the provided public key is invalid or unreadable.
41
- *
42
- * This can happen due to:
43
- * - File not found
44
- * - Invalid PEM format
45
- * - Wrong key algorithm
46
- * - Permission denied
47
- * - Empty key
48
- */
49
- export declare class InvalidPublicKeyError extends HawcxOAuthError {
50
- constructor(message: string);
51
- }
52
- //# sourceMappingURL=exceptions.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"exceptions.d.ts","sourceRoot":"","sources":["../src/exceptions.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH;;GAEG;AACH,qBAAa,eAAgB,SAAQ,KAAK;gBAC5B,OAAO,EAAE,MAAM;CAK5B;AAED;;;;;;;;GAQG;AACH,qBAAa,kBAAmB,SAAQ,eAAe;IACrD,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,YAAY,CAAC,EAAE,GAAG,CAAC;gBAEP,OAAO,EAAE,MAAM,EAAE,UAAU,CAAC,EAAE,MAAM,EAAE,YAAY,CAAC,EAAE,GAAG;IAQpE,QAAQ,IAAI,MAAM;CAOnB;AAED;;;;;;;;;GASG;AACH,qBAAa,oBAAqB,SAAQ,eAAe;IACvD,aAAa,CAAC,EAAE,KAAK,CAAC;gBAEV,OAAO,EAAE,MAAM,EAAE,aAAa,CAAC,EAAE,KAAK;CAMnD;AAED;;;;;;;;;GASG;AACH,qBAAa,qBAAsB,SAAQ,eAAe;gBAC5C,OAAO,EAAE,MAAM;CAK5B"}
@@ -1,81 +0,0 @@
1
- "use strict";
2
- /**
3
- * Custom exceptions for the Hawcx OAuth Client SDK
4
- */
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.InvalidPublicKeyError = exports.JWTVerificationError = exports.OAuthExchangeError = exports.HawcxOAuthError = void 0;
7
- /**
8
- * Base exception for all hawcx_oauth_client errors
9
- */
10
- class HawcxOAuthError extends Error {
11
- constructor(message) {
12
- super(message);
13
- this.name = 'HawcxOAuthError';
14
- Object.setPrototypeOf(this, HawcxOAuthError.prototype);
15
- }
16
- }
17
- exports.HawcxOAuthError = HawcxOAuthError;
18
- /**
19
- * Raised when the OAuth code exchange fails.
20
- *
21
- * This can happen due to:
22
- * - Invalid or expired authorization code
23
- * - Network connectivity issues
24
- * - OAuth server errors
25
- * - Malformed responses
26
- */
27
- class OAuthExchangeError extends HawcxOAuthError {
28
- constructor(message, statusCode, responseBody) {
29
- super(message);
30
- this.name = 'OAuthExchangeError';
31
- this.statusCode = statusCode;
32
- this.responseBody = responseBody;
33
- Object.setPrototypeOf(this, OAuthExchangeError.prototype);
34
- }
35
- toString() {
36
- let base = this.message;
37
- if (this.statusCode) {
38
- base = `${base} (HTTP ${this.statusCode})`;
39
- }
40
- return base;
41
- }
42
- }
43
- exports.OAuthExchangeError = OAuthExchangeError;
44
- /**
45
- * Raised when JWT signature or claims verification fails.
46
- *
47
- * This can happen due to:
48
- * - Invalid JWT signature (tampering detected)
49
- * - Expired token
50
- * - Wrong audience or issuer
51
- * - Token not yet valid (nbf claim)
52
- * - Malformed JWT structure
53
- */
54
- class JWTVerificationError extends HawcxOAuthError {
55
- constructor(message, originalError) {
56
- super(message);
57
- this.name = 'JWTVerificationError';
58
- this.originalError = originalError;
59
- Object.setPrototypeOf(this, JWTVerificationError.prototype);
60
- }
61
- }
62
- exports.JWTVerificationError = JWTVerificationError;
63
- /**
64
- * Raised when the provided public key is invalid or unreadable.
65
- *
66
- * This can happen due to:
67
- * - File not found
68
- * - Invalid PEM format
69
- * - Wrong key algorithm
70
- * - Permission denied
71
- * - Empty key
72
- */
73
- class InvalidPublicKeyError extends HawcxOAuthError {
74
- constructor(message) {
75
- super(message);
76
- this.name = 'InvalidPublicKeyError';
77
- Object.setPrototypeOf(this, InvalidPublicKeyError.prototype);
78
- }
79
- }
80
- exports.InvalidPublicKeyError = InvalidPublicKeyError;
81
- //# sourceMappingURL=exceptions.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"exceptions.js","sourceRoot":"","sources":["../src/exceptions.ts"],"names":[],"mappings":";AAAA;;GAEG;;;AAEH;;GAEG;AACH,MAAa,eAAgB,SAAQ,KAAK;IACxC,YAAY,OAAe;QACzB,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,iBAAiB,CAAC;QAC9B,MAAM,CAAC,cAAc,CAAC,IAAI,EAAE,eAAe,CAAC,SAAS,CAAC,CAAC;IACzD,CAAC;CACF;AAND,0CAMC;AAED;;;;;;;;GAQG;AACH,MAAa,kBAAmB,SAAQ,eAAe;IAIrD,YAAY,OAAe,EAAE,UAAmB,EAAE,YAAkB;QAClE,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,oBAAoB,CAAC;QACjC,IAAI,CAAC,UAAU,GAAG,UAAU,CAAC;QAC7B,IAAI,CAAC,YAAY,GAAG,YAAY,CAAC;QACjC,MAAM,CAAC,cAAc,CAAC,IAAI,EAAE,kBAAkB,CAAC,SAAS,CAAC,CAAC;IAC5D,CAAC;IAED,QAAQ;QACN,IAAI,IAAI,GAAG,IAAI,CAAC,OAAO,CAAC;QACxB,IAAI,IAAI,CAAC,UAAU,EAAE,CAAC;YACpB,IAAI,GAAG,GAAG,IAAI,UAAU,IAAI,CAAC,UAAU,GAAG,CAAC;QAC7C,CAAC;QACD,OAAO,IAAI,CAAC;IACd,CAAC;CACF;AAnBD,gDAmBC;AAED;;;;;;;;;GASG;AACH,MAAa,oBAAqB,SAAQ,eAAe;IAGvD,YAAY,OAAe,EAAE,aAAqB;QAChD,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,sBAAsB,CAAC;QACnC,IAAI,CAAC,aAAa,GAAG,aAAa,CAAC;QACnC,MAAM,CAAC,cAAc,CAAC,IAAI,EAAE,oBAAoB,CAAC,SAAS,CAAC,CAAC;IAC9D,CAAC;CACF;AATD,oDASC;AAED;;;;;;;;;GASG;AACH,MAAa,qBAAsB,SAAQ,eAAe;IACxD,YAAY,OAAe;QACzB,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,uBAAuB,CAAC;QACpC,MAAM,CAAC,cAAc,CAAC,IAAI,EAAE,qBAAqB,CAAC,SAAS,CAAC,CAAC;IAC/D,CAAC;CACF;AAND,sDAMC"}
package/dist/utils.d.ts DELETED
@@ -1,61 +0,0 @@
1
- /**
2
- * Internal utilities for the Hawcx OAuth Client SDK
3
- */
4
- /**
5
- * Load public key from string or file.
6
- *
7
- * @param keyInput - Either a PEM-formatted public key string or a path to a key file
8
- * @returns PEM-formatted public key string
9
- * @throws InvalidPublicKeyError if the key cannot be loaded or is invalid
10
- *
11
- * @example
12
- * ```typescript
13
- * // From file
14
- * const key = loadPublicKey('keys/public.pem');
15
- *
16
- * // From string
17
- * const key = loadPublicKey(process.env.PUBLIC_KEY!);
18
- * ```
19
- */
20
- export declare function loadPublicKey(keyInput: string): string;
21
- /**
22
- * Fetch JWKS (JSON Web Key Set) from a URL with caching.
23
- *
24
- * @param jwksUrl - URL to fetch JWKS from (e.g., https://example.com/.well-known/jwks.json)
25
- * @param timeout - Request timeout in milliseconds (default: 10000)
26
- * @param ttl - Cache TTL in milliseconds (default: 3600000)
27
- * @returns JWKS object containing 'keys' array
28
- * @throws InvalidPublicKeyError if JWKS cannot be fetched or is invalid
29
- *
30
- * @example
31
- * ```typescript
32
- * const jwks = await fetchJwks('https://example.com/.well-known/jwks.json');
33
- * console.log(jwks.keys[0].kid);
34
- * ```
35
- */
36
- export declare function fetchJwks(jwksUrl: string, timeout?: number, ttl?: number): Promise<any>;
37
- /**
38
- * Extract a public key from JWKS and convert to PEM format.
39
- *
40
- * @param jwks - JWKS object (must contain 'keys' array)
41
- * @param kid - Key ID to search for (optional). If not provided, uses first key.
42
- * @returns PEM-formatted public key string
43
- * @throws InvalidPublicKeyError if key cannot be found or converted
44
- *
45
- * @example
46
- * ```typescript
47
- * const jwks = await fetchJwks('https://example.com/.well-known/jwks.json');
48
- * const pemKey = getKeyFromJwks(jwks, 'key-123');
49
- * ```
50
- */
51
- export declare function getKeyFromJwks(jwks: any, kid?: string): string;
52
- /**
53
- * Convert a JWK (JSON Web Key) to PEM format.
54
- * Supports RSA and EC (Elliptic Curve) keys.
55
- *
56
- * @param jwk - JWK object
57
- * @returns PEM-formatted public key string
58
- * @throws InvalidPublicKeyError if JWK cannot be converted
59
- */
60
- export declare function jwkToPem(jwk: any): string;
61
- //# sourceMappingURL=utils.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA;;GAEG;AAUH;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,aAAa,CAAC,QAAQ,EAAE,MAAM,GAAG,MAAM,CAgCtD;AAuCD;;;;;;;;;;;;;;GAcG;AACH,wBAAsB,SAAS,CAC7B,OAAO,EAAE,MAAM,EACf,OAAO,GAAE,MAAc,EACvB,GAAG,GAAE,MAAuB,GAC3B,OAAO,CAAC,GAAG,CAAC,CA2Cd;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAgB,cAAc,CAAC,IAAI,EAAE,GAAG,EAAE,GAAG,CAAC,EAAE,MAAM,GAAG,MAAM,CAyB9D;AAED;;;;;;;GAOG;AACH,wBAAgB,QAAQ,CAAC,GAAG,EAAE,GAAG,GAAG,MAAM,CA+BzC"}
package/dist/utils.js DELETED
@@ -1,263 +0,0 @@
1
- "use strict";
2
- /**
3
- * Internal utilities for the Hawcx OAuth Client SDK
4
- */
5
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
6
- if (k2 === undefined) k2 = k;
7
- var desc = Object.getOwnPropertyDescriptor(m, k);
8
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
9
- desc = { enumerable: true, get: function() { return m[k]; } };
10
- }
11
- Object.defineProperty(o, k2, desc);
12
- }) : (function(o, m, k, k2) {
13
- if (k2 === undefined) k2 = k;
14
- o[k2] = m[k];
15
- }));
16
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
17
- Object.defineProperty(o, "default", { enumerable: true, value: v });
18
- }) : function(o, v) {
19
- o["default"] = v;
20
- });
21
- var __importStar = (this && this.__importStar) || (function () {
22
- var ownKeys = function(o) {
23
- ownKeys = Object.getOwnPropertyNames || function (o) {
24
- var ar = [];
25
- for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
26
- return ar;
27
- };
28
- return ownKeys(o);
29
- };
30
- return function (mod) {
31
- if (mod && mod.__esModule) return mod;
32
- var result = {};
33
- if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
34
- __setModuleDefault(result, mod);
35
- return result;
36
- };
37
- })();
38
- var __importDefault = (this && this.__importDefault) || function (mod) {
39
- return (mod && mod.__esModule) ? mod : { "default": mod };
40
- };
41
- Object.defineProperty(exports, "__esModule", { value: true });
42
- exports.loadPublicKey = loadPublicKey;
43
- exports.fetchJwks = fetchJwks;
44
- exports.getKeyFromJwks = getKeyFromJwks;
45
- exports.jwkToPem = jwkToPem;
46
- const fs = __importStar(require("fs"));
47
- const axios_1 = __importDefault(require("axios"));
48
- const exceptions_1 = require("./exceptions");
49
- // JWKS cache: {url: {jwks: {...}, timestamp: number}}
50
- const jwksCache = {};
51
- const JWKS_CACHE_TTL = 3600000; // 1 hour in milliseconds
52
- /**
53
- * Load public key from string or file.
54
- *
55
- * @param keyInput - Either a PEM-formatted public key string or a path to a key file
56
- * @returns PEM-formatted public key string
57
- * @throws InvalidPublicKeyError if the key cannot be loaded or is invalid
58
- *
59
- * @example
60
- * ```typescript
61
- * // From file
62
- * const key = loadPublicKey('keys/public.pem');
63
- *
64
- * // From string
65
- * const key = loadPublicKey(process.env.PUBLIC_KEY!);
66
- * ```
67
- */
68
- function loadPublicKey(keyInput) {
69
- // Check for empty input first
70
- if (!keyInput || (typeof keyInput === 'string' && !keyInput.trim())) {
71
- throw new exceptions_1.InvalidPublicKeyError('Public key is empty');
72
- }
73
- // Check if it looks like a file path (not starting with -----)
74
- if (!keyInput.includes('-----BEGIN')) {
75
- try {
76
- if (!fs.existsSync(keyInput)) {
77
- throw new exceptions_1.InvalidPublicKeyError(`Public key file not found: ${keyInput}`);
78
- }
79
- const stat = fs.statSync(keyInput);
80
- if (!stat.isFile()) {
81
- throw new exceptions_1.InvalidPublicKeyError(`Public key path is not a file: ${keyInput}`);
82
- }
83
- const keyContent = fs.readFileSync(keyInput, 'utf-8');
84
- return validatePemFormat(keyContent);
85
- }
86
- catch (error) {
87
- if (error instanceof exceptions_1.InvalidPublicKeyError) {
88
- throw error;
89
- }
90
- throw new exceptions_1.InvalidPublicKeyError(`Error reading public key file ${keyInput}: ${error}`);
91
- }
92
- }
93
- // Otherwise, treat as PEM string directly
94
- if (typeof keyInput !== 'string') {
95
- throw new exceptions_1.InvalidPublicKeyError(`Public key must be a string, got ${typeof keyInput}`);
96
- }
97
- return validatePemFormat(keyInput);
98
- }
99
- /**
100
- * Validate that the key content looks like a valid PEM public key.
101
- *
102
- * @param keyContent - The key content to validate
103
- * @returns The validated key content
104
- * @throws InvalidPublicKeyError if the key format is invalid
105
- */
106
- function validatePemFormat(keyContent) {
107
- if (!keyContent || !keyContent.trim()) {
108
- throw new exceptions_1.InvalidPublicKeyError('Public key is empty');
109
- }
110
- const keyStripped = keyContent.trim();
111
- // Check for PEM markers
112
- if (!keyStripped.includes('-----BEGIN') || !keyStripped.includes('-----END')) {
113
- throw new exceptions_1.InvalidPublicKeyError('Public key does not appear to be in PEM format (missing BEGIN/END markers)');
114
- }
115
- // Check for common public key markers
116
- const validMarkers = [
117
- 'BEGIN PUBLIC KEY',
118
- 'BEGIN RSA PUBLIC KEY',
119
- 'BEGIN CERTIFICATE',
120
- ];
121
- if (!validMarkers.some(marker => keyStripped.includes(marker))) {
122
- throw new exceptions_1.InvalidPublicKeyError(`Public key does not contain recognized PEM markers. Expected one of: ${validMarkers.join(', ')}`);
123
- }
124
- return keyStripped;
125
- }
126
- /**
127
- * Fetch JWKS (JSON Web Key Set) from a URL with caching.
128
- *
129
- * @param jwksUrl - URL to fetch JWKS from (e.g., https://example.com/.well-known/jwks.json)
130
- * @param timeout - Request timeout in milliseconds (default: 10000)
131
- * @param ttl - Cache TTL in milliseconds (default: 3600000)
132
- * @returns JWKS object containing 'keys' array
133
- * @throws InvalidPublicKeyError if JWKS cannot be fetched or is invalid
134
- *
135
- * @example
136
- * ```typescript
137
- * const jwks = await fetchJwks('https://example.com/.well-known/jwks.json');
138
- * console.log(jwks.keys[0].kid);
139
- * ```
140
- */
141
- async function fetchJwks(jwksUrl, timeout = 10000, ttl = JWKS_CACHE_TTL) {
142
- if (!jwksUrl || typeof jwksUrl !== 'string') {
143
- throw new exceptions_1.InvalidPublicKeyError('JWKS URL must be a non-empty string');
144
- }
145
- // Check cache
146
- const now = Date.now();
147
- const cached = jwksCache[jwksUrl];
148
- if (cached && now - cached.timestamp < ttl) {
149
- return cached.jwks;
150
- }
151
- // Fetch from URL
152
- try {
153
- const response = await axios_1.default.get(jwksUrl, { timeout });
154
- const jwks = response.data;
155
- // Validate JWKS format
156
- if (!jwks || typeof jwks !== 'object' || !Array.isArray(jwks.keys)) {
157
- throw new exceptions_1.InvalidPublicKeyError(`Invalid JWKS format: missing 'keys' field in response from ${jwksUrl}`);
158
- }
159
- if (jwks.keys.length === 0) {
160
- throw new exceptions_1.InvalidPublicKeyError(`Invalid JWKS format: 'keys' must be a non-empty array in response from ${jwksUrl}`);
161
- }
162
- // Cache the result
163
- jwksCache[jwksUrl] = {
164
- jwks,
165
- timestamp: now,
166
- };
167
- return jwks;
168
- }
169
- catch (error) {
170
- if (axios_1.default.isAxiosError(error)) {
171
- throw new exceptions_1.InvalidPublicKeyError(`Failed to fetch JWKS from ${jwksUrl}: ${error.message}`);
172
- }
173
- throw new exceptions_1.InvalidPublicKeyError(`Failed to fetch JWKS from ${jwksUrl}: ${error}`);
174
- }
175
- }
176
- /**
177
- * Extract a public key from JWKS and convert to PEM format.
178
- *
179
- * @param jwks - JWKS object (must contain 'keys' array)
180
- * @param kid - Key ID to search for (optional). If not provided, uses first key.
181
- * @returns PEM-formatted public key string
182
- * @throws InvalidPublicKeyError if key cannot be found or converted
183
- *
184
- * @example
185
- * ```typescript
186
- * const jwks = await fetchJwks('https://example.com/.well-known/jwks.json');
187
- * const pemKey = getKeyFromJwks(jwks, 'key-123');
188
- * ```
189
- */
190
- function getKeyFromJwks(jwks, kid) {
191
- if (!jwks || typeof jwks !== 'object' || !Array.isArray(jwks.keys)) {
192
- throw new exceptions_1.InvalidPublicKeyError("Invalid JWKS: missing 'keys' field");
193
- }
194
- const keys = jwks.keys;
195
- if (keys.length === 0) {
196
- throw new exceptions_1.InvalidPublicKeyError("Invalid JWKS: 'keys' must be a non-empty array");
197
- }
198
- // Find the key
199
- let jwk;
200
- if (kid) {
201
- // Search for specific key ID
202
- jwk = keys.find((k) => k.kid === kid);
203
- if (!jwk) {
204
- throw new exceptions_1.InvalidPublicKeyError(`Key with kid='${kid}' not found in JWKS`);
205
- }
206
- }
207
- else {
208
- // Use first key
209
- jwk = keys[0];
210
- }
211
- // Convert JWK to PEM
212
- return jwkToPem(jwk);
213
- }
214
- /**
215
- * Convert a JWK (JSON Web Key) to PEM format.
216
- * Supports RSA and EC (Elliptic Curve) keys.
217
- *
218
- * @param jwk - JWK object
219
- * @returns PEM-formatted public key string
220
- * @throws InvalidPublicKeyError if JWK cannot be converted
221
- */
222
- function jwkToPem(jwk) {
223
- if (!jwk || typeof jwk !== 'object') {
224
- throw new exceptions_1.InvalidPublicKeyError('Invalid JWK: must be an object');
225
- }
226
- const { kty } = jwk;
227
- if (!kty) {
228
- throw new exceptions_1.InvalidPublicKeyError('Invalid JWK: missing kty field');
229
- }
230
- try {
231
- // Use Node.js crypto module to convert JWK to PEM
232
- const crypto = require('crypto');
233
- // Create a KeyObject from the JWK
234
- const keyObject = crypto.createPublicKey({
235
- key: jwk,
236
- format: 'jwk'
237
- });
238
- // Export as PEM
239
- const pem = keyObject.export({
240
- type: 'spki',
241
- format: 'pem'
242
- });
243
- return pem.toString();
244
- }
245
- catch (error) {
246
- throw new exceptions_1.InvalidPublicKeyError(`Failed to convert JWK to PEM: ${error}`);
247
- }
248
- }
249
- /**
250
- * Decode base64url-encoded data (RFC 4648)
251
- */
252
- function base64urlDecode(data) {
253
- // Add padding if needed
254
- let padded = data;
255
- const padding = 4 - (data.length % 4);
256
- if (padding !== 4) {
257
- padded += '='.repeat(padding);
258
- }
259
- // Replace URL-safe characters
260
- const base64 = padded.replace(/-/g, '+').replace(/_/g, '/');
261
- return Buffer.from(base64, 'base64');
262
- }
263
- //# sourceMappingURL=utils.js.map
package/dist/utils.js.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"file":"utils.js","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":";AAAA;;GAEG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA0BH,sCAgCC;AAsDD,8BA+CC;AAgBD,wCAyBC;AAUD,4BA+BC;AA/OD,uCAAyB;AACzB,kDAA0B;AAC1B,6CAAqD;AAErD,sDAAsD;AACtD,MAAM,SAAS,GAAqD,EAAE,CAAC;AACvE,MAAM,cAAc,GAAG,OAAO,CAAC,CAAC,yBAAyB;AAEzD;;;;;;;;;;;;;;;GAeG;AACH,SAAgB,aAAa,CAAC,QAAgB;IAC5C,8BAA8B;IAC9B,IAAI,CAAC,QAAQ,IAAI,CAAC,OAAO,QAAQ,KAAK,QAAQ,IAAI,CAAC,QAAQ,CAAC,IAAI,EAAE,CAAC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