diff --git a/api/controllers/console/datasets/rag_pipeline/datasource_auth.py b/api/controllers/console/datasets/rag_pipeline/datasource_auth.py index 3359ec1986..16b32856cb 100644 --- a/api/controllers/console/datasets/rag_pipeline/datasource_auth.py +++ b/api/controllers/console/datasets/rag_pipeline/datasource_auth.py @@ -46,7 +46,7 @@ class DatasourcePluginOAuthAuthorizationUrl(Resource): user_id=current_user.id, tenant_id=tenant_id, plugin_id=plugin_id, provider=provider_name ) oauth_handler = OAuthHandler() - redirect_uri = f"{dify_config.CONSOLE_WEB_URL}/console/api/oauth/plugin/{provider_id}/datasource/callback" + redirect_uri = f"{dify_config.CONSOLE_API_URL}/console/api/oauth/plugin/{provider_id}/datasource/callback" oauth_client_params = oauth_config.system_credentials authorization_url_response = oauth_handler.get_authorization_url( @@ -71,10 +71,7 @@ class DatasourcePluginOAuthAuthorizationUrl(Resource): class DatasourceOAuthCallback(Resource): @setup_required def get(self, provider_id: str): - if not current_user.is_editor: - raise Forbidden() - - context_id = request.cookies.get("context_id") + context_id = request.cookies.get("context_id") or request.args.get("context_id") if not context_id: raise Forbidden("context_id not found") @@ -91,7 +88,7 @@ class DatasourceOAuthCallback(Resource): ) if not plugin_oauth_config: raise NotFound() - redirect_uri = f"{dify_config.CONSOLE_WEB_URL}/console/api/oauth/plugin/{provider_id}/datasource/callback" + redirect_uri = f"{dify_config.CONSOLE_API_URL}/console/api/oauth/plugin/{provider_id}/datasource/callback" oauth_handler = OAuthHandler() oauth_response = oauth_handler.get_credentials( tenant_id=tenant_id, @@ -106,10 +103,11 @@ class DatasourceOAuthCallback(Resource): datasource_provider_service.add_datasource_oauth_provider( tenant_id=tenant_id, provider_id=datasource_provider_id, + avatar_url=oauth_response.metadata.get("avatar_url") or None, + name=oauth_response.metadata.get("name") or None, credentials=dict(oauth_response.credentials), - name=None, ) - return redirect(f"{dify_config.CONSOLE_WEB_URL}") + return redirect(f"{dify_config.CONSOLE_WEB_URL}/oauth-callback") class DatasourceAuth(Resource): @@ -136,8 +134,7 @@ class DatasourceAuth(Resource): ) except CredentialsValidateFailedError as ex: raise ValueError(str(ex)) - - return {"result": "success"}, 201 + return redirect(f"{dify_config.CONSOLE_WEB_URL}/oauth-callback") @setup_required @login_required diff --git a/api/core/plugin/entities/plugin_daemon.py b/api/core/plugin/entities/plugin_daemon.py index fd6a08eba6..1d44bad808 100644 --- a/api/core/plugin/entities/plugin_daemon.py +++ b/api/core/plugin/entities/plugin_daemon.py @@ -191,6 +191,9 @@ class PluginOAuthAuthorizationUrlResponse(BaseModel): class PluginOAuthCredentialsResponse(BaseModel): + metadata: Mapping[str, Any] = Field( + default_factory=dict, description="The metadata of the OAuth, like avatar url, name, etc." + ) credentials: Mapping[str, Any] = Field(description="The credentials of the OAuth.") diff --git a/api/migrations/versions/2025_07_18_1825-2008609cf2bb_add_pipeline_info_12.py b/api/migrations/versions/2025_07_18_1825-2008609cf2bb_add_pipeline_info_12.py new file mode 100644 index 0000000000..03d3e49c41 --- /dev/null +++ b/api/migrations/versions/2025_07_18_1825-2008609cf2bb_add_pipeline_info_12.py @@ -0,0 +1,33 @@ +"""add_pipeline_info_12 + +Revision ID: 2008609cf2bb +Revises: 1bce102aef9a +Create Date: 2025-07-18 18:25:21.280897 + +""" +from alembic import op +import models as models +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = '2008609cf2bb' +down_revision = '1bce102aef9a' +branch_labels = None +depends_on = None + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + with op.batch_alter_table('datasource_providers', schema=None) as batch_op: + batch_op.add_column(sa.Column('avatar_url', sa.String(length=255), nullable=True)) + + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + with op.batch_alter_table('datasource_providers', schema=None) as batch_op: + batch_op.drop_column('avatar_url') + + # ### end Alembic commands ### diff --git a/api/models/oauth.py b/api/models/oauth.py index ed839e4fb1..aa4f7fdab0 100644 --- a/api/models/oauth.py +++ b/api/models/oauth.py @@ -34,5 +34,7 @@ class DatasourceProvider(Base): plugin_id: Mapped[str] = db.Column(db.String(255), nullable=False) auth_type: Mapped[str] = db.Column(db.String(255), nullable=False) encrypted_credentials: Mapped[dict] = db.Column(JSONB, nullable=False) + avatar_url: Mapped[str] = db.Column(db.String(255), nullable=True) + created_at: Mapped[datetime] = db.Column(db.DateTime, nullable=False, default=datetime.now) updated_at: Mapped[datetime] = db.Column(db.DateTime, nullable=False, default=datetime.now) diff --git a/api/services/datasource_provider_service.py b/api/services/datasource_provider_service.py index 5f1251d9d8..e2019c70fd 100644 --- a/api/services/datasource_provider_service.py +++ b/api/services/datasource_provider_service.py @@ -47,6 +47,7 @@ class DatasourceProviderService: name: str | None, tenant_id: str, provider_id: DatasourceProviderID, + avatar_url: str | None, credentials: dict, ) -> None: """ @@ -81,6 +82,7 @@ class DatasourceProviderService: plugin_id=provider_id.plugin_id, auth_type=credential_type.value, encrypted_credentials=credentials, + avatar_url=avatar_url, ) session.add(datasource_provider) session.commit()