You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

oauth.py 3.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. from collections.abc import Mapping
  2. from typing import Any
  3. from werkzeug import Request
  4. from core.plugin.entities.plugin_daemon import PluginOAuthAuthorizationUrlResponse, PluginOAuthCredentialsResponse
  5. from core.plugin.impl.base import BasePluginClient
  6. class OAuthHandler(BasePluginClient):
  7. def get_authorization_url(
  8. self,
  9. tenant_id: str,
  10. user_id: str,
  11. plugin_id: str,
  12. provider: str,
  13. system_credentials: Mapping[str, Any],
  14. ) -> PluginOAuthAuthorizationUrlResponse:
  15. return self._request_with_plugin_daemon_response(
  16. "POST",
  17. f"plugin/{tenant_id}/dispatch/oauth/get_authorization_url",
  18. PluginOAuthAuthorizationUrlResponse,
  19. data={
  20. "user_id": user_id,
  21. "data": {
  22. "provider": provider,
  23. "system_credentials": system_credentials,
  24. },
  25. },
  26. headers={
  27. "X-Plugin-ID": plugin_id,
  28. "Content-Type": "application/json",
  29. },
  30. )
  31. def get_credentials(
  32. self,
  33. tenant_id: str,
  34. user_id: str,
  35. plugin_id: str,
  36. provider: str,
  37. system_credentials: Mapping[str, Any],
  38. request: Request,
  39. ) -> PluginOAuthCredentialsResponse:
  40. """
  41. Get credentials from the given request.
  42. """
  43. # encode request to raw http request
  44. raw_request_bytes = self._convert_request_to_raw_data(request)
  45. return self._request_with_plugin_daemon_response(
  46. "POST",
  47. f"plugin/{tenant_id}/dispatch/oauth/get_credentials",
  48. PluginOAuthCredentialsResponse,
  49. data={
  50. "user_id": user_id,
  51. "data": {
  52. "provider": provider,
  53. "system_credentials": system_credentials,
  54. "raw_request_bytes": raw_request_bytes,
  55. },
  56. },
  57. headers={
  58. "X-Plugin-ID": plugin_id,
  59. "Content-Type": "application/json",
  60. },
  61. )
  62. def _convert_request_to_raw_data(self, request: Request) -> bytes:
  63. """
  64. Convert a Request object to raw HTTP data.
  65. Args:
  66. request: The Request object to convert.
  67. Returns:
  68. The raw HTTP data as bytes.
  69. """
  70. # Start with the request line
  71. method = request.method
  72. path = request.path
  73. protocol = request.headers.get("HTTP_VERSION", "HTTP/1.1")
  74. raw_data = f"{method} {path} {protocol}\r\n".encode()
  75. # Add headers
  76. for header_name, header_value in request.headers.items():
  77. raw_data += f"{header_name}: {header_value}\r\n".encode()
  78. # Add empty line to separate headers from body
  79. raw_data += b"\r\n"
  80. # Add body if exists
  81. body = request.get_data(as_text=False)
  82. if body:
  83. raw_data += body
  84. return raw_data