ljluestc commented on issue #28314:
URL: https://github.com/apache/superset/issues/28314#issuecomment-2726193197

   ```
   
   import os
   import requests
   from flask import Flask, jsonify
   from dotenv import load_dotenv
   
   # Load environment variables
   load_dotenv()
   
   app = Flask(__name__)
   
   # Superset configuration (replace with your values)
   SUPERSET_URL = os.getenv("SUPERSET_URL", "http://localhost:8088";)
   SUPERSET_USERNAME = os.getenv("SUPERSET_USERNAME", "admin")
   SUPERSET_PASSWORD = os.getenv("SUPERSET_PASSWORD", "admin")
   DASHBOARD_ID = os.getenv("DASHBOARD_ID", "your-dashboard-id-here")  # Get 
from Superset UI
   
   def authenticate_superset():
       """Authenticate with Superset and return an access token."""
       url = f"{SUPERSET_URL}/api/v1/security/login"
       payload = {
           "username": SUPERSET_USERNAME,
           "password": SUPERSET_PASSWORD,
           "provider": "db"
       }
       headers = {"Content-Type": "application/json"}
       response = requests.post(url, json=payload, headers=headers)
       response.raise_for_status()
       return response.json()["access_token"]
   
   def get_guest_token(user_id: str, access_token: str):
       """Generate a guest token with RLS clause for the given user_id."""
       url = f"{SUPERSET_URL}/api/v1/security/guest_token/"
       payload = {
           "user": {
               "username": f"user_{user_id}",
               "first_name": "Guest",
               "last_name": "User"
           },
           "resources": [{"type": "dashboard", "id": DASHBOARD_ID}],
           "rls": [{"clause": f"user_id = '{user_id}'"}]  # RLS clause
       }
       headers = {
           "Authorization": f"Bearer {access_token}",
           "Content-Type": "application/json"
       }
       response = requests.post(url, json=payload, headers=headers)
       response.raise_for_status()
       return response.json()["token"]
   
   @app.route("/guest-token/<user_id>", methods=["GET"])
   def serve_guest_token(user_id):
       """Endpoint to serve guest token for a specific user."""
       try:
           access_token = authenticate_superset()
           guest_token = get_guest_token(user_id, access_token)
           return jsonify({"token": guest_token})
       except requests.RequestException as e:
           return jsonify({"
   ​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​
   ```


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