|
| 1 | +/*--------------------------------------------------------------------------------------------- |
| 2 | + * Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | + * Licensed under the MIT License. See License.txt in the project root for license information. |
| 4 | + *--------------------------------------------------------------------------------------------*/ |
| 5 | + |
| 6 | +import { IStorageService, StorageScope, StorageTarget } from '../../../../platform/storage/common/storage.js'; |
| 7 | +import { InstantiationType, registerSingleton } from '../../../../platform/instantiation/common/extensions.js'; |
| 8 | +import { IDynamicAuthenticationProviderStorageService, DynamicAuthenticationProviderInfo, DynamicAuthenticationProviderTokensChangeEvent } from '../common/dynamicAuthenticationProviderStorage.js'; |
| 9 | +import { ISecretStorageService } from '../../../../platform/secrets/common/secrets.js'; |
| 10 | +import { IAuthorizationTokenResponse, isAuthorizationTokenResponse } from '../../../../base/common/oauth.js'; |
| 11 | +import { ILogService } from '../../../../platform/log/common/log.js'; |
| 12 | +import { Emitter, Event } from '../../../../base/common/event.js'; |
| 13 | +import { Disposable } from '../../../../base/common/lifecycle.js'; |
| 14 | +import { Queue } from '../../../../base/common/async.js'; |
| 15 | + |
| 16 | +export class DynamicAuthenticationProviderStorageService extends Disposable implements IDynamicAuthenticationProviderStorageService { |
| 17 | + declare readonly _serviceBrand: undefined; |
| 18 | + |
| 19 | + private static readonly PROVIDERS_STORAGE_KEY = 'dynamicAuthProviders'; |
| 20 | + |
| 21 | + private readonly _onDidChangeTokens = this._register(new Emitter<DynamicAuthenticationProviderTokensChangeEvent>()); |
| 22 | + readonly onDidChangeTokens: Event<DynamicAuthenticationProviderTokensChangeEvent> = this._onDidChangeTokens.event; |
| 23 | + |
| 24 | + constructor( |
| 25 | + @IStorageService private readonly storageService: IStorageService, |
| 26 | + @ISecretStorageService private readonly secretStorageService: ISecretStorageService, |
| 27 | + @ILogService private readonly logService: ILogService |
| 28 | + ) { |
| 29 | + super(); |
| 30 | + |
| 31 | + // Listen for secret storage changes and emit events for dynamic auth provider token changes |
| 32 | + const queue = new Queue<void>(); |
| 33 | + this._register(this.secretStorageService.onDidChangeSecret(async (key: string) => { |
| 34 | + let payload: { isDynamicAuthProvider: boolean; authProviderId: string; clientId: string } | undefined; |
| 35 | + try { |
| 36 | + payload = JSON.parse(key); |
| 37 | + } catch (error) { |
| 38 | + // Ignore errors... must not be a dynamic auth provider |
| 39 | + } |
| 40 | + if (payload?.isDynamicAuthProvider) { |
| 41 | + void queue.queue(async () => { |
| 42 | + const tokens = await this.getSessionsForDynamicAuthProvider(payload.authProviderId, payload.clientId); |
| 43 | + this._onDidChangeTokens.fire({ |
| 44 | + authProviderId: payload.authProviderId, |
| 45 | + clientId: payload.clientId, |
| 46 | + tokens |
| 47 | + }); |
| 48 | + }); |
| 49 | + } |
| 50 | + })); |
| 51 | + } |
| 52 | + |
| 53 | + getClientId(providerId: string): string | undefined { |
| 54 | + const providers = this._getStoredProviders(); |
| 55 | + const provider = providers.find(p => p.providerId === providerId); |
| 56 | + return provider?.clientId; |
| 57 | + } |
| 58 | + |
| 59 | + storeClientId(providerId: string, clientId: string, label?: string, issuer?: string): void { |
| 60 | + // Store provider information in single location |
| 61 | + this._trackProvider(providerId, clientId, label, issuer); |
| 62 | + } |
| 63 | + |
| 64 | + private _trackProvider(providerId: string, clientId: string, label?: string, issuer?: string): void { |
| 65 | + const providers = this._getStoredProviders(); |
| 66 | + |
| 67 | + // Check if provider already exists |
| 68 | + const existingProviderIndex = providers.findIndex(p => p.providerId === providerId); |
| 69 | + if (existingProviderIndex === -1) { |
| 70 | + // Add new provider with provided or default info |
| 71 | + const newProvider: DynamicAuthenticationProviderInfo = { |
| 72 | + providerId, |
| 73 | + label: label || providerId, // Use provided label or providerId as default |
| 74 | + issuer: issuer || providerId, // Use provided issuer or providerId as default |
| 75 | + clientId |
| 76 | + }; |
| 77 | + providers.push(newProvider); |
| 78 | + this._storeProviders(providers); |
| 79 | + } else { |
| 80 | + const existingProvider = providers[existingProviderIndex]; |
| 81 | + // Create new provider object with updated info |
| 82 | + const updatedProvider: DynamicAuthenticationProviderInfo = { |
| 83 | + providerId, |
| 84 | + label: label || existingProvider.label, |
| 85 | + issuer: issuer || existingProvider.issuer, |
| 86 | + clientId |
| 87 | + }; |
| 88 | + providers[existingProviderIndex] = updatedProvider; |
| 89 | + this._storeProviders(providers); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + private _getStoredProviders(): DynamicAuthenticationProviderInfo[] { |
| 94 | + const stored = this.storageService.get(DynamicAuthenticationProviderStorageService.PROVIDERS_STORAGE_KEY, StorageScope.APPLICATION, '[]'); |
| 95 | + try { |
| 96 | + return JSON.parse(stored); |
| 97 | + } catch { |
| 98 | + return []; |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + private _storeProviders(providers: DynamicAuthenticationProviderInfo[]): void { |
| 103 | + this.storageService.store( |
| 104 | + DynamicAuthenticationProviderStorageService.PROVIDERS_STORAGE_KEY, |
| 105 | + JSON.stringify(providers), |
| 106 | + StorageScope.APPLICATION, |
| 107 | + StorageTarget.MACHINE |
| 108 | + ); |
| 109 | + } |
| 110 | + |
| 111 | + getInteractedProviders(): ReadonlyArray<DynamicAuthenticationProviderInfo> { |
| 112 | + return this._getStoredProviders(); |
| 113 | + } |
| 114 | + |
| 115 | + async removeDynamicProvider(providerId: string): Promise<void> { |
| 116 | + // Get provider info before removal for secret cleanup |
| 117 | + const providers = this._getStoredProviders(); |
| 118 | + const providerInfo = providers.find(p => p.providerId === providerId); |
| 119 | + |
| 120 | + // Remove from stored providers |
| 121 | + const filteredProviders = providers.filter(p => p.providerId !== providerId); |
| 122 | + this._storeProviders(filteredProviders); |
| 123 | + |
| 124 | + // Remove sessions from secret storage if we have the provider info |
| 125 | + if (providerInfo) { |
| 126 | + const secretKey = JSON.stringify({ isDynamicAuthProvider: true, authProviderId: providerId, clientId: providerInfo.clientId }); |
| 127 | + await this.secretStorageService.delete(secretKey); |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + async getSessionsForDynamicAuthProvider(authProviderId: string, clientId: string): Promise<(IAuthorizationTokenResponse & { created_at: number })[] | undefined> { |
| 132 | + const key = JSON.stringify({ isDynamicAuthProvider: true, authProviderId, clientId }); |
| 133 | + const value = await this.secretStorageService.get(key); |
| 134 | + if (value) { |
| 135 | + const parsed = JSON.parse(value); |
| 136 | + if (!Array.isArray(parsed) || !parsed.every((t) => typeof t.created_at === 'number' && isAuthorizationTokenResponse(t))) { |
| 137 | + this.logService.error(`Invalid session data for ${authProviderId} (${clientId}) in secret storage:`, parsed); |
| 138 | + await this.secretStorageService.delete(key); |
| 139 | + return undefined; |
| 140 | + } |
| 141 | + return parsed; |
| 142 | + } |
| 143 | + return undefined; |
| 144 | + } |
| 145 | + |
| 146 | + async setSessionsForDynamicAuthProvider(authProviderId: string, clientId: string, sessions: (IAuthorizationTokenResponse & { created_at: number })[]): Promise<void> { |
| 147 | + const key = JSON.stringify({ isDynamicAuthProvider: true, authProviderId, clientId }); |
| 148 | + const value = JSON.stringify(sessions); |
| 149 | + await this.secretStorageService.set(key, value); |
| 150 | + this.logService.trace(`Set session data for ${authProviderId} (${clientId}) in secret storage:`, sessions); |
| 151 | + } |
| 152 | +} |
| 153 | + |
| 154 | +registerSingleton(IDynamicAuthenticationProviderStorageService, DynamicAuthenticationProviderStorageService, InstantiationType.Delayed); |
0 commit comments