Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

2.8 KB
Raw Blame
import { merge } from "icepick";

import type { MetabaseAuthConfig } from "embedding-sdk-shared/types/auth-config";
import {
  type OnBeforeRequestHandlerConfig,
  PLUGIN_API,
} from "metabase/api/client";
import { overrideRequestsForGuestEmbeds } from "metabase/embedding/lib/override-requests-for-embeds";
import { createAsyncThunk } from "metabase/redux/utils";
import { refetchSiteSettings } from "metabase/settings";
import { isJWT } from "metabase/utils/jwt";

import { getOrRefreshGuestSession } from "./auth";

export const initGuestEmbed = createAsyncThunk<void, MetabaseAuthConfig>(
  "sdk/token/INIT_GUEST_EMBED",
  async (authConfig: MetabaseAuthConfig, { dispatch }) => {
    overrideRequestsForGuestEmbeds();

    if (authConfig.isGuest && authConfig.guestEmbedProviderUri) {
      // Replaces the request token with the newly refreshed guest embed token.
      PLUGIN_API.onBeforeRequestHandlers.getOrRefreshGuestSessionHandler =
        async (config: OnBeforeRequestHandlerConfig) => {
          const newToken = await dispatch(
            getOrRefreshGuestSession(authConfig),
          ).unwrap();

          // The URL is templated (e.g. /api/embed/card/:entityIdentifier/params/:paramId/values or /api/embed/card/:token/query).
          // `entityIdentifier` arrives as a URL param (in `data`); `token`
          // arrives in the request body for the saved-card query rewrite (and in
          // `data` for the legacy whole-bag callers). Refresh whichever channel
          // carries it before the URL `:tag` substitution reads it.
          if (newToken && "entityIdentifier" in config.data) {
            return merge(config, {
              data: {
                entityIdentifier: newToken,
              },
            });
          }

          if (newToken && config.body && "token" in config.body) {
            return merge(config, {
              body: {
                token: newToken,
              },
            });
          }

          if (newToken && "token" in config.data) {
            return merge(config, {
              data: {
                token: newToken,
              },
            });
          }

          // The token value is inlined in the URL path (e.g. /api/embed/card/{jwt}/query).
          // Replace it with :entityIdentifier so the parameter substitution in api.js
          // can bake the refreshed token into the URL.
          if (newToken) {
            const jwtInUrl = config.url
              .split("/")
              .find((segment) => isJWT(segment));

            if (jwtInUrl) {
              return merge(config, {
                url: config.url.replace(jwtInUrl, ":entityIdentifier"),
                data: { entityIdentifier: newToken },
              });
            }
          }
        };
    }

    await dispatch(refetchSiteSettings());
  },
);