private LoginResultData TryLogin( UriString host, string username, string password, string code = null ) { var hasTwoFactorCode = code != null; var command = new StringBuilder("login"); if (hasTwoFactorCode) { command.Append(" --twoFactor"); } if (!HostAddress.IsGitHubDotCom(host)) { command.Append(" -h "); command.Append(host.Host); } var loginTask = new OctorunTask(taskManager.Token, environment, command.ToString()); loginTask.Configure(processManager, withInput: true); loginTask.OnStartProcess += proc => { proc.StandardInput.WriteLine(username); proc.StandardInput.WriteLine(password); if (hasTwoFactorCode) { proc.StandardInput.WriteLine(code); } proc.StandardInput.Close(); }; var ret = loginTask.RunSynchronously(); if (ret.IsSuccess) { return(new LoginResultData(LoginResultCodes.Success, null, host, ret.Output[0])); } if (ret.IsTwoFactorRequired) { var resultCodes = hasTwoFactorCode ? LoginResultCodes.CodeFailed : LoginResultCodes.CodeRequired; var message = hasTwoFactorCode ? "Incorrect code. Two Factor Required." : "Two Factor Required."; return(new LoginResultData(resultCodes, message, host, ret.Output[0])); } return(new LoginResultData(LoginResultCodes.Failed, ret.GetApiErrorMessage() ?? "Failed.", host)); }
private async Task <LoginResultData> TryLogin( UriString host, string username, string password, string code = null ) { if (!nodeJsExecutablePath.HasValue) { throw new InvalidOperationException("nodeJsExecutablePath must be set"); } if (!octorunScript.HasValue) { throw new InvalidOperationException("octorunScript must be set"); } var hasTwoFactorCode = code != null; var arguments = hasTwoFactorCode ? "login --twoFactor" : "login"; var loginTask = new OctorunTask(taskManager.Token, nodeJsExecutablePath.Value, octorunScript.Value, arguments, ApplicationInfo.ClientId, ApplicationInfo.ClientSecret); loginTask.Configure(processManager, workingDirectory: octorunScript.Value.Parent.Parent, withInput: true); loginTask.OnStartProcess += proc => { proc.StandardInput.WriteLine(username); proc.StandardInput.WriteLine(password); if (hasTwoFactorCode) { proc.StandardInput.WriteLine(code); } proc.StandardInput.Close(); }; var ret = await loginTask.StartAwait(); if (ret.IsSuccess) { return(new LoginResultData(LoginResultCodes.Success, null, host, ret.Output[0])); } if (ret.IsTwoFactorRequired) { var resultCodes = hasTwoFactorCode ? LoginResultCodes.CodeFailed : LoginResultCodes.CodeRequired; var message = hasTwoFactorCode ? "Incorrect code. Two Factor Required." : "Two Factor Required."; return(new LoginResultData(resultCodes, message, host, ret.Output[0])); } return(new LoginResultData(LoginResultCodes.Failed, ret.GetApiErrorMessage() ?? "Failed.", host)); }
private LoginResultData TryLogin( UriString host, string username, string password, string code = null ) { var hasTwoFactorCode = code != null; var arguments = hasTwoFactorCode ? "login --twoFactor" : "login"; var loginTask = new OctorunTask(taskManager.Token, keychain, environment, arguments); loginTask.Configure(processManager, withInput: true); loginTask.OnStartProcess += proc => { proc.StandardInput.WriteLine(username); proc.StandardInput.WriteLine(password); if (hasTwoFactorCode) { proc.StandardInput.WriteLine(code); } proc.StandardInput.Close(); }; var ret = loginTask.RunSynchronously(); if (ret.IsSuccess) { return(new LoginResultData(LoginResultCodes.Success, null, host, ret.Output[0])); } if (ret.IsTwoFactorRequired) { var resultCodes = hasTwoFactorCode ? LoginResultCodes.CodeFailed : LoginResultCodes.CodeRequired; var message = hasTwoFactorCode ? "Incorrect code. Two Factor Required." : "Two Factor Required."; return(new LoginResultData(resultCodes, message, host, ret.Output[0])); } return(new LoginResultData(LoginResultCodes.Failed, ret.GetApiErrorMessage() ?? "Failed.", host)); }