2023-06-16 21:47:51 +08:00
|
|
|
import logging
|
2026-05-24 15:59:56 +08:00
|
|
|
from uuid import UUID
|
2023-06-16 21:47:51 +08:00
|
|
|
|
2025-09-23 00:07:09 +09:00
|
|
|
import httpx
|
2024-01-12 12:34:01 +08:00
|
|
|
from flask import current_app, redirect, request
|
2026-02-02 18:03:07 +09:00
|
|
|
from flask_restx import Resource
|
|
|
|
|
from pydantic import BaseModel, Field
|
2024-02-06 13:21:13 +08:00
|
|
|
|
2024-07-06 12:05:13 +08:00
|
|
|
from configs import dify_config
|
2026-06-12 22:25:53 +08:00
|
|
|
from controllers.common.fields import RedirectResponse
|
|
|
|
|
from controllers.common.schema import query_params_from_model, register_response_schema_model, register_schema_models
|
2025-11-21 15:25:53 +09: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
|
|
|
|
2025-12-05 13:05:53 +09:00
|
|
|
from .. import console_ns
|
2026-06-19 00:35:29 +08:00
|
|
|
from ..wraps import (
|
|
|
|
|
RBACPermission,
|
|
|
|
|
RBACResourceScope,
|
|
|
|
|
account_initialization_required,
|
|
|
|
|
is_admin_or_owner_required,
|
|
|
|
|
rbac_permission_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
|
|
|
|
2026-02-02 18:03:07 +09:00
|
|
|
class OAuthDataSourceResponse(BaseModel):
|
|
|
|
|
data: str = Field(description="Authorization URL or 'internal' for internal setup")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class OAuthDataSourceBindingResponse(BaseModel):
|
|
|
|
|
result: str = Field(description="Operation result")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class OAuthDataSourceSyncResponse(BaseModel):
|
|
|
|
|
result: str = Field(description="Operation result")
|
|
|
|
|
|
|
|
|
|
|
2026-06-12 22:25:53 +08:00
|
|
|
class OAuthDataSourceCallbackQuery(BaseModel):
|
|
|
|
|
code: str | None = Field(default=None, description="Authorization code from OAuth provider")
|
|
|
|
|
error: str | None = Field(default=None, description="Error message from OAuth provider")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class OAuthDataSourceBindingQuery(BaseModel):
|
|
|
|
|
code: str = Field(description="Authorization code from OAuth provider")
|
|
|
|
|
|
|
|
|
|
|
2026-02-02 18:03:07 +09:00
|
|
|
register_schema_models(
|
|
|
|
|
console_ns,
|
|
|
|
|
OAuthDataSourceResponse,
|
|
|
|
|
OAuthDataSourceBindingResponse,
|
|
|
|
|
OAuthDataSourceSyncResponse,
|
2026-06-12 22:25:53 +08:00
|
|
|
OAuthDataSourceCallbackQuery,
|
|
|
|
|
OAuthDataSourceBindingQuery,
|
2026-02-02 18:03:07 +09:00
|
|
|
)
|
2026-06-12 22:25:53 +08:00
|
|
|
register_response_schema_model(console_ns, RedirectResponse)
|
2026-02-02 18:03:07 +09:00
|
|
|
|
|
|
|
|
|
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-11-24 11:04:11 +09:00
|
|
|
@console_ns.doc("oauth_data_source")
|
|
|
|
|
@console_ns.doc(description="Get OAuth authorization URL for data source provider")
|
|
|
|
|
@console_ns.doc(params={"provider": "Data source provider name (notion)"})
|
|
|
|
|
@console_ns.response(
|
2025-09-10 12:15:47 +08:00
|
|
|
200,
|
|
|
|
|
"Authorization URL or internal setup success",
|
2026-02-02 18:03:07 +09:00
|
|
|
console_ns.models[OAuthDataSourceResponse.__name__],
|
2025-09-10 12:15:47 +08:00
|
|
|
)
|
2025-11-24 11:04:11 +09:00
|
|
|
@console_ns.response(400, "Invalid provider")
|
|
|
|
|
@console_ns.response(403, "Admin privileges required")
|
2025-11-21 15:25:53 +09:00
|
|
|
@is_admin_or_owner_required
|
2026-06-19 00:35:29 +08:00
|
|
|
@rbac_permission_required(RBACResourceScope.WORKSPACE, RBACPermission.CREDENTIAL_MANAGE, resource_required=False)
|
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
|
|
|
|
|
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-11-24 11:04:11 +09:00
|
|
|
@console_ns.doc("oauth_data_source_callback")
|
|
|
|
|
@console_ns.doc(description="Handle OAuth callback from data source provider")
|
2026-06-12 22:25:53 +08:00
|
|
|
@console_ns.doc(params={"provider": "Data source provider name (notion)"})
|
|
|
|
|
@console_ns.doc(params=query_params_from_model(OAuthDataSourceCallbackQuery))
|
|
|
|
|
@console_ns.response(302, "Redirect to console with result", console_ns.models[RedirectResponse.__name__])
|
2025-11-24 11:04:11 +09:00
|
|
|
@console_ns.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-11-24 11:04:11 +09:00
|
|
|
@console_ns.doc("oauth_data_source_binding")
|
|
|
|
|
@console_ns.doc(description="Bind OAuth data source with authorization code")
|
2026-06-12 22:25:53 +08:00
|
|
|
@console_ns.doc(params={"provider": "Data source provider name (notion)"})
|
|
|
|
|
@console_ns.doc(params=query_params_from_model(OAuthDataSourceBindingQuery))
|
2025-11-24 11:04:11 +09:00
|
|
|
@console_ns.response(
|
2025-09-10 12:15:47 +08:00
|
|
|
200,
|
|
|
|
|
"Data source binding success",
|
2026-02-02 18:03:07 +09:00
|
|
|
console_ns.models[OAuthDataSourceBindingResponse.__name__],
|
2025-09-10 12:15:47 +08:00
|
|
|
)
|
2025-11-24 11:04:11 +09:00
|
|
|
@console_ns.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-23 00:07:09 +09:00
|
|
|
except httpx.HTTPStatusError 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-11-24 11:04:11 +09:00
|
|
|
@console_ns.doc("oauth_data_source_sync")
|
|
|
|
|
@console_ns.doc(description="Sync data from OAuth data source")
|
|
|
|
|
@console_ns.doc(params={"provider": "Data source provider name (notion)", "binding_id": "Data source binding ID"})
|
|
|
|
|
@console_ns.response(
|
2025-09-10 12:15:47 +08:00
|
|
|
200,
|
|
|
|
|
"Data source sync success",
|
2026-02-02 18:03:07 +09:00
|
|
|
console_ns.models[OAuthDataSourceSyncResponse.__name__],
|
2025-09-10 12:15:47 +08:00
|
|
|
)
|
2025-11-24 11:04:11 +09:00
|
|
|
@console_ns.response(400, "Invalid provider or sync failed")
|
2023-06-16 21:47:51 +08:00
|
|
|
@setup_required
|
|
|
|
|
@login_required
|
|
|
|
|
@account_initialization_required
|
2026-05-24 15:59:56 +08:00
|
|
|
def get(self, provider: str, binding_id: UUID):
|
|
|
|
|
binding_id_str = str(binding_id)
|
2023-06-16 21:47:51 +08:00
|
|
|
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:
|
2026-05-24 15:59:56 +08:00
|
|
|
oauth_provider.sync_data_source(binding_id_str)
|
2025-09-23 00:07:09 +09:00
|
|
|
except httpx.HTTPStatusError 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
|