bito-code-review[bot] commented on code in PR #38469:
URL: https://github.com/apache/superset/pull/38469#discussion_r2895356898


##########
superset/security/manager.py:
##########
@@ -480,6 +480,60 @@ def request_loader(self, request: Request) -> 
Optional[User]:
             return self.get_guest_user_from_request(request)
         return None
 
+    def set_oauth_session(self, provider: str, oauth_response: dict[str, Any]) 
-> None:
+        """
+        Override to persist the full OAuth token response before FAB reduces 
it to
+        ``session["oauth"] = (access_token, secret)`` tuple.
+        """
+        # pylint: disable=import-outside-toplevel
+        from flask import session
+
+        super().set_oauth_session(provider, oauth_response)
+        # FAB stores only (access_token, secret) in session["oauth"].
+        # We need the full response (expires_in, refresh_token, …) for 
upstream forwarding.
+        session["oauth_full_token"] = dict(oauth_response)  # noqa: S105
+
+    def auth_user_oauth(self, userinfo: dict[str, Any]) -> Any:
+        """
+        Override to save the upstream OAuth token when a user logs in via 
OAuth.
+
+        If ``save_token: True`` is set in the matching OAUTH_PROVIDERS entry 
and
+        ``DATABASE_OAUTH2_UPSTREAM_PROVIDERS`` maps a database to this 
provider,
+        the token will be forwarded to that database instead of triggering a
+        separate OAuth2 dance.
+        """
+        # pylint: disable=import-outside-toplevel
+        from flask import current_app as flask_app, session
+
+        user = super().auth_user_oauth(userinfo)
+        if user:
+            provider = session.get("oauth_provider")
+            # Use the full token dict saved by set_oauth_session, not the
+            # (access_token, secret) tuple that FAB stores in session["oauth"].
+            token = session.get("oauth_full_token")
+            if token and provider:
+                provider_config = next(
+                    (
+                        p
+                        for p in flask_app.config.get("OAUTH_PROVIDERS", [])
+                        if p.get("name") == provider
+                    ),
+                    None,
+                )
+                if provider_config and provider_config.get("save_token"):
+                    from superset.utils.oauth2 import save_user_provider_token
+
+                    try:
+                        save_user_provider_token(user.id, provider, token)
+                    except Exception:  # pylint: disable=broad-except

Review Comment:
   <div>
   
   
   <div id="suggestion">
   <div id="issue"><b>Blind exception catch without specificity</b></div>
   <div id="fix">
   
   Replace the broad `Exception` catch with specific exception types. Consider 
catching `Exception` only if necessary, or specify the expected exceptions 
(e.g., `OSError`, `ValueError`).
   </div>
   
   
   <details>
   <summary>
   <b>Code suggestion</b>
   </summary>
   <blockquote>Check the AI-generated fix before applying</blockquote>
   <div id="code">
   
   
   ````suggestion
                       from superset.utils.oauth2 import 
save_user_provider_token
    
                       try:
                           save_user_provider_token(user.id, provider, token)
                       except (OSError, ValueError) as e:  # pylint: 
disable=broad-except
   ````
   
   </div>
   </details>
   
   
   
   </div>
   
   
   
   
   <small><i>Code Review Run #429f28</i></small>
   </div>
   
   ---
   Should Bito avoid suggestions like this for future reviews? (<a 
href=https://alpha.bito.ai/home/ai-agents/review-rules>Manage Rules</a>)
   - [ ] Yes, avoid them



##########
superset/migrations/versions/2026-03-06_12-00_a1b2c3d4e5f6_add_upstream_oauth_tokens.py:
##########
@@ -0,0 +1,73 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+"""add_upstream_oauth_tokens
+
+Revision ID: a1b2c3d4e5f6
+Revises: f5b5f88d8526
+Create Date: 2026-03-06 12:00:00.000000
+
+"""
+
+import sqlalchemy as sa
+from alembic import op
+
+# revision identifiers, used by Alembic.
+revision = "a1b2c3d4e5f6"
+down_revision = "f5b5f88d8526"
+
+
+def upgrade() -> None:
+    op.create_table(
+        "upstream_oauth_tokens",
+        sa.Column("id", sa.Integer(), nullable=False),
+        sa.Column("user_id", sa.Integer(), nullable=False),
+        sa.Column("provider", sa.String(256), nullable=False),
+        sa.Column("access_token", sa.Text(), nullable=True),
+        sa.Column("access_token_expiration", sa.DateTime(), nullable=True),
+        sa.Column("refresh_token", sa.Text(), nullable=True),
+        sa.Column("created_on", sa.DateTime(), nullable=True),
+        sa.Column("changed_on", sa.DateTime(), nullable=True),
+        sa.Column("created_by_fk", sa.Integer(), nullable=True),
+        sa.Column("changed_by_fk", sa.Integer(), nullable=True),
+        sa.ForeignKeyConstraint(
+            ["created_by_fk"],
+            ["ab_user.id"],
+        ),
+        sa.ForeignKeyConstraint(
+            ["changed_by_fk"],
+            ["ab_user.id"],
+        ),
+        sa.ForeignKeyConstraint(
+            ["user_id"],
+            ["ab_user.id"],
+            ondelete="CASCADE",
+        ),
+        sa.PrimaryKeyConstraint("id"),
+    )
+    op.create_index(
+        "idx_upstream_oauth_tokens_user_provider",
+        "upstream_oauth_tokens",
+        ["user_id", "provider"],
+    )
+

Review Comment:
   <div>
   
   
   <div id="suggestion">
   <div id="issue"><b>Missing unique constraint on OAuth index</b></div>
   <div id="fix">
   
   In OAuth token management, each user should have at most one token per 
provider to avoid conflicts. Making this index unique aligns with OAuth 
standards and prevents data integrity issues, similar to unique indexes used 
elsewhere in the codebase for deduplication.
   </div>
   
   
   <details>
   <summary>
   <b>Code suggestion</b>
   </summary>
   <blockquote>Check the AI-generated fix before applying</blockquote>
   <div id="code">
   
   
   ````suggestion
       op.create_index(
           "idx_upstream_oauth_tokens_user_provider",
           "upstream_oauth_tokens",
           ["user_id", "provider"],
           unique=True,
       )
   ````
   
   </div>
   </details>
   
   
   
   </div>
   
   
   
   
   <small><i>Code Review Run #429f28</i></small>
   </div>
   
   ---
   Should Bito avoid suggestions like this for future reviews? (<a 
href=https://alpha.bito.ai/home/ai-agents/review-rules>Manage Rules</a>)
   - [ ] Yes, avoid them



##########
tests/unit_tests/utils/oauth2_tests.py:
##########
@@ -335,3 +337,68 @@ def test_encode_decode_oauth2_state(
     assert "code_verifier" not in decoded
     assert decoded["database_id"] == 1
     assert decoded["user_id"] == 2
+
+
+# ---- Upstream provider token tests ----
+
+
+def test_get_upstream_provider_token_valid(mocker: MockerFixture) -> None:
+    """
+    Test `get_upstream_provider_token` returns the access token when it is 
valid.
+    """
+    db = mocker.patch("superset.utils.oauth2.db")
+    token = mocker.MagicMock()
+    token.access_token = "valid-token"  # noqa: S105
+    token.access_token_expiration = datetime(2024, 1, 2)

Review Comment:
   <div>
   
   
   <div id="suggestion">
   <div id="issue"><b>Datetime without timezone information</b></div>
   <div id="fix">
   
   Datetime object created without `tzinfo` argument at line 352. Add timezone 
information using `datetime.timezone.utc` or `pytz` for consistency.
   </div>
   
   
   <details>
   <summary>
   <b>Code suggestion</b>
   </summary>
   <blockquote>Check the AI-generated fix before applying</blockquote>
   <div id="code">
   
   
   ```
    -from datetime import datetime
    +from datetime import datetime, timezone
     from typing import cast
    @@ -349,7 +350,7 @@
         db = mocker.patch("superset.utils.oauth2.db")
         token = mocker.MagicMock()
         token.access_token = "valid-token"  # noqa: S105
    -    token.access_token_expiration = datetime(2024, 1, 2)
    +    token.access_token_expiration = datetime(2024, 1, 2, 
tzinfo=timezone.utc)
   ```
   
   </div>
   </details>
   
   
   
   </div>
   
   
   
   
   <small><i>Code Review Run #429f28</i></small>
   </div>
   
   ---
   Should Bito avoid suggestions like this for future reviews? (<a 
href=https://alpha.bito.ai/home/ai-agents/review-rules>Manage Rules</a>)
   - [ ] Yes, avoid them



##########
tests/unit_tests/utils/oauth2_tests.py:
##########
@@ -335,3 +337,68 @@ def test_encode_decode_oauth2_state(
     assert "code_verifier" not in decoded
     assert decoded["database_id"] == 1
     assert decoded["user_id"] == 2
+
+
+# ---- Upstream provider token tests ----
+
+
+def test_get_upstream_provider_token_valid(mocker: MockerFixture) -> None:
+    """
+    Test `get_upstream_provider_token` returns the access token when it is 
valid.
+    """
+    db = mocker.patch("superset.utils.oauth2.db")
+    token = mocker.MagicMock()
+    token.access_token = "valid-token"  # noqa: S105
+    token.access_token_expiration = datetime(2024, 1, 2)
+    db.session.query().filter_by().one_or_none.return_value = token
+
+    with freeze_time("2024-01-01"):
+        result = get_upstream_provider_token("keycloak", 1)
+
+    assert result == "valid-token"
+
+
+def test_get_upstream_provider_token_expired_no_refresh(mocker: MockerFixture) 
-> None:
+    """
+    Test `get_upstream_provider_token` deletes the record and returns None when
+    the token is expired and there is no refresh token.
+    """
+    db = mocker.patch("superset.utils.oauth2.db")
+    token = mocker.MagicMock()
+    token.access_token = "expired-token"  # noqa: S105
+    token.access_token_expiration = datetime(2024, 1, 1)
+    token.refresh_token = None
+    db.session.query().filter_by().one_or_none.return_value = token
+
+    with freeze_time("2024-01-02"):
+        result = get_upstream_provider_token("keycloak", 1)
+
+    assert result is None
+    db.session.delete.assert_called_once_with(token)
+    db.session.commit.assert_called_once()
+
+
+def test_get_upstream_provider_token_expired_calls_refresh(
+    mocker: MockerFixture,
+) -> None:
+    """
+    Test `get_upstream_provider_token` calls the refresh path when the token
+    is expired but a refresh token is present.
+    """
+    db = mocker.patch("superset.utils.oauth2.db")
+    token = mocker.MagicMock()
+    token.access_token = "expired-token"  # noqa: S105
+    token.access_token_expiration = datetime(2024, 1, 1)

Review Comment:
   <div>
   
   
   <div id="suggestion">
   <div id="issue"><b>Datetime without timezone information</b></div>
   <div id="fix">
   
   Datetime object created without `tzinfo` argument at line 369. Add timezone 
information using `datetime.timezone.utc` or `pytz` for consistency.
   </div>
   
   
   <details>
   <summary>
   <b>Code suggestion</b>
   </summary>
   <blockquote>Check the AI-generated fix before applying</blockquote>
   <div id="code">
   
   
   ````suggestion
       db = mocker.patch("superset.utils.oauth2.db")
       token = mocker.MagicMock()
       token.access_token = "expired-token"  # noqa: S105
       token.access_token_expiration = datetime(2024, 1, 1, tzinfo=timezone.utc)
   ````
   
   </div>
   </details>
   
   
   
   </div>
   
   
   
   
   <small><i>Code Review Run #429f28</i></small>
   </div>
   
   ---
   Should Bito avoid suggestions like this for future reviews? (<a 
href=https://alpha.bito.ai/home/ai-agents/review-rules>Manage Rules</a>)
   - [ ] Yes, avoid them



##########
superset/utils/oauth2.py:
##########
@@ -276,3 +276,102 @@ def check_for_oauth2(database: Database) -> 
Iterator[None]:
         if database.is_oauth2_enabled() and 
database.db_engine_spec.needs_oauth2(ex):
             database.db_engine_spec.start_oauth2_dance(database)
         raise
+
+
+def save_user_provider_token(
+    user_id: int,
+    provider: str,
+    token_response: dict[str, Any],
+) -> None:
+    """
+    Upsert an UpstreamOAuthToken row for the given user + provider.
+    """
+    from superset.models.core import UpstreamOAuthToken
+
+    token: UpstreamOAuthToken | None = (
+        db.session.query(UpstreamOAuthToken)
+        .filter_by(user_id=user_id, provider=provider)
+        .one_or_none()
+    )
+    if token is None:
+        token = UpstreamOAuthToken(user_id=user_id, provider=provider)
+
+    token.access_token = token_response.get("access_token")
+    expires_in = token_response.get("expires_in")
+    token.access_token_expiration = (
+        datetime.now() + timedelta(seconds=expires_in) if expires_in else None
+    )
+    token.refresh_token = token_response.get("refresh_token")
+    db.session.add(token)
+    db.session.commit()
+
+
+def get_upstream_provider_token(provider: str, user_id: int) -> str | None:
+    """
+    Retrieve a valid access token for the given provider and user.
+
+    If the token is expired and a refresh token exists, attempt to refresh it.
+    Returns None if no valid token is available.
+    """
+    from superset.models.core import UpstreamOAuthToken
+
+    token: UpstreamOAuthToken | None = (
+        db.session.query(UpstreamOAuthToken)
+        .filter_by(user_id=user_id, provider=provider)
+        .one_or_none()
+    )
+    if token is None:
+        return None
+
+    now = datetime.now()
+    if token.access_token_expiration is None or token.access_token_expiration 
> now:
+        return token.access_token
+
+    # Token is expired
+    if token.refresh_token:
+        return _refresh_upstream_provider_token(token, provider)
+
+    db.session.delete(token)
+    db.session.commit()
+    return None
+
+
+def _refresh_upstream_provider_token(
+    token: UpstreamOAuthToken,
+    provider: str,
+) -> str | None:
+    """
+    Use the refresh token to obtain a new access token from the provider.
+    Updates and persists the token if successful; deletes it on failure.
+    """
+    from flask import current_app as flask_app
+
+    try:
+        remote_app = 
flask_app.extensions["authlib.integrations.flask_client"][provider]
+        token_response = remote_app.fetch_access_token(
+            grant_type="refresh_token",
+            refresh_token=token.refresh_token,
+        )
+    except Exception:  # pylint: disable=broad-except

Review Comment:
   <div>
   
   
   <div id="suggestion">
   <div id="issue"><b>Blind exception catch too broad</b></div>
   <div id="fix">
   
   Line 355 catches a bare `Exception` which is too broad. Catch specific 
exception types instead, such as `AuthlibBaseError` or other provider-specific 
exceptions.
   </div>
   
   
   <details>
   <summary>
   <b>Code suggestion</b>
   </summary>
   <blockquote>Check the AI-generated fix before applying</blockquote>
   <div id="code">
   
   
   ````suggestion
           remote_app = 
flask_app.extensions["authlib.integrations.flask_client"][provider]
           token_response = remote_app.fetch_access_token(
               grant_type="refresh_token",
               refresh_token=token.refresh_token,
           )
       except (AuthlibBaseError, RequestException):  # pylint: 
disable=broad-except
   ````
   
   </div>
   </details>
   
   
   
   </div>
   
   
   
   
   <small><i>Code Review Run #429f28</i></small>
   </div>
   
   ---
   Should Bito avoid suggestions like this for future reviews? (<a 
href=https://alpha.bito.ai/home/ai-agents/review-rules>Manage Rules</a>)
   - [ ] Yes, avoid them



##########
superset/utils/oauth2.py:
##########
@@ -276,3 +276,102 @@ def check_for_oauth2(database: Database) -> 
Iterator[None]:
         if database.is_oauth2_enabled() and 
database.db_engine_spec.needs_oauth2(ex):
             database.db_engine_spec.start_oauth2_dance(database)
         raise
+
+
+def save_user_provider_token(
+    user_id: int,
+    provider: str,
+    token_response: dict[str, Any],
+) -> None:
+    """
+    Upsert an UpstreamOAuthToken row for the given user + provider.
+    """
+    from superset.models.core import UpstreamOAuthToken
+
+    token: UpstreamOAuthToken | None = (
+        db.session.query(UpstreamOAuthToken)
+        .filter_by(user_id=user_id, provider=provider)
+        .one_or_none()
+    )
+    if token is None:
+        token = UpstreamOAuthToken(user_id=user_id, provider=provider)
+
+    token.access_token = token_response.get("access_token")
+    expires_in = token_response.get("expires_in")
+    token.access_token_expiration = (
+        datetime.now() + timedelta(seconds=expires_in) if expires_in else None
+    )
+    token.refresh_token = token_response.get("refresh_token")
+    db.session.add(token)
+    db.session.commit()
+
+
+def get_upstream_provider_token(provider: str, user_id: int) -> str | None:
+    """
+    Retrieve a valid access token for the given provider and user.
+
+    If the token is expired and a refresh token exists, attempt to refresh it.
+    Returns None if no valid token is available.
+    """
+    from superset.models.core import UpstreamOAuthToken
+
+    token: UpstreamOAuthToken | None = (
+        db.session.query(UpstreamOAuthToken)
+        .filter_by(user_id=user_id, provider=provider)
+        .one_or_none()
+    )
+    if token is None:
+        return None
+
+    now = datetime.now()
+    if token.access_token_expiration is None or token.access_token_expiration 
> now:
+        return token.access_token
+
+    # Token is expired
+    if token.refresh_token:
+        return _refresh_upstream_provider_token(token, provider)
+
+    db.session.delete(token)
+    db.session.commit()
+    return None
+
+
+def _refresh_upstream_provider_token(
+    token: UpstreamOAuthToken,
+    provider: str,
+) -> str | None:
+    """
+    Use the refresh token to obtain a new access token from the provider.
+    Updates and persists the token if successful; deletes it on failure.
+    """
+    from flask import current_app as flask_app
+
+    try:
+        remote_app = 
flask_app.extensions["authlib.integrations.flask_client"][provider]
+        token_response = remote_app.fetch_access_token(
+            grant_type="refresh_token",
+            refresh_token=token.refresh_token,
+        )
+    except Exception:  # pylint: disable=broad-except
+        logger.warning(
+            "Failed to refresh upstream OAuth token for provider %s", 
provider, exc_info=True
+        )
+        db.session.delete(token)
+        db.session.commit()
+        return None
+
+    if "access_token" not in token_response:
+        db.session.delete(token)
+        db.session.commit()
+        return None
+
+    token.access_token = token_response["access_token"]
+    expires_in = token_response.get("expires_in")
+    token.access_token_expiration = (
+        datetime.now() + timedelta(seconds=expires_in) if expires_in else None

Review Comment:
   <div>
   
   
   <div id="suggestion">
   <div id="issue"><b>datetime.now() called without timezone</b></div>
   <div id="fix">
   
   Line 302 calls `datetime.now()` without a `tz` argument. Use 
`datetime.now(tz=timezone.utc)` for timezone-aware datetime objects. Note: 
Similar issues exist at lines 112, 167, 326, and 371.
   </div>
   
   
   <details>
   <summary>
   <b>Code suggestion</b>
   </summary>
   <blockquote>Check the AI-generated fix before applying</blockquote>
   <div id="code">
   
   
   ````suggestion
       expires_in = token_response.get("expires_in")
       token.access_token_expiration = (
           datetime.now(tz=timezone.utc) + timedelta(seconds=expires_in) if 
expires_in else None
   ````
   
   </div>
   </details>
   
   
   
   </div>
   
   
   
   
   <small><i>Code Review Run #429f28</i></small>
   </div>
   
   ---
   Should Bito avoid suggestions like this for future reviews? (<a 
href=https://alpha.bito.ai/home/ai-agents/review-rules>Manage Rules</a>)
   - [ ] Yes, avoid them



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to