Skip to content
Playground(another site)

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:

  1. verify the User (Bearer) scheme — failure is 401;
  2. check the claim predicate against the verified token claims — failure is 403;
  3. 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).

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.

A refinement narrows a base actor by a predicate drawn from a closed set for that base:

BasePredicateNarrows byIn a sum
a Bearer actorhasClaim("n"), claimEquals("n", "v")the verified token’s claimsnever a member
CallerIsContext(a.b, …)the calling contextthe 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 Bearer refinement is a handler’s sole by contract; it is never a sum member (use it as the whole contract, or narrow inside a resolved arm).
  • A refinement over Caller restricts which contexts may call a service. It is decided at compile time against the project’s calls; see Know which context called you.

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_*).