Authentication
Every credential travels inside the sealed envelope, so no proxy in the path sees it. A client holds one credential at a time: a session or an API key.
A method that takes a passkey answers the node’s 412 CONFIRM_REQUIRED with
{ confirm: passkeyConfirm(passkey) } (import { passkeyConfirm } from '@calimero-network/mero-js').
Passkeys
Section titled “Passkeys”import { browserPasskey } from '@calimero-network/mero-js';
const passkey = browserPasskey(); // Touch ID, Windows Hello, a security key, a synced passkeyawait client.signUp(passkey, 'alice'); // new account and passkey: asks the passkey twiceawait client.signIn(passkey); // existing passkeyA sign-up prompts twice: once to register the new passkey, once for that
passkey to sign the account it creates. A registration carries no signature
by the new key, so the cluster takes the account only with that second
assertion (the node’s 412 CONFIRM_REQUIRED, which signUp answers).
browserPasskey() wraps navigator.credentials. Anything that implements
PasskeyProvider (create(options) and
get(challenge, rpId, allowCredentials?, options?), answering base64url fields)
works in its place, for example a software authenticator in tests. When the
client answers a confirmation (below) it passes { userVerification: 'required' }:
the node takes those only with the user-verified flag (PIN or biometric). The
sign-up’s own confirmation is the exception: the node takes it with the user
present, as it made the passkey, so signUp passes preferred and a security
key without a PIN can still sign up. A plain sign-in passes nothing, and
browserPasskey() then asks for preferred. A provider that always verifies
the user may ignore it.
Each passkey is a device of the account. Adding or removing one is approved with a passkey the account already has (the new one cannot approve itself):
const credentialId = await client.addPasskey(newDevicePasskey, passkey); // another device, approved by `passkey`await client.auth.passkeyLabel({ cred: credentialId, label: 'work laptop' });await client.auth.passkeyRemove({ cred: credentialId }, { confirm: passkeyConfirm(passkey) }); // never the last oneme() lists the account’s passkeys, each with when it was added and last
signed in (addedAt, lastSignInAt, Unix seconds; null for what happened
before the node kept times), and passkeyId, the one this session was opened
with.
Passkeys are per cluster
Section titled “Passkeys are per cluster”An account and its passkeys belong to one cluster. Clusters may share a
WebAuthn relying-party id (every Calimero Cloud cluster uses
calimero.network), so the browser also offers a passkey made on one cluster
when a page signs in on another. That sign-in fails with
PasskeyUnknownHere (401 PASSKEY_UNKNOWN_HERE): “this passkey belongs to
another Calimero cluster or was removed from this one; sign up here, or open
the app from the link for the cluster your account is on”. A removed passkey
gets the same answer, and the node never says which cluster or account a
passkey is from: it knows only its own.
To tell them apart, the node names each passkey after its cluster: a password
manager lists eu-shared-1 · alice, or eu-shared-1 · alice (kv-store) for one
made by signing up on an app’s frontend.
import { PasskeyUnknownHere } from '@calimero-network/mero-js';
try { await client.signIn(passkey);} catch (e) { if (e instanceof PasskeyUnknownHere) showSignUpOrRightClusterLink(); else throw e;}The client does not refresh for this error (it is not LoginRequired), and
trying the same passkey again fails the same way.
Sign in from your own site
Section titled “Sign in from your own site”A package’s frontend served by the cluster signs people in on the cluster’s
origin. To host it yourself (https://chat.example.com), a manager of the
package registers that origin, approved with their passkey:
await client.packages.originsAdd({ package: 'com.example.chat', origin: 'https://chat.example.com' }, { confirm: passkeyConfirm(passkey) });// { package, origin, rpId: 'chat.example.com', addedBy, addedAt }; rpId defaults to the hostawait (await client.packages.originsList({ package: 'com.example.chat' })).origins;await client.packages.originsRemove({ package: 'com.example.chat', origin: 'https://chat.example.com' }, { confirm: passkeyConfirm(passkey) });To have the cluster serve the package’s frontend on that name too (a custom
domain, from the signed bundle), add it with served: true:
await client.packages.originsAdd({ package: 'com.example.chat', origin: 'https://chat.example.com', served: true }, { confirm: passkeyConfirm(passkey) });The record is the cluster’s half: browsers reach the name once Calimero Cloud holds a certificate and a route for it.
On that site, signUp and signIn work as anywhere else: in a browser whose
window.location.origin is not one of the client’s nodes, they send it, and
the passkey is made for (and asserted with) the relying-party id the node
answers for it. Pass { origin } to name it yourself, or { origin: null } to
send none. The session is bound to the package, as on an app’s own frontend;
me() answers its app and origin, and its passkey confirmations are signed
for the same relying-party id.
try { await client.signIn(browserPasskey());} catch (e) { if (e instanceof OriginNotRegistered) showError(e.message); // "this site (https://…) is not registered for …" else throw e;}Removing the origin ends the sessions opened there: their next request is
refused 401 (LoginRequired).
Sessions
Section titled “Sessions”signUp and signIn answer a CalimeroSession
({ accountId, sessionToken, expiresAt, refreshToken }) and use it for every
request after. Keep it to resume without a passkey prompt:
const session = await client.signIn(passkey);sessionStorage.setItem('calimero', JSON.stringify(session));
// another page loadconst resumed = new CalimeroClient({ baseUrl, policy });resumed.useSession(JSON.parse(sessionStorage.getItem('calimero')!));await resumed.refresh(); // swap the refresh token for a new sessionA refresh token is used once: a reused one revokes the session. A session is
signed by the node that issued it, so it can stop verifying when that node is
replaced or cannot be reached; the client then refreshes by itself, once, on
any node, and repeats the request
(see Restarts and other nodes).
A session that refreshing cannot renew — expired, revoked, or a refresh token
already used — makes requests fail with LoginRequired.
Keep client.session after requests, not only after signIn: an automatic
refresh replaces it, and the refresh token you stored before is then spent.
Bind the session to the device
Section titled “Bind the session to the device”A refresh token is a bearer credential: a script on the page that copies it (a compromised dependency, a cross-site-scripting bug) could refresh the session from anywhere. Give the client a holder key and it cannot:
import { browserSessionHolder, CalimeroClient } from '@calimero-network/mero-js';
const client = new CalimeroClient({ baseUrl, policy, holder: browserSessionHolder() });await client.signIn(passkey); // the session is bound to this browser's keybrowserSessionHolder() makes a P-256 key with WebCrypto as
non-extractable and keeps the CryptoKey itself in IndexedDB, so its
private half never exists as bytes a script could send away, and a reload keeps
it. A sign-in (or the first signUp finish) names its public key
(holderKey); the passkey signs that binding with the session. Every refresh
then carries a proof, the key’s signature over the token’s hash and the time
(holderProof), which the node checks before it spends anything: a token
without one is refused 401 HOLDER_PROOF_REQUIRED, so a copy taken off the
device refreshes nothing. A bound refresh token lives 7 days rather than
30, and an exchange carries the binding to the next token.
A script running on the page can still use the session while it runs there,
and copy the session token, which lives 15 minutes; see
What is not protected yet.
A cluster whose nodes do not all take a holder key yet answers 409 FEATURE_NOT_ACTIVE to the sign-in, and the client signs in unbound, as without
one. forgetSessionHolder() deletes the key, so the next sign-in binds a new
one. mero-react’s
MeroProvider binds every session to browserSessionHolder() unless given
holder: null.
await client.signOut(); // this sessionawait client.signOutEverywhere(); // every session of the account, on every deviceThe account holder can see its sessions and end one of them, a lost phone’s say, from another device. Both take a session on the cluster’s own origin (never an API key or an app’s frontend session) and ask no passkey:
const sessions = await (await client.auth.sessionsList()).sessions; // latest active first; `current` marks this oneconst other = sessions.find((s) => !s.current); // { session, passkey, app, origin, openedAt, lastActiveAt }if (other) await client.auth.sessionRevoke({ session: other.session }); // that device must sign in againlistActivity() answers what the account’s passkeys did, newest first (the
last 100): each sign-in, with the package and origin its session is bound to,
and each change a passkey approved, with its method and subject. Gated as
listSessions:
for (const a of await (await client.auth.activityList({})).activity) { // { at, passkey, kind: 'signIn', app, origin } or { at, passkey, kind: 'approval', method, subject }}API keys
Section titled “API keys”Agents and servers authenticate with an API key (cal_...), scoped and
revocable. Creating one is approved with the account’s passkey, from a passkey
session (an API key cannot mint another). The key is returned once.
const { keyId, key } = await client.auth.apikeyCreate( { label: 'ci', apps: [appId], // only these apps methods: ['get'], // only these methods readOnly: true, ttlSecs: 86_400, }, { confirm: passkeyConfirm(passkey) },);
const agent = new CalimeroClient({ baseUrl, policy });agent.useApiKey(key);
await (await client.auth.apikeyList()).keys; // never the keys themselvesawait client.auth.apikeyRevoke({ key: keyId }, { confirm: passkeyConfirm(passkey) }); // approved with the passkeyUpgrade keys for CI
Section titled “Upgrade keys for CI”An upgrade with a session needs a passkey every time, which a CI job does
not have. An upgrade key is made once, with the passkey, for one app: it
upgrades that app to signed releases of its package (by the package’s pinned
signer) with no passkey, and the node refuses it everything else,
reads included (403 UPGRADE_KEY_ONLY). The approval gates still apply:
who may upgrade the app, release deadlines and deployer grants.
// Once, by the app's owner:const { key } = await client.auth.apikeyCreate({ label: 'ci', upgrades: { app: appId }, ttlSecs: 90 * 86_400 }, { confirm: passkeyConfirm(passkey) });
// In CI, after publishing 1.4.0 to the registry:const ci = new CalimeroClient({ baseUrl, policy });ci.useApiKey(key);await ci.apps.upgrade({ app: appId, version: '1.4.0' });Revoke it with revokeApiKey; the app’s lastUpgrade names the key that
made each upgrade.
Confirmations
Section titled “Confirmations”Some requests need the account holder’s passkey over that exact request: a
method the app declares confirm, deleting an organization, upgrades,
transfers and approvals, and every grant of access: adding or removing a
passkey, creating an API key, setting or removing an organization or team
member, capabilities and invites. The node answers 412 CONFIRM_REQUIRED with a
challenge; a generated method given { confirm: passkeyConfirm(passkey) } (or a
client made with { confirm }) signs it and sends the request again.
await client.apps.call({ app, method: 'clear', args: {} }, { confirm: passkeyConfirm(passkey) });await client.orgs.outboundApproval({ org, package: 'com.example.kv' }, { confirm: passkeyConfirm(passkey) });A prompt the user dismisses rejects with ConfirmCancelled, and nothing more is
sent. A prompt the browser or a password manager would not show (the page was
not focused because an extension’s popup still held it, or another prompt was
still open) rejects with ConfirmPromptBlocked instead, its cause the
browser’s error; nothing was sent there either. The user did not decline, so
offer to try again from a click:
try { await client.apps.call({ app, method: 'clear', args: {} }, { confirm: passkeyConfirm(passkey) });} catch (error) { if (error instanceof ConfirmPromptBlocked) { showRetry('Your password manager or browser blocked the second prompt. Click to try again.'); } else if (!(error instanceof ConfirmCancelled)) { throw error; }}An API key can answer a confirmation only if it was created with
allowConfirm.
Account recovery
Section titled “Account recovery”Someone who loses every passkey cannot sign in, and nobody can act for them. A recovery code, made with a passkey and shown once, is the way back (core #4844):
const code = await (await client.auth.recoveryCreate({}, { confirm: passkeyConfirm(passkey) })).code; // show it once; keep it offline
// Every passkey lost, signed out: the code and a new passkey start a recovery.const { accountId, readyAt } = await fresh.startAccountRecovery(code, browserPasskey());// 72 hours later (until then, any passkey the account still has may cancel it):await fresh.finishAccountRecovery(browserPasskey(), accountId); // signed in; other sessions endA signed-in client sees a recovery under way in me().recoveryPending and
cancels it with cancelAccountRecovery(passkey). A code starts one recovery
and is spent whether it finishes or is cancelled; a wrong or spent one is
WrongRecoveryCode, finishing early RecoveryNotReady (with readyAt).