data_source_oauth.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import logging
  2. import requests
  3. from flask import current_app, redirect, request
  4. from flask_login import current_user
  5. from flask_restful 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. def get_oauth_providers():
  13. with current_app.app_context():
  14. notion_oauth = NotionOAuth(
  15. client_id=dify_config.NOTION_CLIENT_ID,
  16. client_secret=dify_config.NOTION_CLIENT_SECRET,
  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. print(vars(oauth_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": ""}
  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. try:
  66. oauth_provider.get_access_token(code)
  67. except requests.exceptions.HTTPError as e:
  68. logging.exception(
  69. f"An error occurred during the OAuthCallback process with {provider}: {e.response.text}"
  70. )
  71. return {"error": "OAuth data source process failed"}, 400
  72. return {"result": "success"}, 200
  73. class OAuthDataSourceSync(Resource):
  74. @setup_required
  75. @login_required
  76. @account_initialization_required
  77. def get(self, provider, binding_id):
  78. provider = str(provider)
  79. binding_id = str(binding_id)
  80. OAUTH_DATASOURCE_PROVIDERS = get_oauth_providers()
  81. with current_app.app_context():
  82. oauth_provider = OAUTH_DATASOURCE_PROVIDERS.get(provider)
  83. if not oauth_provider:
  84. return {"error": "Invalid provider"}, 400
  85. try:
  86. oauth_provider.sync_data_source(binding_id)
  87. except requests.exceptions.HTTPError as e:
  88. logging.exception(f"An error occurred during the OAuthCallback process with {provider}: {e.response.text}")
  89. return {"error": "OAuth data source process failed"}, 400
  90. return {"result": "success"}, 200
  91. api.add_resource(OAuthDataSource, "/oauth/data-source/<string:provider>")
  92. api.add_resource(OAuthDataSourceCallback, "/oauth/data-source/callback/<string:provider>")
  93. api.add_resource(OAuthDataSourceBinding, "/oauth/data-source/binding/<string:provider>")
  94. api.add_resource(OAuthDataSourceSync, "/oauth/data-source/<string:provider>/<uuid:binding_id>/sync")