def logout() -> Response: """ This deletes a session. :return: A response """ token = request.headers.get('Token') if token is None: return make_response({"error": "no token given"}) session = Session.find(token) if session is None: return make_response({"error": "can not log out from nothing"}) # find session and delete it Session.find(token).delete() # send "ok" return make_response({"ok": True})
def information() -> Response: """ Returns the current session informations by the auth-token :return: The current session with informations of owner """ token = request.headers.get('Token') session = Session.find(token) if session is None: return make_response({"error": "token does not exists"}) return make_response({ "session": session.as_simple_dict(), "user": User.get_by_id(session.owner).as_private_simple_dict() })
def login() -> Response: """ Handles the login which means that this will create a new session if the given credentials are valid. :return: A response """ token = request.headers.get('Token') username = request.form.get("username") password = request.form.get("password") # check if username and password are given if None in (username, password): return make_response({"error": "username or password not given"}) # checks if the token sent by the user does exist and is still valid if token is not None: session = Session.find(token) if session is not None: if session.is_valid(): return make_response({"error": "already signed in"}) user = User.get(username) if not user: return make_response({"error": "incorrect password"}) password_validity = user.validate_password(password) if password_validity: return make_response({ # create session and return its token "token": Session.create(user.id).token }) else: return make_response({"error": "incorrect password"})