helper.py 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. import json
  2. import logging
  3. import random
  4. import re
  5. import string
  6. import subprocess
  7. import time
  8. import uuid
  9. from collections.abc import Generator, Mapping
  10. from datetime import datetime
  11. from hashlib import sha256
  12. from typing import Any, Optional, Union, cast
  13. from zoneinfo import available_timezones
  14. from flask import Response, stream_with_context
  15. from flask_restful import fields # type: ignore
  16. from configs import dify_config
  17. from core.app.features.rate_limiting.rate_limit import RateLimitGenerator
  18. from core.file import helpers as file_helpers
  19. from extensions.ext_redis import redis_client
  20. from models.account import Account
  21. def run(script):
  22. return subprocess.getstatusoutput("source /root/.bashrc && " + script)
  23. class AppIconUrlField(fields.Raw):
  24. def output(self, key, obj):
  25. if obj is None:
  26. return None
  27. from models.model import App, IconType, Site
  28. if isinstance(obj, dict) and "app" in obj:
  29. obj = obj["app"]
  30. if isinstance(obj, App | Site) and obj.icon_type == IconType.IMAGE.value:
  31. return file_helpers.get_signed_file_url(obj.icon)
  32. return None
  33. class AvatarUrlField(fields.Raw):
  34. def output(self, key, obj):
  35. if obj is None:
  36. return None
  37. from models.account import Account
  38. if isinstance(obj, Account) and obj.avatar is not None:
  39. return file_helpers.get_signed_file_url(obj.avatar)
  40. return None
  41. class TimestampField(fields.Raw):
  42. def format(self, value) -> int:
  43. return int(value.timestamp())
  44. def email(email):
  45. # Define a regex pattern for email addresses
  46. pattern = r"^[\w\.!#$%&'*+\-/=?^_`{|}~]+@([\w-]+\.)+[\w-]{2,}$"
  47. # Check if the email matches the pattern
  48. if re.match(pattern, email) is not None:
  49. return email
  50. error = "{email} is not a valid email.".format(email=email)
  51. raise ValueError(error)
  52. def uuid_value(value):
  53. if value == "":
  54. return str(value)
  55. try:
  56. uuid_obj = uuid.UUID(value)
  57. return str(uuid_obj)
  58. except ValueError:
  59. error = "{value} is not a valid uuid.".format(value=value)
  60. raise ValueError(error)
  61. def alphanumeric(value: str):
  62. # check if the value is alphanumeric and underlined
  63. if re.match(r"^[a-zA-Z0-9_]+$", value):
  64. return value
  65. raise ValueError(f"{value} is not a valid alphanumeric value")
  66. def timestamp_value(timestamp):
  67. try:
  68. int_timestamp = int(timestamp)
  69. if int_timestamp < 0:
  70. raise ValueError
  71. return int_timestamp
  72. except ValueError:
  73. error = "{timestamp} is not a valid timestamp.".format(timestamp=timestamp)
  74. raise ValueError(error)
  75. class StrLen:
  76. """Restrict input to an integer in a range (inclusive)"""
  77. def __init__(self, max_length, argument="argument"):
  78. self.max_length = max_length
  79. self.argument = argument
  80. def __call__(self, value):
  81. length = len(value)
  82. if length > self.max_length:
  83. error = "Invalid {arg}: {val}. {arg} cannot exceed length {length}".format(
  84. arg=self.argument, val=value, length=self.max_length
  85. )
  86. raise ValueError(error)
  87. return value
  88. class FloatRange:
  89. """Restrict input to an float in a range (inclusive)"""
  90. def __init__(self, low, high, argument="argument"):
  91. self.low = low
  92. self.high = high
  93. self.argument = argument
  94. def __call__(self, value):
  95. value = _get_float(value)
  96. if value < self.low or value > self.high:
  97. error = "Invalid {arg}: {val}. {arg} must be within the range {lo} - {hi}".format(
  98. arg=self.argument, val=value, lo=self.low, hi=self.high
  99. )
  100. raise ValueError(error)
  101. return value
  102. class DatetimeString:
  103. def __init__(self, format, argument="argument"):
  104. self.format = format
  105. self.argument = argument
  106. def __call__(self, value):
  107. try:
  108. datetime.strptime(value, self.format)
  109. except ValueError:
  110. error = "Invalid {arg}: {val}. {arg} must be conform to the format {format}".format(
  111. arg=self.argument, val=value, format=self.format
  112. )
  113. raise ValueError(error)
  114. return value
  115. def _get_float(value):
  116. try:
  117. return float(value)
  118. except (TypeError, ValueError):
  119. raise ValueError("{} is not a valid float".format(value))
  120. def timezone(timezone_string):
  121. if timezone_string and timezone_string in available_timezones():
  122. return timezone_string
  123. error = "{timezone_string} is not a valid timezone.".format(timezone_string=timezone_string)
  124. raise ValueError(error)
  125. def generate_string(n):
  126. letters_digits = string.ascii_letters + string.digits
  127. result = ""
  128. for i in range(n):
  129. result += random.choice(letters_digits)
  130. return result
  131. def extract_remote_ip(request) -> str:
  132. if request.headers.get("CF-Connecting-IP"):
  133. return cast(str, request.headers.get("Cf-Connecting-Ip"))
  134. elif request.headers.getlist("X-Forwarded-For"):
  135. return cast(str, request.headers.getlist("X-Forwarded-For")[0])
  136. else:
  137. return cast(str, request.remote_addr)
  138. def generate_text_hash(text: str) -> str:
  139. hash_text = str(text) + "None"
  140. return sha256(hash_text.encode()).hexdigest()
  141. def compact_generate_response(
  142. response: Union[Mapping[str, Any], RateLimitGenerator, Generator[str, None, None]],
  143. ) -> Response:
  144. if isinstance(response, dict):
  145. return Response(response=json.dumps(response), status=200, mimetype="application/json")
  146. else:
  147. def generate() -> Generator:
  148. yield from response
  149. return Response(stream_with_context(generate()), status=200, mimetype="text/event-stream")
  150. class TokenManager:
  151. @classmethod
  152. def generate_token(
  153. cls,
  154. token_type: str,
  155. account: Optional[Account] = None,
  156. email: Optional[str] = None,
  157. additional_data: Optional[dict] = None,
  158. ) -> str:
  159. if account is None and email is None:
  160. raise ValueError("Account or email must be provided")
  161. account_id = account.id if account else None
  162. account_email = account.email if account else email
  163. if account_id:
  164. old_token = cls._get_current_token_for_account(account_id, token_type)
  165. if old_token:
  166. if isinstance(old_token, bytes):
  167. old_token = old_token.decode("utf-8")
  168. cls.revoke_token(old_token, token_type)
  169. token = str(uuid.uuid4())
  170. token_data = {"account_id": account_id, "email": account_email, "token_type": token_type}
  171. if additional_data:
  172. token_data.update(additional_data)
  173. expiry_minutes = dify_config.model_dump().get(f"{token_type.upper()}_TOKEN_EXPIRY_MINUTES")
  174. if expiry_minutes is None:
  175. raise ValueError(f"Expiry minutes for {token_type} token is not set")
  176. token_key = cls._get_token_key(token, token_type)
  177. expiry_time = int(expiry_minutes * 60)
  178. redis_client.setex(token_key, expiry_time, json.dumps(token_data))
  179. if account_id:
  180. cls._set_current_token_for_account(account_id, token, token_type, expiry_minutes)
  181. return token
  182. @classmethod
  183. def _get_token_key(cls, token: str, token_type: str) -> str:
  184. return f"{token_type}:token:{token}"
  185. @classmethod
  186. def revoke_token(cls, token: str, token_type: str):
  187. token_key = cls._get_token_key(token, token_type)
  188. redis_client.delete(token_key)
  189. @classmethod
  190. def get_token_data(cls, token: str, token_type: str) -> Optional[dict[str, Any]]:
  191. key = cls._get_token_key(token, token_type)
  192. token_data_json = redis_client.get(key)
  193. if token_data_json is None:
  194. logging.warning(f"{token_type} token {token} not found with key {key}")
  195. return None
  196. token_data: Optional[dict[str, Any]] = json.loads(token_data_json)
  197. return token_data
  198. @classmethod
  199. def _get_current_token_for_account(cls, account_id: str, token_type: str) -> Optional[str]:
  200. key = cls._get_account_token_key(account_id, token_type)
  201. current_token: Optional[str] = redis_client.get(key)
  202. return current_token
  203. @classmethod
  204. def _set_current_token_for_account(
  205. cls, account_id: str, token: str, token_type: str, expiry_hours: Union[int, float]
  206. ):
  207. key = cls._get_account_token_key(account_id, token_type)
  208. expiry_time = int(expiry_hours * 60 * 60)
  209. redis_client.setex(key, expiry_time, token)
  210. @classmethod
  211. def _get_account_token_key(cls, account_id: str, token_type: str) -> str:
  212. return f"{token_type}:account:{account_id}"
  213. class RateLimiter:
  214. def __init__(self, prefix: str, max_attempts: int, time_window: int):
  215. self.prefix = prefix
  216. self.max_attempts = max_attempts
  217. self.time_window = time_window
  218. def _get_key(self, email: str) -> str:
  219. return f"{self.prefix}:{email}"
  220. def is_rate_limited(self, email: str) -> bool:
  221. key = self._get_key(email)
  222. current_time = int(time.time())
  223. window_start_time = current_time - self.time_window
  224. redis_client.zremrangebyscore(key, "-inf", window_start_time)
  225. attempts = redis_client.zcard(key)
  226. if attempts and int(attempts) >= self.max_attempts:
  227. return True
  228. return False
  229. def increment_rate_limit(self, email: str):
  230. key = self._get_key(email)
  231. current_time = int(time.time())
  232. redis_client.zadd(key, {current_time: current_time})
  233. redis_client.expire(key, self.time_window * 2)