2025-07-18 12:47:32 +08:00
|
|
|
from fastapi.encoders import jsonable_encoder
|
|
|
|
from flask import make_response, redirect, request
|
2025-05-27 00:01:23 +08:00
|
|
|
from flask_login import current_user # type: ignore
|
|
|
|
from flask_restful import ( # type: ignore
|
|
|
|
Resource, # type: ignore
|
|
|
|
reqparse,
|
|
|
|
)
|
|
|
|
from werkzeug.exceptions import Forbidden, NotFound
|
|
|
|
|
|
|
|
from configs import dify_config
|
|
|
|
from controllers.console import api
|
|
|
|
from controllers.console.wraps import (
|
|
|
|
account_initialization_required,
|
|
|
|
setup_required,
|
|
|
|
)
|
|
|
|
from core.model_runtime.errors.validate import CredentialsValidateFailedError
|
2025-07-18 12:47:32 +08:00
|
|
|
from core.plugin.entities.plugin import DatasourceProviderID
|
2025-05-27 00:01:23 +08:00
|
|
|
from core.plugin.impl.oauth import OAuthHandler
|
2025-07-21 19:31:58 +08:00
|
|
|
from libs.helper import StrLen
|
2025-05-27 00:01:23 +08:00
|
|
|
from libs.login import login_required
|
|
|
|
from services.datasource_provider_service import DatasourceProviderService
|
2025-07-18 12:47:32 +08:00
|
|
|
from services.plugin.oauth_service import OAuthProxyService
|
2025-05-27 00:01:23 +08:00
|
|
|
|
|
|
|
|
2025-07-18 12:47:32 +08:00
|
|
|
class DatasourcePluginOAuthAuthorizationUrl(Resource):
|
2025-05-27 00:01:23 +08:00
|
|
|
@setup_required
|
|
|
|
@login_required
|
|
|
|
@account_initialization_required
|
2025-07-18 13:13:20 +08:00
|
|
|
def get(self, provider_id: str):
|
2025-07-18 12:47:32 +08:00
|
|
|
user = current_user
|
|
|
|
tenant_id = user.current_tenant_id
|
2025-05-27 00:01:23 +08:00
|
|
|
if not current_user.is_editor:
|
|
|
|
raise Forbidden()
|
2025-07-18 12:47:32 +08:00
|
|
|
|
2025-07-18 13:13:20 +08:00
|
|
|
datasource_provider_id = DatasourceProviderID(provider_id)
|
|
|
|
provider_name = datasource_provider_id.provider_name
|
|
|
|
plugin_id = datasource_provider_id.plugin_id
|
2025-07-21 18:55:21 +08:00
|
|
|
oauth_config = DatasourceProviderService().get_oauth_client(
|
|
|
|
tenant_id=tenant_id,
|
|
|
|
datasource_provider_id=datasource_provider_id,
|
2025-07-18 12:47:32 +08:00
|
|
|
)
|
|
|
|
if not oauth_config:
|
2025-07-18 13:13:20 +08:00
|
|
|
raise ValueError(f"No OAuth Client Config for {provider_id}")
|
2025-07-18 12:47:32 +08:00
|
|
|
|
|
|
|
context_id = OAuthProxyService.create_proxy_context(
|
|
|
|
user_id=current_user.id, tenant_id=tenant_id, plugin_id=plugin_id, provider=provider_name
|
2025-06-03 19:02:57 +08:00
|
|
|
)
|
2025-05-27 00:01:23 +08:00
|
|
|
oauth_handler = OAuthHandler()
|
2025-07-18 19:47:52 +08:00
|
|
|
redirect_uri = f"{dify_config.CONSOLE_API_URL}/console/api/oauth/plugin/{provider_id}/datasource/callback"
|
2025-07-18 12:47:32 +08:00
|
|
|
authorization_url_response = oauth_handler.get_authorization_url(
|
|
|
|
tenant_id=tenant_id,
|
|
|
|
user_id=user.id,
|
|
|
|
plugin_id=plugin_id,
|
|
|
|
provider=provider_name,
|
|
|
|
redirect_uri=redirect_uri,
|
2025-07-21 18:55:21 +08:00
|
|
|
system_credentials=oauth_config,
|
2025-06-17 19:06:17 +08:00
|
|
|
)
|
2025-07-18 12:47:32 +08:00
|
|
|
response = make_response(jsonable_encoder(authorization_url_response))
|
|
|
|
response.set_cookie(
|
|
|
|
"context_id",
|
|
|
|
context_id,
|
|
|
|
httponly=True,
|
|
|
|
samesite="Lax",
|
|
|
|
max_age=OAuthProxyService.__MAX_AGE__,
|
2025-05-27 00:01:23 +08:00
|
|
|
)
|
2025-07-18 12:47:32 +08:00
|
|
|
return response
|
2025-05-27 00:01:23 +08:00
|
|
|
|
2025-06-03 19:02:57 +08:00
|
|
|
|
2025-07-18 12:47:32 +08:00
|
|
|
class DatasourceOAuthCallback(Resource):
|
2025-05-27 00:01:23 +08:00
|
|
|
@setup_required
|
2025-07-18 13:13:20 +08:00
|
|
|
def get(self, provider_id: str):
|
2025-07-18 19:47:52 +08:00
|
|
|
context_id = request.cookies.get("context_id") or request.args.get("context_id")
|
2025-07-18 12:47:32 +08:00
|
|
|
if not context_id:
|
|
|
|
raise Forbidden("context_id not found")
|
|
|
|
|
|
|
|
context = OAuthProxyService.use_proxy_context(context_id)
|
|
|
|
if context is None:
|
|
|
|
raise Forbidden("Invalid context_id")
|
|
|
|
|
|
|
|
user_id, tenant_id = context.get("user_id"), context.get("tenant_id")
|
2025-07-18 13:13:20 +08:00
|
|
|
datasource_provider_id = DatasourceProviderID(provider_id)
|
|
|
|
plugin_id = datasource_provider_id.plugin_id
|
2025-07-21 12:35:07 +08:00
|
|
|
datasource_provider_service = DatasourceProviderService()
|
|
|
|
oauth_client_params = datasource_provider_service.get_oauth_client(
|
|
|
|
tenant_id=tenant_id,
|
|
|
|
datasource_provider_id=datasource_provider_id,
|
2025-06-03 19:02:57 +08:00
|
|
|
)
|
2025-07-21 12:35:07 +08:00
|
|
|
if not oauth_client_params:
|
2025-05-27 00:01:23 +08:00
|
|
|
raise NotFound()
|
2025-07-18 19:47:52 +08:00
|
|
|
redirect_uri = f"{dify_config.CONSOLE_API_URL}/console/api/oauth/plugin/{provider_id}/datasource/callback"
|
2025-07-18 12:47:32 +08:00
|
|
|
oauth_handler = OAuthHandler()
|
|
|
|
oauth_response = oauth_handler.get_credentials(
|
|
|
|
tenant_id=tenant_id,
|
|
|
|
user_id=user_id,
|
|
|
|
plugin_id=plugin_id,
|
2025-07-18 13:13:20 +08:00
|
|
|
provider=datasource_provider_id.provider_name,
|
2025-07-18 12:47:32 +08:00
|
|
|
redirect_uri=redirect_uri,
|
2025-07-21 12:35:07 +08:00
|
|
|
system_credentials=oauth_client_params,
|
2025-06-03 19:02:57 +08:00
|
|
|
request=request,
|
2025-05-27 00:01:23 +08:00
|
|
|
)
|
2025-07-18 12:47:32 +08:00
|
|
|
datasource_provider_service.add_datasource_oauth_provider(
|
|
|
|
tenant_id=tenant_id,
|
2025-07-18 13:13:20 +08:00
|
|
|
provider_id=datasource_provider_id,
|
2025-07-18 19:47:52 +08:00
|
|
|
avatar_url=oauth_response.metadata.get("avatar_url") or None,
|
|
|
|
name=oauth_response.metadata.get("name") or None,
|
2025-07-18 12:47:32 +08:00
|
|
|
credentials=dict(oauth_response.credentials),
|
2025-05-27 00:01:23 +08:00
|
|
|
)
|
2025-07-18 19:47:52 +08:00
|
|
|
return redirect(f"{dify_config.CONSOLE_WEB_URL}/oauth-callback")
|
2025-05-27 00:01:23 +08:00
|
|
|
|
2025-06-03 19:02:57 +08:00
|
|
|
|
2025-05-27 00:01:23 +08:00
|
|
|
class DatasourceAuth(Resource):
|
|
|
|
@setup_required
|
|
|
|
@login_required
|
|
|
|
@account_initialization_required
|
2025-07-18 13:13:20 +08:00
|
|
|
def post(self, provider_id: str):
|
2025-05-27 00:01:23 +08:00
|
|
|
if not current_user.is_editor:
|
|
|
|
raise Forbidden()
|
|
|
|
|
|
|
|
parser = reqparse.RequestParser()
|
2025-07-21 19:31:58 +08:00
|
|
|
parser.add_argument(
|
|
|
|
"name", type=StrLen(max_length=100), required=False, nullable=True, location="json", default=None
|
|
|
|
)
|
2025-05-27 00:01:23 +08:00
|
|
|
parser.add_argument("credentials", type=dict, required=True, nullable=False, location="json")
|
|
|
|
args = parser.parse_args()
|
2025-07-18 13:13:20 +08:00
|
|
|
datasource_provider_id = DatasourceProviderID(provider_id)
|
2025-05-27 00:01:23 +08:00
|
|
|
datasource_provider_service = DatasourceProviderService()
|
|
|
|
|
|
|
|
try:
|
2025-07-18 12:47:32 +08:00
|
|
|
datasource_provider_service.add_datasource_api_key_provider(
|
2025-06-03 19:02:57 +08:00
|
|
|
tenant_id=current_user.current_tenant_id,
|
2025-07-18 13:13:20 +08:00
|
|
|
provider_id=datasource_provider_id,
|
2025-06-03 19:02:57 +08:00
|
|
|
credentials=args["credentials"],
|
2025-07-18 12:47:32 +08:00
|
|
|
name=args["name"],
|
2025-05-27 00:01:23 +08:00
|
|
|
)
|
|
|
|
except CredentialsValidateFailedError as ex:
|
|
|
|
raise ValueError(str(ex))
|
2025-07-21 18:51:46 +08:00
|
|
|
return {"result": "success"}, 200
|
2025-06-03 19:02:57 +08:00
|
|
|
|
2025-05-30 00:37:27 +08:00
|
|
|
@setup_required
|
|
|
|
@login_required
|
|
|
|
@account_initialization_required
|
2025-07-18 13:13:20 +08:00
|
|
|
def get(self, provider_id: str):
|
|
|
|
datasource_provider_id = DatasourceProviderID(provider_id)
|
2025-05-30 00:37:27 +08:00
|
|
|
datasource_provider_service = DatasourceProviderService()
|
|
|
|
datasources = datasource_provider_service.get_datasource_credentials(
|
2025-07-18 12:47:32 +08:00
|
|
|
tenant_id=current_user.current_tenant_id,
|
2025-07-18 13:13:20 +08:00
|
|
|
provider=datasource_provider_id.provider_name,
|
|
|
|
plugin_id=datasource_provider_id.plugin_id,
|
2025-05-30 00:37:27 +08:00
|
|
|
)
|
|
|
|
return {"result": datasources}, 200
|
2025-06-03 19:02:57 +08:00
|
|
|
|
|
|
|
|
2025-07-21 18:51:46 +08:00
|
|
|
class DatasourceAuthDeleteApi(Resource):
|
2025-05-27 00:01:23 +08:00
|
|
|
@setup_required
|
|
|
|
@login_required
|
|
|
|
@account_initialization_required
|
2025-07-21 16:43:50 +08:00
|
|
|
def post(self, provider_id: str):
|
2025-07-18 13:13:20 +08:00
|
|
|
datasource_provider_id = DatasourceProviderID(provider_id)
|
|
|
|
plugin_id = datasource_provider_id.plugin_id
|
|
|
|
provider_name = datasource_provider_id.provider_name
|
2025-05-27 00:01:23 +08:00
|
|
|
if not current_user.is_editor:
|
|
|
|
raise Forbidden()
|
2025-07-21 16:43:50 +08:00
|
|
|
parser = reqparse.RequestParser()
|
|
|
|
parser.add_argument("credential_id", type=str, required=True, nullable=False, location="json")
|
|
|
|
args = parser.parse_args()
|
2025-05-27 00:01:23 +08:00
|
|
|
datasource_provider_service = DatasourceProviderService()
|
|
|
|
datasource_provider_service.remove_datasource_credentials(
|
2025-06-04 17:29:39 +08:00
|
|
|
tenant_id=current_user.current_tenant_id,
|
2025-07-21 16:43:50 +08:00
|
|
|
auth_id=args["credential_id"],
|
2025-07-18 12:47:32 +08:00
|
|
|
provider=provider_name,
|
|
|
|
plugin_id=plugin_id,
|
2025-05-27 00:01:23 +08:00
|
|
|
)
|
|
|
|
return {"result": "success"}, 200
|
|
|
|
|
2025-07-21 16:43:50 +08:00
|
|
|
|
2025-07-21 18:51:46 +08:00
|
|
|
class DatasourceAuthUpdateApi(Resource):
|
2025-06-04 17:29:39 +08:00
|
|
|
@setup_required
|
|
|
|
@login_required
|
|
|
|
@account_initialization_required
|
2025-07-21 16:43:50 +08:00
|
|
|
def post(self, provider_id: str):
|
2025-07-18 13:13:20 +08:00
|
|
|
datasource_provider_id = DatasourceProviderID(provider_id)
|
2025-06-04 17:29:39 +08:00
|
|
|
parser = reqparse.RequestParser()
|
2025-07-21 16:43:50 +08:00
|
|
|
parser.add_argument("credentials", type=dict, required=False, nullable=True, location="json")
|
2025-07-21 19:31:58 +08:00
|
|
|
parser.add_argument("name", type=StrLen(max_length=100), required=False, nullable=True, location="json")
|
2025-07-21 16:43:50 +08:00
|
|
|
parser.add_argument("credential_id", type=str, required=True, nullable=False, location="json")
|
2025-06-04 17:29:39 +08:00
|
|
|
args = parser.parse_args()
|
|
|
|
if not current_user.is_editor:
|
|
|
|
raise Forbidden()
|
2025-07-21 18:51:46 +08:00
|
|
|
datasource_provider_service = DatasourceProviderService()
|
|
|
|
datasource_provider_service.update_datasource_credentials(
|
|
|
|
tenant_id=current_user.current_tenant_id,
|
|
|
|
auth_id=args["credential_id"],
|
|
|
|
provider=datasource_provider_id.provider_name,
|
|
|
|
plugin_id=datasource_provider_id.plugin_id,
|
|
|
|
credentials=args.get("credentials", {}),
|
|
|
|
name=args.get("name", None),
|
|
|
|
)
|
2025-06-04 17:29:39 +08:00
|
|
|
return {"result": "success"}, 201
|
2025-07-09 14:27:49 +08:00
|
|
|
|
|
|
|
|
2025-07-08 17:16:10 +08:00
|
|
|
class DatasourceAuthListApi(Resource):
|
|
|
|
@setup_required
|
|
|
|
@login_required
|
|
|
|
@account_initialization_required
|
|
|
|
def get(self):
|
|
|
|
datasource_provider_service = DatasourceProviderService()
|
|
|
|
datasources = datasource_provider_service.get_all_datasource_credentials(
|
|
|
|
tenant_id=current_user.current_tenant_id
|
|
|
|
)
|
2025-07-21 14:31:26 +08:00
|
|
|
return {"result": jsonable_encoder(datasources)}, 200
|
2025-06-03 19:02:57 +08:00
|
|
|
|
2025-07-21 15:49:26 +08:00
|
|
|
|
2025-07-21 12:35:07 +08:00
|
|
|
class DatasourceAuthOauthCustomClient(Resource):
|
|
|
|
@setup_required
|
|
|
|
@login_required
|
|
|
|
@account_initialization_required
|
|
|
|
def post(self, provider_id: str):
|
|
|
|
if not current_user.is_editor:
|
|
|
|
raise Forbidden()
|
|
|
|
parser = reqparse.RequestParser()
|
|
|
|
parser.add_argument("client_params", type=dict, required=False, nullable=True, location="json")
|
2025-07-21 18:51:46 +08:00
|
|
|
parser.add_argument("enable_oauth_custom_client", type=bool, required=False, nullable=True, location="json")
|
2025-07-21 12:35:07 +08:00
|
|
|
args = parser.parse_args()
|
|
|
|
datasource_provider_id = DatasourceProviderID(provider_id)
|
|
|
|
datasource_provider_service = DatasourceProviderService()
|
|
|
|
datasource_provider_service.setup_oauth_custom_client_params(
|
|
|
|
tenant_id=current_user.current_tenant_id,
|
|
|
|
datasource_provider_id=datasource_provider_id,
|
|
|
|
client_params=args.get("client_params", {}),
|
2025-07-21 18:51:46 +08:00
|
|
|
enabled=args.get("enable_oauth_custom_client", False),
|
2025-07-21 12:35:07 +08:00
|
|
|
)
|
|
|
|
return {"result": "success"}, 200
|
2025-07-09 14:27:49 +08:00
|
|
|
|
2025-07-21 16:43:50 +08:00
|
|
|
@setup_required
|
|
|
|
@login_required
|
|
|
|
@account_initialization_required
|
|
|
|
def delete(self, provider_id: str):
|
|
|
|
datasource_provider_id = DatasourceProviderID(provider_id)
|
|
|
|
datasource_provider_service = DatasourceProviderService()
|
|
|
|
datasource_provider_service.remove_oauth_custom_client_params(
|
|
|
|
tenant_id=current_user.current_tenant_id,
|
|
|
|
datasource_provider_id=datasource_provider_id,
|
|
|
|
)
|
|
|
|
return {"result": "success"}, 200
|
2025-07-21 15:49:26 +08:00
|
|
|
|
2025-07-21 18:51:46 +08:00
|
|
|
|
2025-07-21 15:49:26 +08:00
|
|
|
class DatasourceAuthDefaultApi(Resource):
|
|
|
|
@setup_required
|
|
|
|
@login_required
|
|
|
|
@account_initialization_required
|
|
|
|
def post(self, provider_id: str):
|
|
|
|
if not current_user.is_editor:
|
|
|
|
raise Forbidden()
|
|
|
|
parser = reqparse.RequestParser()
|
2025-07-21 16:43:50 +08:00
|
|
|
parser.add_argument("id", type=str, required=True, nullable=False, location="json")
|
2025-07-21 15:49:26 +08:00
|
|
|
args = parser.parse_args()
|
|
|
|
datasource_provider_id = DatasourceProviderID(provider_id)
|
|
|
|
datasource_provider_service = DatasourceProviderService()
|
|
|
|
datasource_provider_service.set_default_datasource_provider(
|
|
|
|
tenant_id=current_user.current_tenant_id,
|
|
|
|
datasource_provider_id=datasource_provider_id,
|
2025-07-21 16:43:50 +08:00
|
|
|
credential_id=args["id"],
|
2025-07-21 15:49:26 +08:00
|
|
|
)
|
|
|
|
return {"result": "success"}, 200
|
|
|
|
|
2025-07-21 18:51:46 +08:00
|
|
|
|
2025-07-21 15:49:26 +08:00
|
|
|
class DatasourceUpdateProviderNameApi(Resource):
|
|
|
|
@setup_required
|
|
|
|
@login_required
|
|
|
|
@account_initialization_required
|
|
|
|
def post(self, provider_id: str):
|
|
|
|
if not current_user.is_editor:
|
|
|
|
raise Forbidden()
|
|
|
|
parser = reqparse.RequestParser()
|
2025-07-21 19:31:58 +08:00
|
|
|
parser.add_argument("name", type=StrLen(max_length=100), required=True, nullable=False, location="json")
|
2025-07-21 15:49:26 +08:00
|
|
|
parser.add_argument("credential_id", type=str, required=True, nullable=False, location="json")
|
|
|
|
args = parser.parse_args()
|
|
|
|
datasource_provider_id = DatasourceProviderID(provider_id)
|
|
|
|
datasource_provider_service = DatasourceProviderService()
|
|
|
|
datasource_provider_service.update_datasource_provider_name(
|
|
|
|
tenant_id=current_user.current_tenant_id,
|
|
|
|
datasource_provider_id=datasource_provider_id,
|
|
|
|
name=args["name"],
|
|
|
|
credential_id=args["credential_id"],
|
|
|
|
)
|
|
|
|
return {"result": "success"}, 200
|
|
|
|
|
|
|
|
|
2025-05-27 00:01:23 +08:00
|
|
|
api.add_resource(
|
2025-07-18 12:47:32 +08:00
|
|
|
DatasourcePluginOAuthAuthorizationUrl,
|
2025-07-18 13:13:20 +08:00
|
|
|
"/oauth/plugin/<path:provider_id>/datasource/get-authorization-url",
|
2025-05-27 00:01:23 +08:00
|
|
|
)
|
|
|
|
api.add_resource(
|
2025-07-18 12:47:32 +08:00
|
|
|
DatasourceOAuthCallback,
|
2025-07-18 13:13:20 +08:00
|
|
|
"/oauth/plugin/<path:provider_id>/datasource/callback",
|
2025-05-27 00:01:23 +08:00
|
|
|
)
|
|
|
|
api.add_resource(
|
|
|
|
DatasourceAuth,
|
2025-07-18 13:13:20 +08:00
|
|
|
"/auth/plugin/datasource/<path:provider_id>",
|
2025-05-27 00:01:23 +08:00
|
|
|
)
|
2025-06-04 17:29:39 +08:00
|
|
|
|
|
|
|
api.add_resource(
|
2025-07-21 16:43:50 +08:00
|
|
|
DatasourceAuthUpdateApi,
|
|
|
|
"/auth/plugin/datasource/<path:provider_id>/update",
|
|
|
|
)
|
|
|
|
|
|
|
|
api.add_resource(
|
|
|
|
DatasourceAuthDeleteApi,
|
|
|
|
"/auth/plugin/datasource/<path:provider_id>/delete",
|
2025-06-04 17:29:39 +08:00
|
|
|
)
|
2025-07-08 17:16:10 +08:00
|
|
|
|
|
|
|
api.add_resource(
|
|
|
|
DatasourceAuthListApi,
|
|
|
|
"/auth/plugin/datasource/list",
|
2025-07-09 14:27:49 +08:00
|
|
|
)
|
2025-07-21 12:35:07 +08:00
|
|
|
|
|
|
|
api.add_resource(
|
|
|
|
DatasourceAuthOauthCustomClient,
|
|
|
|
"/auth/plugin/datasource/<path:provider_id>/custom-client",
|
|
|
|
)
|
2025-07-21 15:49:26 +08:00
|
|
|
|
|
|
|
api.add_resource(
|
|
|
|
DatasourceAuthDefaultApi,
|
|
|
|
"/auth/plugin/datasource/<path:provider_id>/default",
|
|
|
|
)
|
|
|
|
|
|
|
|
api.add_resource(
|
|
|
|
DatasourceUpdateProviderNameApi,
|
|
|
|
"/auth/plugin/datasource/<path:provider_id>/update-name",
|
2025-07-21 18:51:46 +08:00
|
|
|
)
|