diff --git a/.changeset/early-lights-draw.md b/.changeset/early-lights-draw.md new file mode 100644 index 00000000..2c90d239 --- /dev/null +++ b/.changeset/early-lights-draw.md @@ -0,0 +1,5 @@ +--- +"claude-dev": patch +--- + +Use different keys for separate auth providers diff --git a/src/core/storage/state-keys.ts b/src/core/storage/state-keys.ts index 5f9e8a20..51b9879a 100644 --- a/src/core/storage/state-keys.ts +++ b/src/core/storage/state-keys.ts @@ -176,6 +176,7 @@ export interface Settings { export interface Secrets { apiKey: string | undefined clineAccountId: string | undefined + ["cline:clineAccountId"]: string | undefined // Auth_Provider:AccountId openRouterApiKey: string | undefined awsAccessKey: string | undefined awsSecretKey: string | undefined diff --git a/src/core/storage/utils/state-helpers.ts b/src/core/storage/utils/state-helpers.ts index 7b515ef9..f97d60bb 100644 --- a/src/core/storage/utils/state-helpers.ts +++ b/src/core/storage/utils/state-helpers.ts @@ -1,6 +1,7 @@ import { ANTHROPIC_MIN_THINKING_BUDGET, ApiProvider, fireworksDefaultModelId, type OcaModelInfo } from "@shared/api" import { ExtensionContext } from "vscode" import { Controller } from "@/core/controller" +import { ClineAuthProvider } from "@/services/auth/providers/ClineAuthProvider" import { DEFAULT_AUTO_APPROVAL_SETTINGS } from "@/shared/AutoApprovalSettings" import { DEFAULT_BROWSER_SETTINGS } from "@/shared/BrowserSettings" import { ClineRulesToggles } from "@/shared/cline-rules" @@ -14,6 +15,7 @@ export async function readSecretsFromDisk(context: ExtensionContext): Promise, context.secrets.get("openRouterApiKey") as Promise, context.secrets.get("clineAccountId") as Promise, + context.secrets.get(ClineAuthProvider.secretKeyId) as Promise, context.secrets.get("awsAccessKey") as Promise, context.secrets.get("awsSecretKey") as Promise, context.secrets.get("awsSessionToken") as Promise, @@ -93,7 +96,8 @@ export async function readSecretsFromDisk(context: ExtensionContext): Promise { - if (event.key === "clineAccountId") { + if (event.key === "clineAccountId" || event.key === ClineAuthProvider.secretKeyId) { // Check if the secret was removed (logout) or added/updated (login) - const secretValue = await context.secrets.get("clineAccountId") + const secretValue = await context.secrets.get(event.key) const activeWebview = WebviewProvider.getVisibleInstance() const controller = activeWebview?.controller diff --git a/src/services/auth/AuthService.ts b/src/services/auth/AuthService.ts index a5ee8de0..83b0d29b 100644 --- a/src/services/auth/AuthService.ts +++ b/src/services/auth/AuthService.ts @@ -32,6 +32,7 @@ export interface ClineAuthInfo { */ expiresAt?: number userInfo: ClineAccountUserInfo + provider: string } export interface ClineAccountUserInfo { @@ -63,6 +64,7 @@ export class AuthService { protected _authenticated: boolean = false protected _clineAuthInfo: ClineAuthInfo | null = null protected _provider: IAuthProvider | null = null + protected _fallbackProvider: IAuthProvider | null = null protected _activeAuthStatusUpdateHandlers = new Set>() protected _handlerToController = new Map, Controller>() protected _controller: Controller @@ -108,35 +110,41 @@ export class AuthService { this._controller = controller } - get authProvider(): IAuthProvider | null { - return this._provider - } - - set authProvider(providerName: string) { - this._setProvider(providerName) - } - /** * Returns the current authentication token with the appropriate prefix. * Refreshing it if necessary. */ async getAuthToken(): Promise { + if (!this._provider) { + throw new Error("Auth provider is not set") + } + + const token = await this.internalGetAuthToken(this._provider) + + if (!token && this._fallbackProvider) { + return this.internalGetAuthToken(this._fallbackProvider) + } + + return token + } + + private async internalGetAuthToken(provider: IAuthProvider): Promise { try { let clineAccountAuthToken = this._clineAuthInfo?.idToken - if (!this._clineAuthInfo || !clineAccountAuthToken) { + if (!this._clineAuthInfo || !clineAccountAuthToken || this._clineAuthInfo.provider !== provider.name) { // Not authenticated return null } // Check if token has expired - if (await this._provider?.shouldRefreshIdToken(clineAccountAuthToken, this._clineAuthInfo.expiresAt)) { + if (await provider.shouldRefreshIdToken(clineAccountAuthToken, this._clineAuthInfo.expiresAt)) { console.log("Provider indicates token needs refresh") - const updatedAuthInfo = await this._provider?.retrieveClineAuthInfo(this._controller) + const updatedAuthInfo = await provider.retrieveClineAuthInfo(this._controller) if (updatedAuthInfo) { this._clineAuthInfo = updatedAuthInfo this._authenticated = true clineAccountAuthToken = updatedAuthInfo.idToken - } else { + } else if (this.shouldClearAuthInfo(provider)) { this._clineAuthInfo = null this._authenticated = false telemetryService.captureAuthLoggedOut(this._provider?.name, LogoutReason.ERROR_RECOVERY) @@ -145,7 +153,7 @@ export class AuthService { } // IMPORTANT: Prefix with 'workos:' so backend can route verification to WorkOS provider - const prefix = this._provider?.name === "cline" ? "workos:" : "" + const prefix = provider.name === "cline" ? "workos:" : "" return clineAccountAuthToken ? `${prefix}${clineAccountAuthToken}` : null } catch (error) { console.error("Error getting auth token:", error) @@ -153,12 +161,17 @@ export class AuthService { } } + private shouldClearAuthInfo(provider: IAuthProvider) { + return this._clineAuthInfo?.provider === provider.name + } + protected _setProvider(providerName: string): void { // Only ClineAuthProvider is supported going forward - // Keeping the providerName param for forward compatibility/telemetrye + // Keeping the providerName param for forward compatibility/telemetry switch (providerName) { case "cline": this._provider = new ClineAuthProvider(clineEnvConfig) + this._fallbackProvider = new FirebaseAuthProvider(clineEnvConfig) break case "firebase": default: @@ -254,6 +267,7 @@ export class AuthService { */ async clearAuthToken(): Promise { this._controller.stateManager.setSecret("clineAccountId", undefined) + this._controller.stateManager.setSecret(ClineAuthProvider.secretKeyId, undefined) } /** @@ -266,7 +280,7 @@ export class AuthService { } try { - this._clineAuthInfo = await this._provider.retrieveClineAuthInfo(this._controller) + this._clineAuthInfo = await this.retrieveAuthInfo() if (this._clineAuthInfo) { this._authenticated = true await this.sendAuthStatusUpdate() @@ -285,6 +299,20 @@ export class AuthService { } } + private async retrieveAuthInfo(): Promise { + if (!this._provider) { + throw new Error("Auth provider is not set") + } + + const authInfo = await this._provider.retrieveClineAuthInfo(this._controller) + + if (!authInfo && this._fallbackProvider) { + return this._fallbackProvider.retrieveClineAuthInfo(this._controller) + } + + return authInfo + } + /** * Subscribe to authStatusUpdate events * @param controller The controller instance diff --git a/src/services/auth/AuthServiceMock.ts b/src/services/auth/AuthServiceMock.ts index 585ede2f..27b18008 100644 --- a/src/services/auth/AuthServiceMock.ts +++ b/src/services/auth/AuthServiceMock.ts @@ -97,6 +97,7 @@ export class AuthServiceMock extends AuthService { appBaseUrl: clineEnvConfig.appBaseUrl, subject: authData.userInfo.subject, }, + provider: this._provider?.name || "mock", } console.log(`Successfully authenticated with mock server as ${authData.userInfo.name} (${authData.userInfo.email})`) diff --git a/src/services/auth/providers/ClineAuthProvider.ts b/src/services/auth/providers/ClineAuthProvider.ts index c80bc7fd..3ee29b51 100644 --- a/src/services/auth/providers/ClineAuthProvider.ts +++ b/src/services/auth/providers/ClineAuthProvider.ts @@ -95,7 +95,7 @@ export class ClineAuthProvider implements IAuthProvider { async retrieveClineAuthInfo(controller: Controller): Promise { try { // Get the stored auth data from secure storage - const storedAuthDataString = controller.stateManager.getSecretKey("clineAccountId") + const storedAuthDataString = controller.stateManager.getSecretKey(ClineAuthProvider.secretKeyId) if (!storedAuthDataString) { Logger.debug("No stored authentication data found") @@ -108,13 +108,13 @@ export class ClineAuthProvider implements IAuthProvider { storedAuthData = JSON.parse(storedAuthDataString) } catch (e) { console.error("Failed to parse stored auth data:", e) - controller.stateManager.setSecret("clineAccountId", undefined) + controller.stateManager.setSecret(ClineAuthProvider.secretKeyId, undefined) return null } if (!storedAuthData.refreshToken || !storedAuthData?.idToken) { console.error("No valid token found in stored authentication data") - controller.stateManager.setSecret("clineAccountId", undefined) + controller.stateManager.setSecret(ClineAuthProvider.secretKeyId, undefined) return null } @@ -198,6 +198,7 @@ export class ClineAuthProvider implements IAuthProvider { appBaseUrl: this._config.appBaseUrl, subject: data.data.userInfo.subject || "", }, + provider: this.name, } } catch (error: any) { throw error @@ -300,9 +301,10 @@ export class ClineAuthProvider implements IAuthProvider { organizations: [], }, expiresAt: new Date(tokenData.expiresAt).getTime() / 1000, // "2025-09-17T04:32:24.842636548Z" + provider: this.name, } - controller.stateManager.setSecret("clineAccountId", JSON.stringify(clineAuthInfo)) + controller.stateManager.setSecret(ClineAuthProvider.secretKeyId, JSON.stringify(clineAuthInfo)) return clineAuthInfo } catch (error) { @@ -310,4 +312,6 @@ export class ClineAuthProvider implements IAuthProvider { throw error } } + + static secretKeyId = "cline:clineAccountId" as const // authProvider:clineAccountId } diff --git a/src/services/auth/providers/FirebaseAuthProvider.ts b/src/services/auth/providers/FirebaseAuthProvider.ts index 3a9da3ed..297a39db 100644 --- a/src/services/auth/providers/FirebaseAuthProvider.ts +++ b/src/services/auth/providers/FirebaseAuthProvider.ts @@ -70,7 +70,7 @@ export class FirebaseAuthProvider implements IAuthProvider { // Store user data const userInfo: ClineAccountUserInfo = userResponse.data.data - return { idToken, userInfo } + return { idToken, userInfo, provider: this.name } } catch (error) { ErrorService.get().logException(error) throw error diff --git a/webview-ui/src/components/chat/Announcement.tsx b/webview-ui/src/components/chat/Announcement.tsx index 5d1dca3f..ec19e52d 100644 --- a/webview-ui/src/components/chat/Announcement.tsx +++ b/webview-ui/src/components/chat/Announcement.tsx @@ -44,8 +44,8 @@ Patch releases (3.19.1 → 3.19.2) will not trigger new announcements. const Announcement = ({ version, hideAnnouncement }: AnnouncementProps) => { const minorVersion = version.split(".").slice(0, 2).join(".") // 2.0.0 -> 2.0 const { clineUser } = useClineAuth() - const { apiConfiguration, openRouterModels, setShowChatModelSelector, refreshOpenRouterModels } = useExtensionState() - const user = apiConfiguration?.clineAccountId ? clineUser : undefined + const { openRouterModels, setShowChatModelSelector, refreshOpenRouterModels } = useExtensionState() + const user = clineUser || undefined const { handleFieldsChange } = useApiConfigurationHandlers() const [didClickGrokCodeButton, setDidClickGrokCodeButton] = useState(false) diff --git a/webview-ui/src/components/common/NewModelBanner.tsx b/webview-ui/src/components/common/NewModelBanner.tsx index 82ce4380..cf913a86 100644 --- a/webview-ui/src/components/common/NewModelBanner.tsx +++ b/webview-ui/src/components/common/NewModelBanner.tsx @@ -13,8 +13,8 @@ export const CURRENT_MODEL_BANNER_VERSION = 1 export const NewModelBanner: React.FC = () => { const { clineUser } = useClineAuth() - const { apiConfiguration, openRouterModels, setShowChatModelSelector, refreshOpenRouterModels } = useExtensionState() - const user = apiConfiguration?.clineAccountId ? clineUser : undefined + const { openRouterModels, setShowChatModelSelector, refreshOpenRouterModels } = useExtensionState() + const user = clineUser || undefined const { handleFieldsChange } = useApiConfigurationHandlers() // Need to get latest model list in case user hits shortcut button to set model diff --git a/webview-ui/src/components/settings/ClineAccountInfoCard.tsx b/webview-ui/src/components/settings/ClineAccountInfoCard.tsx index 324fae78..25ee81dd 100644 --- a/webview-ui/src/components/settings/ClineAccountInfoCard.tsx +++ b/webview-ui/src/components/settings/ClineAccountInfoCard.tsx @@ -6,9 +6,9 @@ import { AccountServiceClient } from "@/services/grpc-client" export const ClineAccountInfoCard = () => { const { clineUser } = useClineAuth() - const { apiConfiguration, navigateToAccount } = useExtensionState() + const { navigateToAccount } = useExtensionState() - const user = apiConfiguration?.clineAccountId ? clineUser : undefined + const user = clineUser || undefined const handleLogin = () => { AccountServiceClient.accountLoginClicked(EmptyRequest.create()).catch((err) => diff --git a/webview-ui/src/utils/validate.ts b/webview-ui/src/utils/validate.ts index e33d690f..26d7109a 100644 --- a/webview-ui/src/utils/validate.ts +++ b/webview-ui/src/utils/validate.ts @@ -71,9 +71,6 @@ export function validateApiConfiguration(currentMode: Mode, apiConfiguration?: A } break case "cline": - if (!apiConfiguration.clineAccountId) { - return "You must provide a valid API key or choose a different provider." - } break case "openai": if (!apiConfiguration.openAiBaseUrl || !apiConfiguration.openAiApiKey || !openAiModelId) {