Path: blob/main/extensions/copilot/src/platform/authentication/common/copilotTokenManager.ts
13401 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 { createServiceIdentifier } from '../../../util/common/services';6import { Event } from '../../../util/vs/base/common/event';7import { CopilotToken, TokenError, TokenErrorReason } from './copilotToken';89export const ICopilotTokenManager = createServiceIdentifier<ICopilotTokenManager>('ICopilotTokenManager');1011/**12* @deprecated Use `IAuthenticationService` instead13*/14export interface ICopilotTokenManager {1516readonly _serviceBrand: undefined;1718/**19* Event emitter that will fire an event every time a token refresh is requested.20*21* This is used for example in the repo enablement code (lib/src/enablement.ts),22* where we need to clear the list of cached repos whenever we request a new token.23*/24readonly onDidCopilotTokenRefresh: Event<void>;2526/**27* Return a currently valid Copilot token, retrieving a fresh one if28* necessary.29*30* Note that a Copilot token manager should not provide a Copilot token unless31* telemetry consent has been obtained. If this is not checked by the token manager32* implementation itself, then anything constructing or initialising it should not33* do so without checking this. force will force a refresh of the token, even not expired34*/35getCopilotToken(force?: boolean): Promise<CopilotToken>;3637/**38* Drop the current Copilot token as we received an HTTP error while trying39* to use it that indicates it's no longer valid.40*/41resetCopilotToken(httpError?: number): void;42}4344export function nowSeconds(): number {45return Math.floor(Date.now() / 1000);46}4748export type NotGitHubLoginFailed = { kind: 'success' } | { kind: 'failure'; reason: Exclude<TokenErrorReason, 'GitHubLoginFailed'> };4950//#region Testing Copilot Token Mangers5152/** Intended for use as an add-on to `CopilotTokenManager`,53* that checks that a valid Copilot token is available. For tests.54*/55export interface CheckCopilotToken {56/** Check that the object has access to a valid Copilot token. */57checkCopilotToken(): Promise<{ status: 'OK' } | (TokenError & { reason: Exclude<TokenErrorReason, 'GitHubLoginFailed'> })>;58}596061