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

Commit 8b9740d

Browse files
Simplifying LoginManager
1 parent 887514f commit 8b9740d

File tree

2 files changed

+43
-52
lines changed

2 files changed

+43
-52
lines changed

src/GitHub.Api/Authentication/LoginManager.cs

Lines changed: 40 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -74,22 +74,23 @@ public async Task<LoginResultData> Login(
7474
keychain.Connect(host);
7575
keychain.SetCredentials(new Credential(host, username, password));
7676

77-
string token;
7877
try
7978
{
80-
token = await TryLogin(host, username, password);
81-
if (string.IsNullOrEmpty(token))
79+
var loginResultData = await TryLogin(host, username, password);
80+
if (loginResultData.Code == LoginResultCodes.Success || loginResultData.Code == LoginResultCodes.CodeRequired)
8281
{
83-
throw new InvalidOperationException("Returned token is null or empty");
82+
if (string.IsNullOrEmpty(loginResultData.Token))
83+
{
84+
throw new InvalidOperationException("Returned token is null or empty");
85+
}
86+
87+
keychain.SetToken(host, loginResultData.Token);
88+
await keychain.Save(host);
89+
90+
return loginResultData;
8491
}
85-
}
86-
catch (TwoFactorRequiredException e)
87-
{
88-
LoginResultCodes result;
89-
result = LoginResultCodes.CodeRequired;
90-
logger.Trace("2FA TwoFactorAuthorizationException: {0} {1}", LoginResultCodes.CodeRequired, e.Message);
9192

92-
return new LoginResultData(result, e.Message, host, password);
93+
return loginResultData;
9394
}
9495
catch (Exception e)
9596
{
@@ -98,41 +99,40 @@ public async Task<LoginResultData> Login(
9899
await keychain.Clear(host, false);
99100
return new LoginResultData(LoginResultCodes.Failed, Localization.LoginFailed, host);
100101
}
101-
102-
keychain.SetToken(host, token);
103-
await keychain.Save(host);
104-
105-
return new LoginResultData(LoginResultCodes.Success, "Success", host);
106102
}
107103

108104
public async Task<LoginResultData> ContinueLogin(LoginResultData loginResultData, string twofacode)
109105
{
110-
var token = loginResultData.Token;
111106
var host = loginResultData.Host;
112107
var keychainAdapter = keychain.Connect(host);
113108
var username = keychainAdapter.Credential.Username;
114109
var password = keychainAdapter.Credential.Token;
115110
try
116111
{
117112
logger.Trace("2FA Continue");
118-
token = await TryContinueLogin(host, username, password, twofacode);
113+
loginResultData = await TryContinueLogin(host, username, password, twofacode);
119114

120-
if (string.IsNullOrEmpty(token))
115+
if (loginResultData.Code == LoginResultCodes.Success)
121116
{
122-
throw new InvalidOperationException("Returned token is null or empty");
123-
}
117+
if (string.IsNullOrEmpty(loginResultData.Token))
118+
{
119+
throw new InvalidOperationException("Returned token is null or empty");
120+
}
124121

125-
keychain.SetToken(host, token);
126-
await keychain.Save(host);
122+
keychain.SetToken(host, loginResultData.Token);
123+
await keychain.Save(host);
127124

128-
return new LoginResultData(LoginResultCodes.Success, "", host);
125+
return loginResultData;
126+
}
127+
128+
return loginResultData;
129129
}
130130
catch (Exception e)
131131
{
132-
logger.Trace(e, "Exception: {0}", e.Message);
132+
logger.Warning(e, "Login Exception");
133133

134134
await keychain.Clear(host, false);
135-
return new LoginResultData(LoginResultCodes.Failed, e.Message, host);
135+
return new LoginResultData(LoginResultCodes.Failed, Localization.LoginFailed, host);
136136
}
137137
}
138138

@@ -144,7 +144,7 @@ public async Task Logout(UriString hostAddress)
144144
await new ActionTask(keychain.Clear(hostAddress, true)).StartAwait();
145145
}
146146

147-
private async Task<string> TryLogin(
147+
private async Task<LoginResultData> TryLogin(
148148
UriString host,
149149
string username,
150150
string password
@@ -174,35 +174,33 @@ string password
174174

175175
if (ret.IsSuccess)
176176
{
177-
return ret.Output[0];
177+
return new LoginResultData(LoginResultCodes.Success, null, host, ret.Output[0]);
178178
}
179179

180180
if (ret.IsTwoFactorRequired)
181181
{
182-
keychain.SetToken(host, ret.Output[0]);
183-
await keychain.Save(host);
184-
throw new TwoFactorRequiredException();
182+
return new LoginResultData(LoginResultCodes.CodeRequired, "Two Factor Required.", host, ret.Output[0]);
185183
}
186184

187185
if (ret.IsBadCredentials)
188186
{
189-
throw new Exception("Bad credentials.");
187+
return new LoginResultData(LoginResultCodes.Failed, "Bad credentials.", host, ret.Output[0]);
190188
}
191189

192190
if (ret.IsLocked)
193191
{
194-
throw new Exception("Account locked.");
192+
return new LoginResultData(LoginResultCodes.LockedOut, "Account locked.", host, ret.Output[0]);
195193
}
196194

197195
if (ret.Output.Any())
198196
{
199-
throw new Exception(string.Join(Environment.NewLine, ret.Output));
197+
return new LoginResultData(LoginResultCodes.Failed, "Failed.", host, ret.Output[0]);
200198
}
201199

202-
throw new Exception("Authentication failed");
200+
return new LoginResultData(LoginResultCodes.Failed, "Failed.", host);
203201
}
204202

205-
private async Task<string> TryContinueLogin(
203+
private async Task<LoginResultData> TryContinueLogin(
206204
UriString host,
207205
string username,
208206
string password,
@@ -234,32 +232,30 @@ string code
234232

235233
if (ret.IsSuccess)
236234
{
237-
return ret.Output[0];
235+
return new LoginResultData(LoginResultCodes.Success, null, host, ret.Output[0]);
238236
}
239237

240238
if (ret.IsTwoFactorRequired)
241239
{
242-
keychain.SetToken(host, ret.Output[0]);
243-
await keychain.Save(host);
244-
throw new TwoFactorRequiredException();
240+
return new LoginResultData(LoginResultCodes.CodeFailed, "Incorrect code. Two Factor Required.", host, ret.Output[0]);
245241
}
246242

247243
if (ret.IsBadCredentials)
248244
{
249-
throw new Exception("Bad credentials.");
245+
return new LoginResultData(LoginResultCodes.Failed, "Bad credentials.", host, ret.Output[0]);
250246
}
251247

252248
if (ret.IsLocked)
253249
{
254-
throw new Exception("Account locked.");
250+
return new LoginResultData(LoginResultCodes.LockedOut, "Account locked.", host, ret.Output[0]);
255251
}
256252

257253
if (ret.Output.Any())
258254
{
259-
throw new Exception(string.Join(Environment.NewLine, ret.Output));
255+
return new LoginResultData(LoginResultCodes.Failed, "Failed.", host, ret.Output[0]);
260256
}
261257

262-
throw new Exception("Authentication failed");
258+
return new LoginResultData(LoginResultCodes.Failed, "Failed.", host);
263259
}
264260
}
265261

@@ -284,10 +280,4 @@ internal LoginResultData(LoginResultCodes code, string message, UriString host)
284280
{
285281
}
286282
}
287-
288-
class TwoFactorRequiredException : Exception
289-
{
290-
public TwoFactorRequiredException() : base("Two-Factor authentication required.")
291-
{ }
292-
}
293283
}

src/GitHub.Api/Tasks/OctorunTask.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Diagnostics;
4+
using System.Linq;
45
using System.Threading;
56
using GitHub.Logging;
67

@@ -124,7 +125,7 @@ public OctorunResult(string status, string[] output)
124125
public bool IsSuccess => Status.Equals("success", StringComparison.InvariantCultureIgnoreCase);
125126
public bool IsError => Status.Equals("error", StringComparison.InvariantCultureIgnoreCase);
126127
public bool IsTwoFactorRequired => Status.Equals("2fa", StringComparison.InvariantCultureIgnoreCase);
127-
public bool IsLocked => Status.Equals("locked", StringComparison.InvariantCultureIgnoreCase);
128-
public bool IsBadCredentials => Status.Equals("badcredentials", StringComparison.InvariantCultureIgnoreCase);
128+
public bool IsLocked => Output.First().Equals("locked", StringComparison.InvariantCultureIgnoreCase);
129+
public bool IsBadCredentials => Output.First().Equals("badcredentials", StringComparison.InvariantCultureIgnoreCase);
129130
}
130131
}

0 commit comments

Comments
 (0)