Scoped & masquerade
Exercise application entry routes with scoped keys and support-user credentials
Test entry routes that exchange a pre-signed key or support credentials for an authenticated session. Register the upstream response, then let your application validate the token and handle navigation.
Before you start
Complete the Blueprint Auth tutorial and configure the SDK middleware for your application’s entry routes.
The examples use these application-defined routes, not package defaults:
| Route | Credentials extracted by the app |
|---|---|
/anon/<preSignedKey> | appRoutes.anon.getAnonParams extracts the key |
/masquerade/<userId>/<masqueradeToken> | appRoutes.masquerade.getMasqueradeParams extracts both values |
Successful entry is assumed to show the authenticated viewer’s name; rejected entry goes to /oauth-error. Adapt the URLs and assertions to your configured routes.
Start a test file with a separate mock for the page’s viewer query:
import { HttpResponse } from "msw";
import { expect, test } from "../../lib/playwright/test";
test.beforeEach(({ msw }) => {
msw.use(
msw.graphql
.link("https://api.kraken.test/v1/graphql/")
.query("viewer", () => HttpResponse.json({ data: { viewer: { preferredName: "Alice" } } })),
);
});Open a scoped link
Match the pre-signed key from the entry URL and configure the user represented by the resulting token:
test("opens a scoped link", async ({ page, blueprintAuth }) => {
const preSignedKey = "alice_test_key";
await blueprintAuth.mock.scoped.login({
preSignedKey,
session: { subject: "alice" },
});
await page.goto(`/anon/${encodeURIComponent(preSignedKey)}`);
await expect(page.getByRole("heading", { name: "Alice", exact: true })).toBeVisible();
});Open a masquerade route
Match both credentials passed to your support-access route:
test("opens a masquerade session", async ({ page, blueprintAuth }) => {
const userId = "alice_id";
const masqueradeToken = "support_test_token";
await blueprintAuth.mock.masquerade.login({
userId,
masqueradeToken,
session: { subject: "alice" },
});
await page.goto(
`/masquerade/${encodeURIComponent(userId)}/${encodeURIComponent(masqueradeToken)}`,
);
await expect(page.getByRole("heading", { name: "Alice", exact: true })).toBeVisible();
});Reject an entry credential
Configure an authentication error instead of a session. Keep the same credential in the mock and navigation so the application receives the intended failure response:
test("rejects a scoped link", async ({ page, blueprintAuth }) => {
const preSignedKey = "rejected_key";
await blueprintAuth.mock.scoped.login({
preSignedKey,
errorCode: "KT-CT-1142",
});
await page.goto(`/anon/${encodeURIComponent(preSignedKey)}`);
await expect(page).toHaveURL("/oauth-error");
});For a masquerade failure, use mock.masquerade.login with both credentials and an errorCode instead of session. See the matching and outcome options.