您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

helpers.py 3.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import base64
  2. import hashlib
  3. import hmac
  4. import os
  5. import time
  6. import urllib.parse
  7. from configs import dify_config
  8. def get_signed_file_url(upload_file_id: str, as_attachment=False) -> str:
  9. url = f"{dify_config.FILES_URL}/files/{upload_file_id}/file-preview"
  10. timestamp = str(int(time.time()))
  11. nonce = os.urandom(16).hex()
  12. key = dify_config.SECRET_KEY.encode()
  13. msg = f"file-preview|{upload_file_id}|{timestamp}|{nonce}"
  14. sign = hmac.new(key, msg.encode(), hashlib.sha256).digest()
  15. encoded_sign = base64.urlsafe_b64encode(sign).decode()
  16. query = {"timestamp": timestamp, "nonce": nonce, "sign": encoded_sign}
  17. if as_attachment:
  18. query["as_attachment"] = "true"
  19. query_string = urllib.parse.urlencode(query)
  20. return f"{url}?{query_string}"
  21. def get_signed_file_url_for_plugin(filename: str, mimetype: str, tenant_id: str, user_id: str) -> str:
  22. # Plugin access should use internal URL for Docker network communication
  23. base_url = dify_config.INTERNAL_FILES_URL or dify_config.FILES_URL
  24. url = f"{base_url}/files/upload/for-plugin"
  25. timestamp = str(int(time.time()))
  26. nonce = os.urandom(16).hex()
  27. key = dify_config.SECRET_KEY.encode()
  28. msg = f"upload|{filename}|{mimetype}|{tenant_id}|{user_id}|{timestamp}|{nonce}"
  29. sign = hmac.new(key, msg.encode(), hashlib.sha256).digest()
  30. encoded_sign = base64.urlsafe_b64encode(sign).decode()
  31. return f"{url}?timestamp={timestamp}&nonce={nonce}&sign={encoded_sign}&user_id={user_id}&tenant_id={tenant_id}"
  32. def verify_plugin_file_signature(
  33. *, filename: str, mimetype: str, tenant_id: str, user_id: str, timestamp: str, nonce: str, sign: str
  34. ) -> bool:
  35. data_to_sign = f"upload|{filename}|{mimetype}|{tenant_id}|{user_id}|{timestamp}|{nonce}"
  36. secret_key = dify_config.SECRET_KEY.encode()
  37. recalculated_sign = hmac.new(secret_key, data_to_sign.encode(), hashlib.sha256).digest()
  38. recalculated_encoded_sign = base64.urlsafe_b64encode(recalculated_sign).decode()
  39. # verify signature
  40. if sign != recalculated_encoded_sign:
  41. return False
  42. current_time = int(time.time())
  43. return current_time - int(timestamp) <= dify_config.FILES_ACCESS_TIMEOUT
  44. def verify_image_signature(*, upload_file_id: str, timestamp: str, nonce: str, sign: str) -> bool:
  45. data_to_sign = f"image-preview|{upload_file_id}|{timestamp}|{nonce}"
  46. secret_key = dify_config.SECRET_KEY.encode()
  47. recalculated_sign = hmac.new(secret_key, data_to_sign.encode(), hashlib.sha256).digest()
  48. recalculated_encoded_sign = base64.urlsafe_b64encode(recalculated_sign).decode()
  49. # verify signature
  50. if sign != recalculated_encoded_sign:
  51. return False
  52. current_time = int(time.time())
  53. return current_time - int(timestamp) <= dify_config.FILES_ACCESS_TIMEOUT
  54. def verify_file_signature(*, upload_file_id: str, timestamp: str, nonce: str, sign: str) -> bool:
  55. data_to_sign = f"file-preview|{upload_file_id}|{timestamp}|{nonce}"
  56. secret_key = dify_config.SECRET_KEY.encode()
  57. recalculated_sign = hmac.new(secret_key, data_to_sign.encode(), hashlib.sha256).digest()
  58. recalculated_encoded_sign = base64.urlsafe_b64encode(recalculated_sign).decode()
  59. # verify signature
  60. if sign != recalculated_encoded_sign:
  61. return False
  62. current_time = int(time.time())
  63. return current_time - int(timestamp) <= dify_config.FILES_ACCESS_TIMEOUT