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

Commit 83c2e9f

Browse files
committed
Fix login after failure and 2fa too
1 parent 4316a6d commit 83c2e9f

File tree

5 files changed

+35
-28
lines changed

5 files changed

+35
-28
lines changed

src/GitHub.Api/Authentication/IKeychain.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ interface IKeychain
66
{
77
KeychainAdapter Connect(UriString host);
88
Task<KeychainAdapter> Load(UriString host);
9-
Task Clear(UriString host);
9+
void Clear(UriString host);
10+
void Clear();
1011
Task Flush(UriString host);
1112
void UpdateToken(UriString host, string token);
1213
void Save(ICredential credential);
1314
void Initialize();
1415
bool HasKeys { get; }
15-
Task ClearAll();
1616
}
1717
}

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/Keychain.cs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -129,30 +129,30 @@ private void WriteCacheToDisk()
129129
cachePath.WriteAllText(json);
130130
}
131131

132-
public async Task Clear(UriString host)
132+
/// <summary>
133+
/// Call Flush() to apply these changes
134+
/// </summary>
135+
public void Clear(UriString host)
133136
{
134137
logger.Trace("Clear Host:{0}", host);
135138

136139
// delete connection in the connection list
137140
connectionCache.Remove(host);
138-
WriteCacheToDisk();
139141

140142
// delete credential in octokit store
141-
keychainAdapters.Remove(host);
142-
143-
// delete credential from credential manager
144-
await credentialManager.Delete(host);
143+
FindOrCreateAdapter(host).Clear();
145144
}
146145

147-
public async Task ClearAll()
146+
/// <summary>
147+
/// Call Flush() to apply these changes
148+
/// </summary>
149+
public void Clear()
148150
{
149-
var uriStrings = keychainAdapters.Keys.Union(connectionCache.Keys).Distinct().ToArray();
150-
logger.Trace("ClearAll Count:{0}", uriStrings.Length);
151-
152-
foreach (var uriString in uriStrings)
151+
foreach (var k in keychainAdapters.Values.ToArray())
153152
{
154-
await Clear(uriString);
153+
k.Clear();
155154
}
155+
connectionCache.Clear();
156156
}
157157

158158
public async Task Flush(UriString host)
@@ -171,6 +171,8 @@ public async Task Flush(UriString host)
171171
}
172172

173173
// create new connection in the connection cache for this host
174+
if (connectionCache.ContainsKey(host))
175+
connectionCache.Remove(host);
174176
connectionCache.Add(host, new Connection { Host = host, Username = credentialAdapter.OctokitCredentials.Login });
175177

176178
// flushes credential cache to disk (host and username only)

src/GitHub.Api/Authentication/KeychainAdapter.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ public void UpdateToken(string token)
1919
Credential.UpdateToken(token);
2020
}
2121

22+
public void Clear()
23+
{
24+
OctokitCredentials = Credentials.Anonymous;
25+
}
26+
2227
/// <summary>
2328
/// Implementation for Octokit
2429
/// </summary>

src/GitHub.Api/Authentication/LoginManager.cs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -110,15 +110,15 @@ public async Task<LoginResultData> Login(
110110
{
111111
logger.Warning(e, "Login LoginAttemptsExceededException: {0}", e.Message);
112112

113-
await keychain.Clear(host);
113+
keychain.Clear(host);
114114
return new LoginResultData(LoginResultCodes.LockedOut, Localization.LockedOut, host);
115115
}
116116
catch (ApiValidationException e)
117117
{
118118
logger.Warning(e, "Login ApiValidationException: {0}", e.Message);
119119

120120
var message = e.ApiError.FirstErrorMessageSafe();
121-
await keychain.Clear(host);
121+
keychain.Clear(host);
122122
return new LoginResultData(LoginResultCodes.Failed, message, host);
123123
}
124124
catch (Exception e)
@@ -134,7 +134,7 @@ public async Task<LoginResultData> Login(
134134
}
135135
else
136136
{
137-
await keychain.Clear(host);
137+
keychain.Clear(host);
138138
return new LoginResultData(LoginResultCodes.Failed, Localization.LoginFailed, host);
139139
}
140140
}
@@ -161,11 +161,11 @@ public async Task<LoginResultData> ContinueLogin(LoginResultData loginResultData
161161
twofacode);
162162
EnsureNonNullAuthorization(auth);
163163

164-
var credentialStore = await keychain.Load(host);
165-
var credentials = credentialStore.Credential;
166-
credentials.UpdateToken(auth.Token);
167-
168-
keychain.Save(credentials);
164+
165+
var keychainAdapter = keychain.Connect(host);
166+
keychainAdapter.UpdateToken(auth.Token);
167+
await keychain.Flush(host);
168+
169169
return new LoginResultData(LoginResultCodes.Success, "", host);
170170
}
171171
catch (TwoFactorAuthorizationException e)
@@ -179,27 +179,27 @@ public async Task<LoginResultData> ContinueLogin(LoginResultData loginResultData
179179
logger.Debug(e, "2FA ApiValidationException: {0}", e.Message);
180180

181181
var message = e.ApiError.FirstErrorMessageSafe();
182-
await keychain.Clear(host);
182+
keychain.Clear(host);
183183
return new LoginResultData(LoginResultCodes.Failed, message, host);
184184
}
185185
catch (Exception e)
186186
{
187187
logger.Debug(e, "Exception: {0}", e.Message);
188188

189-
await keychain.Clear(host);
189+
keychain.Clear(host);
190190
return new LoginResultData(LoginResultCodes.Failed, e.Message, host);
191191
}
192192
}
193193

194194
/// <inheritdoc/>
195-
public async Task Logout(UriString hostAddress, IGitHubClient client)
195+
public async Task Logout(UriString hostAddress)
196196
{
197197
Guard.ArgumentNotNull(hostAddress, nameof(hostAddress));
198-
Guard.ArgumentNotNull(client, nameof(client));
199198

200199
logger.Trace("Logout");
201200

202-
await keychain.Clear(hostAddress);
201+
keychain.Clear(hostAddress);
202+
await keychain.Flush(hostAddress);
203203
}
204204

205205
private async Task<ApplicationAuthorization> CreateAndDeleteExistingApplicationAuthorization(

0 commit comments

Comments
 (0)