8000 Master PR for refactoring connections by grokys · Pull Request #1277 · github/VisualStudio · GitHub
[go: up one dir, main page]

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

Master PR for refactoring connections #1277

Merged
merged 43 commits into from
Nov 21, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
b790d96
ILoginCache -> IKeychain
grokys Aug 25, 2017
1c81104
Use IKeychain for credentials.
grokys Aug 25, 2017
21f888e
Updated parameter name.
grokys Aug 25, 2017
7e061d1
Removed unneeded usings.
grokys Aug 25, 2017
c8b8dd7
Updated variable names.
grokys Aug 25, 2017
95f2c0b
Remove `IObservableKeychainAdapter`.
grokys Aug 25, 2017
7b4080f
Merge branch 'master' into fixes/1176-duplicate-login-classes
grokys Aug 30, 2017
3b13736
Store "master" credential.
grokys Aug 30, 2017
a142423
Merge branch 'master' into fixes/1176-duplicate-login-classes
grokys Sep 7, 2017
bcfbd79
Fix broken merge.
grokys Sep 7, 2017
c52a261
Merge branch 'fixes/1176-duplicate-login-classes' into fixes/harden-c…
grokys Sep 7, 2017
e6c5d89
Merge branch 'master' into fixes/1176-duplicate-login-classes
grokys Oct 20, 2017
17c845e
Merge branch 'fixes/1176-duplicate-login-classes' into fixes/harden-c…
grokys Oct 20, 2017
a627271
Merge branch 'master' into refactor/connections-master
grokys Oct 20, 2017
470ea13
Removed `IConnection.Repositories`.
grokys Oct 20, 2017
d98ec51
Refactored ConnectionManager/Connection.
grokys Oct 23, 2017
aa78f03
Refactored RepositoryHost(s).
grokys Oct 23, 2017
7cf89c9
Added DebuggerDisplay back in.
grokys Oct 23, 2017
213a7fc
Moved LocalRepositories to GitHub.VisualStudio.
grokys Oct 24, 2017
90c35c5
Track login metrics.
grokys Oct 26, 2017
9db05a7
Fix failing tests.
grokys Oct 26, 2017
f854b79
WIP: Removing RepositoryHost(s)
grokys Oct 26, 2017
3240372
WIP: Still removing RepositoryHost(s)
grokys Oct 26, 2017
038f232
WIP: Removing RepositoryHost(s) from tests.
grokys Oct 26, 2017
a57a759
Finished migrating tests (I think).
grokys Oct 27, 2017
95cd0b1
Added IModelServiceFactory implemenation.
grokys Oct 27, 2017
6034643
Added IGlobalConnection.
grokys Oct 27, 2017
17aebf8
Removed ConnectionManager.ConnectionCreated.
grokys Oct 27, 2017
00232f0
Merge branch 'master' into refactor/connections-master
grokys Nov 3, 2017
01aad33
Merge branch 'refactor/connections-master' into refactor/connections/…
grokys Nov 3, 2017
217e415
Merge branch 'refactor/connections/local-repositories' into refactor/…
grokys Nov 3, 2017
7e57621
Insert AccountCacheItem for connection.
grokys Nov 7, 2017
e45d0c6
Merge branch 'master' into refactor/connections-master
grokys Nov 7, 2017
8f6d218
Merge branch 'refactor/connections-master' into refactor/connections/…
grokys Nov 7, 2017
32f073a
Merge branch 'refactor/connections/local-repositories' into refactor/…
grokys Nov 7, 2017
9f15db6
Merge branch 'refactor/connection-manager' into refactor/connections/…
grokys Nov 7, 2017
cd546bb
Fix CA errors.
grokys Nov 7, 2017
def6070
Merge pull request #1283 from github/refactor/connections/remove-repo…
grokys Nov 8, 2017
fc57163
Merge branch 'master' into refactor/connections-master
grokys Nov 8, 2017
d9c2a2f
Removed empty file.
grokys Nov 8, 2017
cefd4b9
Merge branch 'master' into refactor/connections-master
grokys Nov 14, 2017
7d7b188
Merge branch 'master' into refactor/connections-master
grokys Nov 20, 2017
4701dd0
Merge branch 'master' into refactor/connections-master
grokys Nov 20, 2017
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/CredentialManagement/Credential.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,28 @@ public Credential(
_lastWriteTime = DateTime.MinValue;
}

public static Credential Load(string key)
{
var result = new Credential();
result.Target = key;
result.Type = CredentialType.Generic;
return result.Load() ? result : null;
}

public static void Save(string key, string username, string password)
{
var result = new Credential(username, password, key);
result.Save();
}

public static void Delete(string key)
{
var result = new Credential();
result.Target = key;
result.Type = CredentialType.Generic;
result.Delete();
}

bool disposed;
void Dispose(bool disposing)
{
Expand Down
6 changes: 3 additions & 3 deletions src/GitHub.Api/GitHub.Api.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,12 @@
<Link>ApiClientConfiguration_User.cs</Link>
</Compile>
<Compile Include="ApiClientConfiguration_User.cs" Condition="$(Buildtype) != 'Internal'" />
<Compile Include="ILoginCache.cs" />
<Compile Include="IKeychain.cs" />
<Compile Include="ILoginManager.cs" />
<Compile Include="ITwoFactorChallengeHandler.cs" />
<Compile Include="LoginManager.cs" />
<Compile Include="SimpleCredentialStore.cs" />
<Compile Include="WindowsLoginCache.cs" />
<Compile Include="KeychainCredentialStore.cs" />
<Compile Include="WindowsKeychain.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="..\common\SolutionInfo.cs">
<Link>Properties\SolutionInfo.cs</Link>
Expand Down
38 changes: 38 additions & 0 deletions src/GitHub.Api/IKeychain.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System;
using System.Threading.Tasks;
using GitHub.Primitives;

namespace GitHub.Api
{
/// <summary>
/// Represents a keychain used to store credentials.
/// </summary>
public interface IKeychain
{
/// <summary>
/// Loads the credentials for the specified host address.
/// </summary>
/// <param name="address">The host address.</param>
/// <returns>
/// A task returning a tuple consisting of the retrieved username and password or null
/// if the credentials were not found.
/// </returns>
Task<Tuple<string, string>> Load(HostAddress address);

/// <summary>
/// Saves the credentials for the specified host address.
/// </summary>
/// <param name="userName">The username.</param>
/// <param name="password">The password.</param>
/// <param name="address">The host address.</param>
/// <returns>A task tracking the operation.</returns>
Task Save(string userName, string password, HostAddress address);

/// <summary>
/// Deletes the login details for the specified host address.
/// </summary>
/// <param name="address"></param>
/// <returns>A task tracking the operation.</returns>
Task Delete(HostAddress address);
}
}
37 changes: 0 additions & 37 deletions src/GitHub.Api/ILoginCache.cs

This file was deleted.

32 changes: 32 additions & 0 deletions src/GitHub.Api/KeychainCredentialStore.cs
Ori F438 ginal file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System;
using System.Threading.Tasks;
using GitHub.Extensions;
using GitHub.Primitives;
using Octokit;

namespace GitHub.Api
{
/// <summary>
/// An Octokit credential store that reads from an <see cref="IKeychain"/>.
/// </summary>
public class KeychainCredentialStore : ICredentialStore
{
readonly IKeychain keychain;
readonly HostAddress address;

public KeychainCredentialStore(IKeychain keychain, HostAddress address)
{
Guard.ArgumentNotNull(keychain, nameof(keychain));
Guard.ArgumentNotNull(address, nameof(keychain));

this.keychain = keychain;
this.address = address;
}

public async Task<Credentials> GetCredentials()
{
var userPass = await keychain.Load(address).ConfigureAwait(false);
return userPass != null ? new Credentials(userPass.Item1, userPass.Item2) : Credentials.Anonymous;
}
}
}
20 changes: 10 additions & 10 deletions src/GitHub.Api/LoginManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace GitHub.Api
public class LoginManager : ILoginManager
{
readonly string[] scopes = { "user", "repo", "gist", "write:public_key" };
readonly ILoginCache loginCache;
readonly IKeychain keychain;
readonly ITwoFactorChallengeHandler twoFactorChallengeHandler;
readonly string clientId;
readonly string clientSecret;
Expand All @@ -23,26 +23,26 @@ public class LoginManager : ILoginManager
/// <summary>
/// Initializes a new instance of the <see cref="LoginManager"/> class.
/// </summary>
/// <param name="loginCache">The cache in which to store login details.</param>
/// <param name="keychain">The keychain in which to store credentials.</param>
/// <param name="twoFactorChallengeHandler">The handler for 2FA challenges.</param>
/// <param name="clientId">The application's client API ID.</param>
/// <param name="clientSecret">The application's client API secret.</param>
/// <param name="authorizationNote">An note to store with the authorization.</param>
/// <param name="fingerprint">The machine fingerprint.</param>
public LoginManager(
ILoginCache loginCache,
IKeychain keychain,
ITwoFactorChallengeHandler twoFactorChallengeHandler,
string clientId,
string clientSecret,
string authorizationNote = null,
string fingerprint = null)
{
Guard.ArgumentNotNull(loginCache, nameof(loginCache));
Guard.ArgumentNotNull(keychain, nameof(keychain));
Guard.ArgumentNotNull(twoFactorChallengeHandler, nameof(twoFactorChallengeHandler));
Guard.ArgumentNotEmptyString(clientId, nameof(clientId));
Guard.ArgumentNotEmptyString(clientSecret, nameof(clientSecret));

this.loginCache = loginCache;
this.keychain = keychain;
this.twoFactorChallengeHandler = twoFactorChallengeHandler;
this.clientId = clientId;
this.clientSecret = clientSecret;
Expand All @@ -64,7 +64,7 @@ public async Task<User> Login(

// Start by saving the username and password, these will be used by the `IGitHubClient`
// until an authorization token has been created and acquired:
await loginCache.SaveLogin(userName, password, hostAddress).ConfigureAwait(false);
await keychain.Save(userName, password, hostAddress).ConfigureAwait(false);

var newAuth = new NewAuthorization
{
Expand Down Expand Up @@ -99,13 +99,13 @@ public async Task<User> Login(
}
else
{
await loginCache.EraseLogin(hostAddress).ConfigureAwait(false);
await keychain.Delete(hostAddress).ConfigureAwait(false);
throw;
}
}
} while (auth == null);

await loginCache.SaveLogin(userName, auth.Token, hostAddress).ConfigureAwait(false);
await keychain.Save(userName, auth.Token, hostAddress).ConfigureAwait(false);

var retry = 0;

Expand Down Expand Up @@ -141,7 +141,7 @@ public async Task Logout(HostAddress hostAddress, IGitHubClient client)
Guard.ArgumentNotNull(hostAddress, nameof(hostAddress));
Guard.ArgumentNotNull(client, nameof(client));

await loginCache.EraseLogin(hostAddress);
await keychain.Delete(hostAddress);
}

async Task<ApplicationAuthorization> CreateAndDeleteExistingApplicationAuthorization(
Expand Down Expand Up @@ -219,7 +219,7 @@ async Task<ApplicationAuthorization> HandleTwoFactorAuthorization(
catch (Exception e)
{
await twoFactorChallengeHandler.ChallengeFailed(e);
await loginCache.EraseLogin(hostAddress).ConfigureAwait(false);
await keychain.Delete(hostAddress).ConfigureAwait(false);
throw;
}
}
Expand Down
12 changes: 9 additions & 3 deletions src/GitHub.Api/SimpleApiClientFactory.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using GitHub.Models;
using GitHub.Primitives;
Expand All @@ -14,15 +13,21 @@ namespace GitHub.Api
[PartCreationPolicy(CreationPolicy.Shared)]
public class SimpleApiClientFactory : ISimpleApiClientFactory
{
readonly IKeychain keychain;
readonly ProductHeaderValue productHeader;
readonly Lazy<IEnterpriseProbeTask> lazyEnterpriseProbe;
readonly Lazy<IWikiProbe> lazyWikiProbe;

static readonly ConcurrentDictionary<UriString, ISimpleApiClient> cache = new ConcurrentDictionary<UriString, ISimpleApiClient>();

[ImportingConstructor]
public SimpleApiClientFactory(IProgram program, Lazy<IEnterpriseProbeTask> enterpriseProbe, Lazy<IWikiProbe> wikiProbe)
public SimpleApiClientFactory(
IProgram program,
IKeychain keychain,
Lazy<IEnterpriseProbeTask> enterpriseProbe,
Lazy<IWikiProbe> wikiProbe)
{
this.keychain = keychain;
productHeader = program.ProductHeader;
lazyEnterpriseProbe = enterpriseProbe;
lazyWikiProbe = wikiProbe;
Expand All @@ -31,8 +36,9 @@ public SimpleApiClientFactory(IProgram program, Lazy<IEnterpriseProbeTask> enter
public Task<ISimpleApiClient> Create(UriString repositoryUrl)
{
var hostAddress = HostAddress.Create(repositoryUrl);
var credentialStore = new KeychainCredentialStore(keychain, hostAddress);
var result = cache.GetOrAdd(repositoryUrl, new SimpleApiClient(repositoryUrl,
new GitHubClient(productHeader, new SimpleCredentialStore(hostAddress), hostAddress.ApiUri),
new GitHubClient(productHeader, credentialStore, hostAddress.ApiUri),
lazyEnterpriseProbe, lazyWikiProbe));
return Task.FromResult(result);
}
Expand Down
83 changes: 0 additions & 83 deletions src/GitHub.Api/SimpleCredentialStore.cs

This file was deleted.

Loading
0