Path: blob/main/src/vs/workbench/services/authentication/common/authenticationQuery.ts
3296 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import { Event } from '../../../../base/common/event.js';6import { createDecorator } from '../../../../platform/instantiation/common/instantiation.js';7import { AuthenticationSessionAccount } from './authentication.js';89/**10* Statistics about authentication usage11*/12export interface IAuthenticationUsageStats {13readonly totalSessions: number;14readonly totalAccounts: number;15readonly recentActivity: {16readonly accountName: string;17readonly lastUsed: number;18readonly usageCount: number;19}[];20}2122/**23* Information about entities using authentication within a provider24*/25export interface IActiveEntities {26readonly extensions: string[];27readonly mcpServers: string[];28}2930/**31* Base query interface with common properties32*/33export interface IBaseQuery {34readonly providerId: string;35}3637/**38* Query interface for operations on a specific account within a provider39*/40export interface IAccountQuery extends IBaseQuery {41readonly accountName: string;4243/**44* Get operations for a specific extension on this account45* @param extensionId The extension id46* @returns An account-extension query interface47*/48extension(extensionId: string): IAccountExtensionQuery;4950/**51* Get operations for a specific MCP server on this account52* @param mcpServerId The MCP server id53* @returns An account-MCP server query interface54*/55mcpServer(mcpServerId: string): IAccountMcpServerQuery;5657/**58* Get operations for all extensions on this account59* @returns An account-extensions query interface60*/61extensions(): IAccountExtensionsQuery;6263/**64* Get operations for all MCP servers on this account65* @returns An account-MCP servers query interface66*/67mcpServers(): IAccountMcpServersQuery;6869/**70* Get operations for all entities (extensions and MCP servers) on this account71* @returns An account-entities query interface for type-agnostic operations72*/73entities(): IAccountEntitiesQuery;7475/**76* Remove all authentication data for this account77*/78remove(): void;79}8081/**82* Query interface for operations on a specific extension within a specific account83*/84export interface IAccountExtensionQuery extends IBaseQuery {85readonly accountName: string;86readonly extensionId: string;8788/**89* Check if this extension is allowed to access this account90* @returns True if allowed, false if denied, undefined if not yet decided91*/92isAccessAllowed(): boolean | undefined;9394/**95* Set access permission for this extension on this account96* @param allowed True to allow, false to deny access97* @param extensionName Optional extension name for display purposes98*/99setAccessAllowed(allowed: boolean, extensionName?: string): void;100101/**102* Add usage record for this extension on this account103* @param scopes The scopes that were used104* @param extensionName The extension name for display purposes105*/106addUsage(scopes: readonly string[], extensionName: string): void;107108/**109* Get usage history for this extension on this account110* @returns Array of usage records111*/112getUsage(): {113readonly extensionId: string;114readonly extensionName: string;115readonly scopes: readonly string[];116readonly lastUsed: number;117}[];118119/**120* Remove all usage data for this extension on this account121*/122removeUsage(): void;123124/**125* Set this account as the preferred account for this extension126*/127setAsPreferred(): void;128129/**130* Check if this account is the preferred account for this extension131*/132isPreferred(): boolean;133134/**135* Check if this extension is trusted (defined in product.json)136* @returns True if the extension is trusted, false otherwise137*/138isTrusted(): boolean;139}140141/**142* Query interface for operations on a specific MCP server within a specific account143*/144export interface IAccountMcpServerQuery extends IBaseQuery {145readonly accountName: string;146readonly mcpServerId: string;147148/**149* Check if this MCP server is allowed to access this account150* @returns True if allowed, false if denied, undefined if not yet decided151*/152isAccessAllowed(): boolean | undefined;153154/**155* Set access permission for this MCP server on this account156* @param allowed True to allow, false to deny access157* @param mcpServerName Optional MCP server name for display purposes158*/159setAccessAllowed(allowed: boolean, mcpServerName?: string): void;160161/**162* Add usage record for this MCP server on this account163* @param scopes The scopes that were used164* @param mcpServerName The MCP server name for display purposes165*/166addUsage(scopes: readonly string[], mcpServerName: string): void;167168/**169* Get usage history for this MCP server on this account170* @returns Array of usage records171*/172getUsage(): {173readonly mcpServerId: string;174readonly mcpServerName: string;175readonly scopes: readonly string[];176readonly lastUsed: number;177}[];178179/**180* Remove all usage data for this MCP server on this account181*/182removeUsage(): void;183184/**185* Set this account as the preferred account for this MCP server186*/187setAsPreferred(): void;188189/**190* Check if this account is the preferred account for this MCP server191*/192isPreferred(): boolean;193194/**195* Check if this MCP server is trusted (defined in product.json)196* @returns True if the MCP server is trusted, false otherwise197*/198isTrusted(): boolean;199}200201/**202* Query interface for operations on all extensions within a specific account203*/204export interface IAccountExtensionsQuery extends IBaseQuery {205readonly accountName: string;206207/**208* Get all extensions that have access to this account with their trusted state209* @returns Array of objects containing extension data including trusted state210*/211getAllowedExtensions(): { id: string; name: string; allowed?: boolean; lastUsed?: number; trusted?: boolean }[];212213/**214* Grant access to this account for all specified extensions215* @param extensionIds Array of extension IDs to grant access to216*/217allowAccess(extensionIds: string[]): void;218219/**220* Remove access to this account for all specified extensions221* @param extensionIds Array of extension IDs to remove access from222*/223removeAccess(extensionIds: string[]): void;224225/**226* Execute a callback for each extension that has used this account227* @param callback Function to execute for each extension228*/229forEach(callback: (extensionQuery: IAccountExtensionQuery) => void): void;230}231232/**233* Query interface for operations on all MCP servers within a specific account234*/235export interface IAccountMcpServersQuery extends IBaseQuery {236readonly accountName: string;237238/**239* Get all MCP servers that have access to this account with their trusted state240* @returns Array of objects containing MCP server data including trusted state241*/242getAllowedMcpServers(): { id: string; name: string; allowed?: boolean; lastUsed?: number; trusted?: boolean }[];243244/**245* Grant access to this account for all specified MCP servers246* @param mcpServerIds Array of MCP server IDs to grant access to247*/248allowAccess(mcpServerIds: string[]): void;249250/**251* Remove access to this account for all specified MCP servers252* @param mcpServerIds Array of MCP server IDs to remove access from253*/254removeAccess(mcpServerIds: string[]): void;255256/**257* Execute a callback for each MCP server that has used this account258* @param callback Function to execute for each MCP server259*/260forEach(callback: (mcpServerQuery: IAccountMcpServerQuery) => void): void;261}262263/**264* Query interface for type-agnostic operations on all entities (extensions and MCP servers) within a specific account265*/266export interface IAccountEntitiesQuery extends IBaseQuery {267readonly accountName: string;268269/**270* Check if this account has been used by any entity (extension or MCP server)271* @returns True if the account has been used, false otherwise272*/273hasAnyUsage(): boolean;274275/**276* Get the total count of entities that have used this account277* @returns Object with counts for extensions and MCP servers278*/279getEntityCount(): { extensions: number; mcpServers: number; total: number };280281/**282* Remove access to this account for all entities (extensions and MCP servers)283*/284removeAllAccess(): void;285286/**287* Execute a callback for each entity that has used this account288* @param callback Function to execute for each entity289*/290forEach(callback: (entityId: string, entityType: 'extension' | 'mcpServer') => void): void;291}292293/**294* Query interface for operations on a specific extension within a provider295*/296export interface IProviderExtensionQuery extends IBaseQuery {297readonly extensionId: string;298299/**300* Get the preferred account for this extension within this provider301* @returns The account name, or undefined if no preference is set302*/303getPreferredAccount(): string | undefined;304305/**306* Set the preferred account for this extension within this provider307* @param account The account to set as preferred308*/309setPreferredAccount(account: AuthenticationSessionAccount): void;310311/**312* Remove the account preference for this extension within this provider313*/314removeAccountPreference(): void;315}316317/**318* Query interface for operations on a specific MCP server within a provider319*/320export interface IProviderMcpServerQuery extends IBaseQuery {321readonly mcpServerId: string;322323/**324* Get the last used account for this MCP server within a provider325* @returns The account name, or undefined if no preference is set326*/327getLastUsedAccount(): Promise<string | undefined>;328329/**330* Get the preferred account for this MCP server within a provider331* @returns The account name, or undefined if no preference is set332*/333getPreferredAccount(): string | undefined;334335/**336* Set the preferred account for this MCP server within a provider337* @param account The account to set as preferred338*/339setPreferredAccount(account: AuthenticationSessionAccount): void;340341/**342* Remove the account preference for this MCP server within a provider343*/344removeAccountPreference(): void;345346/**347* Get all accounts that this MCP server has used within this provider348* @returns Array of account names349*/350getUsedAccounts(): Promise<string[]>;351}352353/**354* Query interface for provider-scoped operations355*/356export interface IProviderQuery extends IBaseQuery {357/**358* Get operations for a specific account within this provider359* @param accountName The account name360* @returns An account query interface361*/362account(accountName: string): IAccountQuery;363364/**365* Get operations for a specific extension within this provider366* @param extensionId The extension id367* @returns A provider-extension query interface368*/369extension(extensionId: string): IProviderExtensionQuery;370371/**372* Get operations for a specific MCP server within this provider373* @param mcpServerId The MCP server id374* @returns A provider-MCP server query interface375*/376mcpServer(mcpServerId: string): IProviderMcpServerQuery;377378/**379* Get information about active entities (extensions and MCP servers) within this provider380* @returns Information about entities that have used authentication381*/382getActiveEntities(): Promise<IActiveEntities>;383384/**385* Get all account names for this provider386* @returns Array of account names387*/388getAccountNames(): Promise<string[]>;389390/**391* Get usage statistics for this provider392* @returns Usage statistics393*/394getUsageStats(): Promise<IAuthenticationUsageStats>;395396/**397* Execute a callback for each account in this provider398* @param callback Function to execute for each account399*/400forEachAccount(callback: (accountQuery: IAccountQuery) => void): Promise<void>;401}402403/**404* Query interface for extension-scoped operations (cross-provider)405*/406export interface IExtensionQuery {407readonly extensionId: string;408409/**410* Get all providers where this extension has access411* @param includeInternal Whether to include internal providers (starting with INTERNAL_AUTH_PROVIDER_PREFIX)412* @returns Array of provider IDs413*/414getProvidersWithAccess(includeInternal?: boolean): Promise<string[]>;415416/**417* Get account preferences for this extension across all providers418* @param includeInternal Whether to include internal providers (starting with INTERNAL_AUTH_PROVIDER_PREFIX)419* @returns Map of provider ID to account name420*/421getAllAccountPreferences(includeInternal?: boolean): Map<string, string>;422423/**424* Get operations for this extension within a specific provider425* @param providerId The provider ID426* @returns A provider-extension query interface427*/428provider(providerId: string): IProviderExtensionQuery;429}430431/**432* Query interface for MCP server-scoped operations (cross-provider)433*/434export interface IMcpServerQuery {435readonly mcpServerId: string;436437/**438* Get all providers where this MCP server has access439* @param includeInternal Whether to include internal providers (starting with INTERNAL_AUTH_PROVIDER_PREFIX)440* @returns Array of provider IDs441*/442getProvidersWithAccess(includeInternal?: boolean): Promise<string[]>;443444/**445* Get account preferences for this MCP server across all providers446* @param includeInternal Whether to include internal providers (starting with INTERNAL_AUTH_PROVIDER_PREFIX)447* @returns Map of provider ID to account name448*/449getAllAccountPreferences(includeInternal?: boolean): Map<string, string>;450451/**452* Get operations for this MCP server within a specific provider453* @param providerId The provider ID454* @returns A provider-MCP server query interface455*/456provider(providerId: string): IProviderMcpServerQuery;457}458459/**460* Main authentication query service interface461*/462export const IAuthenticationQueryService = createDecorator<IAuthenticationQueryService>('IAuthenticationQueryService');463export interface IAuthenticationQueryService {464readonly _serviceBrand: undefined;465466/**467* Fires when authentication preferences change468*/469readonly onDidChangePreferences: Event<{470readonly providerId: string;471readonly entityType: 'extension' | 'mcpServer';472readonly entityIds: string[];473}>;474475/**476* Fires when authentication access permissions change477*/478readonly onDidChangeAccess: Event<{479readonly providerId: string;480readonly accountName: string;481}>;482483/**484* Get operations for a specific authentication provider485* @param providerId The authentication provider id486* @returns A provider query interface487*/488provider(providerId: string): IProviderQuery;489490/**491* Get operations for a specific extension across all providers492* @param extensionId The extension id493* @returns An extension query interface494*/495extension(extensionId: string): IExtensionQuery;496497/**498* Get operations for a specific MCP server across all providers499* @param mcpServerId The MCP server id500* @returns An MCP server query interface501*/502mcpServer(mcpServerId: string): IMcpServerQuery;503504/**505* Get all available provider IDs506* @returns Array of provider IDs507*/508getProviderIds(): string[];509510/**511* Clear all authentication data (for testing/debugging purposes)512* @param confirmation Must be 'CLEAR_ALL_AUTH_DATA' to confirm513* @param includeInternal Whether to include internal providers (defaults to true for complete clearing)514*/515clearAllData(confirmation: 'CLEAR_ALL_AUTH_DATA', includeInternal?: boolean): Promise<void>;516}517518519