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.

wraps.py 2.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. from base64 import b64encode
  2. from collections.abc import Callable
  3. from functools import wraps
  4. from hashlib import sha1
  5. from hmac import new as hmac_new
  6. from typing import ParamSpec, TypeVar
  7. P = ParamSpec("P")
  8. R = TypeVar("R")
  9. from flask import abort, request
  10. from configs import dify_config
  11. from extensions.ext_database import db
  12. from models.model import EndUser
  13. def billing_inner_api_only(view: Callable[P, R]):
  14. @wraps(view)
  15. def decorated(*args: P.args, **kwargs: P.kwargs):
  16. if not dify_config.INNER_API:
  17. abort(404)
  18. # get header 'X-Inner-Api-Key'
  19. inner_api_key = request.headers.get("X-Inner-Api-Key")
  20. if not inner_api_key or inner_api_key != dify_config.INNER_API_KEY:
  21. abort(401)
  22. return view(*args, **kwargs)
  23. return decorated
  24. def enterprise_inner_api_only(view: Callable[P, R]):
  25. @wraps(view)
  26. def decorated(*args: P.args, **kwargs: P.kwargs):
  27. if not dify_config.INNER_API:
  28. abort(404)
  29. # get header 'X-Inner-Api-Key'
  30. inner_api_key = request.headers.get("X-Inner-Api-Key")
  31. if not inner_api_key or inner_api_key != dify_config.INNER_API_KEY:
  32. abort(401)
  33. return view(*args, **kwargs)
  34. return decorated
  35. def enterprise_inner_api_user_auth(view: Callable[P, R]):
  36. @wraps(view)
  37. def decorated(*args: P.args, **kwargs: P.kwargs):
  38. if not dify_config.INNER_API:
  39. return view(*args, **kwargs)
  40. # get header 'X-Inner-Api-Key'
  41. authorization = request.headers.get("Authorization")
  42. if not authorization:
  43. return view(*args, **kwargs)
  44. parts = authorization.split(":")
  45. if len(parts) != 2:
  46. return view(*args, **kwargs)
  47. user_id, token = parts
  48. if " " in user_id:
  49. user_id = user_id.split(" ")[1]
  50. inner_api_key = request.headers.get("X-Inner-Api-Key", "")
  51. data_to_sign = f"DIFY {user_id}"
  52. signature = hmac_new(inner_api_key.encode("utf-8"), data_to_sign.encode("utf-8"), sha1)
  53. signature_base64 = b64encode(signature.digest()).decode("utf-8")
  54. if signature_base64 != token:
  55. return view(*args, **kwargs)
  56. kwargs["user"] = db.session.query(EndUser).where(EndUser.id == user_id).first()
  57. return view(*args, **kwargs)
  58. return decorated
  59. def plugin_inner_api_only(view: Callable[P, R]):
  60. @wraps(view)
  61. def decorated(*args: P.args, **kwargs: P.kwargs):
  62. if not dify_config.PLUGIN_DAEMON_KEY:
  63. abort(404)
  64. # get header 'X-Inner-Api-Key'
  65. inner_api_key = request.headers.get("X-Inner-Api-Key")
  66. if not inner_api_key or inner_api_key != dify_config.INNER_API_KEY_FOR_PLUGIN:
  67. abort(404)
  68. return view(*args, **kwargs)
  69. return decorated