Remote / foldkit-remote-server
Authentication vs authorization
Keep authentication and authorization at separate boundaries:
- Authentication ("who is making this request?") runs in your Effect RPC
middleware or HTTP handler and resolves a
Principal. - Authorization ("which semantic fields may this principal read?") runs in
RemoteServer.entity(..., { authorize }).
RemoteServer.handlers(Server, principal) receives an already-resolved
principal rather than inspecting headers, sessions, cookies, or bearer tokens.
The principal is a value, or a Context key that each call reads, so one
handler set (one RPC layer) answers every request and authentication provides
the key per request:
ts
import { Context } from 'effect'
class CurrentUser extends Context.Service<CurrentUser, Principal>()('app/CurrentUser') {}
const answering = RemoteServer.handlers(Server, CurrentUser)
// per request, once authentication has resolved `user`
RemoteServer.answer(answering, body).pipe(Effect.provideService(CurrentUser, user))Every handler then needs CurrentUser, which the types carry: a key holding
another type than the Server's principal is a type error.
For Entity reads:
- undeclared fields are dropped first;
authorizereceives only declared, requested fields;- omitting
authorizemeans the requested declared fields are readable; - a withheld field is answered as settled, without its reason;
- an id whose every requested field is withheld is not read from the Source: its fields are settled and no entity is contributed, so it looks the same as an id the Source does not know, and existence is not leaked.
Mutation and Query policy belongs in their Sources because those operations are application-specific rather than field-selection policy.