import { CredentialsProvider , Credentials , API as GitAPI } from './typings/git' ; import { workspace , Uri , Disposable } from 'vscode' ; import { getSession } from './auth' ; const EmptyDisposable = { dispose ( ) { } } ; class GitHubCredentialProvider implements CredentialsProvider { async getCredentials ( host ) { if ( ! /github\.com/i . test ( host . authority ) ) { return ; } const session = await getSession ( ) ; return { username : session . account . id , password : session . accessToken } ; } } export class GithubCredentialProviderManager { private providerDisposable : Disposable = EmptyDisposable ; private readonly disposable : Disposable ; private _enabled = false ; private set enabled ( enabled ) { if ( this . _enabled === enabled ) { return ; } this . _enabled = enabled ; if ( enabled ) { this . providerDisposable = this . gitAPI . registerCredentialsProvider ( new GitHubCredentialProvider ( ) ) ; } else { this . providerDisposable . dispose ( ) ; } } constructor ( private gitAPI ) { this . disposable = workspace . onDidChangeConfiguration ( e => { if ( e . affectsConfiguration ( 'github' ) ) { this . refresh ( ) ; } } ) ; this . refresh ( ) ; } private refresh ( ) { const config = workspace . getConfiguration ( 'github' , null ) ; const enabled = config . get < boolean > ( 'gitAuthentication' , true ) ; this . enabled = ! ! enabled ; } dispose ( ) { this . enabled = false ; this . disposable . dispose ( ) ; } }