Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/src/platform/authentication/common/copilotTokenManager.ts
13401 views
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 { createServiceIdentifier } from '../../../util/common/services';
7
import { Event } from '../../../util/vs/base/common/event';
8
import { CopilotToken, TokenError, TokenErrorReason } from './copilotToken';
9
10
export const ICopilotTokenManager = createServiceIdentifier<ICopilotTokenManager>('ICopilotTokenManager');
11
12
/**
13
* @deprecated Use `IAuthenticationService` instead
14
*/
15
export interface ICopilotTokenManager {
16
17
readonly _serviceBrand: undefined;
18
19
/**
20
* Event emitter that will fire an event every time a token refresh is requested.
21
*
22
* This is used for example in the repo enablement code (lib/src/enablement.ts),
23
* where we need to clear the list of cached repos whenever we request a new token.
24
*/
25
readonly onDidCopilotTokenRefresh: Event<void>;
26
27
/**
28
* Return a currently valid Copilot token, retrieving a fresh one if
29
* necessary.
30
*
31
* Note that a Copilot token manager should not provide a Copilot token unless
32
* telemetry consent has been obtained. If this is not checked by the token manager
33
* implementation itself, then anything constructing or initialising it should not
34
* do so without checking this. force will force a refresh of the token, even not expired
35
*/
36
getCopilotToken(force?: boolean): Promise<CopilotToken>;
37
38
/**
39
* Drop the current Copilot token as we received an HTTP error while trying
40
* to use it that indicates it's no longer valid.
41
*/
42
resetCopilotToken(httpError?: number): void;
43
}
44
45
export function nowSeconds(): number {
46
return Math.floor(Date.now() / 1000);
47
}
48
49
export type NotGitHubLoginFailed = { kind: 'success' } | { kind: 'failure'; reason: Exclude<TokenErrorReason, 'GitHubLoginFailed'> };
50
51
//#region Testing Copilot Token Mangers
52
53
/** Intended for use as an add-on to `CopilotTokenManager`,
54
* that checks that a valid Copilot token is available. For tests.
55
*/
56
export interface CheckCopilotToken {
57
/** Check that the object has access to a valid Copilot token. */
58
checkCopilotToken(): Promise<{ status: 'OK' } | (TokenError & { reason: Exclude<TokenErrorReason, 'GitHubLoginFailed'> })>;
59
}
60
61