mirror of
https://github.com/langgenius/dify.git
synced 2025-10-12 17:37:02 +00:00
r2
This commit is contained in:
parent
a82ab1d152
commit
8a147a00e8
@ -129,11 +129,11 @@ class DatasourceAuth(Resource):
|
|||||||
return {"result": datasources}, 200
|
return {"result": datasources}, 200
|
||||||
|
|
||||||
|
|
||||||
class DatasourceAuthDeleteApi(Resource):
|
class DatasourceAuthUpdateDeleteApi(Resource):
|
||||||
@setup_required
|
@setup_required
|
||||||
@login_required
|
@login_required
|
||||||
@account_initialization_required
|
@account_initialization_required
|
||||||
def delete(self):
|
def delete(self, auth_id: str):
|
||||||
parser = reqparse.RequestParser()
|
parser = reqparse.RequestParser()
|
||||||
parser.add_argument("provider", type=str, required=True, nullable=False, location="args")
|
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("plugin_id", type=str, required=True, nullable=False, location="args")
|
||||||
@ -143,11 +143,37 @@ class DatasourceAuthDeleteApi(Resource):
|
|||||||
datasource_provider_service = DatasourceProviderService()
|
datasource_provider_service = DatasourceProviderService()
|
||||||
datasource_provider_service.remove_datasource_credentials(
|
datasource_provider_service.remove_datasource_credentials(
|
||||||
tenant_id=current_user.current_tenant_id,
|
tenant_id=current_user.current_tenant_id,
|
||||||
|
auth_id=auth_id,
|
||||||
provider=args["provider"],
|
provider=args["provider"],
|
||||||
plugin_id=args["plugin_id"]
|
plugin_id=args["plugin_id"]
|
||||||
)
|
)
|
||||||
return {"result": "success"}, 200
|
return {"result": "success"}, 200
|
||||||
|
|
||||||
|
@setup_required
|
||||||
|
@login_required
|
||||||
|
@account_initialization_required
|
||||||
|
def patch(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")
|
||||||
|
parser.add_argument("credentials", type=dict, required=True, nullable=False, location="json")
|
||||||
|
args = parser.parse_args()
|
||||||
|
if not current_user.is_editor:
|
||||||
|
raise Forbidden()
|
||||||
|
try:
|
||||||
|
datasource_provider_service = DatasourceProviderService()
|
||||||
|
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"],
|
||||||
|
credentials=args["credentials"],
|
||||||
|
)
|
||||||
|
except CredentialsValidateFailedError as ex:
|
||||||
|
raise ValueError(str(ex))
|
||||||
|
|
||||||
|
return {"result": "success"}, 201
|
||||||
|
|
||||||
|
|
||||||
# Import Rag Pipeline
|
# Import Rag Pipeline
|
||||||
api.add_resource(
|
api.add_resource(
|
||||||
@ -162,3 +188,8 @@ api.add_resource(
|
|||||||
DatasourceAuth,
|
DatasourceAuth,
|
||||||
"/auth/plugin/datasource",
|
"/auth/plugin/datasource",
|
||||||
)
|
)
|
||||||
|
|
||||||
|
api.add_resource(
|
||||||
|
DatasourceAuth,
|
||||||
|
"/auth/plugin/datasource/<string:auth_id>",
|
||||||
|
)
|
||||||
|
@ -25,7 +25,7 @@ class DatasourceProvider(Base):
|
|||||||
__tablename__ = "datasource_providers"
|
__tablename__ = "datasource_providers"
|
||||||
__table_args__ = (
|
__table_args__ = (
|
||||||
db.PrimaryKeyConstraint("id", name="datasource_provider_pkey"),
|
db.PrimaryKeyConstraint("id", name="datasource_provider_pkey"),
|
||||||
db.UniqueConstraint("plugin_id", "provider", "auth_type", name="datasource_provider_auth_type_provider_idx"),
|
db.UniqueConstraint("plugin_id", "provider", name="datasource_provider_auth_type_provider_idx"),
|
||||||
)
|
)
|
||||||
id = db.Column(StringUUID, server_default=db.text("uuid_generate_v4()"))
|
id = db.Column(StringUUID, server_default=db.text("uuid_generate_v4()"))
|
||||||
tenant_id = db.Column(StringUUID, nullable=False)
|
tenant_id = db.Column(StringUUID, nullable=False)
|
||||||
|
@ -38,7 +38,7 @@ class DatasourceProviderService:
|
|||||||
# Get all provider configurations of the current workspace
|
# Get all provider configurations of the current workspace
|
||||||
datasource_provider = (
|
datasource_provider = (
|
||||||
db.session.query(DatasourceProvider)
|
db.session.query(DatasourceProvider)
|
||||||
.filter_by(tenant_id=tenant_id, plugin_id=plugin_id, auth_type="api_key")
|
.filter_by(tenant_id=tenant_id, plugin_id=plugin_id, provider=provider, auth_type="api_key")
|
||||||
.first()
|
.first()
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -46,7 +46,6 @@ class DatasourceProviderService:
|
|||||||
tenant_id=tenant_id,
|
tenant_id=tenant_id,
|
||||||
provider_id=f"{plugin_id}/{provider}"
|
provider_id=f"{plugin_id}/{provider}"
|
||||||
)
|
)
|
||||||
if not datasource_provider:
|
|
||||||
for key, value in credentials.items():
|
for key, value in credentials.items():
|
||||||
if key in provider_credential_secret_variables:
|
if key in provider_credential_secret_variables:
|
||||||
# if send [__HIDDEN__] in secret input, it will be same as original value
|
# if send [__HIDDEN__] in secret input, it will be same as original value
|
||||||
@ -60,19 +59,6 @@ class DatasourceProviderService:
|
|||||||
)
|
)
|
||||||
db.session.add(datasource_provider)
|
db.session.add(datasource_provider)
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
else:
|
|
||||||
original_credentials = datasource_provider.encrypted_credentials
|
|
||||||
for key, value in credentials.items():
|
|
||||||
if key in provider_credential_secret_variables:
|
|
||||||
# if send [__HIDDEN__] in secret input, it will be same as original value
|
|
||||||
if value == HIDDEN_VALUE and key in original_credentials:
|
|
||||||
original_value = encrypter.encrypt_token(tenant_id, original_credentials[key])
|
|
||||||
credentials[key] = encrypter.encrypt_token(tenant_id, original_value)
|
|
||||||
else:
|
|
||||||
credentials[key] = encrypter.encrypt_token(tenant_id, value)
|
|
||||||
|
|
||||||
datasource_provider.encrypted_credentials = credentials
|
|
||||||
db.session.commit()
|
|
||||||
else:
|
else:
|
||||||
raise CredentialsValidateFailedError()
|
raise CredentialsValidateFailedError()
|
||||||
|
|
||||||
@ -134,7 +120,44 @@ class DatasourceProviderService:
|
|||||||
|
|
||||||
return copy_credentials_list
|
return copy_credentials_list
|
||||||
|
|
||||||
def remove_datasource_credentials(self, tenant_id: str, provider: str, plugin_id: str) -> None:
|
def update_datasource_credentials(self, tenant_id: str, auth_id: str, provider: str, plugin_id: str, credentials: dict) -> None:
|
||||||
|
"""
|
||||||
|
update datasource credentials.
|
||||||
|
"""
|
||||||
|
credential_valid = self.provider_manager.validate_provider_credentials(
|
||||||
|
tenant_id=tenant_id, user_id=current_user.id, provider=provider, credentials=credentials
|
||||||
|
)
|
||||||
|
if credential_valid:
|
||||||
|
# Get all provider configurations of the current workspace
|
||||||
|
datasource_provider = (
|
||||||
|
db.session.query(DatasourceProvider)
|
||||||
|
.filter_by(tenant_id=tenant_id, id=auth_id, provider=provider, plugin_id=plugin_id)
|
||||||
|
.first()
|
||||||
|
)
|
||||||
|
|
||||||
|
provider_credential_secret_variables = self.extract_secret_variables(
|
||||||
|
tenant_id=tenant_id,
|
||||||
|
provider_id=f"{plugin_id}/{provider}"
|
||||||
|
)
|
||||||
|
if not datasource_provider:
|
||||||
|
raise ValueError("Datasource provider not found")
|
||||||
|
else:
|
||||||
|
original_credentials = datasource_provider.encrypted_credentials
|
||||||
|
for key, value in credentials.items():
|
||||||
|
if key in provider_credential_secret_variables:
|
||||||
|
# if send [__HIDDEN__] in secret input, it will be same as original value
|
||||||
|
if value == HIDDEN_VALUE and key in original_credentials:
|
||||||
|
original_value = encrypter.encrypt_token(tenant_id, original_credentials[key])
|
||||||
|
credentials[key] = encrypter.encrypt_token(tenant_id, original_value)
|
||||||
|
else:
|
||||||
|
credentials[key] = encrypter.encrypt_token(tenant_id, value)
|
||||||
|
|
||||||
|
datasource_provider.encrypted_credentials = credentials
|
||||||
|
db.session.commit()
|
||||||
|
else:
|
||||||
|
raise CredentialsValidateFailedError()
|
||||||
|
|
||||||
|
def remove_datasource_credentials(self, tenant_id: str, auth_id: str, provider: str, plugin_id: str) -> None:
|
||||||
"""
|
"""
|
||||||
remove datasource credentials.
|
remove datasource credentials.
|
||||||
|
|
||||||
@ -145,7 +168,7 @@ class DatasourceProviderService:
|
|||||||
"""
|
"""
|
||||||
datasource_provider = (
|
datasource_provider = (
|
||||||
db.session.query(DatasourceProvider)
|
db.session.query(DatasourceProvider)
|
||||||
.filter_by(tenant_id=tenant_id, provider=provider, plugin_id=plugin_id)
|
.filter_by(tenant_id=tenant_id, id=auth_id, provider=provider, plugin_id=plugin_id)
|
||||||
.first()
|
.first()
|
||||||
)
|
)
|
||||||
if datasource_provider:
|
if datasource_provider:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user