qa360 1.4.5 → 2.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 (209) hide show
  1. package/README.md +1 -1
  2. package/dist/commands/ai.d.ts +41 -0
  3. package/dist/commands/ai.js +499 -0
  4. package/dist/commands/ask.js +12 -12
  5. package/dist/commands/coverage.d.ts +8 -0
  6. package/dist/commands/coverage.js +252 -0
  7. package/dist/commands/explain.d.ts +27 -0
  8. package/dist/commands/explain.js +630 -0
  9. package/dist/commands/flakiness.d.ts +73 -0
  10. package/dist/commands/flakiness.js +435 -0
  11. package/dist/commands/generate.d.ts +66 -0
  12. package/dist/commands/generate.js +438 -0
  13. package/dist/commands/init.d.ts +56 -9
  14. package/dist/commands/init.js +217 -10
  15. package/dist/commands/monitor.d.ts +27 -0
  16. package/dist/commands/monitor.js +225 -0
  17. package/dist/commands/ollama.d.ts +40 -0
  18. package/dist/commands/ollama.js +301 -0
  19. package/dist/commands/pack.d.ts +37 -9
  20. package/dist/commands/pack.js +240 -141
  21. package/dist/commands/regression.d.ts +8 -0
  22. package/dist/commands/regression.js +340 -0
  23. package/dist/commands/repair.d.ts +26 -0
  24. package/dist/commands/repair.js +307 -0
  25. package/dist/commands/retry.d.ts +43 -0
  26. package/dist/commands/retry.js +275 -0
  27. package/dist/commands/run.d.ts +8 -3
  28. package/dist/commands/run.js +45 -31
  29. package/dist/commands/slo.d.ts +8 -0
  30. package/dist/commands/slo.js +327 -0
  31. package/dist/core/adapters/playwright-native-api.d.ts +183 -0
  32. package/dist/core/adapters/playwright-native-api.js +461 -0
  33. package/dist/core/adapters/playwright-ui.d.ts +7 -0
  34. package/dist/core/adapters/playwright-ui.js +29 -1
  35. package/dist/core/ai/anthropic-provider.d.ts +50 -0
  36. package/dist/core/ai/anthropic-provider.js +211 -0
  37. package/dist/core/ai/deepseek-provider.d.ts +81 -0
  38. package/dist/core/ai/deepseek-provider.js +254 -0
  39. package/dist/core/ai/index.d.ts +60 -0
  40. package/dist/core/ai/index.js +18 -0
  41. package/dist/core/ai/llm-client.d.ts +45 -0
  42. package/dist/core/ai/llm-client.js +7 -0
  43. package/dist/core/ai/mock-provider.d.ts +49 -0
  44. package/dist/core/ai/mock-provider.js +121 -0
  45. package/dist/core/ai/ollama-provider.d.ts +78 -0
  46. package/dist/core/ai/ollama-provider.js +192 -0
  47. package/dist/core/ai/openai-provider.d.ts +48 -0
  48. package/dist/core/ai/openai-provider.js +188 -0
  49. package/dist/core/ai/provider-factory.d.ts +160 -0
  50. package/dist/core/ai/provider-factory.js +269 -0
  51. package/dist/core/auth/api-key-provider.d.ts +16 -0
  52. package/dist/core/auth/api-key-provider.js +63 -0
  53. package/dist/core/auth/aws-iam-provider.d.ts +35 -0
  54. package/dist/core/auth/aws-iam-provider.js +177 -0
  55. package/dist/core/auth/azure-ad-provider.d.ts +15 -0
  56. package/dist/core/auth/azure-ad-provider.js +99 -0
  57. package/dist/core/auth/basic-auth-provider.d.ts +26 -0
  58. package/dist/core/auth/basic-auth-provider.js +111 -0
  59. package/dist/core/auth/gcp-adc-provider.d.ts +27 -0
  60. package/dist/core/auth/gcp-adc-provider.js +126 -0
  61. package/dist/core/auth/index.d.ts +238 -0
  62. package/dist/core/auth/index.js +82 -0
  63. package/dist/core/auth/jwt-provider.d.ts +19 -0
  64. package/dist/core/auth/jwt-provider.js +160 -0
  65. package/dist/core/auth/manager.d.ts +84 -0
  66. package/dist/core/auth/manager.js +230 -0
  67. package/dist/core/auth/oauth2-provider.d.ts +17 -0
  68. package/dist/core/auth/oauth2-provider.js +114 -0
  69. package/dist/core/auth/totp-provider.d.ts +31 -0
  70. package/dist/core/auth/totp-provider.js +134 -0
  71. package/dist/core/auth/ui-login-provider.d.ts +26 -0
  72. package/dist/core/auth/ui-login-provider.js +198 -0
  73. package/dist/core/cache/index.d.ts +7 -0
  74. package/dist/core/cache/index.js +6 -0
  75. package/dist/core/cache/lru-cache.d.ts +203 -0
  76. package/dist/core/cache/lru-cache.js +397 -0
  77. package/dist/core/coverage/analyzer.d.ts +101 -0
  78. package/dist/core/coverage/analyzer.js +415 -0
  79. package/dist/core/coverage/collector.d.ts +74 -0
  80. package/dist/core/coverage/collector.js +459 -0
  81. package/dist/core/coverage/config.d.ts +37 -0
  82. package/dist/core/coverage/config.js +156 -0
  83. package/dist/core/coverage/index.d.ts +11 -0
  84. package/dist/core/coverage/index.js +15 -0
  85. package/dist/core/coverage/types.d.ts +267 -0
  86. package/dist/core/coverage/types.js +6 -0
  87. package/dist/core/coverage/vault.d.ts +95 -0
  88. package/dist/core/coverage/vault.js +405 -0
  89. package/dist/core/dashboard/assets.d.ts +6 -0
  90. package/dist/core/dashboard/assets.js +690 -0
  91. package/dist/core/dashboard/index.d.ts +6 -0
  92. package/dist/core/dashboard/index.js +5 -0
  93. package/dist/core/dashboard/server.d.ts +72 -0
  94. package/dist/core/dashboard/server.js +354 -0
  95. package/dist/core/dashboard/types.d.ts +70 -0
  96. package/dist/core/dashboard/types.js +5 -0
  97. package/dist/core/discoverer/index.d.ts +115 -0
  98. package/dist/core/discoverer/index.js +250 -0
  99. package/dist/core/flakiness/index.d.ts +228 -0
  100. package/dist/core/flakiness/index.js +384 -0
  101. package/dist/core/generation/code-formatter.d.ts +111 -0
  102. package/dist/core/generation/code-formatter.js +307 -0
  103. package/dist/core/generation/code-generator.d.ts +144 -0
  104. package/dist/core/generation/code-generator.js +293 -0
  105. package/dist/core/generation/generator.d.ts +40 -0
  106. package/dist/core/generation/generator.js +76 -0
  107. package/dist/core/generation/index.d.ts +30 -0
  108. package/dist/core/generation/index.js +28 -0
  109. package/dist/core/generation/pack-generator.d.ts +107 -0
  110. package/dist/core/generation/pack-generator.js +416 -0
  111. package/dist/core/generation/prompt-builder.d.ts +132 -0
  112. package/dist/core/generation/prompt-builder.js +672 -0
  113. package/dist/core/generation/source-analyzer.d.ts +213 -0
  114. package/dist/core/generation/source-analyzer.js +657 -0
  115. package/dist/core/generation/test-optimizer.d.ts +117 -0
  116. package/dist/core/generation/test-optimizer.js +328 -0
  117. package/dist/core/generation/types.d.ts +214 -0
  118. package/dist/core/generation/types.js +4 -0
  119. package/dist/core/index.d.ts +23 -1
  120. package/dist/core/index.js +39 -0
  121. package/dist/core/pack/validator.js +31 -1
  122. package/dist/core/pack-v2/index.d.ts +9 -0
  123. package/dist/core/pack-v2/index.js +8 -0
  124. package/dist/core/pack-v2/loader.d.ts +62 -0
  125. package/dist/core/pack-v2/loader.js +231 -0
  126. package/dist/core/pack-v2/migrator.d.ts +56 -0
  127. package/dist/core/pack-v2/migrator.js +455 -0
  128. package/dist/core/pack-v2/validator.d.ts +61 -0
  129. package/dist/core/pack-v2/validator.js +577 -0
  130. package/dist/core/regression/detector.d.ts +107 -0
  131. package/dist/core/regression/detector.js +497 -0
  132. package/dist/core/regression/index.d.ts +9 -0
  133. package/dist/core/regression/index.js +11 -0
  134. package/dist/core/regression/trend-analyzer.d.ts +102 -0
  135. package/dist/core/regression/trend-analyzer.js +345 -0
  136. package/dist/core/regression/types.d.ts +222 -0
  137. package/dist/core/regression/types.js +7 -0
  138. package/dist/core/regression/vault.d.ts +87 -0
  139. package/dist/core/regression/vault.js +289 -0
  140. package/dist/core/repair/engine/fixer.d.ts +24 -0
  141. package/dist/core/repair/engine/fixer.js +226 -0
  142. package/dist/core/repair/engine/suggestion-engine.d.ts +18 -0
  143. package/dist/core/repair/engine/suggestion-engine.js +187 -0
  144. package/dist/core/repair/index.d.ts +10 -0
  145. package/dist/core/repair/index.js +13 -0
  146. package/dist/core/repair/repairer.d.ts +90 -0
  147. package/dist/core/repair/repairer.js +284 -0
  148. package/dist/core/repair/types.d.ts +91 -0
  149. package/dist/core/repair/types.js +6 -0
  150. package/dist/core/repair/utils/error-analyzer.d.ts +28 -0
  151. package/dist/core/repair/utils/error-analyzer.js +264 -0
  152. package/dist/core/retry/flakiness-integration.d.ts +60 -0
  153. package/dist/core/retry/flakiness-integration.js +228 -0
  154. package/dist/core/retry/index.d.ts +14 -0
  155. package/dist/core/retry/index.js +16 -0
  156. package/dist/core/retry/retry-engine.d.ts +80 -0
  157. package/dist/core/retry/retry-engine.js +296 -0
  158. package/dist/core/retry/types.d.ts +178 -0
  159. package/dist/core/retry/types.js +52 -0
  160. package/dist/core/retry/vault.d.ts +77 -0
  161. package/dist/core/retry/vault.js +304 -0
  162. package/dist/core/runner/e2e-helpers.d.ts +102 -0
  163. package/dist/core/runner/e2e-helpers.js +153 -0
  164. package/dist/core/runner/phase3-runner.d.ts +101 -2
  165. package/dist/core/runner/phase3-runner.js +559 -24
  166. package/dist/core/self-healing/assertion-healer.d.ts +97 -0
  167. package/dist/core/self-healing/assertion-healer.js +371 -0
  168. package/dist/core/self-healing/engine.d.ts +122 -0
  169. package/dist/core/self-healing/engine.js +538 -0
  170. package/dist/core/self-healing/index.d.ts +10 -0
  171. package/dist/core/self-healing/index.js +11 -0
  172. package/dist/core/self-healing/selector-healer.d.ts +103 -0
  173. package/dist/core/self-healing/selector-healer.js +372 -0
  174. package/dist/core/self-healing/types.d.ts +152 -0
  175. package/dist/core/self-healing/types.js +6 -0
  176. package/dist/core/slo/config.d.ts +107 -0
  177. package/dist/core/slo/config.js +360 -0
  178. package/dist/core/slo/index.d.ts +11 -0
  179. package/dist/core/slo/index.js +15 -0
  180. package/dist/core/slo/sli-calculator.d.ts +92 -0
  181. package/dist/core/slo/sli-calculator.js +364 -0
  182. package/dist/core/slo/slo-tracker.d.ts +148 -0
  183. package/dist/core/slo/slo-tracker.js +379 -0
  184. package/dist/core/slo/types.d.ts +281 -0
  185. package/dist/core/slo/types.js +7 -0
  186. package/dist/core/slo/vault.d.ts +102 -0
  187. package/dist/core/slo/vault.js +427 -0
  188. package/dist/core/tui/index.d.ts +7 -0
  189. package/dist/core/tui/index.js +6 -0
  190. package/dist/core/tui/monitor.d.ts +92 -0
  191. package/dist/core/tui/monitor.js +271 -0
  192. package/dist/core/tui/renderer.d.ts +33 -0
  193. package/dist/core/tui/renderer.js +218 -0
  194. package/dist/core/tui/types.d.ts +63 -0
  195. package/dist/core/tui/types.js +5 -0
  196. package/dist/core/types/pack-v2.d.ts +425 -0
  197. package/dist/core/types/pack-v2.js +8 -0
  198. package/dist/core/vault/index.d.ts +116 -0
  199. package/dist/core/vault/index.js +400 -5
  200. package/dist/core/watch/index.d.ts +7 -0
  201. package/dist/core/watch/index.js +6 -0
  202. package/dist/core/watch/watch-mode.d.ts +213 -0
  203. package/dist/core/watch/watch-mode.js +389 -0
  204. package/dist/index.js +68 -68
  205. package/dist/utils/config.d.ts +5 -0
  206. package/dist/utils/config.js +136 -0
  207. package/package.json +5 -1
  208. package/dist/core/adapters/playwright-api.d.ts +0 -82
  209. package/dist/core/adapters/playwright-api.js +0 -264
@@ -0,0 +1,238 @@
1
+ /**
2
+ * QA360 Authentication Module
3
+ *
4
+ * Comprehensive authentication support for test adapters.
5
+ * Supports JWT, OAuth2, API Keys, Bearer tokens, Basic auth, TOTP,
6
+ * UI Login, and cloud provider credentials (GCP, AWS, Azure).
7
+ */
8
+ /**
9
+ * Authentication result
10
+ */
11
+ export interface AuthResult {
12
+ success: boolean;
13
+ credentials?: AuthCredentials;
14
+ error?: string;
15
+ expiresAt?: number;
16
+ }
17
+ /**
18
+ * Authentication credentials to be used in requests
19
+ */
20
+ export interface AuthCredentials {
21
+ type: AuthType;
22
+ headers?: Record<string, string>;
23
+ queryParams?: Record<string, string>;
24
+ cookies?: Cookie[];
25
+ body?: Record<string, unknown>;
26
+ }
27
+ /**
28
+ * Cookie for session management
29
+ */
30
+ export interface Cookie {
31
+ name: string;
32
+ value: string;
33
+ domain?: string;
34
+ path?: string;
35
+ expires?: Date;
36
+ httpOnly?: boolean;
37
+ secure?: boolean;
38
+ }
39
+ /**
40
+ * Authentication types
41
+ */
42
+ export type AuthType = 'none' | 'jwt' | 'oauth2' | 'api_key' | 'bearer' | 'basic' | 'totp' | 'ui_login' | 'gcp_adc' | 'aws_iam' | 'azure_ad';
43
+ /**
44
+ * Base configuration for all auth providers
45
+ */
46
+ export interface BaseAuthConfig {
47
+ type: AuthType;
48
+ enabled?: boolean;
49
+ cache?: {
50
+ enabled?: boolean;
51
+ ttl?: number;
52
+ };
53
+ }
54
+ /**
55
+ * JWT configuration
56
+ */
57
+ export interface JWTAuthConfig extends BaseAuthConfig {
58
+ type: 'jwt';
59
+ issuer?: string;
60
+ audience?: string;
61
+ subject?: string;
62
+ client_id?: string;
63
+ client_secret?: string;
64
+ token_endpoint?: string;
65
+ scopes?: string[];
66
+ auto_refresh?: boolean;
67
+ token?: string;
68
+ }
69
+ /**
70
+ * OAuth2 configuration
71
+ */
72
+ export interface OAuth2AuthConfig extends BaseAuthConfig {
73
+ type: 'oauth2';
74
+ token_url: string;
75
+ client_id: string;
76
+ client_secret?: string;
77
+ scopes?: string[];
78
+ grant_type?: 'client_credentials' | 'authorization_code' | 'password';
79
+ username?: string;
80
+ password?: string;
81
+ }
82
+ /**
83
+ * API Key configuration
84
+ */
85
+ export interface APIKeyAuthConfig extends BaseAuthConfig {
86
+ type: 'api_key';
87
+ key: string;
88
+ header_name?: string;
89
+ prefix?: string;
90
+ location?: 'header' | 'query';
91
+ }
92
+ /**
93
+ * Bearer token configuration
94
+ */
95
+ export interface BearerAuthConfig extends BaseAuthConfig {
96
+ type: 'bearer';
97
+ token: string;
98
+ }
99
+ /**
100
+ * Basic auth configuration
101
+ */
102
+ export interface BasicAuthConfig extends BaseAuthConfig {
103
+ type: 'basic';
104
+ username: string;
105
+ password: string;
106
+ }
107
+ /**
108
+ * TOTP configuration
109
+ */
110
+ export interface TOTPAuthConfig extends BaseAuthConfig {
111
+ type: 'totp';
112
+ secret: string;
113
+ digits?: number;
114
+ period?: number;
115
+ algorithm?: 'sha1' | 'sha256' | 'sha512';
116
+ }
117
+ /**
118
+ * UI Login configuration
119
+ */
120
+ export interface UILoginAuthConfig extends BaseAuthConfig {
121
+ type: 'ui_login';
122
+ url: string;
123
+ username?: string;
124
+ password?: string;
125
+ username_selector?: string;
126
+ password_selector?: string;
127
+ submit_selector?: string;
128
+ totp_secret?: string;
129
+ totp_selector?: string;
130
+ session_file?: string;
131
+ }
132
+ /**
133
+ * GCP ADC configuration
134
+ */
135
+ export interface GCPADCConfig extends BaseAuthConfig {
136
+ type: 'gcp_adc';
137
+ project_id?: string;
138
+ scopes?: string[];
139
+ }
140
+ /**
141
+ * AWS IAM configuration
142
+ */
143
+ export interface AWSIamConfig extends BaseAuthConfig {
144
+ type: 'aws_iam';
145
+ region?: string;
146
+ access_key_id?: string;
147
+ secret_access_key?: string;
148
+ session_token?: string;
149
+ role_arn?: string;
150
+ profile?: string;
151
+ }
152
+ /**
153
+ * Azure AD configuration
154
+ */
155
+ export interface AzureADConfig extends BaseAuthConfig {
156
+ type: 'azure_ad';
157
+ tenant_id: string;
158
+ client_id: string;
159
+ client_secret?: string;
160
+ scope?: string;
161
+ token_endpoint?: string;
162
+ }
163
+ /**
164
+ * No-auth configuration (for unauthenticated requests)
165
+ */
166
+ export interface NoneAuthConfig extends BaseAuthConfig {
167
+ type: 'none';
168
+ }
169
+ /**
170
+ * Union type for all auth configurations
171
+ */
172
+ export type AuthConfig = JWTAuthConfig | OAuth2AuthConfig | APIKeyAuthConfig | BearerAuthConfig | BasicAuthConfig | TOTPAuthConfig | UILoginAuthConfig | GCPADCConfig | AWSIamConfig | AzureADConfig | NoneAuthConfig;
173
+ /**
174
+ * Base interface for all auth providers
175
+ */
176
+ export interface AuthProvider<T extends AuthConfig = AuthConfig> {
177
+ /**
178
+ * Provider type identifier
179
+ */
180
+ readonly type: AuthType;
181
+ /**
182
+ * Authenticate and retrieve credentials
183
+ */
184
+ authenticate(config: T): Promise<AuthResult>;
185
+ /**
186
+ * Refresh credentials if applicable
187
+ */
188
+ refresh?(config: T): Promise<AuthResult>;
189
+ /**
190
+ * Clear cached credentials
191
+ */
192
+ clear?(config: T): Promise<void>;
193
+ /**
194
+ * Validate current credentials
195
+ */
196
+ validate?(config: T): Promise<boolean>;
197
+ }
198
+ /**
199
+ * Simple in-memory cache for auth credentials
200
+ */
201
+ export declare class AuthCache {
202
+ private cache;
203
+ private defaultTTL;
204
+ set(key: string, credentials: AuthCredentials, ttl?: number): void;
205
+ get(key: string): AuthCredentials | null;
206
+ clear(key?: string): void;
207
+ has(key: string): boolean;
208
+ }
209
+ /**
210
+ * Global auth cache instance
211
+ */
212
+ export declare const authCache: AuthCache;
213
+ /**
214
+ * Create cache key from config
215
+ */
216
+ export declare function createCacheKey(type: AuthType, identifier: string): string;
217
+ /**
218
+ * Check if credentials are expired
219
+ */
220
+ export declare function isExpired(expiresAt?: number): boolean;
221
+ /**
222
+ * Parse token from string (extracts token without "Bearer " prefix)
223
+ */
224
+ export declare function parseToken(token: string): string;
225
+ /**
226
+ * Encode basic auth header
227
+ */
228
+ export declare function encodeBasicAuth(username: string, password: string): string;
229
+ export { JWTProvider } from './jwt-provider.js';
230
+ export { OAuth2Provider } from './oauth2-provider.js';
231
+ export { APIKeyProvider } from './api-key-provider.js';
232
+ export { BearerProvider, BasicAuthProvider } from './basic-auth-provider.js';
233
+ export { TOTPProvider } from './totp-provider.js';
234
+ export { UILoginProvider } from './ui-login-provider.js';
235
+ export { GCPADCProvider } from './gcp-adc-provider.js';
236
+ export { AWSIamProvider } from './aws-iam-provider.js';
237
+ export { AzureADProvider } from './azure-ad-provider.js';
238
+ export { AuthManager, authManager, authenticate, createAuthHeaders, applyAuthToRequest, AuthError } from './manager.js';
@@ -0,0 +1,82 @@
1
+ /**
2
+ * QA360 Authentication Module
3
+ *
4
+ * Comprehensive authentication support for test adapters.
5
+ * Supports JWT, OAuth2, API Keys, Bearer tokens, Basic auth, TOTP,
6
+ * UI Login, and cloud provider credentials (GCP, AWS, Azure).
7
+ */
8
+ /**
9
+ * Simple in-memory cache for auth credentials
10
+ */
11
+ export class AuthCache {
12
+ cache = new Map();
13
+ defaultTTL = 3600; // 1 hour
14
+ set(key, credentials, ttl) {
15
+ const expiresAt = Date.now() + (ttl || this.defaultTTL) * 1000;
16
+ this.cache.set(key, { credentials, expiresAt });
17
+ }
18
+ get(key) {
19
+ const entry = this.cache.get(key);
20
+ if (!entry)
21
+ return null;
22
+ if (Date.now() > entry.expiresAt) {
23
+ this.cache.delete(key);
24
+ return null;
25
+ }
26
+ return entry.credentials;
27
+ }
28
+ clear(key) {
29
+ if (key) {
30
+ this.cache.delete(key);
31
+ }
32
+ else {
33
+ this.cache.clear();
34
+ }
35
+ }
36
+ has(key) {
37
+ return this.get(key) !== null;
38
+ }
39
+ }
40
+ /**
41
+ * Global auth cache instance
42
+ */
43
+ export const authCache = new AuthCache();
44
+ /**
45
+ * Create cache key from config
46
+ */
47
+ export function createCacheKey(type, identifier) {
48
+ return `${type}:${identifier}`;
49
+ }
50
+ /**
51
+ * Check if credentials are expired
52
+ */
53
+ export function isExpired(expiresAt) {
54
+ if (!expiresAt)
55
+ return false;
56
+ return Date.now() >= expiresAt;
57
+ }
58
+ /**
59
+ * Parse token from string (extracts token without "Bearer " prefix)
60
+ */
61
+ export function parseToken(token) {
62
+ return token.replace(/^Bearer\s+/i, '').trim();
63
+ }
64
+ /**
65
+ * Encode basic auth header
66
+ */
67
+ export function encodeBasicAuth(username, password) {
68
+ const encoded = Buffer.from(`${username}:${password}`).toString('base64');
69
+ return `Basic ${encoded}`;
70
+ }
71
+ // Re-export all providers
72
+ export { JWTProvider } from './jwt-provider.js';
73
+ export { OAuth2Provider } from './oauth2-provider.js';
74
+ export { APIKeyProvider } from './api-key-provider.js';
75
+ export { BearerProvider, BasicAuthProvider } from './basic-auth-provider.js';
76
+ export { TOTPProvider } from './totp-provider.js';
77
+ export { UILoginProvider } from './ui-login-provider.js';
78
+ export { GCPADCProvider } from './gcp-adc-provider.js';
79
+ export { AWSIamProvider } from './aws-iam-provider.js';
80
+ export { AzureADProvider } from './azure-ad-provider.js';
81
+ // Re-export manager and factory functions
82
+ export { AuthManager, authManager, authenticate, createAuthHeaders, applyAuthToRequest, AuthError } from './manager.js';
@@ -0,0 +1,19 @@
1
+ /**
2
+ * JWT Authentication Provider
3
+ *
4
+ * Handles JWT bearer token authentication with support for:
5
+ * - Pre-existing tokens
6
+ * - OAuth2 token endpoint (client credentials flow)
7
+ * - Automatic token refresh
8
+ */
9
+ import { AuthProvider, AuthResult, JWTAuthConfig } from './index.js';
10
+ export declare class JWTProvider implements AuthProvider<JWTAuthConfig> {
11
+ readonly type: "jwt";
12
+ authenticate(config: JWTAuthConfig): Promise<AuthResult>;
13
+ refresh(config: JWTAuthConfig): Promise<AuthResult>;
14
+ clear(config: JWTAuthConfig): Promise<void>;
15
+ validate(config: JWTAuthConfig): Promise<boolean>;
16
+ private fetchToken;
17
+ private createTokenResult;
18
+ private getCacheKey;
19
+ }
@@ -0,0 +1,160 @@
1
+ /**
2
+ * JWT Authentication Provider
3
+ *
4
+ * Handles JWT bearer token authentication with support for:
5
+ * - Pre-existing tokens
6
+ * - OAuth2 token endpoint (client credentials flow)
7
+ * - Automatic token refresh
8
+ */
9
+ import { authCache, createCacheKey, parseToken } from './index.js';
10
+ export class JWTProvider {
11
+ type = 'jwt';
12
+ async authenticate(config) {
13
+ // If we have a pre-existing token, use it directly
14
+ if (config.token) {
15
+ return this.createTokenResult(config.token, config);
16
+ }
17
+ // If token_endpoint is provided, fetch token via OAuth2
18
+ if (config.token_endpoint) {
19
+ return await this.fetchToken(config);
20
+ }
21
+ return {
22
+ success: false,
23
+ error: 'JWT token or token_endpoint must be provided'
24
+ };
25
+ }
26
+ async refresh(config) {
27
+ if (!config.token_endpoint) {
28
+ return {
29
+ success: false,
30
+ error: 'Cannot refresh: no token_endpoint configured'
31
+ };
32
+ }
33
+ return await this.fetchToken(config);
34
+ }
35
+ clear(config) {
36
+ const key = this.getCacheKey(config);
37
+ authCache.clear(key);
38
+ return Promise.resolve();
39
+ }
40
+ async validate(config) {
41
+ if (!config.token)
42
+ return false;
43
+ try {
44
+ const parts = config.token.split('.');
45
+ if (parts.length !== 3)
46
+ return false;
47
+ // Decode payload (no verification, just check structure)
48
+ const payload = JSON.parse(Buffer.from(parts[1], 'base64').toString());
49
+ // Check expiration if present
50
+ if (payload.exp) {
51
+ const now = Math.floor(Date.now() / 1000);
52
+ return payload.exp > now;
53
+ }
54
+ return true;
55
+ }
56
+ catch {
57
+ return false;
58
+ }
59
+ }
60
+ async fetchToken(config) {
61
+ const cacheKey = this.getCacheKey(config);
62
+ // Check cache first
63
+ if (config.cache?.enabled !== false) {
64
+ const cached = authCache.get(cacheKey);
65
+ if (cached) {
66
+ return { success: true, credentials: cached };
67
+ }
68
+ }
69
+ // Prepare token request
70
+ const body = {
71
+ grant_type: 'client_credentials',
72
+ client_id: config.client_id,
73
+ client_secret: config.client_secret,
74
+ };
75
+ if (config.scopes && config.scopes.length > 0) {
76
+ body.scope = config.scopes.join(' ');
77
+ }
78
+ if (config.audience)
79
+ body.audience = config.audience;
80
+ try {
81
+ const endpoint = config.token_endpoint;
82
+ if (!endpoint) {
83
+ return { success: false, error: 'Token endpoint is required' };
84
+ }
85
+ const response = await fetch(endpoint, {
86
+ method: 'POST',
87
+ headers: {
88
+ 'Content-Type': 'application/json',
89
+ },
90
+ body: JSON.stringify(body),
91
+ });
92
+ if (!response.ok) {
93
+ const error = await response.text().catch(() => 'Unknown error');
94
+ return {
95
+ success: false,
96
+ error: `Token request failed: ${response.status} ${error}`
97
+ };
98
+ }
99
+ const data = await response.json();
100
+ const token = data.access_token;
101
+ if (!token) {
102
+ return {
103
+ success: false,
104
+ error: 'No access_token in response'
105
+ };
106
+ }
107
+ return this.createTokenResult(token, config, data.expires_in);
108
+ }
109
+ catch (error) {
110
+ return {
111
+ success: false,
112
+ error: `Token fetch failed: ${error.message}`
113
+ };
114
+ }
115
+ }
116
+ createTokenResult(token, config, expiresIn) {
117
+ const cleanToken = parseToken(token);
118
+ const credentials = {
119
+ type: 'jwt',
120
+ headers: {
121
+ 'Authorization': `Bearer ${cleanToken}`
122
+ }
123
+ };
124
+ // Calculate expiration
125
+ let expiresAt;
126
+ if (expiresIn) {
127
+ expiresAt = Date.now() + expiresIn * 1000;
128
+ }
129
+ else {
130
+ // Try to decode JWT and get exp claim
131
+ try {
132
+ const parts = cleanToken.split('.');
133
+ if (parts.length === 3) {
134
+ const payload = JSON.parse(Buffer.from(parts[1], 'base64').toString());
135
+ if (payload.exp) {
136
+ expiresAt = payload.exp * 1000;
137
+ }
138
+ }
139
+ }
140
+ catch {
141
+ // Ignore decode errors
142
+ }
143
+ }
144
+ // Cache if enabled
145
+ if (config.cache?.enabled !== false && expiresAt) {
146
+ const ttl = config.cache?.ttl || Math.floor((expiresAt - Date.now()) / 1000);
147
+ const key = this.getCacheKey(config);
148
+ authCache.set(key, credentials, ttl);
149
+ }
150
+ return {
151
+ success: true,
152
+ credentials,
153
+ expiresAt
154
+ };
155
+ }
156
+ getCacheKey(config) {
157
+ const identifier = config.client_id || config.issuer || 'default';
158
+ return createCacheKey('jwt', identifier);
159
+ }
160
+ }
@@ -0,0 +1,84 @@
1
+ /**
2
+ * Authentication Manager and Factory
3
+ *
4
+ * Central manager for handling multiple authentication profiles
5
+ * and creating auth providers based on configuration.
6
+ */
7
+ import { AuthProvider, AuthConfig, AuthResult, AuthCredentials } from './index.js';
8
+ /**
9
+ * Authentication error
10
+ */
11
+ export declare class AuthError extends Error {
12
+ code: string;
13
+ provider?: string;
14
+ constructor(message: string, provider?: string);
15
+ }
16
+ /**
17
+ * Authentication Manager
18
+ *
19
+ * Manages multiple authentication profiles and provides
20
+ * a unified interface for authentication operations.
21
+ */
22
+ export declare class AuthManager {
23
+ private profiles;
24
+ /**
25
+ * Register an authentication profile
26
+ */
27
+ registerProfile(name: string, config: AuthConfig): void;
28
+ /**
29
+ * Unregister an authentication profile
30
+ */
31
+ unregisterProfile(name: string): void;
32
+ /**
33
+ * Get a registered profile
34
+ */
35
+ getProfile(name: string): AuthConfig | undefined;
36
+ /**
37
+ * List all registered profiles
38
+ */
39
+ listProfiles(): string[];
40
+ /**
41
+ * Authenticate using a named profile
42
+ */
43
+ authenticate(name: string): Promise<AuthResult>;
44
+ /**
45
+ * Authenticate using a configuration directly
46
+ */
47
+ authenticateWithConfig(config: AuthConfig): Promise<AuthResult>;
48
+ /**
49
+ * Create an auth provider based on configuration type
50
+ */
51
+ createProvider(config: AuthConfig): AuthProvider;
52
+ /**
53
+ * Refresh authentication for a named profile
54
+ */
55
+ refresh(name: string): Promise<AuthResult>;
56
+ /**
57
+ * Clear authentication for a named profile
58
+ */
59
+ clear(name: string): Promise<void>;
60
+ /**
61
+ * Validate authentication for a named profile
62
+ */
63
+ validate(name: string): Promise<boolean>;
64
+ /**
65
+ * Clear all cached credentials
66
+ */
67
+ clearAll(): void;
68
+ }
69
+ /**
70
+ * Global auth manager instance
71
+ */
72
+ export declare const authManager: AuthManager;
73
+ /**
74
+ * Helper function to authenticate with a single config
75
+ */
76
+ export declare function authenticate(config: AuthConfig): Promise<AuthResult>;
77
+ /**
78
+ * Helper function to create auth credentials for use in requests
79
+ */
80
+ export declare function createAuthHeaders(credentials: AuthCredentials): Record<string, string>;
81
+ /**
82
+ * Apply auth credentials to a fetch request init
83
+ */
84
+ export declare function applyAuthToRequest(credentials: AuthCredentials, init?: RequestInit): RequestInit;