move credential provider to github
This commit is contained in:
parent
b629dba454
commit
a85a2e6044
11 changed files with 84 additions and 79 deletions
|
@ -1678,10 +1678,7 @@
|
|||
"description": "%config.terminalAuthentication%"
|
||||
},
|
||||
"git.githubAuthentication": {
|
||||
"type": "boolean",
|
||||
"scope": "resource",
|
||||
"default": true,
|
||||
"description": "%config.githubAuthentication%"
|
||||
"deprecationMessage": "This setting is now deprecated, please use `github.gitAuthentication` instead."
|
||||
}
|
||||
}
|
||||
},
|
||||
|
|
|
@ -146,7 +146,6 @@
|
|||
"config.untrackedChanges.hidden": "Untracked changes are hidden and excluded from several actions.",
|
||||
"config.showCommitInput": "Controls whether to show the commit input in the Git source control panel.",
|
||||
"config.terminalAuthentication": "Controls whether to enable VS Code to be the authentication handler for git processes spawned in the integrated terminal. Note: terminals need to be restarted to pick up a change in this setting.",
|
||||
"config.githubAuthentication": "Controls whether to enable automatic GitHub authentication for git commands within VS Code.",
|
||||
"colors.added": "Color for added resources.",
|
||||
"colors.modified": "Color for modified resources.",
|
||||
"colors.deleted": "Color for deleted resources.",
|
||||
|
|
|
@ -1,67 +0,0 @@
|
|||
/*---------------------------------------------------------------------------------------------
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the MIT License. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
|
||||
import { CredentialsProvider, Credentials } from './api/git';
|
||||
import { IDisposable, filterEvent, EmptyDisposable } from './util';
|
||||
import { workspace, Uri, AuthenticationSession, authentication } from 'vscode';
|
||||
import { Askpass } from './askpass';
|
||||
|
||||
export class GitHubCredentialProvider implements CredentialsProvider {
|
||||
|
||||
async getCredentials(host: Uri): Promise<Credentials | undefined> {
|
||||
if (!/github\.com/i.test(host.authority)) {
|
||||
return;
|
||||
}
|
||||
|
||||
const session = await this.getSession();
|
||||
return { username: session.account.id, password: await session.getAccessToken() };
|
||||
}
|
||||
|
||||
private async getSession(): Promise<AuthenticationSession> {
|
||||
const authenticationSessions = await authentication.getSessions('github', ['repo']);
|
||||
|
||||
if (authenticationSessions.length) {
|
||||
return await authenticationSessions[0];
|
||||
} else {
|
||||
return await authentication.login('github', ['repo']);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export class GithubCredentialProviderManager {
|
||||
|
||||
private providerDisposable: IDisposable = EmptyDisposable;
|
||||
private readonly disposable: IDisposable;
|
||||
|
||||
private _enabled = false;
|
||||
private set enabled(enabled: boolean) {
|
||||
if (this._enabled === enabled) {
|
||||
return;
|
||||
}
|
||||
|
||||
this._enabled = enabled;
|
||||
|
||||
if (enabled) {
|
||||
this.providerDisposable = this.askpass.registerCredentialsProvider(new GitHubCredentialProvider());
|
||||
} else {
|
||||
this.providerDisposable.dispose();
|
||||
}
|
||||
}
|
||||
|
||||
constructor(private readonly askpass: Askpass) {
|
||||
this.disposable = filterEvent(workspace.onDidChangeConfiguration, e => e.affectsConfiguration('git'))(this.refresh, this);
|
||||
this.refresh();
|
||||
}
|
||||
|
||||
private refresh(): void {
|
||||
const config = workspace.getConfiguration('git', null);
|
||||
this.enabled = config.get<boolean>('enabled', true) && config.get('githubAuthentication', true);
|
||||
}
|
||||
|
||||
dispose(): void {
|
||||
this.enabled = false;
|
||||
this.disposable.dispose();
|
||||
}
|
||||
}
|
|
@ -22,7 +22,6 @@ import * as path from 'path';
|
|||
import * as fs from 'fs';
|
||||
import { GitTimelineProvider } from './timelineProvider';
|
||||
import { registerAPICommands } from './api/api1';
|
||||
import { GithubCredentialProviderManager } from './github';
|
||||
import { TerminalEnvironmentManager } from './terminal';
|
||||
|
||||
const deactivateTasks: { (): Promise<any>; }[] = [];
|
||||
|
@ -44,9 +43,6 @@ async function createModel(context: ExtensionContext, outputChannel: OutputChann
|
|||
const terminalEnvironmentManager = new TerminalEnvironmentManager(context, env);
|
||||
disposables.push(terminalEnvironmentManager);
|
||||
|
||||
const githubCredentialProviderManager = new GithubCredentialProviderManager(askpass);
|
||||
context.subscriptions.push(githubCredentialProviderManager);
|
||||
|
||||
const git = new Git({ gitPath: info.path, version: info.version, env });
|
||||
const model = new Model(git, askpass, context.globalState, outputChannel);
|
||||
disposables.push(model);
|
||||
|
|
|
@ -25,6 +25,19 @@
|
|||
"title": "Publish to GitHub"
|
||||
}
|
||||
],
|
||||
"configuration": [
|
||||
{
|
||||
"title": "GitHub",
|
||||
"properties": {
|
||||
"github.gitAuthentication": {
|
||||
"type": "boolean",
|
||||
"scope": "resource",
|
||||
"default": true,
|
||||
"description": "%config.gitAuthentication%"
|
||||
}
|
||||
}
|
||||
}
|
||||
],
|
||||
"viewsWelcome": [
|
||||
{
|
||||
"view": "scm",
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
{
|
||||
"displayName": "GitHub",
|
||||
"description": "GitHub",
|
||||
"config.gitAuthentication": "Controls whether to enable automatic GitHub authentication for git commands within VS Code.",
|
||||
"welcome.publishFolder": "You can also directly publish this folder to a GitHub repository.\n[$(github) Publish to GitHub](command:github.publish)"
|
||||
}
|
||||
|
|
|
@ -26,7 +26,7 @@ function getAgent(url: string | undefined = process.env.HTTPS_PROXY): Agent {
|
|||
|
||||
const scopes = ['repo'];
|
||||
|
||||
async function getSession(): Promise<AuthenticationSession> {
|
||||
export async function getSession(): Promise<AuthenticationSession> {
|
||||
const authenticationSessions = await authentication.getSessions('github', scopes);
|
||||
|
||||
if (authenticationSessions.length) {
|
|
@ -5,7 +5,7 @@
|
|||
|
||||
import * as vscode from 'vscode';
|
||||
import { API as GitAPI } from './typings/git';
|
||||
import { getOctokit } from './octokit';
|
||||
import { getOctokit } from './auth';
|
||||
|
||||
function sanitizeRepositoryName(value: string): string {
|
||||
return value.trim().replace(/[^a-z0-9_.]/ig, '-');
|
||||
|
|
64
extensions/github/src/credentialProvider.ts
Normal file
64
extensions/github/src/credentialProvider.ts
Normal file
|
@ -0,0 +1,64 @@
|
|||
/*---------------------------------------------------------------------------------------------
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the MIT License. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
|
||||
import { CredentialsProvider, Credentials, API as GitAPI } from './typings/git';
|
||||
import { workspace, Uri, Disposable } from 'vscode';
|
||||
import { getSession } from './auth';
|
||||
|
||||
const EmptyDisposable: Disposable = { dispose() { } };
|
||||
|
||||
class GitHubCredentialProvider implements CredentialsProvider {
|
||||
|
||||
async getCredentials(host: Uri): Promise<Credentials | undefined> {
|
||||
if (!/github\.com/i.test(host.authority)) {
|
||||
return;
|
||||
}
|
||||
|
||||
const session = await getSession();
|
||||
return { username: session.account.id, password: await session.getAccessToken() };
|
||||
}
|
||||
}
|
||||
|
||||
export class GithubCredentialProviderManager {
|
||||
|
||||
private providerDisposable: Disposable = EmptyDisposable;
|
||||
private readonly disposable: Disposable;
|
||||
|
||||
private _enabled = false;
|
||||
private set enabled(enabled: boolean) {
|
||||
if (this._enabled === enabled) {
|
||||
return;
|
||||
}
|
||||
|
||||
this._enabled = enabled;
|
||||
|
||||
if (enabled) {
|
||||
this.providerDisposable = this.gitAPI.registerCredentialsProvider(new GitHubCredentialProvider());
|
||||
} else {
|
||||
this.providerDisposable.dispose();
|
||||
}
|
||||
}
|
||||
|
||||
constructor(private gitAPI: GitAPI) {
|
||||
this.disposable = workspace.onDidChangeConfiguration(e => {
|
||||
if (e.affectsConfiguration('github')) {
|
||||
this.refresh();
|
||||
}
|
||||
});
|
||||
|
||||
this.refresh();
|
||||
}
|
||||
|
||||
private refresh(): void {
|
||||
const config = workspace.getConfiguration('github', null);
|
||||
const enabled = config.get<boolean>('gitAuthentication', true);
|
||||
this.enabled = !!enabled;
|
||||
}
|
||||
|
||||
dispose(): void {
|
||||
this.enabled = false;
|
||||
this.disposable.dispose();
|
||||
}
|
||||
}
|
|
@ -7,6 +7,7 @@ import * as vscode from 'vscode';
|
|||
import { GithubRemoteSourceProvider } from './remoteSourceProvider';
|
||||
import { GitExtension } from './typings/git';
|
||||
import { registerCommands } from './commands';
|
||||
import { GithubCredentialProviderManager } from './credentialProvider';
|
||||
|
||||
export async function activate(context: vscode.ExtensionContext) {
|
||||
const gitExtension = vscode.extensions.getExtension<GitExtension>('vscode.git')!.exports;
|
||||
|
@ -14,4 +15,5 @@ export async function activate(context: vscode.ExtensionContext) {
|
|||
|
||||
context.subscriptions.push(...registerCommands(gitAPI));
|
||||
context.subscriptions.push(gitAPI.registerRemoteSourceProvider(new GithubRemoteSourceProvider()));
|
||||
context.subscriptions.push(new GithubCredentialProviderManager(gitAPI));
|
||||
}
|
||||
|
|
|
@ -4,7 +4,7 @@
|
|||
*--------------------------------------------------------------------------------------------*/
|
||||
|
||||
import { RemoteSourceProvider, RemoteSource } from './typings/git';
|
||||
import { getOctokit } from './octokit';
|
||||
import { getOctokit } from './auth';
|
||||
import { Octokit } from '@octokit/rest';
|
||||
|
||||
function asRemoteSource(raw: any): RemoteSource {
|
||||
|
|
Loading…
Reference in a new issue