|
3 | 3 | type AxiosInstance, |
4 | 4 | type AxiosHeaders, |
5 | 5 | type AxiosResponseTransformer, |
6 | | - isAxiosError, |
7 | | - type AxiosError, |
8 | 6 | } from "axios"; |
9 | 7 | import { Api } from "coder/site/src/api/api"; |
10 | 8 | import { |
@@ -33,8 +31,6 @@ import { |
33 | 31 | HttpClientLogLevel, |
34 | 32 | } from "../logging/types"; |
35 | 33 | import { sizeOf } from "../logging/utils"; |
36 | | -import { parseOAuthError, requiresReAuthentication } from "../oauth/errors"; |
37 | | -import { type OAuthSessionManager } from "../oauth/sessionManager"; |
38 | 34 | import { HttpStatusCode } from "../websocket/codes"; |
39 | 35 | import { |
40 | 36 | type UnidirectionalStream, |
@@ -76,15 +72,14 @@ export class CoderApi extends Api { |
76 | 72 | baseUrl: string, |
77 | 73 | token: string | undefined, |
78 | 74 | output: Logger, |
79 | | - oauthSessionManager?: OAuthSessionManager, |
80 | 75 | ): CoderApi { |
81 | 76 | const client = new CoderApi(output); |
82 | 77 | client.setHost(baseUrl); |
83 | 78 | if (token) { |
84 | 79 | client.setSessionToken(token); |
85 | 80 | } |
86 | 81 |
|
87 | | - setupInterceptors(client, output, oauthSessionManager); |
| 82 | + setupInterceptors(client, output); |
88 | 83 | return client; |
89 | 84 | } |
90 | 85 |
|
@@ -395,11 +390,7 @@ export class CoderApi extends Api { |
395 | 390 | /** |
396 | 391 | * Set up logging and request interceptors for the CoderApi instance. |
397 | 392 | */ |
398 | | -function setupInterceptors( |
399 | | - client: CoderApi, |
400 | | - output: Logger, |
401 | | - oauthSessionManager?: OAuthSessionManager, |
402 | | -): void { |
| 393 | +function setupInterceptors(client: CoderApi, output: Logger): void { |
403 | 394 | addLoggingInterceptors(client.getAxiosInstance(), output); |
404 | 395 |
|
405 | 396 | client.getAxiosInstance().interceptors.request.use(async (config) => { |
@@ -437,11 +428,6 @@ function setupInterceptors( |
437 | 428 | } |
438 | 429 | }, |
439 | 430 | ); |
440 | | - |
441 | | - // OAuth token refresh interceptors |
442 | | - if (oauthSessionManager) { |
443 | | - addOAuthInterceptors(client, output, oauthSessionManager); |
444 | | - } |
445 | 431 | } |
446 | 432 |
|
447 | 433 | function addLoggingInterceptors(client: AxiosInstance, logger: Logger) { |
@@ -487,116 +473,6 @@ function addLoggingInterceptors(client: AxiosInstance, logger: Logger) { |
487 | 473 | ); |
488 | 474 | } |
489 | 475 |
|
490 | | -/** |
491 | | - * Add OAuth token refresh interceptors. |
492 | | - * Success interceptor: proactively refreshes token when approaching expiry. |
493 | | - * Error interceptor: reactively refreshes token on 401 responses. |
494 | | - */ |
495 | | -function addOAuthInterceptors( |
496 | | - client: CoderApi, |
497 | | - logger: Logger, |
498 | | - oauthSessionManager: OAuthSessionManager, |
499 | | -) { |
500 | | - client.getAxiosInstance().interceptors.response.use( |
501 | | - // Success response interceptor: proactive token refresh |
502 | | - (response) => { |
503 | | - if (oauthSessionManager.shouldRefreshToken()) { |
504 | | - logger.debug( |
505 | | - "Token approaching expiry, triggering proactive refresh in background", |
506 | | - ); |
507 | | - |
508 | | - // Fire-and-forget: don't await, don't block response |
509 | | - oauthSessionManager.refreshToken().catch((error) => { |
510 | | - logger.warn("Background token refresh failed:", error); |
511 | | - }); |
512 | | - } |
513 | | - |
514 | | - return response; |
515 | | - }, |
516 | | - // Error response interceptor: reactive token refresh on 401 |
517 | | - async (error: unknown) => { |
518 | | - if (!isAxiosError(error)) { |
519 | | - throw error; |
520 | | - } |
521 | | - |
522 | | - if (error.config) { |
523 | | - const config = error.config as { |
524 | | - _oauthRetryAttempted?: boolean; |
525 | | - }; |
526 | | - if (config._oauthRetryAttempted) { |
527 | | - throw error; |
528 | | - } |
529 | | - } |
530 | | - |
531 | | - const status = error.response?.status; |
532 | | - |
533 | | - // These could indicate permanent auth failures that won't be fixed by token refresh |
534 | | - if (status === 400 || status === 403) { |
535 | | - handlePossibleOAuthError(error, logger, oauthSessionManager); |
536 | | - throw error; |
537 | | - } else if (status === 401) { |
538 | | - return handle401Error(error, client, logger, oauthSessionManager); |
539 | | - } |
540 | | - |
541 | | - throw error; |
542 | | - }, |
543 | | - ); |
544 | | -} |
545 | | - |
546 | | -function handlePossibleOAuthError( |
547 | | - error: unknown, |
548 | | - logger: Logger, |
549 | | - oauthSessionManager: OAuthSessionManager, |
550 | | -): void { |
551 | | - const oauthError = parseOAuthError(error); |
552 | | - if (oauthError && requiresReAuthentication(oauthError)) { |
553 | | - logger.error( |
554 | | - `OAuth error requires re-authentication: ${oauthError.errorCode}`, |
555 | | - ); |
556 | | - |
557 | | - oauthSessionManager.showReAuthenticationModal(oauthError).catch((err) => { |
558 | | - logger.error("Failed to show re-auth modal:", err); |
559 | | - }); |
560 | | - } |
561 | | -} |
562 | | - |
563 | | -async function handle401Error( |
564 | | - error: AxiosError, |
565 | | - client: CoderApi, |
566 | | - logger: Logger, |
567 | | - oauthSessionManager: OAuthSessionManager, |
568 | | -): Promise<void> { |
569 | | - if (!oauthSessionManager.isLoggedInWithOAuth()) { |
570 | | - throw error; |
571 | | - } |
572 | | - |
573 | | - logger.info("Received 401 response, attempting token refresh"); |
574 | | - |
575 | | - try { |
576 | | - const newTokens = await oauthSessionManager.refreshToken(); |
577 | | - client.setSessionToken(newTokens.access_token); |
578 | | - |
579 | | - logger.info("Token refresh successful, retrying request"); |
580 | | - |
581 | | - // Retry the original request with the new token |
582 | | - if (error.config) { |
583 | | - const config = error.config as RequestConfigWithMeta & { |
584 | | - _oauthRetryAttempted?: boolean; |
585 | | - }; |
586 | | - config._oauthRetryAttempted = true; |
587 | | - config.headers[coderSessionTokenHeader] = newTokens.access_token; |
588 | | - return client.getAxiosInstance().request(config); |
589 | | - } |
590 | | - |
591 | | - throw error; |
592 | | - } catch (refreshError) { |
593 | | - logger.error("Token refresh failed:", refreshError); |
594 | | - |
595 | | - handlePossibleOAuthError(refreshError, logger, oauthSessionManager); |
596 | | - throw error; |
597 | | - } |
598 | | -} |
599 | | - |
600 | 476 | function wrapRequestTransform( |
601 | 477 | transformer: AxiosResponseTransformer | AxiosResponseTransformer[], |
602 | 478 | config: RequestConfigWithMeta, |
|
0 commit comments