Example #1
0
 def test_password(p):
     hashed = hash_password(p)
     hashed2 = hash_password(p)
     self.assertNotEqual(hashed, hashed2) # Should be salted
     self.assertTrue(is_password_correct(p, hashed))
     self.assertTrue(is_password_correct(p, hashed2))
     self.assertFalse(is_password_correct(p + "a", hashed))
Example #2
0
 def test_password(p):
     hashed = hash_password(p)
     hashed2 = hash_password(p)
     self.assertNotEqual(hashed, hashed2)  # Should be salted
     self.assertTrue(is_password_correct(p, hashed))
     self.assertTrue(is_password_correct(p, hashed2))
     self.assertFalse(is_password_correct(p + "a", hashed))
Example #3
0
def get_user_by_email_and_password(db_sess, email, password):
    """Test if the password is correct, and retrieve the user record.

    Returns user (if correct) or None (if incorrect), where user is
    the record of data from the database.  In particular, user.email
    is the true email address from the database (which may differ in
    case or other collation-invariant ways from the supplied email).
    """
    cursor = db_sess.execute("""
                             SELECT user_id, password, full_name, email, expires FROM users
                             WHERE email = :email
                             """, {"email": email})

    try:
        user_id, hashed, full_name, true_email, expires = cursor.fetchone()
        _log.debug("User email %s (%s) has hashed password %r", email, true_email, hashed)
    except TypeError: # pragma: no cover
        # Deleted under our feet?
        _log.warning("User email %s not found", email)
        return None
    else:
        if utils.is_password_correct(password, hashed):
            return {"user_id":         user_id,
                    "hashed_password": hashed,
                    "full_name":       full_name,
                    "email":           true_email,
                    "expires":         expires}
        else:
            return None
    finally:
        cursor.close()