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.

data_source_oauth.py 4.6KB

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