data_source_oauth.py 4.7 KB

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