Пример #1
0
        public async Task AddStoredCredential(UserCredential newCredential, string password)
        {
            var userInfoDb = await GetUserInfoDB();

			try
			{
				var currentCredentials = await StoredCredentials();
				var existingCredential = currentCredentials.FirstOrDefault(credential => credential.Username == newCredential.Username);
				if (existingCredential != null)
				{
					var lastCookie = existingCredential.LoginCookie;
					//we already exist in the credentials, just update our login token and password (if its set)
					if (existingCredential.LoginCookie != newCredential.LoginCookie ||
						existingCredential.IsDefault != newCredential.IsDefault)
					{
						existingCredential.LoginCookie = newCredential.LoginCookie;
						existingCredential.IsDefault = newCredential.IsDefault;

						//go find the one we're updating and actually do it
						var userCredentialsCursor = await userInfoDb.SelectAsync(userInfoDb.GetKeys().First(), "credentials", DBReadFlags.NoLock);
						if (userCredentialsCursor != null)
						{
							using (userCredentialsCursor)
							{
								do
								{
									var credential = JsonConvert.DeserializeObject<UserCredential>(userCredentialsCursor.GetString());
									if (credential.Username == newCredential.Username)
									{
										await userCredentialsCursor.UpdateAsync(JsonConvert.SerializeObject(existingCredential));
										break;
									}
								} while (await userCredentialsCursor.MoveNextAsync());
							}
						}
					}
					if (!string.IsNullOrWhiteSpace(password))
					{
						AddOrUpdateWindowsCredential(existingCredential, password, lastCookie);
					}
				}
				else
				{
					await userInfoDb.InsertAsync("credentials", JsonConvert.SerializeObject(newCredential));
					var newPassData = new PasswordData { Password = password, LastCookie = newCredential.LoginCookie };
					await userInfoDb.InsertAsync("passwords", JsonConvert.SerializeObject(newPassData));
					//force a re-get of the credentials next time someone wants them
					_storedCredentials = null;
				}
			}
			catch
			{
				//let it fail
			}
        }
Пример #2
0
        private async Task<User> LoginWithCredentials(UserCredential credential, bool userInitiated)
        {
            var originalCookie = credential.LoginCookie;
            if (!string.IsNullOrWhiteSpace(credential.LoginCookie))
            {
                var loggedInUser = new User { Username = credential.Username, LoginCookie = credential.LoginCookie, NeedsCaptcha = false };
                if (userInitiated)
                {
                    loggedInUser.Me = await _redditService.GetMe(loggedInUser);
                }
                else
                {
                    ThreadPool.RunAsync(async (o) =>
                    {
                        await Task.Delay(5000);
                        try
                        {
                            loggedInUser.Me = await _redditService.GetMe(loggedInUser);
                        }
                        catch { }
                        if (loggedInUser.Me == null)
                            ServiceLocator.Current.GetInstance<INotificationService>().CreateNotification(string.Format("Failed to login with user {0}", credential.Username));
                    });
                }
                return loggedInUser;
            }
            else
            {
                //we dont currently posses a valid login cookie, see if windows has a stored credential we can use for this username
				var userInfoDb = await GetUserInfoDB();
				var passwordCursor = await userInfoDb.SelectAsync(userInfoDb.GetKeys().First(), "passwords", DBReadFlags.NoLock);
				if (passwordCursor != null)
				{
					using (passwordCursor)
					{
						do
						{
							try
							{
								var passwordData = JsonConvert.DeserializeObject<PasswordData>(passwordCursor.GetString());
								if (credential.LoginCookie == passwordData.LastCookie)
								{
									return await _redditService.Login(credential.Username, passwordData.Password);
								}
							}
							catch
							{

							}
						} while (await passwordCursor.MoveNextAsync());
					}
				}
            }
            return null;
        }
Пример #3
0
 private async Task<User> LoginWithCredentials(UserCredential credential)
 {
     if (await _redditService.CheckLogin(credential.LoginCookie))
     {
         var loggedInUser = new User { Username = credential.Username, LoginCookie = credential.LoginCookie, NeedsCaptcha = false };
         loggedInUser.Me = await _redditService.GetMe(loggedInUser);
         return loggedInUser;
     }
     else
     {
         //we dont currently posses a valid login cookie, see if windows has a stored credential we can use for this username
         var passwordVault = new Windows.Security.Credentials.PasswordVault();
         try
         {
             var windowsCredentials = passwordVault.FindAllByResource("Baconography");
             var matchingWindowsCredential = windowsCredentials.FirstOrDefault(windowsCredential => string.Compare(windowsCredential.UserName, credential.Username, StringComparison.CurrentCultureIgnoreCase) == 0);
             if (matchingWindowsCredential != null)
             {
                 matchingWindowsCredential.RetrievePassword();
                 return await _redditService.Login(matchingWindowsCredential.UserName, matchingWindowsCredential.Password);
             }
         }
         catch
         {
         }
     }
     return null;
 }
Пример #4
0
        private async void AddOrUpdateWindowsCredential(UserCredential existingCredential, string password, string lastCookie)
        {
			var userInfoDb = await GetUserInfoDB();
			try
			{

				var passwordCursor = await userInfoDb.SelectAsync(userInfoDb.GetKeys().First(), "passwords", DBReadFlags.AutoLock);
				if (passwordCursor != null)
				{
					using (passwordCursor)
					{
						do
						{
							var passwordData = JsonConvert.DeserializeObject<PasswordData>(passwordCursor.GetString());
							if (lastCookie == passwordData.LastCookie)
							{
								var newPassData = new PasswordData { Password = password, LastCookie = existingCredential.LoginCookie };
								await passwordCursor.UpdateAsync(JsonConvert.SerializeObject(newPassData));
								break;
							}
						} while (await passwordCursor.MoveNextAsync());
					}
				}
			}
			catch
			{
				//let it fail
			}
        }
Пример #5
0
 private void AddOrUpdateWindowsCredential(UserCredential existingCredential, string password)
 {
     var passwordVault = new Windows.Security.Credentials.PasswordVault();
     try
     {
         var windowsCredentials = passwordVault.FindAllByResource("Baconography");
         var matchingWindowsCredential = windowsCredentials.FirstOrDefault(credential => string.Compare(credential.UserName, existingCredential.Username, StringComparison.CurrentCultureIgnoreCase) == 0);
         if (matchingWindowsCredential != null)
         {
             matchingWindowsCredential.RetrievePassword();
             if (matchingWindowsCredential.Password != password)
             {
                 passwordVault.Remove(matchingWindowsCredential);
             }
             else
                 passwordVault.Add(new Windows.Security.Credentials.PasswordCredential("Baconography", existingCredential.Username, password));
         }
         else
             passwordVault.Add(new Windows.Security.Credentials.PasswordCredential("Baconography", existingCredential.Username, password));
     }
     catch
     {
         passwordVault.Add(new Windows.Security.Credentials.PasswordCredential("Baconography", existingCredential.Username, password));
     }
 }