From 1aba128e3ee462869b906b479c7ce811c158e6e3 Mon Sep 17 00:00:00 2001 From: Kasim Necdet Percinel Date: Wed, 5 Nov 2025 17:06:54 +0000 Subject: [PATCH] initial implementation with example --- docroot/superset/guestok.php | 157 +++++++++++++++++++++++++++++ docroot/superset/index.html | 186 +++++++++++++++++++++++++++++++++++ 2 files changed, 343 insertions(+) create mode 100644 docroot/superset/guestok.php create mode 100644 docroot/superset/index.html diff --git a/docroot/superset/guestok.php b/docroot/superset/guestok.php new file mode 100644 index 00000000..7b8fbceb --- /dev/null +++ b/docroot/superset/guestok.php @@ -0,0 +1,157 @@ + 'JWT', + 'alg' => $algorithm + ]; + + $headerEncoded = base64UrlEncode(json_encode($header)); + $payloadEncoded = base64UrlEncode(json_encode($payload)); + + // Data to sign + $dataToSign = $headerEncoded . '.' . $payloadEncoded; + + // Sign with RSA private key + $signature = ''; + $success = openssl_sign($dataToSign, $signature, $keyResource, OPENSSL_ALGO_SHA256); + + if (!$success) { + throw new Exception("Failed to sign JWT. Error: " . openssl_error_string()); + } + + // Note: In PHP 8.0+, key resources are automatically freed, no need to call openssl_free_key() + + $signatureEncoded = base64UrlEncode($signature); + + // JWT token + return $headerEncoded . '.' . $payloadEncoded . '.' . $signatureEncoded; +} + +/** + * Generate guest token payload + */ +function generateGuestTokenPayload($dashboardId, $audience, $expiration) { + $now = time(); + + return [ + // User information + 'user' => [ + 'username' => 'guest_' . uniqid(), + 'first_name' => 'Guest', + 'last_name' => 'User' + ], + + // Resources this token grants access to + 'resources' => [ + [ + 'type' => 'dashboard', + 'id' => $dashboardId + ] + ], + + // Row Level Security rules (empty = no restrictions) + 'rls_rules' => [], + + // JWT standard claims + 'aud' => $audience, + 'iat' => $now, + 'exp' => $now + $expiration, + 'type' => 'guest' + ]; +} + +// ============================================================================ +// Main Execution +// ============================================================================ + +try { + // Only allow POST requests + if ($_SERVER['REQUEST_METHOD'] !== 'POST') { + http_response_code(405); + echo json_encode([ + 'error' => 'Method not allowed. Use POST.' + ]); + exit; + } + + // Validate configuration + if (!file_exists($PRIVATE_KEY_PATH)) { + throw new Exception("Private key file not found at: $PRIVATE_KEY_PATH"); + } + + // Generate token payload + $payload = generateGuestTokenPayload($DASHBOARD_ID, $JWT_AUDIENCE, $TOKEN_EXPIRATION); + + // Create JWT token using RSA private key + $token = createJWT($payload, $PRIVATE_KEY_PATH); + + // Return the guest token + echo json_encode([ + 'token' => $token, + 'success' => true + ]); + +} catch (Exception $e) { + http_response_code(500); + echo json_encode([ + 'error' => $e->getMessage(), + 'success' => false + ]); + + // Log the error + error_log("Superset guest token error: " . $e->getMessage()); +} diff --git a/docroot/superset/index.html b/docroot/superset/index.html new file mode 100644 index 00000000..6fbfbecf --- /dev/null +++ b/docroot/superset/index.html @@ -0,0 +1,186 @@ + + + + + + Superset Embedded Dashboard + + + +
+
+

Superset Dashboard

+
+
+
Loading dashboard...
+
+
+ + + + + + +