Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/github-authentication/src/extension.ts
3314 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 * as vscode from 'vscode';
7
import { GitHubAuthenticationProvider, UriEventHandler } from './github';
8
9
const settingNotSent = '"github-enterprise.uri" not set';
10
const settingInvalid = '"github-enterprise.uri" invalid';
11
12
class NullAuthProvider implements vscode.AuthenticationProvider {
13
private _onDidChangeSessions = new vscode.EventEmitter<vscode.AuthenticationProviderAuthenticationSessionsChangeEvent>();
14
onDidChangeSessions = this._onDidChangeSessions.event;
15
16
private readonly _disposable: vscode.Disposable;
17
18
constructor(private readonly _errorMessage: string) {
19
this._disposable = vscode.authentication.registerAuthenticationProvider('github-enterprise', 'GitHub Enterprise', this);
20
}
21
22
createSession(): Thenable<vscode.AuthenticationSession> {
23
throw new Error(this._errorMessage);
24
}
25
26
getSessions(): Thenable<vscode.AuthenticationSession[]> {
27
return Promise.resolve([]);
28
}
29
removeSession(): Thenable<void> {
30
throw new Error(this._errorMessage);
31
}
32
33
dispose() {
34
this._onDidChangeSessions.dispose();
35
this._disposable.dispose();
36
}
37
}
38
39
function initGHES(context: vscode.ExtensionContext, uriHandler: UriEventHandler): vscode.Disposable {
40
const settingValue = vscode.workspace.getConfiguration().get<string>('github-enterprise.uri');
41
if (!settingValue) {
42
const provider = new NullAuthProvider(settingNotSent);
43
context.subscriptions.push(provider);
44
return provider;
45
}
46
47
// validate user value
48
let uri: vscode.Uri;
49
try {
50
uri = vscode.Uri.parse(settingValue, true);
51
} catch (e) {
52
vscode.window.showErrorMessage(vscode.l10n.t('GitHub Enterprise Server URI is not a valid URI: {0}', e.message ?? e));
53
const provider = new NullAuthProvider(settingInvalid);
54
context.subscriptions.push(provider);
55
return provider;
56
}
57
58
const githubEnterpriseAuthProvider = new GitHubAuthenticationProvider(context, uriHandler, uri);
59
context.subscriptions.push(githubEnterpriseAuthProvider);
60
return githubEnterpriseAuthProvider;
61
}
62
63
export function activate(context: vscode.ExtensionContext) {
64
const uriHandler = new UriEventHandler();
65
context.subscriptions.push(uriHandler);
66
context.subscriptions.push(vscode.window.registerUriHandler(uriHandler));
67
68
context.subscriptions.push(new GitHubAuthenticationProvider(context, uriHandler));
69
70
let before = vscode.workspace.getConfiguration().get<string>('github-enterprise.uri');
71
let githubEnterpriseAuthProvider = initGHES(context, uriHandler);
72
context.subscriptions.push(vscode.workspace.onDidChangeConfiguration(e => {
73
if (e.affectsConfiguration('github-enterprise.uri')) {
74
const after = vscode.workspace.getConfiguration().get<string>('github-enterprise.uri');
75
if (before !== after) {
76
githubEnterpriseAuthProvider?.dispose();
77
before = after;
78
githubEnterpriseAuthProvider = initGHES(context, uriHandler);
79
}
80
}
81
}));
82
83
// Listener to prompt for reload when the fetch implementation setting changes
84
const beforeFetchSetting = vscode.workspace.getConfiguration().get<boolean>('github-authentication.useElectronFetch', true);
85
context.subscriptions.push(vscode.workspace.onDidChangeConfiguration(async e => {
86
if (e.affectsConfiguration('github-authentication.useElectronFetch')) {
87
const afterFetchSetting = vscode.workspace.getConfiguration().get<boolean>('github-authentication.useElectronFetch', true);
88
if (beforeFetchSetting !== afterFetchSetting) {
89
const selection = await vscode.window.showInformationMessage(
90
vscode.l10n.t('GitHub Authentication - Reload required'),
91
{
92
modal: true,
93
detail: vscode.l10n.t('A reload is required for the fetch setting change to take effect.')
94
},
95
vscode.l10n.t('Reload Window')
96
);
97
if (selection) {
98
await vscode.commands.executeCommand('workbench.action.reloadWindow');
99
}
100
}
101
}
102
}));
103
}
104
105