UMIS SDK
    Preparing search index...

    Module @umis/gateway

    @umis/gateway

    This package is the gateway of a checkpoint and decides offline on the text of a scanned QR code. It opens a relay only on an accept, with a signed command that authorises exactly one request. You need it when a scan at a checkpoint should open a door.

    It also holds the relay command format and a client for Shelly Gen3 relays. For Node, it adds an emulator of such a relay and a decision journal in a file.

    npm install @umis/gateway @umis/core @umis/verifier
    
    Entry Exports Runs in
    @umis/gateway createGateway, shellyRpcRelay, signRelayCommand, verifyRelayCommand, shellySwitchSetBody, computeDigestResponse browsers and Node
    @umis/gateway/node startShellyEmulator, createFileJournal Node
    import { importSigner } from '@umis/core';
    import { createGateway, shellyRpcRelay } from '@umis/gateway';

    const station = await importSigner(stationPrivateJwk);
    const relay = shellyRpcRelay({
    url: 'http://127.0.0.1:8089/rpc',
    relayAud: 'http://127.0.0.1:8089',
    signer: station,
    iss: 'door-1',
    });
    const gateway = createGateway({
    context: () => context, // the VerifierContext of the door: policy, trusted issuers, status lists, replay store
    relay,
    now: () => Math.floor(Date.now() / 1000),
    onDecision: (result) => console.log(result.decision, result.reasons[0]),
    });

    const { result, relay: opened } = await gateway.handle(scannedText);
    // result is the decision; opened is { ok, status } when the relay was asked to open

    handle(text) trims the text first. It turns away a text longer than maxInputChars as MALFORMED, before anything is decoded. The same goes for a text with a character that QR alphanumeric mode cannot carry. By default, maxInputChars is the longest QR code plus 16.

    Any other text goes to verifyAtCheckpoint of @umis/verifier, with the context that context() returns for each scan. handle replaces the clock of that context with now(), so that the decision and the relay command agree. Only an accept asks the relay to open switch switchId, which is 0 by default. The switch opens for openSeconds, else for the relay.openSeconds of the policy, else for 3 seconds.

    • A relay that fails or does not answer never changes the decision. relay then reports { ok: false, status }.
    • onDecision hears every decision, accepted or not, before the relay is asked. Nothing it does can change the outcome, even when it throws or returns a rejected promise.
    • When context() fails, handle throws PROTOCOL, opens nothing and returns no decision.

    A relay command is a JWS with typ umis-relay-cmd+jwt. It travels in the X-Umis-Command header next to the request it authorises and carries the claims below.

    Claim Meaning
    aud the relay the command is meant for, as the relay lists itself
    iss the optional name of the station or device that sends it
    iat, exp its lifetime, 10 seconds by default and 30 at most
    jti 16 random bytes, so a relay accepts a command once
    bh the SHA-256 of the UTF-8 body in base64url, so the body cannot be swapped after signing

    signRelayCommand({ signer, relayAud, body, now, iss?, ttlSeconds? }) makes one.

    verifyRelayCommand(jws, body, { keys, audiences, now, replay, skewSeconds? }) checks the input, the signature against the trusted keys, the body hash, the audience, the lifetime, the clock and the replay store, in this order. A valid input is a command of at most 4 096 characters with claims of at most 512. The signature check tries at most eight of the trusted keys, the one the kid names first. The function never throws. A refusal is one of MALFORMED, SIGNATURE_INVALID, AUDIENCE_MISMATCH, PRESENTATION_TOO_LONG_LIVED, CLOCK_SKEW, PRESENTATION_EXPIRED and REPLAY. A body that does not match bh is also refused as SIGNATURE_INVALID. The replay id is relay:<jti>, and the replay store remembers it until exp plus the skew. The check claims it last, so a command refused for another reason is not used up.

    shellyRpcRelay({ url, signer, relayAud, iss?, src?, fetch?, timeoutMs? }) returns a client. Its open({ switchId, seconds, now }) posts a signed Switch.Set to url, with on: true and the flip-back timer toggle_after. url is the full address of the /rpc endpoint. relayAud is the relay’s identity as the relay lists it, normally its origin. The JSON-RPC src is src, else iss, else umis-gateway. A relay that does not answer within timeoutMs counts as down, and timeoutMs is 1 000 by default.

    open does not throw for anything the relay or the network does. It answers { ok, status, error? }. status is 0 when no reply came. error is one of timeout, network error, HTTP <status>, RPC error <code> and command could not be made.

    The client runs in browsers. A page needs the relay to allow its origin, which the emulator does with --cors.

    Two helpers serve a client of your own. shellySwitchSetBody builds the JSON-RPC body. computeDigestResponse computes the SHA-256 response of Shelly digest authentication, following RFC 7616 with qop=auth.

    startShellyEmulator stands in for a Shelly Gen3 relay with one switch. It comes from the entry @umis/gateway/node, and @umis/cli runs it as umis relay-emu. Only Switch.Set needs the signed relay command in X-Umis-Command. When digestPasswordEnv names the variable that holds a password, Switch.Set needs Shelly digest authentication as well. Switch.GetStatus and Shelly.GetDeviceInfo answer without either, so the status page at / can poll the switch without holding any credential. Neither call changes the switch.

    A real Shelly with authentication enabled asks for the digest on its status calls too. A client that reads the status of a real relay therefore has to send the digest there.

    The emulator also works as follows.

    • Options. The emulator starts with startShellyEmulator({ port, host?, trust, audiences?, cors?, requireSignature?, digestPasswordEnv?, now, log? }). port: 0 picks a free port, and host is 127.0.0.1 unless you set another. The call resolves with { url, state(), close() }.
    • Audiences. Unless audiences replaces them, a command may name urn:umis:relay:emu, the emulator’s url, http://127.0.0.1:<port> or http://localhost:<port>.
    • Browsers. Only the exact origins in cors may call the emulator. Each origin has the form https://host[:port], with no path and no wildcard. The emulator answers their preflight, including the private-network one.
    • Digest. The username is admin and the algorithm is SHA-256. The emulator reads the password from the named environment variable and never prints it.
    • Limits. The emulator answers a body over 8 KiB with 413. The state follows the clock it is given, not timers. The switch is on while now is before the end of the flip-back timer.
    • Status. Switch.GetStatus adds two fields that a real relay does not have. umis_now is the emulator’s clock. umis_last is the last command, with its at, the first 8 characters of its jti and its iss.
    • Log. log receives one line for each command obeyed, such as OPEN 3 s · jti … · iss …. It also receives one line for each request refused, such as 401 <reason> for a command that does not verify.

    createFileJournal(path) keeps decision events in a file, one JSON object per line. Each entry is chained to the one before it by SHA-256 over the canonical form of its event. An edited, removed or moved line therefore breaks the chain from there on. append(event) needs the next seq, which is 1 for the first event. verify() returns { ok: true } or the index of the first entry that breaks the chain.

    The file is created with mode 0600, and every append is synced to disk before the call returns. A file that does not verify makes append fail with PROTOCOL and is never extended. createDecisionEvent of @umis/verifier turns a decision into an event without claim values.

    See also the UMIS SDK, the integration guide, the profile and the API reference.

    Copyright 2026 Ishtvan Komonii. Licensed under the Apache License 2.0.

    node