Skip to content
This repository was archived by the owner on Dec 5, 2024. It is now read-only.

Commit 9cec196

Browse files
Merge pull request #310 from github/shana/thinking-about-auth
Fixing authentication issues
2 parents 2b58acc + f38a3f8 commit 9cec196

28 files changed

+399
-162
lines changed

src/GitHub.Api/Api/ApiClient.cs

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,32 +7,39 @@ namespace GitHub.Unity
77
{
88
class ApiClient : IApiClient
99
{
10+
public static IApiClient Create(UriString repositoryUrl, IKeychain keychain, IAppConfiguration appConfiguration)
11+
{
12+
var credentialStore = keychain.Connect(repositoryUrl);
13+
var hostAddress = HostAddress.Create(repositoryUrl);
14+
15+
return new ApiClient(repositoryUrl, keychain,
16+
new GitHubClient(appConfiguration.ProductHeader, credentialStore, hostAddress.ApiUri));
17+
}
18+
1019
private static readonly Unity.ILogging logger = Unity.Logging.GetLogger<ApiClient>();
1120
public HostAddress HostAddress { get; }
1221
public UriString OriginalUrl { get; }
1322

23+
private readonly IKeychain keychain;
1424
private readonly IGitHubClient githubClient;
15-
private readonly IGitHubClient githubAppClient;
16-
private readonly ICredentialManager credentialManager;
1725
private readonly ILoginManager loginManager;
1826
private static readonly SemaphoreSlim sem = new SemaphoreSlim(1);
1927

2028
Octokit.Repository repositoryCache = new Octokit.Repository();
2129
string owner;
2230
bool? isEnterprise;
2331

24-
public ApiClient(UriString hostUrl, ICredentialManager credentialManager, IGitHubClient githubClient, IGitHubClient githubAppClient)
32+
public ApiClient(UriString hostUrl, IKeychain keychain, IGitHubClient githubClient)
2533
{
2634
Guard.ArgumentNotNull(hostUrl, nameof(hostUrl));
27-
Guard.ArgumentNotNull(credentialManager, nameof(credentialManager));
35+
Guard.ArgumentNotNull(keychain, nameof(keychain));
2836
Guard.ArgumentNotNull(githubClient, nameof(githubClient));
2937

3038
HostAddress = HostAddress.Create(hostUrl);
3139
OriginalUrl = hostUrl;
40+
this.keychain = keychain;
3241
this.githubClient = githubClient;
33-
this.githubAppClient = githubAppClient;
34-
this.credentialManager = credentialManager;
35-
loginManager = new LoginManager(credentialManager, ApplicationInfo.ClientId, ApplicationInfo.ClientSecret);
42+
loginManager = new LoginManager(keychain, ApplicationInfo.ClientId, ApplicationInfo.ClientSecret);
3643
}
3744

3845
public async void GetRepository(Action<Octokit.Repository> callback)
@@ -54,6 +61,7 @@ public async Task Login(string username, string password, Action<LoginResult> ne
5461
}
5562
catch (Exception ex)
5663
{
64+
logger.Warning(ex);
5765
result(false, ex.Message);
5866
return;
5967
}
@@ -176,10 +184,12 @@ public async Task<bool> ValidateCredentials()
176184
{
177185
try
178186
{
179-
var credential = await credentialManager.Load(OriginalUrl);
180-
if (credential != null)
187+
var store = keychain.Connect(OriginalUrl);
188+
189+
if (store.OctokitCredentials != Credentials.Anonymous)
181190
{
182-
await githubAppClient.Authorization.CheckApplicationAuthentication(ApplicationInfo.ClientId, credential.Token);
191+
var credential = store.Credential;
192+
await githubClient.Authorization.CheckApplicationAuthentication(ApplicationInfo.ClientId, credential.Token);
183193
}
184194
}
185195
catch

src/GitHub.Api/Api/ApiClientFactory.cs

Lines changed: 0 additions & 42 deletions
This file was deleted.

src/GitHub.Api/Api/IApiClientFactory.cs

Lines changed: 0 additions & 8 deletions
This file was deleted.

src/GitHub.Api/ApplicationManagerBase.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ class ApplicationManagerBase : IApplicationManager
88
{
99
protected static readonly ILogging logger = Logging.GetLogger<IApplicationManager>();
1010

11+
private AppConfiguration appConfiguration;
1112
private RepositoryLocator repositoryLocator;
1213
private RepositoryManager repositoryManager;
1314

@@ -30,8 +31,7 @@ protected void Initialize(IUIDispatcher uiDispatcher)
3031

3132
Platform = new Platform(Environment, FileSystem, uiDispatcher);
3233
ProcessManager = new ProcessManager(Environment, Platform.GitEnvironment, CancellationToken);
33-
Platform.Initialize(ProcessManager);
34-
ApiClientFactory.Instance = new ApiClientFactory(new AppConfiguration(), Platform.CredentialManager);
34+
Platform.Initialize(Environment, ProcessManager);
3535
}
3636

3737
public virtual Task Run()
@@ -178,6 +178,14 @@ public void Dispose()
178178
Dispose(true);
179179
}
180180

181+
public AppConfiguration AppConfiguration
182+
{
183+
get
184+
{
185+
return appConfiguration ?? (appConfiguration = new AppConfiguration());
186+
}
187+
}
188+
181189
public virtual IEnvironment Environment { get; set; }
182190
public IFileSystem FileSystem { get; protected set; }
183191
public IPlatform Platform { get; protected set; }

src/GitHub.Api/Authentication/AppCredentialStore.cs

Lines changed: 0 additions & 22 deletions
This file was deleted.

src/GitHub.Api/Authentication/CredentialStore.cs

Lines changed: 0 additions & 25 deletions
This file was deleted.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System.Threading.Tasks;
2+
3+
namespace GitHub.Unity
4+
{
5+
interface IKeychain
6+
{
7+
KeychainAdapter Connect(UriString host);
8+
Task<KeychainAdapter> Load(UriString host);
9+
void Clear(UriString host);
10+
void Clear();
11+
Task Flush(UriString host);
12+
void UpdateToken(UriString host, string token);
13+
void Save(ICredential credential);
14+
void Initialize();
15+
bool HasKeys { get; }
16+
}
17+
}

src/GitHub.Api/Authentication/ILoginManager.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,6 @@ interface ILoginManager
2626
/// Logs out of GitHub server.
2727
/// </summary>
2828
/// <param name="hostAddress">The address of the server.</param>
29-
Task Logout(UriString hostAddress, IGitHubClient client);
29+
Task Logout(UriString hostAddress);
3030
}
3131
}

src/GitHub.Api/Authentication/IPlatform.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@ namespace GitHub.Unity
55
{
66
interface IPlatform
77
{
8-
Task<IPlatform> Initialize(IProcessManager processManager);
8+
Task<IPlatform> Initialize(IEnvironment environment, IProcessManager processManager);
99
IProcessEnvironment GitEnvironment { get; }
1010
ICredentialManager CredentialManager { get; }
1111
IEnvironment Environment { get; }
1212
IProcessManager ProcessManager { get; }
1313
IUIDispatcher UIDispatcher { get; }
14+
IKeychain Keychain { get; }
1415
}
1516
}

0 commit comments

Comments
 (0)