feat: add avatar_url to datasource providers and update OAuth handling

This commit is contained in:
Harry 2025-07-18 19:47:52 +08:00
parent 34a6ed74b6
commit 23a5ff410e
5 changed files with 47 additions and 10 deletions

View File

@ -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

View File

@ -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.")

View File

@ -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 ###

View File

@ -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)

View File

@ -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()