site_authentication.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import requests
  2. from fate_flow.db.service_registry import ServiceRegistry
  3. from fate_flow.entity.types import RegistryServiceName
  4. from fate_flow.hook import HookManager
  5. from fate_flow.hook.common.parameters import SignatureParameters, AuthenticationParameters, AuthenticationReturn,\
  6. SignatureReturn
  7. from fate_flow.settings import HOOK_SERVER_NAME, PARTY_ID
  8. @HookManager.register_site_signature_hook
  9. def signature(parm: SignatureParameters) -> SignatureReturn:
  10. service_list = ServiceRegistry.load_service(server_name=HOOK_SERVER_NAME, service_name=RegistryServiceName.SIGNATURE.value)
  11. if not service_list:
  12. raise Exception(f"signature error: no found server {HOOK_SERVER_NAME} service signature")
  13. service = service_list[0]
  14. response = getattr(requests, service.f_method.lower(), None)(
  15. url=service.f_url,
  16. json=parm.to_dict()
  17. )
  18. if response.status_code == 200:
  19. if response.json().get("code") == 0:
  20. return SignatureReturn(site_signature=response.json().get("data"))
  21. else:
  22. raise Exception(f"signature error: request signature url failed, result: {response.json()}")
  23. else:
  24. raise Exception(f"signature error: request signature url failed, status code {response.status_code}")
  25. @HookManager.register_site_authentication_hook
  26. def authentication(parm: AuthenticationParameters) -> AuthenticationReturn:
  27. if not parm.src_party_id or str(parm.src_party_id) == "0":
  28. parm.src_party_id = PARTY_ID
  29. service_list = ServiceRegistry.load_service(server_name=HOOK_SERVER_NAME,
  30. service_name=RegistryServiceName.SITE_AUTHENTICATION.value)
  31. if not service_list:
  32. raise Exception(
  33. f"site authentication error: no found server {HOOK_SERVER_NAME} service site_authentication")
  34. service = service_list[0]
  35. response = getattr(requests, service.f_method.lower(), None)(
  36. url=service.f_url,
  37. json=parm.to_dict()
  38. )
  39. if response.status_code != 200:
  40. raise Exception(
  41. f"site authentication error: request site_authentication url failed, status code {response.status_code}")
  42. elif response.json().get("code") != 0:
  43. return AuthenticationReturn(code=response.json().get("code"), message=response.json().get("msg"))
  44. return AuthenticationReturn()