Example #1
0
        /// <summary>
        /// Logs in a user with the given user name and password, using the given key to decrypt the password
        /// </summary>
        /// <param name="userName">The name of the user logging on</param>
        /// <param name="password">The password of the user logging on</param>
        /// <param name="passwordKey">The key to decrypt to the password</param>
        /// <returns>The logged-on user</returns>
        public User Login(string userName, string password, string passwordKey)
        {
            // check that a user with the given user name exists
            List <User> allUsers = DataManager.GetDataContext <User>().GetAll <User>();
            User        user     = allUsers.FirstOrDefault(u => u.UserName == userName);

            if (user == null)
            {
                throw new Exception(USER_NOT_FOUND);
            }

            // decrypt the server password
            string storedPassword = EncryptionUtility.Decrypt(ServerPasswordKey, user.Password);

            // decrypt the given password
            string givenPassword = EncryptionUtility.Decrypt(passwordKey, password);

            // check that the passwords match
            if (storedPassword != givenPassword)
            {
                throw new Exception(INVALID_PASSWORD);
            }

            // add to logged on users
            if (!LoggedOnUsers.Any(u => u.Email == user.Email))
            {
                LoggedOnUsers.Add(user);
            }

            return(user);
        }
Example #2
0
        /// <summary>
        /// Registers a new user using the given password key to decrypt their password
        /// </summary>
        /// <param name="newUser"></param>
        /// <param name="passwordKey"></param>
        public void Register(User newUser, string passwordKey, string facebookAuthCode)
        {
            // check if user already exists
            List <User> allUsers = DataManager.GetDataContext <User>().GetAll <User>();

            if (allUsers.Any(u => u.UserName == newUser.UserName))
            {
                throw new Exception(USER_EXISTS);
            }

            if (newUser.SyncWithFacebook)
            {
                newUser.FacebookAccessToken = FacebookManager.GetFacebookAccessToken(facebookAuthCode);
            }

            // decrypt password
            string decrypted = EncryptionUtility.Decrypt(passwordKey, newUser.Password);

            // re-encrypt password with server key

            string serverEncrypt = EncryptionUtility.Encrypt(ServerPasswordKey, decrypted);

            // set the user's password to the server-encrypted version
            newUser.Password = serverEncrypt;

            // add the users to the data context and save
            DataManager.GetDataContext <User>().AddObjects <User>(new User[] { newUser }.ToList());
            DataManager.GetDataContext <User>().Save();

            // add the user the logged on users
            if (!LoggedOnUsers.Any(u => u.Email == newUser.Email))
            {
                LoggedOnUsers.Add(newUser);
            }
        }