2023-06-16 21:47:51 +08:00
|
|
|
import logging
|
|
|
|
|
|
|
|
|
|
import requests
|
2024-01-12 12:34:01 +08:00
|
|
|
from flask import current_app, redirect, request
|
2025-05-06 11:58:49 +08:00
|
|
|
from flask_login import current_user
|
2025-09-10 12:15:47 +08:00
|
|
|
from flask_restx import Resource, fields
|
2024-02-06 13:21:13 +08:00
|
|
|
from werkzeug.exceptions import Forbidden
|
|
|
|
|
|
2024-07-06 12:05:13 +08:00
|
|
|
from configs import dify_config
|
2025-09-10 12:15:47 +08:00
|
|
|
from controllers.console import api, console_ns
|
2023-10-08 05:21:32 -05:00
|
|
|
from libs.login import login_required
|
2023-06-16 21:47:51 +08:00
|
|
|
from libs.oauth_data_source import NotionOAuth
|
2024-01-12 12:34:01 +08:00
|
|
|
|
2024-11-01 15:51:22 +08:00
|
|
|
from ..wraps import account_initialization_required, setup_required
|
2023-06-16 21:47:51 +08:00
|
|
|
|
2025-08-26 18:10:31 +08:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
2023-06-16 21:47:51 +08:00
|
|
|
|
|
|
|
|
def get_oauth_providers():
|
|
|
|
|
with current_app.app_context():
|
2024-08-26 15:29:10 +08:00
|
|
|
notion_oauth = NotionOAuth(
|
2024-12-24 18:38:51 +08:00
|
|
|
client_id=dify_config.NOTION_CLIENT_ID or "",
|
|
|
|
|
client_secret=dify_config.NOTION_CLIENT_SECRET or "",
|
2024-08-26 15:29:10 +08:00
|
|
|
redirect_uri=dify_config.CONSOLE_API_URL + "/console/api/oauth/data-source/callback/notion",
|
|
|
|
|
)
|
2023-06-16 21:47:51 +08:00
|
|
|
|
2024-08-26 15:29:10 +08:00
|
|
|
OAUTH_PROVIDERS = {"notion": notion_oauth}
|
2023-06-16 21:47:51 +08:00
|
|
|
return OAUTH_PROVIDERS
|
|
|
|
|
|
|
|
|
|
|
2025-09-10 12:15:47 +08:00
|
|
|
@console_ns.route("/oauth/data-source/<string:provider>")
|
2023-06-16 21:47:51 +08:00
|
|
|
class OAuthDataSource(Resource):
|
2025-09-10 12:15:47 +08:00
|
|
|
@api.doc("oauth_data_source")
|
|
|
|
|
@api.doc(description="Get OAuth authorization URL for data source provider")
|
|
|
|
|
@api.doc(params={"provider": "Data source provider name (notion)"})
|
|
|
|
|
@api.response(
|
|
|
|
|
200,
|
|
|
|
|
"Authorization URL or internal setup success",
|
|
|
|
|
api.model(
|
|
|
|
|
"OAuthDataSourceResponse",
|
|
|
|
|
{"data": fields.Raw(description="Authorization URL or 'internal' for internal setup")},
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
@api.response(400, "Invalid provider")
|
|
|
|
|
@api.response(403, "Admin privileges required")
|
2023-06-16 21:47:51 +08:00
|
|
|
def get(self, provider: str):
|
|
|
|
|
# The role of the current user in the table must be admin or owner
|
2024-01-26 12:47:42 +08:00
|
|
|
if not current_user.is_admin_or_owner:
|
2023-06-16 21:47:51 +08:00
|
|
|
raise Forbidden()
|
|
|
|
|
OAUTH_DATASOURCE_PROVIDERS = get_oauth_providers()
|
|
|
|
|
with current_app.app_context():
|
|
|
|
|
oauth_provider = OAUTH_DATASOURCE_PROVIDERS.get(provider)
|
|
|
|
|
if not oauth_provider:
|
2024-08-26 15:29:10 +08:00
|
|
|
return {"error": "Invalid provider"}, 400
|
|
|
|
|
if dify_config.NOTION_INTEGRATION_TYPE == "internal":
|
2024-07-06 12:05:13 +08:00
|
|
|
internal_secret = dify_config.NOTION_INTERNAL_SECRET
|
|
|
|
|
if not internal_secret:
|
2024-08-26 15:29:10 +08:00
|
|
|
return ({"error": "Internal secret is not set"},)
|
2023-06-17 19:50:21 +08:00
|
|
|
oauth_provider.save_internal_access_token(internal_secret)
|
2025-07-01 04:58:00 -04:00
|
|
|
return {"data": "internal"}
|
2023-06-17 19:50:21 +08:00
|
|
|
else:
|
|
|
|
|
auth_url = oauth_provider.get_authorization_url()
|
2024-08-26 15:29:10 +08:00
|
|
|
return {"data": auth_url}, 200
|
2023-06-16 21:47:51 +08:00
|
|
|
|
|
|
|
|
|
2025-09-10 12:15:47 +08:00
|
|
|
@console_ns.route("/oauth/data-source/callback/<string:provider>")
|
2023-06-16 21:47:51 +08:00
|
|
|
class OAuthDataSourceCallback(Resource):
|
2025-09-10 12:15:47 +08:00
|
|
|
@api.doc("oauth_data_source_callback")
|
|
|
|
|
@api.doc(description="Handle OAuth callback from data source provider")
|
|
|
|
|
@api.doc(
|
|
|
|
|
params={
|
|
|
|
|
"provider": "Data source provider name (notion)",
|
|
|
|
|
"code": "Authorization code from OAuth provider",
|
|
|
|
|
"error": "Error message from OAuth provider",
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
@api.response(302, "Redirect to console with result")
|
|
|
|
|
@api.response(400, "Invalid provider")
|
2023-09-28 14:39:13 +08:00
|
|
|
def get(self, provider: str):
|
|
|
|
|
OAUTH_DATASOURCE_PROVIDERS = get_oauth_providers()
|
|
|
|
|
with current_app.app_context():
|
|
|
|
|
oauth_provider = OAUTH_DATASOURCE_PROVIDERS.get(provider)
|
|
|
|
|
if not oauth_provider:
|
2024-08-26 15:29:10 +08:00
|
|
|
return {"error": "Invalid provider"}, 400
|
|
|
|
|
if "code" in request.args:
|
|
|
|
|
code = request.args.get("code")
|
2023-09-28 14:39:13 +08:00
|
|
|
|
2024-08-26 15:29:10 +08:00
|
|
|
return redirect(f"{dify_config.CONSOLE_WEB_URL}?type=notion&code={code}")
|
|
|
|
|
elif "error" in request.args:
|
|
|
|
|
error = request.args.get("error")
|
2023-09-28 14:39:13 +08:00
|
|
|
|
2024-08-26 15:29:10 +08:00
|
|
|
return redirect(f"{dify_config.CONSOLE_WEB_URL}?type=notion&error={error}")
|
2023-09-28 14:39:13 +08:00
|
|
|
else:
|
2024-08-26 15:29:10 +08:00
|
|
|
return redirect(f"{dify_config.CONSOLE_WEB_URL}?type=notion&error=Access denied")
|
|
|
|
|
|
2023-09-28 14:39:13 +08:00
|
|
|
|
2025-09-10 12:15:47 +08:00
|
|
|
@console_ns.route("/oauth/data-source/binding/<string:provider>")
|
2023-09-28 14:39:13 +08:00
|
|
|
class OAuthDataSourceBinding(Resource):
|
2025-09-10 12:15:47 +08:00
|
|
|
@api.doc("oauth_data_source_binding")
|
|
|
|
|
@api.doc(description="Bind OAuth data source with authorization code")
|
|
|
|
|
@api.doc(
|
|
|
|
|
params={"provider": "Data source provider name (notion)", "code": "Authorization code from OAuth provider"}
|
|
|
|
|
)
|
|
|
|
|
@api.response(
|
|
|
|
|
200,
|
|
|
|
|
"Data source binding success",
|
|
|
|
|
api.model("OAuthDataSourceBindingResponse", {"result": fields.String(description="Operation result")}),
|
|
|
|
|
)
|
|
|
|
|
@api.response(400, "Invalid provider or code")
|
2023-06-16 21:47:51 +08:00
|
|
|
def get(self, provider: str):
|
|
|
|
|
OAUTH_DATASOURCE_PROVIDERS = get_oauth_providers()
|
|
|
|
|
with current_app.app_context():
|
|
|
|
|
oauth_provider = OAUTH_DATASOURCE_PROVIDERS.get(provider)
|
|
|
|
|
if not oauth_provider:
|
2024-08-26 15:29:10 +08:00
|
|
|
return {"error": "Invalid provider"}, 400
|
|
|
|
|
if "code" in request.args:
|
2025-04-17 16:58:29 +08:00
|
|
|
code = request.args.get("code", "")
|
|
|
|
|
if not code:
|
|
|
|
|
return {"error": "Invalid code"}, 400
|
2023-06-16 21:47:51 +08:00
|
|
|
try:
|
|
|
|
|
oauth_provider.get_access_token(code)
|
2025-09-02 16:05:13 +08:00
|
|
|
except requests.HTTPError as e:
|
2025-08-26 18:10:31 +08:00
|
|
|
logger.exception(
|
2025-07-25 11:32:48 +09:00
|
|
|
"An error occurred during the OAuthCallback process with %s: %s", provider, e.response.text
|
2024-08-26 15:29:10 +08:00
|
|
|
)
|
|
|
|
|
return {"error": "OAuth data source process failed"}, 400
|
2023-06-16 21:47:51 +08:00
|
|
|
|
2024-08-26 15:29:10 +08:00
|
|
|
return {"result": "success"}, 200
|
2023-06-16 21:47:51 +08:00
|
|
|
|
|
|
|
|
|
2025-09-10 12:15:47 +08:00
|
|
|
@console_ns.route("/oauth/data-source/<string:provider>/<uuid:binding_id>/sync")
|
2023-06-16 21:47:51 +08:00
|
|
|
class OAuthDataSourceSync(Resource):
|
2025-09-10 12:15:47 +08:00
|
|
|
@api.doc("oauth_data_source_sync")
|
|
|
|
|
@api.doc(description="Sync data from OAuth data source")
|
|
|
|
|
@api.doc(params={"provider": "Data source provider name (notion)", "binding_id": "Data source binding ID"})
|
|
|
|
|
@api.response(
|
|
|
|
|
200,
|
|
|
|
|
"Data source sync success",
|
|
|
|
|
api.model("OAuthDataSourceSyncResponse", {"result": fields.String(description="Operation result")}),
|
|
|
|
|
)
|
|
|
|
|
@api.response(400, "Invalid provider or sync failed")
|
2023-06-16 21:47:51 +08:00
|
|
|
@setup_required
|
|
|
|
|
@login_required
|
|
|
|
|
@account_initialization_required
|
|
|
|
|
def get(self, provider, binding_id):
|
|
|
|
|
provider = str(provider)
|
|
|
|
|
binding_id = str(binding_id)
|
|
|
|
|
OAUTH_DATASOURCE_PROVIDERS = get_oauth_providers()
|
|
|
|
|
with current_app.app_context():
|
|
|
|
|
oauth_provider = OAUTH_DATASOURCE_PROVIDERS.get(provider)
|
|
|
|
|
if not oauth_provider:
|
2024-08-26 15:29:10 +08:00
|
|
|
return {"error": "Invalid provider"}, 400
|
2023-06-16 21:47:51 +08:00
|
|
|
try:
|
|
|
|
|
oauth_provider.sync_data_source(binding_id)
|
2025-09-02 16:05:13 +08:00
|
|
|
except requests.HTTPError as e:
|
2025-08-26 18:10:31 +08:00
|
|
|
logger.exception(
|
2025-07-25 11:32:48 +09:00
|
|
|
"An error occurred during the OAuthCallback process with %s: %s", provider, e.response.text
|
|
|
|
|
)
|
2024-08-26 15:29:10 +08:00
|
|
|
return {"error": "OAuth data source process failed"}, 400
|
2023-06-16 21:47:51 +08:00
|
|
|
|
2024-08-26 15:29:10 +08:00
|
|
|
return {"result": "success"}, 200
|