__init__.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import importlib
  2. from fate_flow.hook.common.parameters import SignatureParameters, AuthenticationParameters, PermissionCheckParameters, \
  3. SignatureReturn, AuthenticationReturn, PermissionReturn, ClientAuthenticationReturn, ClientAuthenticationParameters
  4. from fate_flow.settings import HOOK_MODULE, stat_logger
  5. from fate_flow.entity import RetCode
  6. class HookManager:
  7. SITE_SIGNATURE = []
  8. SITE_AUTHENTICATION = []
  9. CLIENT_AUTHENTICATION = []
  10. PERMISSION_CHECK = []
  11. @staticmethod
  12. def init():
  13. if HOOK_MODULE is not None:
  14. for modules in HOOK_MODULE.values():
  15. for module in modules.split(";"):
  16. try:
  17. importlib.import_module(module)
  18. except Exception as e:
  19. stat_logger.exception(e)
  20. @staticmethod
  21. def register_site_signature_hook(func):
  22. HookManager.SITE_SIGNATURE.append(func)
  23. @staticmethod
  24. def register_site_authentication_hook(func):
  25. HookManager.SITE_AUTHENTICATION.append(func)
  26. @staticmethod
  27. def register_client_authentication_hook(func):
  28. HookManager.CLIENT_AUTHENTICATION.append(func)
  29. @staticmethod
  30. def register_permission_check_hook(func):
  31. HookManager.PERMISSION_CHECK.append(func)
  32. @staticmethod
  33. def client_authentication(parm: ClientAuthenticationParameters) -> ClientAuthenticationReturn:
  34. if HookManager.CLIENT_AUTHENTICATION:
  35. return HookManager.CLIENT_AUTHENTICATION[0](parm)
  36. return ClientAuthenticationReturn()
  37. @staticmethod
  38. def site_signature(parm: SignatureParameters) -> SignatureReturn:
  39. if HookManager.SITE_SIGNATURE:
  40. return HookManager.SITE_SIGNATURE[0](parm)
  41. return SignatureReturn()
  42. @staticmethod
  43. def site_authentication(parm: AuthenticationParameters) -> AuthenticationReturn:
  44. if HookManager.SITE_AUTHENTICATION:
  45. return HookManager.SITE_AUTHENTICATION[0](parm)
  46. return AuthenticationReturn()
  47. @staticmethod
  48. def permission_check(parm: PermissionCheckParameters) -> PermissionReturn:
  49. if HookManager.PERMISSION_CHECK:
  50. for permission_check_func in HookManager.PERMISSION_CHECK:
  51. result = permission_check_func(parm)
  52. if result.code != RetCode.SUCCESS:
  53. return result
  54. return PermissionReturn()