def log_in_user(email, password): """ Check the credentials are correct for logging in and set up session. Returns None if the credentials are incorrect. """ user = User.get_by_email(email) if not user: return None if current_app.config.get('USER_REQUIRE_ACTIVATION', True) and not user.activated: return None key = user.unlock_key_with_password(password) if not key: return None user._unlocked_key = key session['user_id'] = user.id session['key'] = key.exportKey( format='PEM', pkcs=1, passphrase=current_app.secret_key ) return user
def log_in_user(email, password): """ Check the credentials are correct for logging in and set up session. Returns None if the credentials are incorrect. """ user = User.get_by_email(email) if not user: return None if not user.activated: return None key = user.unlock_key_with_password(password) if not key: return None user._unlocked_key = key session['user_id'] = user.id session['key'] = key.exportKey( format='PEM', pkcs=1, passphrase=current_app.secret_key ) return user
def logged_in_user(fetch=True): """ Return the User object for the currently logged in user, or None if the session is not logged in. """ user_id = session.get('user_id', None) if not user_id: return None private_key = session.get('key', None) if not private_key: return None try: unlocked_key = RSA.importKey( private_key, passphrase=current_app.secret_key ) except (ValueError, IndexError, TypeError): return None if not fetch: return True user = User.get(user_id) user._unlocked_key = unlocked_key return user