@@ -8,7 +8,7 @@ import fetch, { Response } from 'node-fetch';
8
8
import { v4 as uuid } from 'uuid' ;
9
9
import { PromiseAdapter , promiseFromEvent } from './common/utils' ;
10
10
import { ExperimentationTelemetry } from './experimentationService' ;
11
- import { AuthProviderType } from './github' ;
11
+ import { AuthProviderType , UriEventHandler } from './github' ;
12
12
import { Log } from './common/logger' ;
13
13
import { isSupportedEnvironment } from './common/env' ;
14
14
import { LoopbackAuthServer } from './authServer' ;
@@ -21,23 +21,11 @@ const NETWORK_ERROR = 'network error';
21
21
const REDIRECT_URL_STABLE = 'https://vscode.dev/redirect' ;
22
22
const REDIRECT_URL_INSIDERS = 'https://insiders.vscode.dev/redirect' ;
23
23
24
- class UriEventHandler extends vscode . EventEmitter < vscode . Uri > implements vscode . UriHandler {
25
- constructor ( private readonly Logger : Log ) {
26
- super ( ) ;
27
- }
28
-
29
- public handleUri ( uri : vscode . Uri ) {
30
- this . Logger . trace ( 'Handling Uri...' ) ;
31
- this . fire ( uri ) ;
32
- }
33
- }
34
-
35
- export interface IGitHubServer extends vscode . Disposable {
24
+ export interface IGitHubServer {
36
25
login ( scopes : string ) : Promise < string > ;
37
26
getUserInfo ( token : string ) : Promise < { id : string ; accountName : string } > ;
38
27
sendAdditionalTelemetryInfo ( token : string ) : Promise < void > ;
39
28
friendlyName : string ;
40
- type : AuthProviderType ;
41
29
}
42
30
43
31
interface IGitHubDeviceCodeResponse {
@@ -73,38 +61,35 @@ async function getScopes(token: string, serverUri: vscode.Uri, logger: Log): Pro
73
61
export class GitHubServer implements IGitHubServer {
74
62
readonly friendlyName : string ;
75
63
76
- private _pendingNonces = new Map < string , string [ ] > ( ) ;
77
- private _codeExchangePromises = new Map < string , { promise : Promise < string > ; cancel : vscode . EventEmitter < void > } > ( ) ;
78
- private _disposable : vscode . Disposable | undefined ;
79
- private static _uriHandler : UriEventHandler | undefined ;
64
+ private readonly _pendingNonces = new Map < string , string [ ] > ( ) ;
65
+ private readonly _codeExchangePromises = new Map < string , { promise : Promise < string > ; cancel : vscode . EventEmitter < void > } > ( ) ;
66
+ private readonly _type : AuthProviderType ;
67
+
80
68
private _redirectEndpoint : string | undefined ;
81
69
82
70
constructor (
83
- public readonly type : AuthProviderType ,
84
- private readonly _supportDeviceCodeFlow : boolean ,
85
71
private readonly _logger : Log ,
86
- private readonly _telemetryReporter : ExperimentationTelemetry
72
+ private readonly _telemetryReporter : ExperimentationTelemetry ,
73
+ private readonly _uriHandler : UriEventHandler ,
74
+ private readonly _supportDeviceCodeFlow : boolean ,
75
+ private readonly _ghesUri ?: vscode . Uri
87
76
) {
88
- this . friendlyName = type === AuthProviderType . github ? 'GitHub' : 'GitHub Enterprise' ;
89
-
90
- if ( ! GitHubServer . _uriHandler ) {
91
- GitHubServer . _uriHandler = new UriEventHandler ( this . _logger ) ;
92
- this . _disposable = vscode . window . registerUriHandler ( GitHubServer . _uriHandler ) ;
93
- }
77
+ this . _type = _ghesUri ? AuthProviderType . githubEnterprise : AuthProviderType . github ;
78
+ this . friendlyName = this . _type === AuthProviderType . github ? 'GitHub' : _ghesUri ?. authority ! ;
94
79
}
95
80
96
81
get baseUri ( ) {
97
- if ( this . type === AuthProviderType . github ) {
82
+ if ( this . _type === AuthProviderType . github ) {
98
83
return vscode . Uri . parse ( 'https://github.com/' ) ;
99
84
}
100
- return vscode . Uri . parse ( vscode . workspace . getConfiguration ( 'github-enterprise' ) . get < string > ( 'uri' ) || '' , true ) ;
85
+ return this . _ghesUri ! ;
101
86
}
102
87
103
88
private async getRedirectEndpoint ( ) : Promise < string > {
104
89
if ( this . _redirectEndpoint ) {
105
90
return this . _redirectEndpoint ;
106
91
}
107
- if ( this . type === AuthProviderType . github ) {
92
+ if ( this . _type === AuthProviderType . github ) {
108
93
const proxyEndpoints = await vscode . commands . executeCommand < { [ providerId : string ] : string } | undefined > ( 'workbench.getCodeExchangeProxyEndpoints' ) ;
109
94
// If we are running in insiders vscode.dev, then ensure we use the redirect route on that.
110
95
this . _redirectEndpoint = REDIRECT_URL_STABLE ;
@@ -139,10 +124,6 @@ export class GitHubServer implements IGitHubServer {
139
124
return this . _redirectEndpoint ;
140
125
}
141
126
142
- dispose ( ) {
143
- this . _disposable ?. dispose ( ) ;
144
- }
145
-
146
127
// TODO@joaomoreno TODO@TylerLeonhardt
147
128
private async isNoCorsEnvironment ( ) : Promise < boolean > {
148
129
const uri = await vscode . env . asExternalUri ( vscode . Uri . parse ( `${ vscode . env . uriScheme } ://vscode.github-authentication/dummy` ) ) ;
@@ -246,7 +227,7 @@ export class GitHubServer implements IGitHubServer {
246
227
// before completing it.
247
228
let codeExchangePromise = this . _codeExchangePromises . get ( scopes ) ;
248
229
if ( ! codeExchangePromise ) {
249
- codeExchangePromise = promiseFromEvent ( GitHubServer . _uriHandler ! . event , this . handleUri ( scopes ) ) ;
230
+ codeExchangePromise = promiseFromEvent ( this . _uriHandler ! . event , this . handleUri ( scopes ) ) ;
250
231
this . _codeExchangePromises . set ( scopes , codeExchangePromise ) ;
251
232
}
252
233
@@ -467,7 +448,7 @@ export class GitHubServer implements IGitHubServer {
467
448
const endpointUrl = proxyEndpoints ?. github ? `${ proxyEndpoints . github } login/oauth/access_token` : GITHUB_TOKEN_URL ;
468
449
469
450
const body = new URLSearchParams ( [ [ 'code' , code ] ] ) ;
470
- if ( this . type === AuthProviderType . githubEnterprise ) {
451
+ if ( this . _type === AuthProviderType . githubEnterprise ) {
471
452
body . append ( 'github_enterprise' , this . baseUri . toString ( true ) ) ;
472
453
body . append ( 'redirect_uri' , await this . getRedirectEndpoint ( ) ) ;
473
454
}
@@ -495,11 +476,11 @@ export class GitHubServer implements IGitHubServer {
495
476
}
496
477
497
478
private getServerUri ( path : string = '' ) {
498
- if ( this . type === AuthProviderType . github ) {
479
+ if ( this . _type === AuthProviderType . github ) {
499
480
return vscode . Uri . parse ( 'https://api.github.com' ) . with ( { path } ) ;
500
481
}
501
482
// GHES
502
- const apiUri = vscode . Uri . parse ( vscode . workspace . getConfiguration ( 'github-enterprise' ) . get < string > ( 'uri' ) || '' , true ) ;
483
+ const apiUri = this . baseUri ;
503
484
return vscode . Uri . parse ( `${ apiUri . scheme } ://${ apiUri . authority } /api/v3${ path } ` ) ;
504
485
}
505
486
@@ -553,7 +534,7 @@ export class GitHubServer implements IGitHubServer {
553
534
return ;
554
535
}
555
536
556
- if ( this . type === AuthProviderType . github ) {
537
+ if ( this . _type === AuthProviderType . github ) {
557
538
return await this . checkEduDetails ( token ) ;
558
539
}
559
540
0 commit comments