|
|
|
|
@ -1,6 +1,5 @@
|
|
|
|
|
import random
|
|
|
|
|
|
|
|
|
|
from flask import redirect, request
|
|
|
|
|
from fastapi.encoders import jsonable_encoder
|
|
|
|
|
from flask import make_response, redirect, request
|
|
|
|
|
from flask_login import current_user # type: ignore
|
|
|
|
|
from flask_restful import ( # type: ignore
|
|
|
|
|
Resource, # type: ignore
|
|
|
|
|
@ -15,76 +14,101 @@ from controllers.console.wraps import (
|
|
|
|
|
setup_required,
|
|
|
|
|
)
|
|
|
|
|
from core.model_runtime.errors.validate import CredentialsValidateFailedError
|
|
|
|
|
from core.plugin.entities.plugin import DatasourceProviderID
|
|
|
|
|
from core.plugin.impl.oauth import OAuthHandler
|
|
|
|
|
from extensions.ext_database import db
|
|
|
|
|
from libs.login import login_required
|
|
|
|
|
from models.oauth import DatasourceOauthParamConfig, DatasourceProvider
|
|
|
|
|
from models.oauth import DatasourceOauthParamConfig
|
|
|
|
|
from services.datasource_provider_service import DatasourceProviderService
|
|
|
|
|
from services.plugin.oauth_service import OAuthProxyService
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class DatasourcePluginOauthApi(Resource):
|
|
|
|
|
class DatasourcePluginOAuthAuthorizationUrl(Resource):
|
|
|
|
|
@setup_required
|
|
|
|
|
@login_required
|
|
|
|
|
@account_initialization_required
|
|
|
|
|
def get(self):
|
|
|
|
|
parser = reqparse.RequestParser()
|
|
|
|
|
parser.add_argument("provider", type=str, required=True, nullable=False, location="args")
|
|
|
|
|
parser.add_argument("plugin_id", type=str, required=True, nullable=False, location="args")
|
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
provider = args["provider"]
|
|
|
|
|
plugin_id = args["plugin_id"]
|
|
|
|
|
# Check user role first
|
|
|
|
|
def get(self, provider: str):
|
|
|
|
|
user = current_user
|
|
|
|
|
tenant_id = user.current_tenant_id
|
|
|
|
|
if not current_user.is_editor:
|
|
|
|
|
raise Forbidden()
|
|
|
|
|
# get all plugin oauth configs
|
|
|
|
|
plugin_oauth_config = (
|
|
|
|
|
db.session.query(DatasourceOauthParamConfig).filter_by(provider=provider, plugin_id=plugin_id).first()
|
|
|
|
|
|
|
|
|
|
provider_id = DatasourceProviderID(provider)
|
|
|
|
|
provider_name = provider_id.provider_name
|
|
|
|
|
plugin_id = provider_id.plugin_id
|
|
|
|
|
oauth_config = (
|
|
|
|
|
db.session.query(DatasourceOauthParamConfig).filter_by(provider=provider_name, plugin_id=plugin_id).first()
|
|
|
|
|
)
|
|
|
|
|
if not oauth_config:
|
|
|
|
|
raise ValueError(f"No OAuth Client Config for {provider}")
|
|
|
|
|
|
|
|
|
|
context_id = OAuthProxyService.create_proxy_context(
|
|
|
|
|
user_id=current_user.id, tenant_id=tenant_id, plugin_id=plugin_id, provider=provider_name
|
|
|
|
|
)
|
|
|
|
|
if not plugin_oauth_config:
|
|
|
|
|
raise NotFound()
|
|
|
|
|
oauth_handler = OAuthHandler()
|
|
|
|
|
redirect_url = (
|
|
|
|
|
f"{dify_config.CONSOLE_WEB_URL}/oauth/datasource/callback?provider={provider}&plugin_id={plugin_id}"
|
|
|
|
|
redirect_uri = f"{dify_config.CONSOLE_WEB_URL}/console/api/oauth/plugin/{provider}/datasource/callback"
|
|
|
|
|
oauth_client_params = oauth_config.system_credentials
|
|
|
|
|
|
|
|
|
|
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,
|
|
|
|
|
system_credentials=oauth_client_params,
|
|
|
|
|
)
|
|
|
|
|
system_credentials = plugin_oauth_config.system_credentials
|
|
|
|
|
if system_credentials:
|
|
|
|
|
system_credentials["redirect_url"] = redirect_url
|
|
|
|
|
response = oauth_handler.get_authorization_url(
|
|
|
|
|
current_user.current_tenant.id, current_user.id, plugin_id, provider, system_credentials=system_credentials
|
|
|
|
|
response = make_response(jsonable_encoder(authorization_url_response))
|
|
|
|
|
response.set_cookie(
|
|
|
|
|
"context_id",
|
|
|
|
|
context_id,
|
|
|
|
|
httponly=True,
|
|
|
|
|
samesite="Lax",
|
|
|
|
|
max_age=OAuthProxyService.__MAX_AGE__,
|
|
|
|
|
)
|
|
|
|
|
return response.model_dump()
|
|
|
|
|
return response
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class DatasourceOauthCallback(Resource):
|
|
|
|
|
class DatasourceOAuthCallback(Resource):
|
|
|
|
|
@setup_required
|
|
|
|
|
@login_required
|
|
|
|
|
@account_initialization_required
|
|
|
|
|
def get(self):
|
|
|
|
|
parser = reqparse.RequestParser()
|
|
|
|
|
parser.add_argument("provider", type=str, required=True, nullable=False, location="args")
|
|
|
|
|
parser.add_argument("plugin_id", type=str, required=True, nullable=False, location="args")
|
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
provider = args["provider"]
|
|
|
|
|
plugin_id = args["plugin_id"]
|
|
|
|
|
oauth_handler = OAuthHandler()
|
|
|
|
|
def get(self, provider: str):
|
|
|
|
|
if not current_user.is_editor:
|
|
|
|
|
raise Forbidden()
|
|
|
|
|
|
|
|
|
|
context_id = request.cookies.get("context_id")
|
|
|
|
|
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")
|
|
|
|
|
provider_id = DatasourceProviderID(provider)
|
|
|
|
|
provider_name = provider_id.provider_name
|
|
|
|
|
plugin_id = provider_id.plugin_id
|
|
|
|
|
plugin_oauth_config = (
|
|
|
|
|
db.session.query(DatasourceOauthParamConfig).filter_by(provider=provider, plugin_id=plugin_id).first()
|
|
|
|
|
db.session.query(DatasourceOauthParamConfig).filter_by(provider=provider_name, plugin_id=plugin_id).first()
|
|
|
|
|
)
|
|
|
|
|
if not plugin_oauth_config:
|
|
|
|
|
raise NotFound()
|
|
|
|
|
credentials = oauth_handler.get_credentials(
|
|
|
|
|
current_user.current_tenant.id,
|
|
|
|
|
current_user.id,
|
|
|
|
|
plugin_id,
|
|
|
|
|
provider,
|
|
|
|
|
redirect_uri = f"{dify_config.CONSOLE_WEB_URL}/console/api/oauth/plugin/{provider}/datasource/callback"
|
|
|
|
|
oauth_handler = OAuthHandler()
|
|
|
|
|
oauth_response = oauth_handler.get_credentials(
|
|
|
|
|
tenant_id=tenant_id,
|
|
|
|
|
user_id=user_id,
|
|
|
|
|
plugin_id=plugin_id,
|
|
|
|
|
provider=provider_id.provider_name,
|
|
|
|
|
redirect_uri=redirect_uri,
|
|
|
|
|
system_credentials=plugin_oauth_config.system_credentials,
|
|
|
|
|
request=request,
|
|
|
|
|
)
|
|
|
|
|
datasource_provider = DatasourceProvider(
|
|
|
|
|
plugin_id=plugin_id, provider=provider, auth_type="oauth", encrypted_credentials=credentials
|
|
|
|
|
datasource_provider_service = DatasourceProviderService()
|
|
|
|
|
datasource_provider_service.add_datasource_oauth_provider(
|
|
|
|
|
tenant_id=tenant_id,
|
|
|
|
|
provider_id=provider_id,
|
|
|
|
|
credentials=dict(oauth_response.credentials),
|
|
|
|
|
name=None,
|
|
|
|
|
)
|
|
|
|
|
db.session.add(datasource_provider)
|
|
|
|
|
db.session.commit()
|
|
|
|
|
return redirect(f"{dify_config.CONSOLE_WEB_URL}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -92,26 +116,23 @@ class DatasourceAuth(Resource):
|
|
|
|
|
@setup_required
|
|
|
|
|
@login_required
|
|
|
|
|
@account_initialization_required
|
|
|
|
|
def post(self):
|
|
|
|
|
def post(self, provider: str):
|
|
|
|
|
if not current_user.is_editor:
|
|
|
|
|
raise Forbidden()
|
|
|
|
|
|
|
|
|
|
parser = reqparse.RequestParser()
|
|
|
|
|
parser.add_argument("provider", type=str, required=True, nullable=False, location="json")
|
|
|
|
|
parser.add_argument("name", type=str, required=False, nullable=False, location="json", default="test")
|
|
|
|
|
parser.add_argument("plugin_id", type=str, required=True, nullable=False, location="json")
|
|
|
|
|
parser.add_argument("name", type=str, required=False, nullable=True, location="json", default=None)
|
|
|
|
|
parser.add_argument("credentials", type=dict, required=True, nullable=False, location="json")
|
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
|
|
provider_id = DatasourceProviderID(provider)
|
|
|
|
|
datasource_provider_service = DatasourceProviderService()
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
datasource_provider_service.datasource_provider_credentials_validate(
|
|
|
|
|
datasource_provider_service.add_datasource_api_key_provider(
|
|
|
|
|
tenant_id=current_user.current_tenant_id,
|
|
|
|
|
provider=args["provider"],
|
|
|
|
|
plugin_id=args["plugin_id"],
|
|
|
|
|
provider_id=provider_id,
|
|
|
|
|
credentials=args["credentials"],
|
|
|
|
|
name="test" + str(random.randint(1, 1000000)), # noqa: S311
|
|
|
|
|
name=args["name"],
|
|
|
|
|
)
|
|
|
|
|
except CredentialsValidateFailedError as ex:
|
|
|
|
|
raise ValueError(str(ex))
|
|
|
|
|
@ -121,14 +142,13 @@ class DatasourceAuth(Resource):
|
|
|
|
|
@setup_required
|
|
|
|
|
@login_required
|
|
|
|
|
@account_initialization_required
|
|
|
|
|
def get(self):
|
|
|
|
|
parser = reqparse.RequestParser()
|
|
|
|
|
parser.add_argument("provider", type=str, required=True, nullable=False, location="args")
|
|
|
|
|
parser.add_argument("plugin_id", type=str, required=True, nullable=False, location="args")
|
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
def get(self, provider: str):
|
|
|
|
|
provider_id = DatasourceProviderID(provider)
|
|
|
|
|
datasource_provider_service = DatasourceProviderService()
|
|
|
|
|
datasources = datasource_provider_service.get_datasource_credentials(
|
|
|
|
|
tenant_id=current_user.current_tenant_id, provider=args["provider"], plugin_id=args["plugin_id"]
|
|
|
|
|
tenant_id=current_user.current_tenant_id,
|
|
|
|
|
provider=provider_id.provider_name,
|
|
|
|
|
plugin_id=provider_id.plugin_id,
|
|
|
|
|
)
|
|
|
|
|
return {"result": datasources}, 200
|
|
|
|
|
|
|
|
|
|
@ -137,29 +157,27 @@ class DatasourceAuthUpdateDeleteApi(Resource):
|
|
|
|
|
@setup_required
|
|
|
|
|
@login_required
|
|
|
|
|
@account_initialization_required
|
|
|
|
|
def delete(self, auth_id: str):
|
|
|
|
|
parser = reqparse.RequestParser()
|
|
|
|
|
parser.add_argument("provider", type=str, required=True, nullable=False, location="args")
|
|
|
|
|
parser.add_argument("plugin_id", type=str, required=True, nullable=False, location="args")
|
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
def delete(self, provider: str, auth_id: str):
|
|
|
|
|
provider_id = DatasourceProviderID(provider)
|
|
|
|
|
plugin_id = provider_id.plugin_id
|
|
|
|
|
provider_name = provider_id.provider_name
|
|
|
|
|
if not current_user.is_editor:
|
|
|
|
|
raise Forbidden()
|
|
|
|
|
datasource_provider_service = DatasourceProviderService()
|
|
|
|
|
datasource_provider_service.remove_datasource_credentials(
|
|
|
|
|
tenant_id=current_user.current_tenant_id,
|
|
|
|
|
auth_id=auth_id,
|
|
|
|
|
provider=args["provider"],
|
|
|
|
|
plugin_id=args["plugin_id"],
|
|
|
|
|
provider=provider_name,
|
|
|
|
|
plugin_id=plugin_id,
|
|
|
|
|
)
|
|
|
|
|
return {"result": "success"}, 200
|
|
|
|
|
|
|
|
|
|
@setup_required
|
|
|
|
|
@login_required
|
|
|
|
|
@account_initialization_required
|
|
|
|
|
def patch(self, auth_id: str):
|
|
|
|
|
def patch(self, provider: str, auth_id: str):
|
|
|
|
|
provider_id = DatasourceProviderID(provider)
|
|
|
|
|
parser = reqparse.RequestParser()
|
|
|
|
|
parser.add_argument("provider", type=str, required=True, nullable=False, location="args")
|
|
|
|
|
parser.add_argument("plugin_id", type=str, required=True, nullable=False, location="args")
|
|
|
|
|
parser.add_argument("credentials", type=dict, required=True, nullable=False, location="json")
|
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
if not current_user.is_editor:
|
|
|
|
|
@ -169,8 +187,8 @@ class DatasourceAuthUpdateDeleteApi(Resource):
|
|
|
|
|
datasource_provider_service.update_datasource_credentials(
|
|
|
|
|
tenant_id=current_user.current_tenant_id,
|
|
|
|
|
auth_id=auth_id,
|
|
|
|
|
provider=args["provider"],
|
|
|
|
|
plugin_id=args["plugin_id"],
|
|
|
|
|
provider=provider_id.provider_name,
|
|
|
|
|
plugin_id=provider_id.plugin_id,
|
|
|
|
|
credentials=args["credentials"],
|
|
|
|
|
)
|
|
|
|
|
except CredentialsValidateFailedError as ex:
|
|
|
|
|
@ -193,21 +211,21 @@ class DatasourceAuthListApi(Resource):
|
|
|
|
|
|
|
|
|
|
# Import Rag Pipeline
|
|
|
|
|
api.add_resource(
|
|
|
|
|
DatasourcePluginOauthApi,
|
|
|
|
|
"/oauth/plugin/datasource",
|
|
|
|
|
DatasourcePluginOAuthAuthorizationUrl,
|
|
|
|
|
"/oauth/plugin/<path:provider>/datasource/get-authorization-url",
|
|
|
|
|
)
|
|
|
|
|
api.add_resource(
|
|
|
|
|
DatasourceOauthCallback,
|
|
|
|
|
"/oauth/plugin/datasource/callback",
|
|
|
|
|
DatasourceOAuthCallback,
|
|
|
|
|
"/oauth/plugin/<path:provider>/datasource/callback",
|
|
|
|
|
)
|
|
|
|
|
api.add_resource(
|
|
|
|
|
DatasourceAuth,
|
|
|
|
|
"/auth/plugin/datasource",
|
|
|
|
|
"/auth/plugin/datasource/<path:provider>",
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
api.add_resource(
|
|
|
|
|
DatasourceAuthUpdateDeleteApi,
|
|
|
|
|
"/auth/plugin/datasource/<string:auth_id>",
|
|
|
|
|
"/auth/plugin/datasource/<path:provider>/<string:auth_id>",
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
api.add_resource(
|
|
|
|
|
|