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.

49 lines
1.4 KiB

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