data_source_oauth.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import logging
  2. import requests
  3. from flask import current_app, redirect, request
  4. from flask_login import current_user # type: ignore
  5. from flask_restful import Resource # type: ignore
  6. from werkzeug.exceptions import Forbidden
  7. from configs import dify_config
  8. from controllers.console import api
  9. from libs.login import login_required
  10. from libs.oauth_data_source import NotionOAuth
  11. from ..wraps import account_initialization_required, setup_required
  12. def get_oauth_providers():
  13. with current_app.app_context():
  14. notion_oauth = NotionOAuth(
  15. client_id=dify_config.NOTION_CLIENT_ID or "",
  16. client_secret=dify_config.NOTION_CLIENT_SECRET or "",
  17. redirect_uri=dify_config.CONSOLE_API_URL + "/console/api/oauth/data-source/callback/notion",
  18. )
  19. OAUTH_PROVIDERS = {"notion": notion_oauth}
  20. return OAUTH_PROVIDERS
  21. class OAuthDataSource(Resource):
  22. def get(self, provider: str):
  23. # The role of the current user in the table must be admin or owner
  24. if not current_user.is_admin_or_owner:
  25. raise Forbidden()
  26. OAUTH_DATASOURCE_PROVIDERS = get_oauth_providers()
  27. with current_app.app_context():
  28. oauth_provider = OAUTH_DATASOURCE_PROVIDERS.get(provider)
  29. if not oauth_provider:
  30. return {"error": "Invalid provider"}, 400
  31. if dify_config.NOTION_INTEGRATION_TYPE == "internal":
  32. internal_secret = dify_config.NOTION_INTERNAL_SECRET
  33. if not internal_secret:
  34. return ({"error": "Internal secret is not set"},)
  35. oauth_provider.save_internal_access_token(internal_secret)
  36. return {"data": ""}
  37. else:
  38. auth_url = oauth_provider.get_authorization_url()
  39. return {"data": auth_url}, 200
  40. class OAuthDataSourceCallback(Resource):
  41. def get(self, provider: str):
  42. OAUTH_DATASOURCE_PROVIDERS = get_oauth_providers()
  43. with current_app.app_context():
  44. oauth_provider = OAUTH_DATASOURCE_PROVIDERS.get(provider)
  45. if not oauth_provider:
  46. return {"error": "Invalid provider"}, 400
  47. if "code" in request.args:
  48. code = request.args.get("code")
  49. return redirect(f"{dify_config.CONSOLE_WEB_URL}?type=notion&code={code}")
  50. elif "error" in request.args:
  51. error = request.args.get("error")
  52. return redirect(f"{dify_config.CONSOLE_WEB_URL}?type=notion&error={error}")
  53. else:
  54. return redirect(f"{dify_config.CONSOLE_WEB_URL}?type=notion&error=Access denied")
  55. class OAuthDataSourceBinding(Resource):
  56. def get(self, provider: str):
  57. OAUTH_DATASOURCE_PROVIDERS = get_oauth_providers()
  58. with current_app.app_context():
  59. oauth_provider = OAUTH_DATASOURCE_PROVIDERS.get(provider)
  60. if not oauth_provider:
  61. return {"error": "Invalid provider"}, 400
  62. if "code" in request.args:
  63. code = request.args.get("code")
  64. try:
  65. oauth_provider.get_access_token(code)
  66. except requests.exceptions.HTTPError as e:
  67. logging.exception(
  68. f"An error occurred during the OAuthCallback process with {provider}: {e.response.text}"
  69. )
  70. return {"error": "OAuth data source process failed"}, 400
  71. return {"result": "success"}, 200
  72. class OAuthDataSourceSync(Resource):
  73. @setup_required
  74. @login_required
  75. @account_initialization_required
  76. def get(self, provider, binding_id):
  77. provider = str(provider)
  78. binding_id = str(binding_id)
  79. OAUTH_DATASOURCE_PROVIDERS = get_oauth_providers()
  80. with current_app.app_context():
  81. oauth_provider = OAUTH_DATASOURCE_PROVIDERS.get(provider)
  82. if not oauth_provider:
  83. return {"error": "Invalid provider"}, 400
  84. try:
  85. oauth_provider.sync_data_source(binding_id)
  86. except requests.exceptions.HTTPError as e:
  87. logging.exception(f"An error occurred during the OAuthCallback process with {provider}: {e.response.text}")
  88. return {"error": "OAuth data source process failed"}, 400
  89. return {"result": "success"}, 200
  90. api.add_resource(OAuthDataSource, "/oauth/data-source/<string:provider>")
  91. api.add_resource(OAuthDataSourceCallback, "/oauth/data-source/callback/<string:provider>")
  92. api.add_resource(OAuthDataSourceBinding, "/oauth/data-source/binding/<string:provider>")
  93. api.add_resource(OAuthDataSourceSync, "/oauth/data-source/<string:provider>/<uuid:binding_id>/sync")