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

Commit

Permalink
Merge pull request #298 from github-for-unity/fixes/kepp-auth-if-user…
Browse files Browse the repository at this point in the history
…-does-not-match

Keeping the authentication if the username doesn't match in the keychain
  • Loading branch information
StanleyGoldman authored Sep 12, 2017
2 parents 06cdff2 + 0076abb commit 600faeb
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 81 deletions.
13 changes: 7 additions & 6 deletions src/GitHub.Api/Authentication/Keychain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,15 @@ public async Task<IKeychainAdapter> Load(UriString host)
logger.Warning("Cannot load host from Credential Manager; removing from cache");
await Clear(host, false);
}
else if (keychainItem.Username != cachedConnection.Username)
{
logger.Warning("Item loaded from credential manager does not match connection cache ; removing from cache");
await Clear(host, false);
}
else
{
logger.Trace($@"Loaded from Credential Manager Host:""{keychainItem.Host}"" Username:""{keychainItem.Username}""");
if (keychainItem.Username != cachedConnection.Username)
{
logger.Warning("Keychain Username: {0} does not match; Hopefully it works", keychainItem.Username);
}

logger.Trace("Loaded from Credential Manager Host:\"{0}\" Username:\"{1}\"", keychainItem.Host, keychainItem.Username);

keychainAdapter.Set(keychainItem);
}

Expand Down
75 changes: 0 additions & 75 deletions src/tests/UnitTests/Authentication/KeychainTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -247,81 +247,6 @@ public void ShouldDeleteFromCacheWhenLoadReturnsNullFromConnectionManager()
credentialManager.DidNotReceive().Save(Arg.Any<ICredential>());
}

[Test]
public void ShouldDeleteFromCacheWhenLoadReturnsNullFromConnectionManagerDueToUserMismatch()
{
const string connectionsCachePath = @"c:\UserCachePath\";
const string connectionsCacheFile = @"c:\UserCachePath\connections.json";

const string cachedUsername = "SomeCachedUser";
const string credentialedUsername = "SomeCredentialedUser";

const string token = "SomeToken";

var hostUri = new UriString("https://github.com/");

var fileSystem = SubstituteFactory.CreateFileSystem(new CreateFileSystemOptions
{
FilesThatExist = new List<string> { connectionsCacheFile },
FileContents = new Dictionary<string, IList<string>> {
{connectionsCacheFile, new List<string> {$@"[{{""Host"":""https://github.com/"",""Username"":""{cachedUsername}""}}]"
}}
}
});

NPath.FileSystem = fileSystem;

var environment = SubstituteFactory.CreateEnvironment();
environment.UserCachePath.Returns(info => connectionsCachePath.ToNPath());
environment.FileSystem.Returns(fileSystem);

var credentialManager = Substitute.For<ICredentialManager>();
credentialManager.Load(hostUri).Returns(info =>
{
var credential = Substitute.For<ICredential>();
credential.Username.Returns(credentialedUsername);
credential.Token.Returns(token);
credential.Host.Returns(hostUri);
return TaskEx.FromResult(credential);
});

var keychain = new Keychain(environment, credentialManager);
keychain.Initialize();

fileSystem.Received(1).FileExists(connectionsCacheFile);
fileSystem.DidNotReceive().FileDelete(Args.String);
fileSystem.Received(1).ReadAllText(connectionsCacheFile);
fileSystem.DidNotReceive().ReadAllLines(Args.String);
fileSystem.DidNotReceive().WriteAllText(Args.String, Args.String);
fileSystem.DidNotReceive().WriteAllLines(Args.String, Arg.Any<string[]>());

credentialManager.DidNotReceive().Load(Args.UriString);
credentialManager.DidNotReceive().HasCredentials();
credentialManager.DidNotReceive().Delete(Args.UriString);
credentialManager.DidNotReceive().Save(Arg.Any<ICredential>());

fileSystem.ClearReceivedCalls();

var uriString = keychain.Hosts.FirstOrDefault();
var keychainAdapter = keychain.Load(uriString).Result;
keychainAdapter.Credential.Should().BeNull();

keychainAdapter.OctokitCredentials.AuthenticationType.Should().Be(AuthenticationType.Anonymous);
keychainAdapter.OctokitCredentials.Login.Should().BeNull();
keychainAdapter.OctokitCredentials.Password.Should().BeNull();

fileSystem.DidNotReceive().FileExists(Args.String);
fileSystem.DidNotReceive().ReadAllText(Args.String);
fileSystem.DidNotReceive().FileDelete(Args.String);
fileSystem.Received(1).WriteAllText(connectionsCacheFile, "[]");
fileSystem.DidNotReceive().WriteAllLines(Args.String, Arg.Any<string[]>());

credentialManager.Received(1).Load(hostUri);
credentialManager.DidNotReceive().HasCredentials();
credentialManager.DidNotReceive().Delete(Args.UriString);
credentialManager.DidNotReceive().Save(Arg.Any<ICredential>());
}

[Test]
public void ShouldConnectSetCredentialsTokenAndSave()
{
Expand Down

0 comments on commit 600faeb

Please sign in to comment.