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

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