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.

60 lines
1.7 KiB

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