Пример #1
0
        public static async Task LoginAccount(GitHubAccount account, UIViewController ctrl)
        {
            bool exists = Application.Accounts.Find(account.Username) != null;

            //If it does not exist, or there is no oauth then we need to request it!
            if (exists == false || string.IsNullOrEmpty(account.OAuth))
            {
                await Authenticate(account.Domain, account.Username, account.Password, null, ctrl);

                return;
            }

            //Create the client
            var client = GitHubSharp.Client.BasicOAuth(account.OAuth, account.Domain ?? GitHubSharp.Client.DefaultApi);
            await ctrl.DoWorkTest("Logging in...", async() => {
                var userInfo      = (await client.ExecuteAsync(client.AuthenticatedUser.GetInfo())).Data;
                account.Username  = userInfo.Login;
                account.AvatarUrl = userInfo.AvatarUrl;
            });

            Application.Accounts.Update(account);
            Application.SetUser(account, client);
            TransitionToSlideout();
        }
Пример #2
0
        public static async Task Authenticate(string domain, string user, string pass, string twoFactor, UIViewController ctrl)
        {
            //Fill these variables in during the proceeding try/catch
            GitHubAccount account;
            bool          exists;
            string        apiUrl = domain;

            try
            {
                //Make some valid checks
                if (string.IsNullOrEmpty(user))
                {
                    throw new ArgumentException("Username is invalid".t());
                }
                if (string.IsNullOrEmpty(pass))
                {
                    throw new ArgumentException("Password is invalid".t());
                }
                if (apiUrl != null && !Uri.IsWellFormedUriString(apiUrl, UriKind.Absolute))
                {
                    throw new ArgumentException("Domain is invalid".t());
                }

                //Does this user exist?
                account = Application.Accounts.Find(user);
                exists  = account != null;
                if (!exists)
                {
                    account = new GitHubAccount {
                        Username = user
                    }
                }
                ;

                account.Domain = apiUrl;
                var client = twoFactor == null?GitHubSharp.Client.Basic(user, pass, apiUrl) : GitHubSharp.Client.BasicTwoFactorAuthentication(user, pass, twoFactor, apiUrl);

                await ctrl.DoWorkTest("Authenticating...", async() => {
                    var auth      = await client.ExecuteAsync(client.Authorizations.GetOrCreate("72f4fb74bdba774b759d", "9253ab615f8c00738fff5d1c665ca81e581875cb", new System.Collections.Generic.List <string>(Scopes), "CodeHub", null));
                    account.OAuth = auth.Data.Token;
                });

                if (exists)
                {
                    Application.Accounts.Update(account);
                }
                else
                {
                    Application.Accounts.Insert(account);
                }

                //Create a new client to be used that uses oAuth
                await LoginAccount(account, ctrl);
            }
            catch (StatusCodeException ex)
            {
                //Looks like we need to ask for the key!
                if (ex.Headers.ContainsKey("X-GitHub-OTP"))
                {
                    HandleTwoFactorAuth(apiUrl, user, pass, ctrl);
                    return;
                }


                throw new Exception("Unable to login as user " + user + ". Please check your credentials and try again.");
            }
        }