helper.py 8.8 KB

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