Skip to content

@ogygia/federation

Visitor identity

The shell signs who is browsing into each call, on the visitor's behalf, so the other team's app knows the visitor without a cookie of its own. Each kind of call gets its claims from one place, and the receiving app reads them with user(c).

On this page

A signature says which app is calling. The claims say who is browsing: a plain object such as { sub: 'u_42', roles: ['admin'] } that the shell works out from its own session and signs into the call. The other team’s app gets the visitor without a cookie of its own, and the claims never pass through the browser, so a visitor can’t change them.

orders stands for your own data module.

import { routes, page } from '@ogygia/router';
import { user } from '@ogygia/federation';
import Orders from '$lib/views/Orders.svelte';
import { orders } from '$lib/server/orders';

type Viewer = { sub: string; roles?: string[] };

export const cmsRouter = routes(
  {
    '/orders': page(Orders, {
      load: async (c) => {
        const viewer = user<Viewer>(c); // undefined on your own door and for anonymous calls
        return {
          orders: viewer ? await orders.for(viewer.sub) : []
        };
      }
    })
  },
  { base: '/cms' }
);

Claims per call

Each kind of call takes its claims from one place:

CallClaims signed into it
mount(peer) in a route tableThe router’s c.visitor, plus the request’s flag decisions under experiments.
mount(peer, { user })What user(c) returns, plus the request’s flag decisions.
mount.kit(peer, { user })What user(e) returns. Nothing without it, and no flag decisions.
a static peer.page() or peer.widget()Only the claims you pass in its dials. Without them the call is anonymous.
a deferred peer.page() or peer.widget()What federate({ visitor }) returns, run with the hole’s own request when the browser fetches it.
a thaw noticeNone.

federate({ visitor }) is read only for deferred regions. A mount reads your table’s visitor, so give the table one too; it can call the same function. With open: true on the receiving app, no claims count, signed or not (Open endpoints).

Reading claims

On the receiving app, user(c) returns the verified claims in any load, action or endpoint of the exposed table, and a widget gets them as info.user. Pass a type for the claims you expect, as in user<Viewer>(c); nothing checks the shape at runtime, so the type is a promise between the two teams.

c.visitor reads the same claims: when a call arrives with claims, they’re the visitor, and your table’s own visitor function doesn’t run. A signed call with no claims, such as a static region without claims, attaches nothing, so your visitor function does run, against the calling server’s forwarded headers, not a browser’s. So flags read through c stick to the shell’s visitor by its sub or vid, and your loads can use c.visitor on both doors.

user(c) is undefined on your own front door, for an anonymous call, and on an open app. It reads a value your app attached in process, under a JavaScript symbol, after the signature passed. An HTTP request can’t set a symbol, and the raw x-og-user header is removed before your table runs, so no header can pose as a visitor there. A widget’s info.request is the raw call with the header still in it (#2311, item 13), so a widget reads identity only from info.user.

Flag decisions

A mount adds every flag decision the request has made so far to the claims, as experiments: { <flag>: <variant> }. On the receiving app, a carried decision comes before the app’s own source and rules, so the visitor sees one variant in every team’s part of the page. A decision already carried in from further up wins over the shell’s own. Routing and federation covers it, with the table flags option for a shell that routes but reads no flag itself.

Static and deferred regions don’t carry decisions. Pass them in a static region’s claims yourself if a peer’s widget must match the page. The value is the variant’s name, which is what the receiving app accepts: a weighted flag returns it, and a boolean flag’s names are 'on' and 'off', so pass flag(event) ? 'on' : 'off' for one. Anything else is dropped without an error.

hero stands for a weighted flag you declared, such as flag('hero', { control: 50, bold: 50 }), so hero(event) is 'control' or 'bold'. Reading it makes the page per visitor, as any flag read does.

import type { PageServerLoad } from './$types';
import { dash } from '$lib/server/peers';
import { hero } from '$lib/flags';

export const load: PageServerLoad = async (event) => ({
  kpis: await dash.widget(
    'kpis',
    { org: 'acme' },
    { claims: { experiments: { hero: hero(event) } } }
  )
});

Chains

An app that is itself called can call a third team. Say the shell mounts the cms, and the cms places a dash widget. The cms signs its call to the dash with its own key, and forwards the claims it got. A mount() in the cms’s table does that by itself, since its c.visitor is those claims. A static region passes them by hand: dash.widget('kpis', props, { claims: user(c) }). The dash lists the cms’s public key, so it trusts the cms for the call and the claims ride inside the cms’s signature.

Limits

  • Signed, not encrypted. Anyone who can read the traffic between servers can read them. Use TLS between apps.
  • Plain JSON. They’re sent as base64 JSON, so keep them small: an id, roles, a locale. Every distinct set of claims is its own entry in the peer’s document cache.
  • The shell’s word. The receiving app trusts whatever a listed key signs. A shell that signs roles: ['admin'] for everyone gets admin pages for everyone (Threat model).