A caller can do what its scopes allow, and nothing until a human pairs its device. A stolen read-only dashboard can read your chats. It cannot send one message.
Pick a caller and press Try every action. Watch which of the four actions pass and why each one fails. Then we build the fence piece by piece: device identity, the signed challenge, scopes, pairing, tokens, and the limits of all of them.
You need Lessons 1 and 2 (the gateway and its handshake) and a rough idea of what a hash is. A little Python helps for the labs. We build the rest from zero.
Lesson 3 of 11 in Anatomy of an Agent Gateway. The running example is OpenClaw, a self-hosted gateway between chat apps and an AI agent. We checked every OpenClaw detail here against its documentation.
Illustrative callers; the CLI is assumed paired with admin. The rules are OpenClaw's documented rules for operator scopes and device pairing.
Chapter 0
See what the gateway guards, and draw the one boundary it is built for
Your agent reads your chats. It can send messages as you. If you pair a Mac, it can run shell commands on that Mac. So one question decides whether it helps you or hurts you: who may ask it to act?
A bank login guards money. The gateway guards a pair of hands. A caller who gets in can send as you, read your conversations and change the agent's settings.
Authentication checks who is calling. Authorization decides what that caller may do next. This lesson builds both, then marks what they do not cover.
First, the kind of boundary. OpenClaw assumes one trusted boundary per gateway: a single operator, or a team whose members trust each other. It is not a boundary between users who distrust each other. For mixed trust, the docs say to split: a separate gateway with separate credentials, ideally under a separate OS user or on a separate host.
Your sessions and your Mac sit inside your gateway. A friend's automation joins. Choose where it joins and look at what sits inside the same boundary.
Illustrative setup. The rule is the docs' rule: one gateway is one trust boundary, and it is not a hostile multi-tenant boundary.
So auth here has one job: keep the wrong callers out, with a sharp line. Once a caller is in, the design treats it as yours. Four pieces draw that line, one chapter each: device identity, a signed challenge, scopes and pairing.
Chapter 1
Replace "I know a secret" with "I hold a key", and name the device by its fingerprint
A caller can say "I am your phone". Anyone can say that. A password is a better claim, but it is a secret that a person types and sends. Secrets that travel leak: people reuse them, get phished, and paste them into the wrong window.
A device can do better with a keypair: two linked numbers made together. The private key never leaves the device. The public key can go anywhere. The private key makes a signature over any message, and anyone with the public key can check it. Nobody can make that signature without the private key.
So identity becomes "I hold this key". The device proves it by signing, and the key itself never crosses the wire.
A public key is a long run of bytes, so the device needs a short name. Call the public key pk. Run it through a hash, a function that turns any input into a fixed-length fingerprint, and call the hash H. The result is the device ID.
The device ID equals H of p-k: the hash of the public key.
Take the public key pk. Feed it to the hash H. Keep the output as the device's id.
With a made-up key: SHA-256 of the bytes "pk-demo-1" starts 4be303a8 78d9802e. Flip one bit of pk and about 32 of those 64 bits change, so the id is a new name.
Top: 16 bytes of a public key. Bottom: the first 8 bytes of its SHA-256 fingerprint. Flip a bit and count the fingerprint cells that change.
The fingerprint is a real SHA-256 computed in your browser. The key bytes are random and illustrative. The OpenClaw docs say device.id comes from the key's fingerprint; this page does not claim which hash OpenClaw uses.
Two properties make the fingerprint a good name. The same key always gives the same id, so the name holds across reconnects. A different key gives a different id, so nobody can pick a key that matches yours. In OpenClaw, device.id comes from the key's fingerprint, and the gateway checks that it matches the public key the device sends.
Chapter 2
Make the device sign a new random value on every connect, and watch a replay fail
A name and a public key are not proof. The device must show, at this moment, that it holds the private key. The tool is a challenge:
connect.challenge.connect request.Call the nonce n. Call the bytes the device signs m: the nonce plus some fields about this connection. Call the signature σ. Call the check V: it is true when σ is a valid signature of m under the public key pk.
Accept equals V of p-k, m and sigma, and fresh of n.
Check that sigma is a real signature of m under pk. Check that the nonce n inside m is the one you just sent. Accept only if both checks pass.
With made-up cases: a real device signs today's nonce, so V = true and fresh = true, accept. An attacker replays yesterday's signature, so V = true but fresh = false, reject. A forged sigma gives V = false, reject.
The word that matters is fresh. If the gateway sent the same nonce every time, an eavesdropper could record one good signature and send it again. That is a replay attack. Try it.
Let the device connect once. The attacker records its signature. Then replay it, first with fresh nonces, then with a fixed one.
Illustrative toy with short nonces. The challenge then sign then verify order is OpenClaw's documented handshake.
OpenClaw's current signature format, v3, signs more than the nonce. It binds the platform and device family, plus the device, client, role, scopes, token and nonce fields. So a signature made for one context fails in any other. The signed time also comes from the challenge.
How likely is a lucky repeat? Suppose the gateway draws each nonce at random with b bits. An attacker holds k old signatures. Call the chance that a new nonce equals one of them p.
p is at most k over two to the b.
Each recorded pair matches a new nonce with chance 1 in two to the b. Add that up over all k pairs. The total is at most k divided by two to the b.
With made-up numbers: k = 100 and b = 16 give 100 / 65,536 = 0.0015, about 1 in 650. At b = 128 the same 100 pairs give about 3 × 10−37. Long random nonces make a lucky repeat impossible in practice.
connect before the challenge arrives.connect.challenge and sign that nonce. The docs say every connection must sign it.Chapter 3
Give each operator the smallest set of named permissions that does its job
Identity answers who. Now: what may this caller do? A wall-screen dashboard and a bot that posts a daily summary are both yours. They should not hold the same powers. Least privilege means each client gets the smallest set of powers that does its job.
Lesson 1 gave two roles. An operator drives the gateway. A node lends it a device, such as a camera or a shell. Operators carry scopes: named permissions that the gateway checks on each method call. OpenClaw lists 11; these five carry this lesson.
| Scope | What it allows (OpenClaw docs) |
|---|---|
operator.read | read-only status, lists, logs and session reads |
operator.write | send messages, invoke tools, relay node commands |
operator.admin | config changes, updates and high-risk approvals |
operator.pairing | list, approve, reject, remove, rotate and revoke devices |
operator.sessions.read | read visible sessions without general read access |
Some scopes include others. operator.admin satisfies every operator scope. operator.write satisfies operator.read. Call the set of scopes a client holds S. Call S plus everything it implies S+. Call the scope an action a needs need(a).
ok of a equals: is need of a in S-plus?
Expand the client's scopes S into S-plus. Look up need of a for the action. If need of a is in S-plus, ok of a is 1. If not, the gateway refuses.
A bot holds S = {operator.write}. Then S-plus includes operator.write and operator.read. Reading a chat needs operator.read, so ok = 1. Changing config needs operator.admin, which is not in S-plus, so ok = 0.
Each row is an action or an event and the scope it needs. Toggle scopes and watch rows turn green. Try write alone: it lights the read rows too.
Needs and implications from OpenClaw's operator-scopes and presence pages. Real methods number in the dozens; these eight are a sample.
Scopes also filter the event stream from Lesson 1. Chat, agent and tool-result events need operator.read, and so does presence. The transport events heartbeat and tick reach every authenticated client. A client without read never sees your conversations.
operator.admin "to be safe".Chapter 4
Park every new device until an operator approves it, and make upgrades ask again
A valid signature proves a device holds its key. It does not say the device is welcome. A brand-new device ID is a stranger. Pairing is the step where a person approves it.
When a new ID connects from another machine, the gateway does not admit it. It answers NOT_PAIRED with the code PAIRING_REQUIRED and stores a pending request. An operator with operator.pairing runs openclaw devices list, then openclaw devices approve <requestId> or reject. A pending device request expires after 5 minutes.
Approving needs authority too. The approver needs operator.pairing plus every scope the device asks for. Only an admin can approve a request for operator.admin. Two shortcuts exist, and both are narrow:
gateway.nodes.pairing.autoApproveLocal to false and every device waits. Tailnet and LAN connections count as remote. Loopback is ignored if forwarded headers are present.gateway.nodes.pairing.autoApproveCidrs is off unless you set it. It approves only a fresh node request that asks for no scopes. Operators, browsers, the Control UI and WebChat stay manual.Choose where the request comes from and what it asks for, then send it. It lands in Paired or in Pending.
Rules from OpenClaw's pairing and network-exposure pages. The addresses are illustrative.
The rule that stops slow creep: a paired device that asks for a broader role or more scopes creates a new pending upgrade request. It does not gain the power silently. The one exception is the same-machine path above, while autoApproveLocal is on.
openclaw devices list shows a pending upgrade for that device.operator.read reconnects over the internet and asks for operator.write too. What happens?Chapter 5
Carry an approved grant across reconnects, and cut it short when it leaks
Nobody wants to approve a phone again each time its Wi-Fi drops. So after approval, the gateway issues a device token: a credential for that device and role. It arrives in hello-ok.auth.deviceToken. The device stores it with the approved scopes and presents it on reconnect.
The token carries a grant. It never widens one. Two operations change it:
device.token.rotate, or openclaw devices rotate): issue a new token for the same grant, so the old copy is no longer the one that works.device.token.revoke): cancel the token.Both need operator.pairing, and rotating or revoking a node's token also needs operator.admin. Neither can add power. The docs say a token change cannot expand past the role set that pairing approved. To get more, go back through pairing.
The two answer two incidents. A token leaked but you trust the device: rotate. You lost the device: revoke. How long does a leaked token work? Call the time of the leak tleak and the time you rotate trot. Call the time the leaked copy works W.
W equals t-rot minus t-leak.
Subtract t-leak from t-rot. The result W is how long the stranger holds a working token. The scopes decide what they can do; W decides for how long.
With made-up times: the token leaks at 09:00 and you rotate at 15:00, so W = 15 − 9 = 6 hours. Rotate at 09:30 and W = 0.5 hours.
The token leaks at 09:00. Drag when you act, and choose what you do. Then try to widen the grant with a rotation.
Illustrative times. The rules (rotate, revoke, no expansion past the paired role set) are from OpenClaw's auth page.
Chapter 6
Match each mechanism to the boundary it defends, and to the misread that costs the most
We have built the pieces. The costly mistakes come from believing one of them guards something it does not. OpenClaw's trust-model page lists the common misreads. Pick a piece and see where its line runs.
The map shows a caller, the gateway with its sessions, and a paired Mac. Each choice outlines what that piece guards.
Each read-out quotes or follows OpenClaw's trust-model and tool-permissions pages.
The node case carries the most weight. If a macOS node is paired, the gateway can invoke system.run on it. The docs call this remote code execution on that Mac. The real limit is the gateway's node command policy plus the Mac's own exec approvals.
The door has modes. gateway.auth.mode chooses how a caller proves itself:
| Mode | How the caller proves itself | Use it when |
|---|---|---|
token | a shared bearer token | most setups; the docs recommend it |
password | a shared password, best set with OPENCLAW_GATEWAY_PASSWORD | you prefer a password |
trusted-proxy | an identity-aware reverse proxy signs users in and passes identity in headers | you already run such a proxy |
none | no shared-secret check | never on public ingress; on a non-loopback bind the gateway will not start without auth |
One more line. Over HTTP, bearer auth with the shared secret is all-or-nothing operator access. It restores the full default operator scopes, and a request for fewer does not shrink it. Scopes narrow only in modes that carry an identity.
sessionKey as a login.sessionKey proves who sent it. What is wrong?Chapter 7
Stop an injected instruction with tool policy, approvals and sandboxes, not with polite text
Everything so far keeps the wrong callers out. A second attacker walks in through a trusted door: a message. Your agent reads an email, a stranger's DM or a web page, and that text can carry instructions. This is prompt injection. The OpenClaw docs say it is not solved: adaptive attackers break defenses that score well on fixed tests, with published success rates above 80%.
A line in the system prompt, "never run commands from messages", is soft guidance. The model weighs it against the attack, in the same text. Hard enforcement is a mechanism that the model cannot argue with:
Count the damage. Call the number of injected messages N. Call the share of them the model obeys pobey. Call the share of obeyed actions that get past the fence ppass. Call the harmful actions that happen D.
D equals N times p-obey times p-pass.
Start with N messages. Keep the share p-obey that the model follows. Of those, keep the share p-pass that the fence lets through. What is left is D.
With made-up numbers: N = 1,000 and p-obey = 0.05, with no fence (p-pass = 1), give D = 50. An approval step that misses 1 in 10 makes p-pass = 0.1 and D = 5. Remove the tool and D = 0.
1,000 injected messages arrive. Set how often the model obeys, then switch defenses on. Watch which defense can reach zero.
Illustrative rates: the prompt rule halves p-obey, approvals miss 1 in 10. Real rates depend on the model and the attacker; the docs cite adaptive attacks above 80%.
The docs add a rule about models: do not run tool-enabled agents or untrusted inboxes on older, weaker or smaller model tiers. A weak model raises p-obey. If you must use one, give it read-only tools, a strong sandbox and strict allowlists.
Chapter 8
Build a caller, pick an action, and walk it through every check in order
Every request passes four gates in order. The door: gateway.auth admits the connection. Identity: the signed nonce proves the key. Pairing: a person approved this device ID. Scope: the grant covers the method. Write each gate as 1 if it passes and 0 if it fails: d, i, p and s.
Allow equals d times i times p times s.
Check d, then i, then p, then s. Multiplying 0s and 1s is "and": one 0 anywhere makes allow 0, and the request stops at that gate.
A paired phone with read wants to send. Then d = 1, i = 1, p = 1, s = 0, so allow = 1 × 1 × 1 × 0 = 0. A new laptop wants to read: d = 1, i = 1, p = 0, so allow = 0 before scope is even checked.
Start from a preset, change any switch, choose an action and press Run. The request stops at the first gate that says 0.
Illustrative presets. The gate order follows OpenClaw's handshake: challenge, signed connect, pairing check, then scope checks per method. The CLI preset assumes a local CLI paired with admin.
The shell action has a fifth gate. Relaying a node command needs operator.write, but the Mac decides too: the gateway's node command policy and the Mac's own exec approvals must allow system.run.
Chapter 9
Keep the terms, the rules, a checklist and the core code in one place
A device is a keypair, named by its fingerprint. It signs a fresh nonce on every connect. A person pairs it, and upgrades ask again. Scopes cap what it may do, and its token carries that grant without widening it. Auth keeps the wrong callers out; hard enforcement limits what a fooled agent can do.
| Term | Meaning | OpenClaw value |
|---|---|---|
| device ID | the public key's fingerprint | device.id, checked against the public key |
| challenge | a nonce the device must sign | connect.challenge; signature format v3 |
| roles | what kind of client | operator, node |
| scopes | named operator permissions | 11, such as operator.read, .write, .admin, .pairing |
| pending device request | waits for a person | expires after 5 minutes |
| DM pairing code | lets a chat sender in | 8 characters, valid 1 hour |
| device token | carries the approved grant | hello-ok.auth.deviceToken; rotate, revoke |
| auth modes | how the door checks | token, password, trusted-proxy, none |
| sessionKey | picks the conversation | a routing selector, not an auth token |
A toy check for one request, with all four gates. It is a sketch for reading, not OpenClaw's code.
pythonimport hashlib, secrets IMPLIES = {"operator.admin": {"operator.read", "operator.write", "operator.pairing"}, "operator.write": {"operator.read"}} def expand(scopes): # S-plus out = set(scopes) for s in scopes: out |= IMPLIES.get(s, set()) return out class Gate: def __init__(self, paired): self.paired = paired # device id -> approved scopes self.pending = [] def challenge(self, conn): # a new nonce for every connection conn.nonce = secrets.token_bytes(32) return conn.nonce def connect(self, conn, dev_id, pub, sig, scopes, verify): if dev_id != hashlib.sha256(pub).hexdigest(): # id = H(pk) return "reject: id does not match key" if not verify(pub, conn.nonce, sig): # i: fresh nonce, real signature return "reject: bad signature" if dev_id not in self.paired: # p: a person must approve self.pending.append((dev_id, scopes)); return "NOT_PAIRED" if not set(scopes) <= set(self.paired[dev_id]): # broader ask: never silent self.pending.append((dev_id, scopes)); return "pending upgrade" conn.scopes = expand(scopes) return "hello-ok" def call(self, conn, need): # s: scope check on every method return need in conn.scopes
Now press Present or Teach and explain it from memory. Cover the key and its fingerprint, the fresh nonce, scopes, pairing and upgrades, and rotate against revoke. End with the sessionKey misread and soft against hard. Then go back to the hero and try every caller again.