11using System ;
22using System . ComponentModel . Composition ;
33using System . Diagnostics . CodeAnalysis ;
4- using System . Net . Http ;
54using System . Threading . Tasks ;
65using GitHub . Api ;
76using GitHub . Primitives ;
87using Octokit ;
8+ using IProgram = GitHub . Models . IProgram ;
99
1010namespace GitHub . Services
1111{
@@ -16,23 +16,29 @@ public class EnterpriseCapabilitiesService : IEnterpriseCapabilitiesService
1616 static readonly Version MinimumOAuthVersion = new Version ( "2.12.2" ) ;
1717 readonly ISimpleApiClientFactory apiClientFactory ;
1818 readonly IEnterpriseProbe probe ;
19+ readonly IProgram program ;
1920
2021 [ ImportingConstructor ]
2122 public EnterpriseCapabilitiesService (
2223 ISimpleApiClientFactory apiClientFactory ,
23- IEnterpriseProbe probe )
24+ IEnterpriseProbe probe ,
25+ IProgram program )
2426 {
2527 this . apiClientFactory = apiClientFactory ;
2628 this . probe = probe ;
29+ this . program = program ;
2730 }
2831
2932 public Task < EnterpriseProbeResult > Probe ( Uri enterpriseBaseUrl ) => probe . Probe ( enterpriseBaseUrl ) ;
3033
3134 public async Task < EnterpriseLoginMethods > ProbeLoginMethods ( Uri enterpriseBaseUrl )
3235 {
36+ // It's important that we don't use our cached credentials on this connection, as they
37+ // may be wrong - we're trying to log in after all.
38+ var hostAddress = HostAddress . Create ( enterpriseBaseUrl ) ;
39+ var connection = new Octokit . Connection ( program . ProductHeader , hostAddress . ApiUri ) ;
40+ var meta = await GetMetadata ( connection ) . ConfigureAwait ( false ) ;
3341 var result = EnterpriseLoginMethods . Token ;
34- var client = await apiClientFactory . Create ( UriString . ToUriString ( enterpriseBaseUrl ) ) . ConfigureAwait ( false ) ;
35- var meta = await GetMetadata ( client . Client ) . ConfigureAwait ( false ) ;
3642
3743 if ( meta . VerifiablePasswordAuthentication ) result |= EnterpriseLoginMethods . UsernameAndPassword ;
3844
@@ -45,23 +51,10 @@ public async Task<EnterpriseLoginMethods> ProbeLoginMethods(Uri enterpriseBaseUr
4551 return result ;
4652 }
4753
48- private Uri GetLoginUrl ( IConnection connection )
49- {
50- var oauthClient = new OauthClient ( connection ) ;
51- var oauthLoginRequest = new OauthLoginRequest ( ApiClientConfiguration . ClientId )
52- {
53- RedirectUri = new Uri ( OAuthCallbackListener . CallbackUrl ) ,
54- } ;
55- var uri = oauthClient . GetGitHubLoginUrl ( oauthLoginRequest ) ;
56-
57- // OauthClient.GetGitHubLoginUrl seems to give the wrong URL. Fix this.
58- return new Uri ( uri . ToString ( ) . Replace ( "/api/v3" , "" ) ) ;
59- }
60-
61- private async Task < EnterpriseMeta > GetMetadata ( IGitHubClient client )
54+ private async Task < EnterpriseMeta > GetMetadata ( IConnection connection )
6255 {
6356 var endpoint = new Uri ( "meta" , UriKind . Relative ) ;
64- var response = await client . Connection . Get < EnterpriseMeta > ( endpoint , null , null ) . ConfigureAwait ( false ) ;
57+ var response = await connection . Get < EnterpriseMeta > ( endpoint , null , null ) . ConfigureAwait ( false ) ;
6558 return response . Body ;
6659 }
6760
0 commit comments