public bool LoginWithToken(UriString host, string token)
        {
            Guard.ArgumentNotNull(host, nameof(host));
            Guard.ArgumentNotNullOrWhiteSpace(token, nameof(token));

            var keychainAdapter = keychain.Connect(host);

            keychainAdapter.Set(new Credential(host, "[token]", token));

            try
            {
                var username = RetrieveUsername(token, host);
                keychainAdapter.Update(token, username);
                keychain.SaveToSystem(host);

                return(true);
            }
            catch (Exception e)
            {
                logger.Warning(e, "Login Exception");

                keychain.Clear(host, false);
                return(false);
            }
        }
Exemple #2
0
        /// <inheritdoc/>
        public LoginResultData Login(
            UriString host,
            string username,
            string password)
        {
            Guard.ArgumentNotNull(host, nameof(host));
            Guard.ArgumentNotNullOrWhiteSpace(username, nameof(username));
            Guard.ArgumentNotNullOrWhiteSpace(password, nameof(password));

            // Start by saving the username and password, these will be used by the `IGitHubClient`
            // until an authorization token has been created and acquired:
            var keychainAdapter = keychain.Connect(host);

            keychainAdapter.Set(new Credential(host, username, password));

            try
            {
                var loginResultData = TryLogin(host, username, password);
                if (loginResultData.Code == LoginResultCodes.Success || loginResultData.Code == LoginResultCodes.CodeRequired)
                {
                    if (string.IsNullOrEmpty(loginResultData.Token))
                    {
                        throw new InvalidOperationException("Returned token is null or empty");
                    }

                    keychainAdapter.Update(loginResultData.Token, username);

                    if (loginResultData.Code == LoginResultCodes.Success)
                    {
                        username = RetrieveUsername();
                        keychainAdapter.Update(loginResultData.Token, username);
                        keychain.SaveToSystem(host);
                    }

                    return(loginResultData);
                }

                return(loginResultData);
            }
            catch (Exception e)
            {
                logger.Warning(e, "Login Exception");

                keychain.Clear(host, false);
                return(new LoginResultData(LoginResultCodes.Failed, Localization.LoginFailed, host));
            }
        }