|
| 1 | +import { type AxiosError, isAxiosError } from "axios"; |
| 2 | + |
| 3 | +import { toSafeHost } from "../util"; |
| 4 | + |
| 5 | +import type * as vscode from "vscode"; |
| 6 | + |
| 7 | +import type { SecretsManager } from "../core/secretsManager"; |
| 8 | +import type { Logger } from "../logging/logger"; |
| 9 | +import type { RequestConfigWithMeta } from "../logging/types"; |
| 10 | +import type { OAuthSessionManager } from "../oauth/sessionManager"; |
| 11 | + |
| 12 | +import type { CoderApi } from "./coderApi"; |
| 13 | + |
| 14 | +const coderSessionTokenHeader = "Coder-Session-Token"; |
| 15 | + |
| 16 | +/** |
| 17 | + * Callback invoked when authentication is required. |
| 18 | + * Returns true if user successfully re-authenticated. |
| 19 | + */ |
| 20 | +export type AuthRequiredHandler = (hostname: string) => Promise<boolean>; |
| 21 | + |
| 22 | +/** |
| 23 | + * Intercepts 401 responses and handles re-authentication. |
| 24 | + * |
| 25 | + * Always attached to the axios instance. Handles both OAuth (automatic refresh) |
| 26 | + * and non-OAuth (interactive re-auth via callback) authentication failures. |
| 27 | + */ |
| 28 | +export class AuthInterceptor implements vscode.Disposable { |
| 29 | + private readonly interceptorId: number; |
| 30 | + |
| 31 | + constructor( |
| 32 | + private readonly client: CoderApi, |
| 33 | + private readonly logger: Logger, |
| 34 | + private readonly oauthSessionManager: OAuthSessionManager, |
| 35 | + private readonly secretsManager: SecretsManager, |
| 36 | + private readonly onAuthRequired?: AuthRequiredHandler, |
| 37 | + ) { |
| 38 | + this.interceptorId = this.client |
| 39 | + .getAxiosInstance() |
| 40 | + .interceptors.response.use( |
| 41 | + (r) => r, |
| 42 | + (error: unknown) => this.handleError(error), |
| 43 | + ); |
| 44 | + this.logger.debug("Auth interceptor attached"); |
| 45 | + } |
| 46 | + |
| 47 | + private async handleError(error: unknown): Promise<unknown> { |
| 48 | + if (!isAxiosError(error)) { |
| 49 | + throw error; |
| 50 | + } |
| 51 | + |
| 52 | + if (error.config) { |
| 53 | + const config = error.config as { _retryAttempted?: boolean }; |
| 54 | + if (config._retryAttempted) { |
| 55 | + throw error; |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + if (error.response?.status !== 401) { |
| 60 | + throw error; |
| 61 | + } |
| 62 | + |
| 63 | + const baseUrl = this.client.getHost(); |
| 64 | + if (!baseUrl) { |
| 65 | + throw error; |
| 66 | + } |
| 67 | + const hostname = toSafeHost(baseUrl); |
| 68 | + |
| 69 | + return this.handle401Error(error, hostname); |
| 70 | + } |
| 71 | + |
| 72 | + private async handle401Error( |
| 73 | + error: AxiosError, |
| 74 | + hostname: string, |
| 75 | + ): Promise<unknown> { |
| 76 | + this.logger.debug("Received 401 response, attempting recovery"); |
| 77 | + |
| 78 | + if (await this.oauthSessionManager.isLoggedInWithOAuth(hostname)) { |
| 79 | + try { |
| 80 | + const newTokens = await this.oauthSessionManager.refreshToken(); |
| 81 | + this.client.setSessionToken(newTokens.access_token); |
| 82 | + this.logger.debug("Token refresh successful, retrying request"); |
| 83 | + return this.retryRequest(error, newTokens.access_token); |
| 84 | + } catch (refreshError) { |
| 85 | + this.logger.error("OAuth refresh failed:", refreshError); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + if (this.onAuthRequired) { |
| 90 | + this.logger.debug("Triggering interactive re-authentication"); |
| 91 | + const success = await this.onAuthRequired(hostname); |
| 92 | + if (success) { |
| 93 | + const auth = await this.secretsManager.getSessionAuth(hostname); |
| 94 | + if (auth) { |
| 95 | + this.logger.debug("Re-authentication successful, retrying request"); |
| 96 | + return this.retryRequest(error, auth.token); |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + throw error; |
| 102 | + } |
| 103 | + |
| 104 | + private retryRequest(error: AxiosError, token: string): Promise<unknown> { |
| 105 | + if (!error.config) { |
| 106 | + throw error; |
| 107 | + } |
| 108 | + |
| 109 | + const config = error.config as RequestConfigWithMeta & { |
| 110 | + _retryAttempted?: boolean; |
| 111 | + }; |
| 112 | + config._retryAttempted = true; |
| 113 | + config.headers[coderSessionTokenHeader] = token; |
| 114 | + return this.client.getAxiosInstance().request(config); |
| 115 | + } |
| 116 | + |
| 117 | + public dispose(): void { |
| 118 | + this.client |
| 119 | + .getAxiosInstance() |
| 120 | + .interceptors.response.eject(this.interceptorId); |
| 121 | + this.logger.debug("Auth interceptor detached"); |
| 122 | + } |
| 123 | +} |
0 commit comments