Esempio n. 1
0
        private async Task GetOrganizationInternal(Action <Organization[]> onSuccess, Action <Exception> onError = null)
        {
            try
            {
                logger.Trace("Getting Organizations");

                await ValidateKeychain();
                await ValidateCurrentUserInternal();

                var uriString       = keychain.Connections.First().Host;
                var keychainAdapter = await keychain.Load(uriString);

                var octorunTask = new OctorunTask(taskManager.Token, nodeJsExecutablePath, octorunScriptPath, "organizations",
                                                  user: keychainAdapter.Credential.Username, userToken: keychainAdapter.Credential.Token)
                                  .Configure(processManager);

                var ret = await octorunTask.StartAsAsync();

                if (ret.IsSuccess)
                {
                    var organizations = new List <Organization>();
                    for (var i = 0; i < ret.Output.Length; i = i + 2)
                    {
                        organizations.Add(new Organization
                        {
                            Name  = ret.Output[i],
                            Login = ret.Output[i + 1]
                        });
                    }

                    onSuccess(organizations.ToArray());
                    return;
                }

                if (ret.Output.Any())
                {
                    throw new ApiClientException(string.Join(Environment.NewLine, ret.Output));
                }

                throw new ApiClientException("Error getting organizations");
            }
            catch (Exception ex)
            {
                logger.Error(ex, "Error Getting Organizations");
                onError?.Invoke(ex);
            }
        }
Esempio n. 2
0
        private async Task <GitHubUser> GetCurrentUserInternal()
        {
            try
            {
                logger.Trace("Getting Current User");
                await ValidateKeychain();

                var uriString       = keychain.Connections.First().Host;
                var keychainAdapter = await keychain.Load(uriString);

                logger.Trace("Username: {0} Token: {1}", keychainAdapter.Credential.Username, keychainAdapter.Credential.Token);

                var octorunTask = new OctorunTask(taskManager.Token, nodeJsExecutablePath, octorunScriptPath, "validate",
                                                  user: keychainAdapter.Credential.Username, userToken: keychainAdapter.Credential.Token)
                                  .Configure(processManager);

                var ret = await octorunTask.StartAsAsync();

                if (ret.IsSuccess)
                {
                    return(new GitHubUser
                    {
                        Name = ret.Output[0],
                        Login = ret.Output[1]
                    });
                }

                if (ret.Output.Any())
                {
                    throw new ApiClientException(string.Join(Environment.NewLine, ret.Output));
                }

                throw new ApiClientException("Error validating current user");
            }
            catch (KeychainEmptyException)
            {
                logger.Warning("Keychain is empty");
                throw;
            }
            catch (Exception ex)
            {
                logger.Error(ex, "Error Getting Current User");
                throw;
            }
        }
Esempio n. 3
0
        private async Task <string> RetrieveUsername(LoginResultData loginResultData, string username)
        {
            if (!username.Contains("@"))
            {
                return(username);
            }

            var octorunTask = new OctorunTask(taskManager.Token, nodeJsExecutablePath.Value, octorunScript.Value, "validate",
                                              user: username, userToken: loginResultData.Token).Configure(processManager);

            var validateResult = await octorunTask.StartAsAsync();

            if (!validateResult.IsSuccess)
            {
                throw new InvalidOperationException("Authentication validation failed");
            }

            return(validateResult.Output[1]);
        }
Esempio n. 4
0
        private async Task <GitHubUser> GetValidatedGitHubUser(Connection keychainConnection, IKeychainAdapter keychainAdapter)
        {
            try
            {
                var octorunTask = new OctorunTask(taskManager.Token, nodeJsExecutablePath, octorunScriptPath, "validate",
                                                  user: keychainConnection.Username, userToken: keychainAdapter.Credential.Token)
                                  .Configure(processManager);

                var ret = await octorunTask.StartAsAsync();

                if (ret.IsSuccess)
                {
                    var login = ret.Output[1];

                    if (login != keychainConnection.Username)
                    {
                        logger.Trace("LoadKeychainInternal: Api username does not match");
                        throw new TokenUsernameMismatchException(keychainConnection.Username, login);
                    }

                    return(new GitHubUser
                    {
                        Name = ret.Output[0],
                        Login = login
                    });
                }

                throw new ApiClientException(ret.GetApiErrorMessage() ?? "Error validating current user");
            }
            catch (KeychainEmptyException)
            {
                logger.Warning("Keychain is empty");
                throw;
            }
            catch (Exception ex)
            {
                logger.Error(ex, "Error Getting Current User");
                throw;
            }
        }
Esempio n. 5
0
        private async Task GetOrganizationInternal(Action <Organization[]> onSuccess, Action <Exception> onError = null)
        {
            try
            {
                logger.Trace("Getting Organizations");

                var user = await GetCurrentUser();

                var keychainAdapter = keychain.Connect(OriginalUrl);

                var octorunTask = new OctorunTask(taskManager.Token, nodeJsExecutablePath, octorunScriptPath, "organizations",
                                                  user: user.Login, userToken: keychainAdapter.Credential.Token)
                                  .Configure(processManager);

                var ret = await octorunTask.StartAsAsync();

                if (ret.IsSuccess)
                {
                    var organizations = new List <Organization>();
                    for (var i = 0; i < ret.Output.Length; i = i + 2)
                    {
                        organizations.Add(new Organization
                        {
                            Name  = ret.Output[i],
                            Login = ret.Output[i + 1]
                        });
                    }

                    onSuccess(organizations.ToArray());
                    return;
                }

                throw new ApiClientException(ret.GetApiErrorMessage() ?? "Error getting organizations");
            }
            catch (Exception ex)
            {
                logger.Error(ex, "Error Getting Organizations");
                onError?.Invoke(ex);
            }
        }