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

Commit f435e4d

Browse files
Conslidating TryLogin and TryContinueLogin
1 parent 7fe7205 commit f435e4d

File tree

1 file changed

+14
-61
lines changed

1 file changed

+14
-61
lines changed

src/GitHub.Api/Authentication/LoginManager.cs

Lines changed: 14 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ public async Task<LoginResultData> ContinueLogin(LoginResultData loginResultData
110110
try
111111
{
112112
logger.Trace("2FA Continue");
113-
loginResultData = await TryContinueLogin(host, username, password, twofacode);
113+
loginResultData = await TryLogin(host, username, password, twofacode);
114114

115115
if (loginResultData.Code == LoginResultCodes.Success)
116116
{
@@ -145,66 +145,10 @@ public async Task Logout(UriString hostAddress)
145145
}
146146

147147
private async Task<LoginResultData> TryLogin(
148-
UriString host,
149-
string username,
150-
string password
151-
)
152-
{
153-
if (!nodeJsExecutablePath.HasValue)
154-
{
155-
throw new InvalidOperationException("nodeJsExecutablePath must be set");
156-
}
157-
158-
if (!octorunScript.HasValue)
159-
{
160-
throw new InvalidOperationException("octorunScript must be set");
161-
}
162-
163-
var loginTask = new OctorunTask(taskManager.Token, nodeJsExecutablePath.Value, octorunScript.Value,
164-
"login", ApplicationInfo.ClientId, ApplicationInfo.ClientSecret);
165-
loginTask.Configure(processManager, workingDirectory: octorunScript.Value.Parent.Parent, withInput: true);
166-
loginTask.OnStartProcess += proc =>
167-
{
168-
proc.StandardInput.WriteLine(username);
169-
proc.StandardInput.WriteLine(password);
170-
proc.StandardInput.Close();
171-
};
172-
173-
var ret = await loginTask.StartAwait();
174-
175-
if (ret.IsSuccess)
176-
{
177-
return new LoginResultData(LoginResultCodes.Success, null, host, ret.Output[0]);
178-
}
179-
180-
if (ret.IsTwoFactorRequired)
181-
{
182-
return new LoginResultData(LoginResultCodes.CodeRequired, "Two Factor Required.", host, ret.Output[0]);
183-
}
184-
185-
if (ret.IsBadCredentials)
186-
{
187-
return new LoginResultData(LoginResultCodes.Failed, "Bad credentials.", host, ret.Output[0]);
188-
}
189-
190-
if (ret.IsLocked)
191-
{
192-
return new LoginResultData(LoginResultCodes.LockedOut, "Account locked.", host, ret.Output[0]);
193-
}
194-
195-
if (ret.Output.Any())
196-
{
197-
return new LoginResultData(LoginResultCodes.Failed, "Failed.", host, ret.Output[0]);
198-
}
199-
200-
return new LoginResultData(LoginResultCodes.Failed, "Failed.", host);
201-
}
202-
203-
private async Task<LoginResultData> TryContinueLogin(
204148
UriString host,
205149
string username,
206150
string password,
207-
string code
151+
string code = null
208152
)
209153
{
210154
if (!nodeJsExecutablePath.HasValue)
@@ -217,14 +161,20 @@ string code
217161
throw new InvalidOperationException("octorunScript must be set");
218162
}
219163

164+
var hasTwoFactorCode = code != null;
165+
166+
var arguments = hasTwoFactorCode ? "login --twoFactor" : "login";
220167
var loginTask = new OctorunTask(taskManager.Token, nodeJsExecutablePath.Value, octorunScript.Value,
221-
"login --twoFactor", ApplicationInfo.ClientId, ApplicationInfo.ClientSecret);
168+
arguments, ApplicationInfo.ClientId, ApplicationInfo.ClientSecret);
222169
loginTask.Configure(processManager, workingDirectory: octorunScript.Value.Parent.Parent, withInput: true);
223170
loginTask.OnStartProcess += proc =>
224171
{
225172
proc.StandardInput.WriteLine(username);
226173
proc.StandardInput.WriteLine(password);
227-
proc.StandardInput.WriteLine(code);
174+
if (hasTwoFactorCode)
175+
{
176+
proc.StandardInput.WriteLine(code);
177+
}
228178
proc.StandardInput.Close();
229179
};
230180

@@ -237,7 +187,10 @@ string code
237187

238188
if (ret.IsTwoFactorRequired)
239189
{
240-
return new LoginResultData(LoginResultCodes.CodeFailed, "Incorrect code. Two Factor Required.", host, ret.Output[0]);
190+
var resultCodes = hasTwoFactorCode ? LoginResultCodes.CodeFailed : LoginResultCodes.CodeRequired;
191+
var message = hasTwoFactorCode ? "Incorrect code. Two Factor Required." : "Two Factor Required.";
192+
193+
return new LoginResultData(resultCodes, message, host, ret.Output[0]);
241194
}
242195

243196
if (ret.IsBadCredentials)

0 commit comments

Comments
 (0)