An ebook/comic library service and web client
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

53 lines
1.4 KiB

  1. """Authentication API blueprint and endpoint definitions."""
  2. from flask import Blueprint, g
  3. from atheneum.api.decorators import return_json
  4. from atheneum.api.model import APIResponse
  5. from atheneum.middleware import authentication_middleware
  6. from atheneum.service import (
  7. user_token_service,
  8. authentication_service,
  9. user_service
  10. )
  11. AUTH_BLUEPRINT = Blueprint(
  12. name='auth', import_name=__name__, url_prefix='/auth')
  13. @AUTH_BLUEPRINT.route('/login', methods=['POST'])
  14. @return_json
  15. @authentication_middleware.require_basic_auth
  16. def login() -> APIResponse:
  17. """
  18. Get a token for continued authentication.
  19. :return: A login token for continued authentication
  20. """
  21. user_token = user_token_service.create(g.user)
  22. return APIResponse(user_token, 200)
  23. @AUTH_BLUEPRINT.route('/bump', methods=['POST'])
  24. @return_json
  25. @authentication_middleware.require_token_auth
  26. def login_bump() -> APIResponse:
  27. """
  28. Update the user last seen timestamp.
  29. :return: A time stamp for the bumped login
  30. """
  31. user_service.update_last_login_time(g.user)
  32. return APIResponse({'lastLoginTime': g.user.last_login_time}, 200)
  33. @AUTH_BLUEPRINT.route('/logout', methods=['POST'])
  34. @return_json
  35. @authentication_middleware.require_token_auth
  36. def logout() -> APIResponse:
  37. """
  38. Logout and delete a token.
  39. :return:
  40. """
  41. authentication_service.logout(g.user_token)
  42. return APIResponse(None, 200)