Authorization
Two gates, both the app's. Who reaches which object is decided before the request gets there, as the
Usage example does with routeAgentRequest's onBeforeConnect (and onBeforeRequest for HTTP): each
user reaches the object named after them alone. A Worker that routes requests itself checks the same
before getByName(...).fetch(request). Browsers cannot set headers on a WebSocket upgrade, so the
sign-in travels in a cookie or in useAgent's query.
Each call on a connection that got through is decided by authorize, when the app passes one. It runs
before every call, after its params parse, with the connection and the call as { method, params }
(MotokoCall, typed from the protocol's schemas). false answers the call forbidden and runs
nothing; a throw is reported as a motoko:socket_error and answered internal. It may be async: the
reply still goes out when the call has run, and subscribe's still before its first event. Without
authorize, every connection may make every call on its instance's sessions, and an object serves one
instance.
A connection the SDK marks readonly only follows: subscribe, unsubscribe and listSessions run,
and createSession, submit, continue, approve, reply, abort, withdraw, compact, renameSession and deleteSession are answered forbidden before
authorize is asked. Motoko reads the mark off connections: an Agent's isConnectionReadonly
(set with shouldConnectionBeReadonly or setConnectionReadonly), or the WebSockets capability's
isReadonly (its readonly option or setReadonly), so connections: this and
connections: this.webSockets need nothing more.
What a session runs with is the object's to choose: a createSession call carries only its request
ID. sessionOptions(connection) answers the options of each session a createSession on the socket
makes, as core's createSession takes them without requestId (model, thinkingLevel, maxTurns,
tools, requiresApproval, metadata), such as a turn limit by the user's plan, which the app keeps in the
connection's state or its own storage. It is asked for each such call, a retry's too, whose first
session stands as it was made. A throw is reported and answered internal, and makes no session.
Without it, a session runs with the agent's defaults. An RPC method of the app's that calls
instance.createSession passes its options itself.
An object shared by several users would need checks per session: authorize sees each call's
sessionId, and which sessions a connection's user may touch is the app's to keep. Simpler still, give
each principal an object of its own.