default_callbacks.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. """
  2. These are the default methods implementations that are used in this extension.
  3. All of these can be updated on an app by app basis using the JWTManager
  4. loader decorators. For further information, check out the following links:
  5. http://flask-jwt-extended.readthedocs.io/en/latest/changing_default_behavior.html
  6. http://flask-jwt-extended.readthedocs.io/en/latest/tokens_from_complex_object.html
  7. """
  8. from http import HTTPStatus
  9. from typing import Any
  10. from flask import jsonify
  11. from flask.typing import ResponseReturnValue
  12. from flask_jwt_extended.config import config
  13. def default_additional_claims_callback(userdata: Any) -> dict:
  14. """
  15. By default, we add no additional claims to the access tokens.
  16. :param userdata: data passed in as the ```identity``` argument to the
  17. ```create_access_token``` and ```create_refresh_token```
  18. functions
  19. """
  20. return {}
  21. def default_blocklist_callback(jwt_headers: dict, jwt_data: dict) -> bool:
  22. return False
  23. def default_jwt_headers_callback(default_headers) -> dict:
  24. """
  25. By default header typically consists of two parts: the type of the token,
  26. which is JWT, and the signing algorithm being used, such as HMAC SHA256
  27. or RSA. But we don't set the default header here we set it as empty which
  28. further by default set while encoding the token
  29. :return: default we set None here
  30. """
  31. return {}
  32. def default_user_identity_callback(userdata: Any) -> Any:
  33. """
  34. By default, we use the passed in object directly as the jwt identity.
  35. See this for additional info:
  36. :param userdata: data passed in as the ```identity``` argument to the
  37. ```create_access_token``` and ```create_refresh_token```
  38. functions
  39. """
  40. return userdata
  41. def default_expired_token_callback(
  42. _expired_jwt_header: dict, _expired_jwt_data: dict
  43. ) -> ResponseReturnValue:
  44. """
  45. By default, if an expired token attempts to access a protected endpoint,
  46. we return a generic error message with a 401 status
  47. """
  48. return jsonify({config.error_msg_key: "Token has expired"}), HTTPStatus.UNAUTHORIZED
  49. def default_invalid_token_callback(error_string: str) -> ResponseReturnValue:
  50. """
  51. By default, if an invalid token attempts to access a protected endpoint, we
  52. return the error string for why it is not valid with a 422 status code
  53. :param error_string: String indicating why the token is invalid
  54. """
  55. return (
  56. jsonify({config.error_msg_key: error_string}),
  57. HTTPStatus.UNPROCESSABLE_ENTITY,
  58. )
  59. def default_unauthorized_callback(error_string: str) -> ResponseReturnValue:
  60. """
  61. By default, if a protected endpoint is accessed without a JWT, we return
  62. the error string indicating why this is unauthorized, with a 401 status code
  63. :param error_string: String indicating why this request is unauthorized
  64. """
  65. return jsonify({config.error_msg_key: error_string}), HTTPStatus.UNAUTHORIZED
  66. def default_needs_fresh_token_callback(
  67. jwt_header: dict, jwt_data: dict
  68. ) -> ResponseReturnValue:
  69. """
  70. By default, if a non-fresh jwt is used to access a ```fresh_jwt_required```
  71. endpoint, we return a general error message with a 401 status code
  72. """
  73. return (
  74. jsonify({config.error_msg_key: "Fresh token required"}),
  75. HTTPStatus.UNAUTHORIZED,
  76. )
  77. def default_revoked_token_callback(
  78. jwt_header: dict, jwt_data: dict
  79. ) -> ResponseReturnValue:
  80. """
  81. By default, if a revoked token is used to access a protected endpoint, we
  82. return a general error message with a 401 status code
  83. """
  84. return (
  85. jsonify({config.error_msg_key: "Token has been revoked"}),
  86. HTTPStatus.UNAUTHORIZED,
  87. )
  88. def default_user_lookup_error_callback(
  89. _jwt_header: dict, jwt_data: dict
  90. ) -> ResponseReturnValue:
  91. """
  92. By default, if a user_lookup callback is defined and the callback
  93. function returns None, we return a general error message with a 401
  94. status code
  95. """
  96. identity = jwt_data[config.identity_claim_key]
  97. result = {config.error_msg_key: f"Error loading the user {identity}"}
  98. return jsonify(result), HTTPStatus.UNAUTHORIZED
  99. def default_token_verification_callback(_jwt_header: dict, _jwt_data: dict) -> bool:
  100. """
  101. By default, we do not do any verification of the user claims.
  102. """
  103. return True
  104. def default_token_verification_failed_callback(
  105. _jwt_header: dict, _jwt_data: dict
  106. ) -> ResponseReturnValue:
  107. """
  108. By default, if the user claims verification failed, we return a generic
  109. error message with a 400 status code
  110. """
  111. return (
  112. jsonify({config.error_msg_key: "User claims verification failed"}),
  113. HTTPStatus.BAD_REQUEST,
  114. )
  115. def default_decode_key_callback(jwt_header: dict, jwt_data: dict) -> str:
  116. """
  117. By default, the decode key specified via the JWT_SECRET_KEY or
  118. JWT_PUBLIC_KEY settings will be used to decode all tokens
  119. """
  120. return config.decode_key
  121. def default_encode_key_callback(identity: Any) -> str:
  122. """
  123. By default, the encode key specified via the JWT_SECRET_KEY or
  124. JWT_PRIVATE_KEY settings will be used to encode all tokens
  125. """
  126. return config.encode_key