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

Commit b790d96

Browse files
committed
ILoginCache -> IKeychain
1 parent c1687a7 commit b790d96

File tree

4 files changed

+39
-39
lines changed

4 files changed

+39
-39
lines changed
Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,33 +5,33 @@
55
namespace GitHub.Api
66
{
77
/// <summary>
8-
/// Stores login details.
8+
/// Represents a keychain used to store credentials.
99
/// </summary>
10-
public interface ILoginCache
10+
public interface IKeychain
1111
{
1212
/// <summary>
13-
/// Gets the login details for the specified host address.
13+
/// Loads the credentials for the specified host address.
1414
/// </summary>
1515
/// <param name="hostAddress">The host address.</param>
1616
/// <returns>
17-
/// A task returning a tuple containing the retrieved username and password.
17+
/// A task returning a tuple consisting of the retrieved username and password.
1818
/// </returns>
19-
Task<Tuple<string, string>> GetLogin(HostAddress hostAddress);
19+
Task<Tuple<string, string>> Load(HostAddress hostAddress);
2020

2121
/// <summary>
22-
/// Saves the login details for the specified host address.
22+
/// Saves the credentials for the specified host address.
2323
/// </summary>
2424
/// <param name="userName">The username.</param>
2525
/// <param name="password">The password.</param>
2626
/// <param name="hostAddress">The host address.</param>
2727
/// <returns>A task tracking the operation.</returns>
28-
Task SaveLogin(string userName, string password, HostAddress hostAddress);
28+
Task Save(string userName, string password, HostAddress hostAddress);
2929

3030
/// <summary>
31-
/// Removes the login details for the specified host address.
31+
/// Deletes the login details for the specified host address.
3232
/// </summary>
3333
/// <param name="hostAddress"></param>
3434
/// <returns>A task tracking the operation.</returns>
35-
Task EraseLogin(HostAddress hostAddress);
35+
Task Delete(HostAddress hostAddress);
3636
}
3737
}

src/GitHub.Api/LoginManager.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ namespace GitHub.Api
1313
public class LoginManager : ILoginManager
1414
{
1515
readonly string[] scopes = { "user", "repo", "gist", "write:public_key" };
16-
readonly ILoginCache loginCache;
16+
readonly IKeychain loginCache;
1717
readonly ITwoFactorChallengeHandler twoFactorChallengeHandler;
1818
readonly string clientId;
1919
readonly string clientSecret;
@@ -30,7 +30,7 @@ public class LoginManager : ILoginManager
3030
/// <param name="authorizationNote">An note to store with the authorization.</param>
3131
/// <param name="fingerprint">The machine fingerprint.</param>
3232
public LoginManager(
33-
ILoginCache loginCache,
33+
IKeychain loginCache,
3434
ITwoFactorChallengeHandler twoFactorChallengeHandler,
3535
string clientId,
3636
string clientSecret,
@@ -64,7 +64,7 @@ public async Task<User> Login(
6464

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

6969
var newAuth = new NewAuthorization
7070
{
@@ -99,13 +99,13 @@ public async Task<User> Login(
9999
}
100100
else
101101
{
102-
await loginCache.EraseLogin(hostAddress).ConfigureAwait(false);
102+
await loginCache.Delete(hostAddress).ConfigureAwait(false);
103103
throw;
104104
}
105105
}
106106
} while (auth == null);
107107

108-
await loginCache.SaveLogin(userName, auth.Token, hostAddress).ConfigureAwait(false);
108+
await loginCache.Save(userName, auth.Token, hostAddress).ConfigureAwait(false);
109109

110110
var retry = 0;
111111

@@ -141,7 +141,7 @@ public async Task Logout(HostAddress hostAddress, IGitHubClient client)
141141
Guard.ArgumentNotNull(hostAddress, nameof(hostAddress));
142142
Guard.ArgumentNotNull(client, nameof(client));
143143

144-
await loginCache.EraseLogin(hostAddress);
144+
await loginCache.Delete(hostAddress);
145145
}
146146

147147
async Task<ApplicationAuthorization> CreateAndDeleteExistingApplicationAuthorization(
@@ -219,7 +219,7 @@ async Task<ApplicationAuthorization> HandleTwoFactorAuthorization(
219219
catch (Exception e)
220220
{
221221
await twoFactorChallengeHandler.ChallengeFailed(e);
222-
await loginCache.EraseLogin(hostAddress).ConfigureAwait(false);
222+
await loginCache.Delete(hostAddress).ConfigureAwait(false);
223223
throw;
224224
}
225225
}
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@
88
namespace GitHub.Api
99
{
1010
/// <summary>
11-
/// A login cache that stores logins in the windows credential cache.
11+
/// A keychain that stores logins in the windows credential store.
1212
/// </summary>
13-
[Export(typeof(ILoginCache))]
13+
[Export(typeof(IKeychain))]
1414
[PartCreationPolicy(CreationPolicy.Shared)]
15-
public class WindowsLoginCache : ILoginCache
15+
public class WindowsKeychain : IKeychain
1616
{
1717
/// <inheritdoc/>
18-
public Task<Tuple<string, string>> GetLogin(HostAddress hostAddress)
18+
public Task<Tuple<string, string>> Load(HostAddress hostAddress)
1919
{
2020
Guard.ArgumentNotNull(hostAddress, nameof(hostAddress));
2121

@@ -33,7 +33,7 @@ public Task<Tuple<string, string>> GetLogin(HostAddress hostAddress)
3333
}
3434

3535
/// <inheritdoc/>
36-
public Task SaveLogin(string userName, string password, HostAddress hostAddress)
36+
public Task Save(string userName, string password, HostAddress hostAddress)
3737
{
3838
Guard.ArgumentNotEmptyString(userName, nameof(userName));
3939
Guard.ArgumentNotEmptyString(password, nameof(password));
@@ -56,7 +56,7 @@ public Task SaveLogin(string userName, string password, HostAddress hostAddress)
5656
}
5757

5858
/// <inheritdoc/>
59-
public Task EraseLogin(HostAddress hostAddress)
59+
public Task Delete(HostAddress hostAddress)
6060
{
6161
Guard.ArgumentNotNull(hostAddress, nameof(hostAddress));
6262

src/UnitTests/GitHub.Api/LoginManagerTests.cs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@ public async Task LoginTokenIsSavedToCache()
2121
client.Authorization.GetOrCreateApplicationAuthentication("id", "secret", Arg.Any<NewAuthorization>())
2222
.Returns(new ApplicationAuthorization("123abc"));
2323

24-
var loginCache = Substitute.For<ILoginCache>();
24+
var loginCache = Substitute.For<IKeychain>();
2525
var tfa = Substitute.For<ITwoFactorChallengeHandler>();
2626

2727
var target = new LoginManager(loginCache, tfa, "id", "secret");
2828
await target.Login(host, client, "foo", "bar");
2929

30-
await loginCache.Received().SaveLogin("foo", "123abc", host);
30+
await loginCache.Received().Save("foo", "123abc", host);
3131
}
3232

3333
[Fact]
@@ -39,7 +39,7 @@ public async Task LoggedInUserIsReturned()
3939
.Returns(new ApplicationAuthorization("123abc"));
4040
client.User.Current().Returns(user);
4141

42-
var loginCache = Substitute.For<ILoginCache>();
42+
var loginCache = Substitute.For<IKeychain>();
4343
var tfa = Substitute.For<ITwoFactorChallengeHandler>();
4444

4545
var target = new LoginManager(loginCache, tfa, "id", "secret");
@@ -62,15 +62,15 @@ public async Task DeletesExistingAuthenticationIfNullTokenReturned()
6262
new ApplicationAuthorization("123abc"));
6363
client.User.Current().Returns(user);
6464

65-
var loginCache = Substitute.For<ILoginCache>();
65+
var loginCache = Substitute.For<IKeychain>();
6666
var tfa = Substitute.For<ITwoFactorChallengeHandler>();
6767

6868
var target = new LoginManager(loginCache, tfa, "id", "secret");
6969
var result = await target.Login(host, client, "foo", "bar");
7070

7171
await client.Authorization.Received(2).GetOrCreateApplicationAuthentication("id", "secret", Arg.Any<NewAuthorization>());
7272
await client.Authorization.Received(1).Delete(0);
73-
await loginCache.Received().SaveLogin("foo", "123abc", host);
73+
await loginCache.Received().Save("foo", "123abc", host);
7474
}
7575

7676
[Fact]
@@ -84,7 +84,7 @@ public async Task TwoFactorExceptionIsPassedToHandler()
8484
client.Authorization.GetOrCreateApplicationAuthentication("id", "secret", Arg.Any<NewAuthorization>(), "123456")
8585
.Returns(new ApplicationAuthorization("123abc"));
8686

87-
var loginCache = Substitute.For<ILoginCache>();
87+
var loginCache = Substitute.For<IKeychain>();
8888
var tfa = Substitute.For<ITwoFactorChallengeHandler>();
8989
tfa.HandleTwoFactorException(exception).Returns(new TwoFactorChallengeResult("123456"));
9090

@@ -111,7 +111,7 @@ public async Task Failed2FACodeResultsInRetry()
111111
client.Authorization.GetOrCreateApplicationAuthentication("id", "secret", Arg.Any<NewAuthorization>(), "123456")
112112
.Returns(new ApplicationAuthorization("123abc"));
113113

114-
var loginCache = Substitute.For<ILoginCache>();
114+
var loginCache = Substitute.For<IKeychain>();
115115
var tfa = Substitute.For<ITwoFactorChallengeHandler>();
116116
tfa.HandleTwoFactorException(exception).Returns(
117117
new TwoFactorChallengeResult("111111"),
@@ -146,7 +146,7 @@ public async Task HandlerNotifiedOfExceptionIn2FAChallengeResponse()
146146
client.Authorization.GetOrCreateApplicationAuthentication("id", "secret", Arg.Any<NewAuthorization>(), "111111")
147147
.Returns<ApplicationAuthorization>(_ => { throw loginAttemptsException; });
148148

149-
var loginCache = Substitute.For<ILoginCache>();
149+
var loginCache = Substitute.For<IKeychain>();
150150
var tfa = Substitute.For<ITwoFactorChallengeHandler>();
151151
tfa.HandleTwoFactorException(twoFaException).Returns(
152152
new TwoFactorChallengeResult("111111"),
@@ -176,7 +176,7 @@ public async Task RequestResendCodeResultsInRetryingLogin()
176176
.Returns(new ApplicationAuthorization("456def"));
177177
client.User.Current().Returns(user);
178178

179-
var loginCache = Substitute.For<ILoginCache>();
179+
var loginCache = Substitute.For<IKeychain>();
180180
var tfa = Substitute.For<ITwoFactorChallengeHandler>();
181181
tfa.HandleTwoFactorException(exception).Returns(
182182
TwoFactorChallengeResult.RequestResendCode,
@@ -201,13 +201,13 @@ public async Task UsesUsernameAndPasswordInsteadOfAuthorizationTokenWhenEnterpri
201201
});
202202
client.User.Current().Returns(user);
203203

204-
var loginCache = Substitute.For<ILoginCache>();
204+
var loginCache = Substitute.For<IKeychain>();
205205
var tfa = Substitute.For<ITwoFactorChallengeHandler>();
206206

207207
var target = new LoginManager(loginCache, tfa, "id", "secret");
208208
await target.Login(enterprise, client, "foo", "bar");
209209

210-
await loginCache.Received().SaveLogin("foo", "bar", enterprise);
210+
await loginCache.Received().Save("foo", "bar", enterprise);
211211
}
212212

213213
[Fact]
@@ -219,13 +219,13 @@ public async Task ErasesLoginWhenUnauthorized()
219219
client.Authorization.GetOrCreateApplicationAuthentication("id", "secret", Arg.Any<NewAuthorization>())
220220
.Returns<ApplicationAuthorization>(_ => { throw new AuthorizationException(); });
221221

222-
var loginCache = Substitute.For<ILoginCache>();
222+
var loginCache = Substitute.For<IKeychain>();
223223
var tfa = Substitute.For<ITwoFactorChallengeHandler>();
224224

225225
var target = new LoginManager(loginCache, tfa, "id", "secret");
226226
await Assert.ThrowsAsync<AuthorizationException>(async () => await target.Login(enterprise, client, "foo", "bar"));
227227

228-
await loginCache.Received().EraseLogin(enterprise);
228+
await loginCache.Received().Delete(enterprise);
229229
}
230230

231231
[Fact]
@@ -237,13 +237,13 @@ public async Task ErasesLoginWhenNonOctokitExceptionThrown()
237237
client.Authorization.GetOrCreateApplicationAuthentication("id", "secret", Arg.Any<NewAuthorization>())
238238
.Returns<ApplicationAuthorization>(_ => { throw new InvalidOperationException(); });
239239

240-
var loginCache = Substitute.For<ILoginCache>();
240+
var loginCache = Substitute.For<IKeychain>();
241241
var tfa = Substitute.For<ITwoFactorChallengeHandler>();
242242

243243
var target = new LoginManager(loginCache, tfa, "id", "secret");
244244
await Assert.ThrowsAsync<InvalidOperationException>(async () => await target.Login(host, client, "foo", "bar"));
245245

246-
await loginCache.Received().EraseLogin(host);
246+
await loginCache.Received().Delete(host);
247247
}
248248

249249
[Fact]
@@ -259,14 +259,14 @@ public async Task ErasesLoginWhenNonOctokitExceptionThrownIn2FA()
259259
.Returns<ApplicationAuthorization>(_ => { throw new InvalidOperationException(); });
260260
client.User.Current().Returns(user);
261261

262-
var loginCache = Substitute.For<ILoginCache>();
262+
var loginCache = Substitute.For<IKeychain>();
263263
var tfa = Substitute.For<ITwoFactorChallengeHandler>();
264264
tfa.HandleTwoFactorException(exception).Returns(new TwoFactorChallengeResult("123456"));
265265

266266
var target = new LoginManager(loginCache, tfa, "id", "secret");
267267
await Assert.ThrowsAsync<InvalidOperationException>(async () => await target.Login(host, client, "foo", "bar"));
268268

269-
await loginCache.Received().EraseLogin(host);
269+
await loginCache.Received().Delete(host);
270270
}
271271
}
272272
}

0 commit comments

Comments
 (0)