wraps.py 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. from collections.abc import Callable
  2. from datetime import datetime
  3. from enum import Enum
  4. from functools import wraps
  5. from typing import Optional
  6. from flask import current_app, request
  7. from flask_login import user_logged_in
  8. from flask_restful import Resource
  9. from pydantic import BaseModel
  10. from werkzeug.exceptions import Forbidden, NotFound, Unauthorized
  11. from extensions.ext_database import db
  12. from libs.login import _get_user
  13. from models.account import Account, Tenant, TenantAccountJoin
  14. from models.model import ApiToken, App, EndUser
  15. from services.feature_service import FeatureService
  16. class WhereisUserArg(Enum):
  17. """
  18. Enum for whereis_user_arg.
  19. """
  20. QUERY = 'query'
  21. JSON = 'json'
  22. FORM = 'form'
  23. class FetchUserArg(BaseModel):
  24. fetch_from: WhereisUserArg
  25. required: bool = False
  26. def validate_app_token(view: Optional[Callable] = None, *, fetch_user_arg: Optional[FetchUserArg] = None):
  27. def decorator(view_func):
  28. @wraps(view_func)
  29. def decorated_view(*args, **kwargs):
  30. api_token = validate_and_get_api_token('app')
  31. app_model = db.session.query(App).filter(App.id == api_token.app_id).first()
  32. if not app_model:
  33. raise NotFound()
  34. if app_model.status != 'normal':
  35. raise NotFound()
  36. if not app_model.enable_api:
  37. raise NotFound()
  38. kwargs['app_model'] = app_model
  39. if fetch_user_arg:
  40. if fetch_user_arg.fetch_from == WhereisUserArg.QUERY:
  41. user_id = request.args.get('user')
  42. elif fetch_user_arg.fetch_from == WhereisUserArg.JSON:
  43. user_id = request.get_json().get('user')
  44. elif fetch_user_arg.fetch_from == WhereisUserArg.FORM:
  45. user_id = request.form.get('user')
  46. else:
  47. # use default-user
  48. user_id = None
  49. if not user_id and fetch_user_arg.required:
  50. raise ValueError("Arg user must be provided.")
  51. if user_id:
  52. user_id = str(user_id)
  53. kwargs['end_user'] = create_or_update_end_user_for_user_id(app_model, user_id)
  54. return view_func(*args, **kwargs)
  55. return decorated_view
  56. if view is None:
  57. return decorator
  58. else:
  59. return decorator(view)
  60. def cloud_edition_billing_resource_check(resource: str,
  61. api_token_type: str,
  62. error_msg: str = "You have reached the limit of your subscription."):
  63. def interceptor(view):
  64. def decorated(*args, **kwargs):
  65. api_token = validate_and_get_api_token(api_token_type)
  66. features = FeatureService.get_features(api_token.tenant_id)
  67. if features.billing.enabled:
  68. members = features.members
  69. apps = features.apps
  70. vector_space = features.vector_space
  71. documents_upload_quota = features.documents_upload_quota
  72. if resource == 'members' and 0 < members.limit <= members.size:
  73. raise Forbidden(error_msg)
  74. elif resource == 'apps' and 0 < apps.limit <= apps.size:
  75. raise Forbidden(error_msg)
  76. elif resource == 'vector_space' and 0 < vector_space.limit <= vector_space.size:
  77. raise Forbidden(error_msg)
  78. elif resource == 'documents' and 0 < documents_upload_quota.limit <= documents_upload_quota.size:
  79. raise Forbidden(error_msg)
  80. else:
  81. return view(*args, **kwargs)
  82. return view(*args, **kwargs)
  83. return decorated
  84. return interceptor
  85. def cloud_edition_billing_knowledge_limit_check(resource: str,
  86. api_token_type: str,
  87. error_msg: str = "To unlock this feature and elevate your Dify experience, please upgrade to a paid plan."):
  88. def interceptor(view):
  89. @wraps(view)
  90. def decorated(*args, **kwargs):
  91. api_token = validate_and_get_api_token(api_token_type)
  92. features = FeatureService.get_features(api_token.tenant_id)
  93. if features.billing.enabled:
  94. if resource == 'add_segment':
  95. if features.billing.subscription.plan == 'sandbox':
  96. raise Forbidden(error_msg)
  97. else:
  98. return view(*args, **kwargs)
  99. return view(*args, **kwargs)
  100. return decorated
  101. return interceptor
  102. def validate_dataset_token(view=None):
  103. def decorator(view):
  104. @wraps(view)
  105. def decorated(*args, **kwargs):
  106. api_token = validate_and_get_api_token('dataset')
  107. tenant_account_join = db.session.query(Tenant, TenantAccountJoin) \
  108. .filter(Tenant.id == api_token.tenant_id) \
  109. .filter(TenantAccountJoin.tenant_id == Tenant.id) \
  110. .filter(TenantAccountJoin.role.in_(['owner'])) \
  111. .one_or_none() # TODO: only owner information is required, so only one is returned.
  112. if tenant_account_join:
  113. tenant, ta = tenant_account_join
  114. account = Account.query.filter_by(id=ta.account_id).first()
  115. # Login admin
  116. if account:
  117. account.current_tenant = tenant
  118. current_app.login_manager._update_request_context_with_user(account)
  119. user_logged_in.send(current_app._get_current_object(), user=_get_user())
  120. else:
  121. raise Unauthorized("Tenant owner account does not exist.")
  122. else:
  123. raise Unauthorized("Tenant does not exist.")
  124. return view(api_token.tenant_id, *args, **kwargs)
  125. return decorated
  126. if view:
  127. return decorator(view)
  128. # if view is None, it means that the decorator is used without parentheses
  129. # use the decorator as a function for method_decorators
  130. return decorator
  131. def validate_and_get_api_token(scope=None):
  132. """
  133. Validate and get API token.
  134. """
  135. auth_header = request.headers.get('Authorization')
  136. if auth_header is None or ' ' not in auth_header:
  137. raise Unauthorized("Authorization header must be provided and start with 'Bearer'")
  138. auth_scheme, auth_token = auth_header.split(None, 1)
  139. auth_scheme = auth_scheme.lower()
  140. if auth_scheme != 'bearer':
  141. raise Unauthorized("Authorization scheme must be 'Bearer'")
  142. api_token = db.session.query(ApiToken).filter(
  143. ApiToken.token == auth_token,
  144. ApiToken.type == scope,
  145. ).first()
  146. if not api_token:
  147. raise Unauthorized("Access token is invalid")
  148. api_token.last_used_at = datetime.utcnow()
  149. db.session.commit()
  150. return api_token
  151. def create_or_update_end_user_for_user_id(app_model: App, user_id: Optional[str] = None) -> EndUser:
  152. """
  153. Create or update session terminal based on user ID.
  154. """
  155. if not user_id:
  156. user_id = 'DEFAULT-USER'
  157. end_user = db.session.query(EndUser) \
  158. .filter(
  159. EndUser.tenant_id == app_model.tenant_id,
  160. EndUser.app_id == app_model.id,
  161. EndUser.session_id == user_id,
  162. EndUser.type == 'service_api'
  163. ).first()
  164. if end_user is None:
  165. end_user = EndUser(
  166. tenant_id=app_model.tenant_id,
  167. app_id=app_model.id,
  168. type='service_api',
  169. is_anonymous=True if user_id == 'DEFAULT-USER' else False,
  170. session_id=user_id
  171. )
  172. db.session.add(end_user)
  173. db.session.commit()
  174. return end_user
  175. class DatasetApiResource(Resource):
  176. method_decorators = [validate_dataset_token]