data_source_oauth.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. notion_oauth = NotionOAuth(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. OAUTH_PROVIDERS = {
  19. 'notion': notion_oauth
  20. }
  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. print(vars(oauth_provider))
  31. if not oauth_provider:
  32. return {'error': 'Invalid provider'}, 400
  33. if dify_config.NOTION_INTEGRATION_TYPE == 'internal':
  34. internal_secret = dify_config.NOTION_INTERNAL_SECRET
  35. if not internal_secret:
  36. return {'error': 'Internal secret is not set'},
  37. oauth_provider.save_internal_access_token(internal_secret)
  38. return { 'data': '' }
  39. else:
  40. auth_url = oauth_provider.get_authorization_url()
  41. return { 'data': auth_url }, 200
  42. class OAuthDataSourceCallback(Resource):
  43. def get(self, provider: str):
  44. OAUTH_DATASOURCE_PROVIDERS = get_oauth_providers()
  45. with current_app.app_context():
  46. oauth_provider = OAUTH_DATASOURCE_PROVIDERS.get(provider)
  47. if not oauth_provider:
  48. return {'error': 'Invalid provider'}, 400
  49. if 'code' in request.args:
  50. code = request.args.get('code')
  51. return redirect(f'{dify_config.CONSOLE_WEB_URL}?type=notion&code={code}')
  52. elif 'error' in request.args:
  53. error = request.args.get('error')
  54. return redirect(f'{dify_config.CONSOLE_WEB_URL}?type=notion&error={error}')
  55. else:
  56. return redirect(f'{dify_config.CONSOLE_WEB_URL}?type=notion&error=Access denied')
  57. class OAuthDataSourceBinding(Resource):
  58. def get(self, provider: str):
  59. OAUTH_DATASOURCE_PROVIDERS = get_oauth_providers()
  60. with current_app.app_context():
  61. oauth_provider = OAUTH_DATASOURCE_PROVIDERS.get(provider)
  62. if not oauth_provider:
  63. return {'error': 'Invalid provider'}, 400
  64. if 'code' in request.args:
  65. code = request.args.get('code')
  66. try:
  67. oauth_provider.get_access_token(code)
  68. except requests.exceptions.HTTPError as e:
  69. logging.exception(
  70. f"An error occurred during the OAuthCallback process with {provider}: {e.response.text}")
  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(
  89. f"An error occurred during the OAuthCallback process with {provider}: {e.response.text}")
  90. return {'error': 'OAuth data source process failed'}, 400
  91. return {'result': 'success'}, 200
  92. api.add_resource(OAuthDataSource, '/oauth/data-source/<string:provider>')
  93. api.add_resource(OAuthDataSourceCallback, '/oauth/data-source/callback/<string:provider>')
  94. api.add_resource(OAuthDataSourceBinding, '/oauth/data-source/binding/<string:provider>')
  95. api.add_resource(OAuthDataSourceSync, '/oauth/data-source/<string:provider>/<uuid:binding_id>/sync')