Add an authorisation invariant
Goal: require that a verified user is also allowed — an admin-only route —
and keep “not logged in” (401) distinct from “logged in but not permitted”
(403).
Authentication answers who you are; authorisation answers whether you may. A refinement actor carves an authorisation invariant out of a base actor:
context api
type UserId = String where NonEmpty
actor User { auth = Bearer(secret = "AUTH_JWT_SECRET"), identity = UserId }
actor Admin = User where hasClaim("admin")
service api from http { on GET("/admin") () -> Effect[HttpResult[UserId]] by a: Admin { Ok(a.identity) }}Admin is “a User who additionally satisfies the predicate.” A handler
by a: Admin runs through three steps at the boundary:
- verify the
User(Bearer) scheme — failure is401; - check the claim predicate against the verified token claims — failure is
403; - mint the identity and run the body.
The two failures are distinct response channels: the runtime never answers “who
are you” (401) when it means “you may not” (403).
The predicate vocabulary
Section titled “The predicate vocabulary”Token claims are untyped, so the where predicate is a closed set:
hasClaim("name")— the claim is present and truthy;claimEquals("name", "value")— the claim equals a string;- composed with
&&,||, and!.
actor Admin = User where hasClaim("admin") && claimEquals("tier", "gold")The claims are an authorisation-time input only: your body still sees just the
sealed identity (a.identity is the base UserId — an Admin is a User),
so you use an Admin anywhere a User fits.
The rules
Section titled “The rules”A refinement narrows a base actor by a predicate drawn from a closed set for that base:
| Base | Predicate | Narrows by | In a sum |
|---|---|---|---|
a Bearer actor | hasClaim("n"), claimEquals("n", "v") | the verified token’s claims | never a member |
Caller | IsContext(a.b, …) | the calling context | the sum’s one Internal member |
Both compose with &&, || and !, and a predicate from the other base is
bynk.actor.refinement_predicate_unsupported. Any other base is
bynk.actor.refinement_base_unsupported.
- A
Bearerrefinement is a handler’s solebycontract; it is never a sum member (use it as the whole contract, or narrow inside a resolved arm). - A refinement over
Callerrestricts which contexts may call a service. It is decided at compile time against the project’s calls; see Know which context called you.
Testing it
Section titled “Testing it”A test acts as the refinement with its base’s identity:
case "an admin is answered" { let r <- api.GET("/admin") by Admin("root") expect r == Ok("root")}Below the system tier the claim is given, not checked. In a system suite
the signed token carries claims that satisfy the predicate, so the Worker
admits by Admin("root") and answers by User("root") with 403. See
acting as an actor.
See also: Reference — Actors,
Diagnostic index (bynk.actor.refinement_*).