From fcdbfbda4f19ee190f260d884502ec4fbd86bdcb Mon Sep 17 00:00:00 2001 From: jyong <718720800@qq.com> Date: Tue, 12 Aug 2025 17:56:28 +0800 Subject: [PATCH] add credential id --- .../entities/datasource_entities.py | 37 ++++----- api/services/datasource_provider_service.py | 75 +++++++++---------- 2 files changed, 56 insertions(+), 56 deletions(-) diff --git a/api/core/datasource/entities/datasource_entities.py b/api/core/datasource/entities/datasource_entities.py index 2f8f48f0ea..65ce838b72 100644 --- a/api/core/datasource/entities/datasource_entities.py +++ b/api/core/datasource/entities/datasource_entities.py @@ -309,53 +309,54 @@ class DatasourceMessage(ToolInvokeMessage): ######################### -# Online driver file +# Online drive file ######################### class OnlineDriveFile(BaseModel): """ - Online driver file + Online drive file """ - key: str = Field(..., description="The key of the file") - size: int = Field(..., description="The size of the file") + id: str = Field(..., description="The file ID") + name: str = Field(..., description="The file name") + size: int = Field(..., description="The file size") + type: str = Field(..., description="The file type: folder or file") class OnlineDriveFileBucket(BaseModel): """ - Online driver file bucket + Online drive file bucket """ - bucket: Optional[str] = Field(None, description="The bucket of the file") - files: list[OnlineDriveFile] = Field(..., description="The files of the bucket") - is_truncated: bool = Field(False, description="Whether the bucket has more files") + bucket: Optional[str] = Field(None, description="The file bucket") + files: list[OnlineDriveFile] = Field(..., description="The file list") + is_truncated: bool = Field(False, description="Whether the result is truncated") + next_page_parameters: Optional[dict] = Field(None, description="Parameters for fetching the next page") class OnlineDriveBrowseFilesRequest(BaseModel): """ - Get online driver file list request + Get online drive file list request """ - prefix: Optional[str] = Field(None, description="File path prefix for filtering eg: 'docs/dify/'") - bucket: Optional[str] = Field(None, description="Storage bucket name") - max_keys: int = Field(20, description="Maximum number of files to return") - start_after: Optional[str] = Field( - None, description="Pagination token for continuing from a specific file eg: 'docs/dify/1.txt'" - ) + bucket: Optional[str] = Field(None, description="The file bucket") + prefix: str = Field(..., description="The parent folder ID") + max_keys: int = Field(20, description="Page size for pagination") + next_page_parameters: Optional[dict] = Field(None, description="Parameters for fetching the next page") class OnlineDriveBrowseFilesResponse(BaseModel): """ - Get online driver file list response + Get online drive file list response """ - result: list[OnlineDriveFileBucket] = Field(..., description="The bucket of the files") + result: list[OnlineDriveFileBucket] = Field(..., description="The list of file buckets") class OnlineDriveDownloadFileRequest(BaseModel): """ - Get online driver file + Get online drive file """ id: str = Field(..., description="The id of the file") diff --git a/api/services/datasource_provider_service.py b/api/services/datasource_provider_service.py index c2c0b0f713..ef973612b0 100644 --- a/api/services/datasource_provider_service.py +++ b/api/services/datasource_provider_service.py @@ -165,44 +165,43 @@ class DatasourceProviderService: # refresh the credentials real_credentials_list = [] for datasource_provider in datasource_providers: - if datasource_provider.expires_at != -1 and (datasource_provider.expires_at - 60) < int(time.time()): - decrypted_credentials = self.decrypt_datasource_provider_credentials( - tenant_id=tenant_id, - datasource_provider=datasource_provider, - plugin_id=plugin_id, - provider=provider, - ) - datasource_provider_id = DatasourceProviderID(f"{plugin_id}/{provider}") - provider_name = datasource_provider_id.provider_name - redirect_uri = ( - f"{dify_config.CONSOLE_API_URL}/console/api/oauth/plugin/" - f"{datasource_provider_id}/datasource/callback" - ) - system_credentials = self.get_oauth_client(tenant_id, datasource_provider_id) - refreshed_credentials = OAuthHandler().refresh_credentials( - tenant_id=tenant_id, - user_id=current_user.id, - plugin_id=datasource_provider_id.plugin_id, - provider=provider_name, - redirect_uri=redirect_uri, - system_credentials=system_credentials or {}, - credentials=decrypted_credentials, - ) - datasource_provider.encrypted_credentials = self.encrypt_datasource_provider_credentials( - tenant_id=tenant_id, - raw_credentials=refreshed_credentials.credentials, - provider=provider, - plugin_id=plugin_id, - datasource_provider=datasource_provider, - ) - datasource_provider.expires_at = refreshed_credentials.expires_at - real_credentials = self.decrypt_datasource_provider_credentials( - tenant_id=tenant_id, - datasource_provider=datasource_provider, - plugin_id=plugin_id, - provider=provider, - ) - real_credentials_list.append(real_credentials) + decrypted_credentials = self.decrypt_datasource_provider_credentials( + tenant_id=tenant_id, + datasource_provider=datasource_provider, + plugin_id=plugin_id, + provider=provider, + ) + datasource_provider_id = DatasourceProviderID(f"{plugin_id}/{provider}") + provider_name = datasource_provider_id.provider_name + redirect_uri = ( + f"{dify_config.CONSOLE_API_URL}/console/api/oauth/plugin/" + f"{datasource_provider_id}/datasource/callback" + ) + system_credentials = self.get_oauth_client(tenant_id, datasource_provider_id) + refreshed_credentials = OAuthHandler().refresh_credentials( + tenant_id=tenant_id, + user_id=current_user.id, + plugin_id=datasource_provider_id.plugin_id, + provider=provider_name, + redirect_uri=redirect_uri, + system_credentials=system_credentials or {}, + credentials=decrypted_credentials, + ) + datasource_provider.encrypted_credentials = self.encrypt_datasource_provider_credentials( + tenant_id=tenant_id, + raw_credentials=refreshed_credentials.credentials, + provider=provider, + plugin_id=plugin_id, + datasource_provider=datasource_provider, + ) + datasource_provider.expires_at = refreshed_credentials.expires_at + real_credentials = self.decrypt_datasource_provider_credentials( + tenant_id=tenant_id, + datasource_provider=datasource_provider, + plugin_id=plugin_id, + provider=provider, + ) + real_credentials_list.append(real_credentials) session.commit() return real_credentials_list