@superset-ui/embedded-sdk
TypeScript icon, indicating that this package has built-in type declarations

0.2.0 • Public • Published

Version Libraries.io

Superset Embedded SDK

The Embedded SDK allows you to embed dashboards from Superset into your own app, using your app's authentication.

Embedding is done by inserting an iframe, containing a Superset page, into the host application.

Prerequisites

  • Activate the feature flag EMBEDDED_SUPERSET
  • Set a strong password in configuration variable GUEST_TOKEN_JWT_SECRET (see configuration file config.py). Be aware that its default value must be changed in production.

Embedding a Dashboard

Using npm:

npm install --save @superset-ui/embedded-sdk
import { embedDashboard } from "@superset-ui/embedded-sdk";

embedDashboard({
  id: "abc123", // given by the Superset embedding UI
  supersetDomain: "https://superset.example.com",
  mountPoint: document.getElementById("my-superset-container"), // any html element that can contain an iframe
  fetchGuestToken: () => fetchGuestTokenFromBackend(),
  dashboardUiConfig: { // dashboard UI config: hideTitle, hideTab, hideChartControls, filters.visible, filters.expanded (optional), urlParams (optional)
      hideTitle: true,
      filters: {
          expanded: true,
      },
      urlParams: {
          foo: 'value1',
          bar: 'value2',
          // ...
      }
  },
    // optional additional iframe sandbox attributes
  iframeSandboxExtras: ['allow-top-navigation', 'allow-popups-to-escape-sandbox'],
  // optional config to enforce a particular referrerPolicy
  referrerPolicy: "same-origin"
});

You can also load the Embedded SDK from a CDN. The SDK will be available as supersetEmbeddedSdk globally:

<script src="https://unpkg.com/@superset-ui/embedded-sdk"></script>

<script>
  supersetEmbeddedSdk.embedDashboard({
    // ... here you supply the same parameters as in the example above
  });
</script>

Authentication/Authorization with Guest Tokens

Embedded resources use a special auth token called a Guest Token to grant Superset access to your users, without requiring your users to log in to Superset directly. Your backend must create a Guest Token by requesting Superset's POST /security/guest_token endpoint, and pass that guest token to your frontend.

The Embedding SDK takes the guest token and use it to embed a dashboard.

Creating a Guest Token

From the backend, http POST to /security/guest_token with some parameters to define what the guest token will grant access to. Guest tokens can have Row Level Security rules which filter data for the user carrying the token.

The agent making the POST request must be authenticated with the can_grant_guest_token permission.

Within your app, using the Guest Token will then allow authentication to your Superset instance via creating an Anonymous user object. This guest anonymous user will default to the public role as per this setting GUEST_ROLE_NAME = "Public".

The user parameters in the example below are optional and are provided as a means of passing user attributes that may be accessed in jinja templates inside your charts.

Example POST /security/guest_token payload:

{
  "user": {
    "username": "stan_lee",
    "first_name": "Stan",
    "last_name": "Lee"
  },
  "resources": [{
    "type": "dashboard",
    "id": "abc123"
  }],
  "rls": [
    { "clause": "publisher = 'Nintendo'" }
  ]
}

Alternatively, a guest token can be created directly in your app without interacting with the Superset API. To do this, you should update the GUEST_TOKEN_JWT_SECRET in the Superset config.py. Also set the GUEST_TOKEN_JWT_AUDIENCE variable that matches what is set for the aud in the JSON payload:

{
  "user": {
    "username": "embedded@embedded.fr",
    "first_name": "embedded",
    "last_name": "embedded"
  },
  "resources": [
    {
      "type": "dashboard",
      "id": "d73e7841-9342-4afd-8e29-b4a416a2498c"
    }
  ],
  "rls_rules": [],
  "iat": 1730883214,
  "exp": 1732956814,
  "aud": "superset",
  "type": "guest"
}

In this example, the configuration file includes the following setting:

GUEST_TOKEN_JWT_AUDIENCE="superset"

Sandbox iframe

The Embedded SDK creates an iframe with sandbox mode by default which applies certain restrictions to the iframe's content. To pass additional sandbox attributes you can use iframeSandboxExtras:

  // optional additional iframe sandbox attributes
  iframeSandboxExtras: ['allow-top-navigation', 'allow-popups-to-escape-sandbox']

Enforcing a ReferrerPolicy on the request triggered by the iframe

By default, the Embedded SDK creates an iframe element without a referrerPolicy value enforced. This means that a policy defined for iframe elements at the host app level would reflect to it.

This can be an issue as during the embedded enablement for a dashboard it's possible to specify which domain(s) are allowed to embed the dashboard, and this validation happens throuth the Referrer header. That said, in case the hosting app has a more restrictive policy that would omit this header, this validation would fail.

Use the referrerPolicy parameter in the embedDashboard method to specify a particular policy that works for your implementation.

/@superset-ui/embedded-sdk/

    Package Sidebar

    Install

    npm i @superset-ui/embedded-sdk

    Weekly Downloads

    45,605

    Version

    0.2.0

    License

    Apache-2.0

    Unpacked Size

    89.1 kB

    Total Files

    16

    Last publish

    Collaborators

    • suddjian
    • superset-ui-owner
    • kristw
    • data-ui
    • conglei
    • christopher.card.williams
    • soboko
    • mistercrunch
    • cdchambers
    • khtruong
    • erik_ritter
    • graceguo-supercat
    • michellet
    • supersetbot
    • ktmud
    • villebro
    • rusackas
    • kgabryje
    • eschutho
    • michael.s.molina
    • arash_suri