Exemplo n.º 1
0
 public void Update(GitHubAccount account)
 {
     lock (_userDatabase)
     {
         _userDatabase.Update(account);
     }
 }
Exemplo n.º 2
0
 public void SetDefault(GitHubAccount account)
 {
     if (account == null)
         _defaults.Clear("DEFAULT_ACCOUNT");
     else
         _defaults.Set("DEFAULT_ACCOUNT", account.Id);
 }
Exemplo n.º 3
0
 public void Insert(GitHubAccount account)
 {
     lock (_userDatabase)
     {
         _userDatabase.Insert(account);
     }
 }
Exemplo n.º 4
0
 private async Task DeleteAccount(GitHubAccount account)
 {
     if (Equals(_sessionService.Account, account))
         ActiveAccount = null;
     _accounts.Remove(account);
     await _accountsRepository.Remove(account);
 }
        public async Task Register(GitHubAccount account)
		{
			var del = (AppDelegate)UIApplication.SharedApplication.Delegate;

			if (string.IsNullOrEmpty(del.DeviceToken))
				throw new InvalidOperationException("Push notifications has not been enabled for this app!");
                
            if (account.IsEnterprise)
                throw new InvalidOperationException("Push notifications are for GitHub.com accounts only!");

            var client = new HttpClient();
            var content = new FormUrlEncodedContent(new[] 
            {
                new KeyValuePair<string, string>("token", del.DeviceToken),
                new KeyValuePair<string, string>("user", account.Username),
                new KeyValuePair<string, string>("domain", "https://api.github.com"),
                new KeyValuePair<string, string>("oauth", account.OAuth)
            });

            client.Timeout = new TimeSpan(0, 0, 30);
            var response = await client.PostAsync(RegisterUri, content);
            if (response.StatusCode != System.Net.HttpStatusCode.OK && response.StatusCode != System.Net.HttpStatusCode.Conflict)
                throw new InvalidOperationException("Unable to register! Server returned a " + response.StatusCode + " status code");
            System.Diagnostics.Debug.WriteLine("Push notifications registered for: " + account.Username + " (" + account.OAuth + ") on device <" + del.DeviceToken + ">");
		}
Exemplo n.º 6
0
        public async Task<Client> LoginAccount(GitHubAccount account)
        {
            //Create the client
            Client client = null;
            if (!string.IsNullOrEmpty(account.OAuth))
            {
                client = Client.BasicOAuth(account.OAuth, account.Domain ?? Client.DefaultApi);
            }
            else if (account.IsEnterprise || !string.IsNullOrEmpty(account.Password))
            {
                client = Client.Basic(account.Username, account.Password, account.Domain ?? Client.DefaultApi);
            }

            var data = await client.ExecuteAsync(client.AuthenticatedUser.GetInfo());
            var userInfo = data.Data;
            account.Username = userInfo.Login;
            account.AvatarUrl = userInfo.AvatarUrl;
            client.Username = userInfo.Login;

            if (_accounts.Exists(account))
                _accounts.Update(account);
            else
                _accounts.Insert(account);
            return client;
        }
Exemplo n.º 7
0
        public void ActivateUser(GitHubAccount account, Client client)
        {
            Accounts.SetActiveAccount(account);
            Account = account;
            Client = client;

            //Set the default account
            Accounts.SetDefault(account);

            //Check the cache size
            CheckCacheSize(account.Cache);

            //Assign the cache
            Client.Cache = new GitHubCache(account);

            // Show the menu & show a page on the slideout
            _viewDispatcher.ShowViewModel(new MvxViewModelRequest {ViewModelType = typeof (MenuViewModel)});

            // A user has been activated!
            if (_activationAction != null)
            {
                _activationAction();
                _activationAction = null;
            }

            //Activate push notifications
            PromptForPushNotifications();
        }
Exemplo n.º 8
0
 private AccountItemViewModel CreateAccountItem(GitHubAccount githubAccount)
 {
     var viewModel = new AccountItemViewModel();
     viewModel.Account = githubAccount;
     viewModel.Selected = Equals(githubAccount, ActiveAccount);
     viewModel.DeleteCommand.Subscribe(_ => DeleteAccountCommand.ExecuteIfCan(githubAccount));
     viewModel.SelectCommand.Subscribe(_ => LoginCommand.ExecuteIfCan(githubAccount));
     return viewModel;
 }
Exemplo n.º 9
0
        internal AccountItemViewModel(GitHubAccount account)
        {
            DeleteCommand = ReactiveCommand.Create();
            GoToCommand = ReactiveCommand.Create();

            Id = account.Key;
            Username = account.Username;
            AvatarUrl = account.AvatarUrl;
            Domain = account.WebDomain ?? "https://api.github.com";
        }
Exemplo n.º 10
0
        public void SetActiveAccount(GitHubAccount account)
        {
            if (account != null)
            {
                var accountDir = CreateAccountDirectory(account);
                if (!Directory.Exists(accountDir))
                    Directory.CreateDirectory(accountDir);
            }

            ActiveAccount = account;
        }
Exemplo n.º 11
0
        private async Task LoginAccount(GitHubAccount account)
        {
            if (!Equals(_sessionService.Account, account))
            {
                ActiveAccount = account;
                await _accountsRepository.SetDefault(account);
                MessageBus.Current.SendMessage(new LogoutMessage());
            }

            Dismiss();
        }
Exemplo n.º 12
0
        public void Remove(GitHubAccount account)
        {
            lock (_userDatabase)
            {
                _userDatabase.Delete(account);
            }
            var accountDir = CreateAccountDirectory(account);

            if (!Directory.Exists(accountDir))
                return;
            Directory.Delete(accountDir, true);
        }
Exemplo n.º 13
0
        public async Task<LoginData> Authenticate(string domain, string user, string pass, string twoFactor, bool enterprise, GitHubAccount account)
        {
            //Fill these variables in during the proceeding try/catch
            var apiUrl = domain;

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

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

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

				if (enterprise)
				{
					account.Password = pass;
				}
				else
				{
                    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)
                    _accounts.Update(account);
                else
                    _accounts.Insert(account);

				return new LoginData { Client = client, Account = account };
            }
            catch (StatusCodeException ex)
            {
                //Looks like we need to ask for the key!
                if (ex.Headers.ContainsKey("X-GitHub-OTP"))
                    throw new TwoFactorRequiredException();
                throw new Exception("Unable to login as user " + user + ". Please check your credentials and try again.");
            }
        }
Exemplo n.º 14
0
        public void Init(NavObject navObject)
        {
			if (navObject.AttemptedAccountId >= 0)
				_attemptedAccount = this.GetApplication().Accounts.Find(navObject.AttemptedAccountId) as GitHubAccount;

            if (_attemptedAccount != null)
            {
                Username = _attemptedAccount.Username;
                IsEnterprise = _attemptedAccount.Domain != null;
                if (IsEnterprise)
                    Domain = _attemptedAccount.Domain;
            }
            else
            {
                IsEnterprise = navObject.IsEnterprise;
            }
        }
Exemplo n.º 15
0
        public void ActivateUser(GitHubAccount account, Client client)
        {
            Accounts.SetActiveAccount(account);
            Account = account;
            Client = client;

			//Set the default account
			Accounts.SetDefault(account);

			//Check the cache size
			CheckCacheSize(account.Cache);

			Client.Cache = new GitHubCache(account);

            // Show the menu & show a page on the slideout
            _viewDispatcher.ShowViewModel(new MvxViewModelRequest {ViewModelType = typeof (MenuViewModel)});
        }
Exemplo n.º 16
0
        public async Task SetSessionAccount(GitHubAccount account)
        {
            if (account == null)
            {
                Account = null;
                Client = null;
                GitHubClient = null;
                return;
            }

            try
            {
                var domain = account.Domain ?? Client.DefaultApi;
                var credentials = new Credentials(account.OAuth);
                var oldClient = Client.BasicOAuth(account.OAuth, domain);
                var newClient = OctokitClientFactory.Create(new Uri(domain), credentials);

                var userInfo = await newClient.User.Current();
                account.Name = userInfo.Name;
                account.Email = userInfo.Email;
                account.AvatarUrl = userInfo.AvatarUrl;
                await _accountsRepository.Update(account);

                // Set all the good stuff.
                Client = oldClient;
                GitHubClient = newClient;
                Account = account;

                // Identify for the analytics service
                _analyticsService.Identify(Account.Username);
                _analyticsService.Track("login");
            }
            catch
            {
                Account = null;
                GitHubClient = null;
                Client = null;
                throw;
            }
        }
Exemplo n.º 17
0
		public async Task<LoginData> LoginWithToken(string clientId, string clientSecret, string code, string redirect, string requestDomain, string apiDomain, GitHubAccount account)
        {
			var token = await Client.RequestAccessToken(clientId, clientSecret, code, redirect, requestDomain);
			var client = Client.BasicOAuth(token.AccessToken, apiDomain);
			var info = await client.ExecuteAsync(client.AuthenticatedUser.GetInfo());
            var username = info.Data.Login;

            //Does this user exist?
            var exists = account != null;
			if (!exists)
                account = new GitHubAccount { Username = username };
			account.OAuth = token.AccessToken;
            account.AvatarUrl = info.Data.AvatarUrl;
			account.Domain = apiDomain;
			account.WebDomain = requestDomain;
			client.Username = username;

            if (exists)
                _accounts.Update(account);
            else
                _accounts.Insert(account);
			return new LoginData { Client = client, Account = account };
        }
Exemplo n.º 18
0
 public bool Exists(GitHubAccount account)
 {
     return Find(account.Domain, account.Username) != null;
 }
Exemplo n.º 19
0
 public bool Exists(GitHubAccount account)
 {
     return Find(account.Id) != null;
 }
Exemplo n.º 20
0
 public async Task<bool> Exists(GitHubAccount account)
 {
     return (await Find(account.Domain, account.Username)) != null;
 }
Exemplo n.º 21
0
 private AccountItemViewModel CreateAccountItem(GitHubAccount githubAccount)
 {
     var viewModel = new AccountItemViewModel(githubAccount);
     viewModel.Selected = Equals(githubAccount, ActiveAccount);
     viewModel.DeleteCommand.Subscribe(_ => DeleteAccount(githubAccount));
     viewModel.GoToCommand.Subscribe(_ => LoginAccount(githubAccount));
     return viewModel;
 }
Exemplo n.º 22
0
 public async Task <bool> Exists(GitHubAccount account)
 {
     return((await Find(account.Domain, account.Username)) != null);
 }
Exemplo n.º 23
0
 protected string CreateAccountDirectory(GitHubAccount account)
 {
     return Path.Combine(_accountsPath, account.Id.ToString(CultureInfo.InvariantCulture));
 }
Exemplo n.º 24
0
 public static NavObject CreateDontRemember(GitHubAccount account)
 {
     return new NavObject
     { 
         WebDomain = account.WebDomain, 
         IsEnterprise = !string.Equals(account.Domain, GitHubSharp.Client.DefaultApi), 
         Username = account.Username,
         AttemptedAccountId = account.Id
     };
 }
Exemplo n.º 25
0
 public Task Update(GitHubAccount account)
 {
     return(Insert(account));
 }
Exemplo n.º 26
0
 public LoggingInMessage(GitHubAccount account)
 {
     Account = account;
 }
Exemplo n.º 27
0
 public void Update(GitHubAccount account)
 {
     Insert(account);
 }
Exemplo n.º 28
0
 public Task Update(GitHubAccount account)
 {
     return Insert(account);
 }
Exemplo n.º 29
0
 public void Remove(GitHubAccount account)
 {
     BlobCache.UserAccount.Invalidate("user_" + account.Key).Wait();
 }
Exemplo n.º 30
0
 public void Insert(GitHubAccount account)
 {
     BlobCache.UserAccount.InsertObject("user_" + account.Key, account).Wait();
 }
Exemplo n.º 31
0
 public Task Insert(GitHubAccount account)
 {
     return(BlobCache.UserAccount.InsertObject("user_" + account.Key, account).ToTask());
 }
Exemplo n.º 32
0
 public static NavObject CreateDontRemember(GitHubAccount account)
 {
     return new NavObject
     { 
         WebDomain = account.WebDomain, 
         Username = account.Username,
         AttemptedAccountId = account.Id
     };
 }
Exemplo n.º 33
0
 public Task Remove(GitHubAccount account)
 {
     return BlobCache.UserAccount.Invalidate("user_" + account.Key).ToTask();
 }
Exemplo n.º 34
0
 public Task SetDefault(GitHubAccount account)
 {
     _defaults.Set("DEFAULT_ACCOUNT", account == null ? null : account.Key);
     return(Task.FromResult(0));
 }
Exemplo n.º 35
0
 public Task Remove(GitHubAccount account)
 {
     return(BlobCache.UserAccount.Invalidate("user_" + account.Key).ToTask());
 }