aminghadersohi commented on code in PR #33976:
URL: https://github.com/apache/superset/pull/33976#discussion_r2230100232


##########
superset/mcp_service/auth.py:
##########
@@ -0,0 +1,96 @@
+# 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.
+
+import logging
+from typing import Any, Optional
+
+logger = logging.getLogger(__name__)
+
+
+def get_user_from_request() -> Any:
+    """
+    Extract user info from the request context (e.g., from Bearer token, 
headers, etc.).
+    By default, returns admin user. Override for OIDC/OAuth/Okta integration.
+    """
+    from flask import current_app
+
+    from superset.extensions import security_manager
+
+    admin_username = current_app.config.get("MCP_ADMIN_USERNAME", "admin")
+    return security_manager.get_user_by_username(admin_username)
+
+
+def impersonate_user(user: Any, run_as: Optional[str] = None) -> Any:
+    """
+    Optionally impersonate another user if allowed. By default, returns the 
same user.
+    Override to enforce impersonation rules.
+    """
+    return user
+
+
+def has_permission(user: Any, tool_func: Any) -> bool:
+    """
+    Check if the user has permission to run the tool. By default, always True.
+    Override for RBAC.
+    """
+    return True
+
+
+def log_access(user: Any, tool_name: str, args: Any, kwargs: Any) -> None:
+    """
+    Log access/action for observability/audit. By default, does nothing.
+    Override to log to your system.
+    """
+    pass
+
+
+def mcp_auth_hook(tool_func: Any) -> Any:
+    """
+    Decorator for MCP tool functions to enforce auth, impersonation, RBAC, and 
logging.
+    Also sets up Flask user context (g.user) for downstream DAO/model code.
+    All logic is overridable for enterprise integration.
+    """
+    import functools
+
+    from flask import current_app, g
+    from flask_login import AnonymousUserMixin
+
+    from superset.extensions import security_manager
+
+    @functools.wraps(tool_func)
+    def wrapper(*args: Any, **kwargs: Any) -> Any:
+        # --- Setup user context (was _setup_user_context) ---
+        admin_username = current_app.config.get("MCP_ADMIN_USERNAME", "admin")
+        admin_user = security_manager.get_user_by_username(admin_username)

Review Comment:
     @dpgaspar Thanks for the feedback on authentication flexibility! I've 
already implemented the
     JWT-based authentication as discussed, and now made it fully configurable 
as you suggested.
   
     What's implemented:
   
     1. JWT User Authentication - No more hardcoded admin:
       - Extracts user from JWT token claims via FastMCP's get_access_token()
       - Maps JWT identity to actual Superset users in the database
       - Configurable user resolver for different JWT claim structures
     2. Scope-Based Permissions:
       - JWT scopes mapped to tool permissions (e.g., dashboard:read, 
chart:write)
       - Falls back gracefully when no JWT is present (dev mode)
       - Enhanced audit logging with JWT context
     3. Full Configurability via superset_config.py:
   
   ### Configure auth factory
   ```
     MCP_AUTH_FACTORY = my_custom_auth_factory
   ```
   
   ### Configure how to extract username from JWT
   ```
     def custom_user_resolver(access_token):
         # Handle your specific JWT structure
         return access_token.payload.get('preferred_username')
   
     MCP_USER_RESOLVER = custom_user_resolver
   ```
   
   ### Configure scopes, fallback users, etc.
   ```
     MCP_REQUIRED_SCOPES = ["superset:read", "superset:admin"]
     MCP_DEV_USERNAME = "dev_user"  # For local development
   ```
     4. Flexible Integration:
       - Works with any JWT provider (Auth0, Keycloak, Okta, etc.)
       - Supports both JWKS and direct public key validation
       - Compatible with Superset's existing auth providers
   



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