Skip to content
This repository was archived by the owner on Jun 21, 2023. It is now read-only.

Commit d53c4c6

Browse files
committed
Fix enterprise logins.
- Ported octokit.net#1726 to our fork - Ensure we're not trying to access the `meta` endpoint with invalid credentials
1 parent 610dc8e commit d53c4c6

File tree

2 files changed

+13
-20
lines changed

2 files changed

+13
-20
lines changed

src/GitHub.App/Services/EnterpriseCapabilitiesService.cs

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
using System;
22
using System.ComponentModel.Composition;
33
using System.Diagnostics.CodeAnalysis;
4-
using System.Net.Http;
54
using System.Threading.Tasks;
65
using GitHub.Api;
76
using GitHub.Primitives;
87
using Octokit;
8+
using IProgram = GitHub.Models.IProgram;
99

1010
namespace 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

Comments
 (0)