Signing in
The tokens in the last chapter’s sessions were minted by the store for a caller id. A real application lets people sign in somewhere else, at an identity provider, which hands them a login token: a short signed statement of who they are. The application has to decide whether to believe it. This chapter declares a provider and gives a flow what the provider says about the person. Nothing in it is required: a program with no trust line has no sign-in and loses nothing; a package never meets it.
// The door checks a login token before any flow runs; a flow that uses `identity` receives what the token says.
// Who the members are is the program's own state.
trust jwt issuer "https://login.example" audience "notes-app";
row Member { id: id, subject: string(255), issuer: string(255), email: option<string(254)>, verified: bool }
unit Members {
caps storage;
state members: Member[8];
// a first sign-in adds the member; a later one keeps the email the token carries now. One caller id is one
// identity at the door, so a known id that arrives with another subject is refused rather than trusted.
contract sign_in(member: id, who: Identity) {
case member in members && (members[member].subject != who.subject || members[member].issuer != who.issuer) => fail Mismatch;
case member in members => Returned: members[member].email := who.email, members[member].verified := who.email_verified;
else => Joined: members += [Member { id: member, subject: who.subject, issuer: who.issuer, email: who.email, verified: who.email_verified }];
examples {
{} (7, Identity { subject: "ada", email: some("ada@example.com"), email_verified: true, issuer: "https://login.example" }) => Joined { members: [Member { id: 7, subject: "ada", issuer: "https://login.example", email: some("ada@example.com"), verified: true }] };
{ members: [Member { id: 7, subject: "ada", issuer: "https://login.example", email: none, verified: false }] } (7, Identity { subject: "ada", email: some("ada@new.example"), email_verified: false, issuer: "https://login.example" }) => Returned { members: [Member { id: 7, subject: "ada", issuer: "https://login.example", email: some("ada@new.example"), verified: false }] };
{ members: [Member { id: 7, subject: "ada", issuer: "https://login.example", email: none, verified: false }] } (7, Identity { subject: "bob", email: none, email_verified: false, issuer: "https://login.example" }) => Mismatch;
}
}
}
flow sign_in() uses caller, identity atomic { Members.sign_in(caller, identity); }
view me() uses caller = where(m in Members.members: m.id == caller);
route POST "/sign-in" => sign_in();
route GET "/me" => me();
scenario two_people {
as 7 identity "ada" "ada@example.com" sign_in() => Joined;
as 7 identity "ada" "ada@new.example" unverified sign_in() => Returned;
as 7 me() => Value([Member { id: 7, subject: "ada", issuer: "https://login.example", email: some("ada@new.example"), verified: false }]);
as 8 sign_in() => Joined;
as 8 me() => Value([Member { id: 8, subject: "8", issuer: "", email: none, verified: false }]);
as 7 identity "bob" sign_in() => Mismatch;
}
The door checks, the program keeps users
The work is split in two. The door verifies: before any flow runs, it checks the token’s signature against the keys the provider publishes, its issuer, its audience and its expiry. That step is the runtime’s, configured by one line of the program, with no code for any particular provider. Everything after the door is the program’s: who the members are, what a first sign-in does, what a person may see. Here that is Members, ordinary state with an ordinary contract. The language never learns what a user is; it learns only that a request comes from a verified identity.
The trust line
trust jwt issuer "https://login.example" audience "notes-app"; names one provider. issuer is exactly what the provider’s tokens carry in their iss claim. audience is this application’s id at the provider, which a token must carry in aud, so a token issued for another application is refused; audience env "APP_AUDIENCE" reads it from the environment when the door starts instead, and the door refuses to start while the variable is missing or empty. Without a keys clause the door finds the provider’s keys through its discovery document, <issuer>/.well-known/openid-configuration, so the issuer must be an https address. algorithms names RS256, ES256 or both, and both when the clause is left out; an unsigned token and shared-secret signatures such as HS256 are refused. Several providers are several lines. https://login.example is an address reserved for examples, so no login token gets into this program until the line names a real provider.
What a flow receives
uses identity binds identity, a row the language declares in every program that trusts an issuer:
row Identity { subject: string(255), email: option<string(254)>, email_verified: bool, issuer: string(255) }
The door fills it from the verified token only: the subject and the issuer from its claims, the email when the token carries one of at most 254 bytes, and email_verified true only when the provider says so. caller stays an id: the door gives each pair of issuer and subject a caller id of its own, kept in the store, the same on every sign-in, so a flow keyed by caller works unchanged. A caller with a minted token has an identity too, with the caller id as its subject, no email and an empty issuer, so identity.issuer == "" tells the two kinds apart. A view may use caller, never identity.
A request never supplies an identity: in a program that trusts an issuer a flow parameter may not hold one. And uses identity with nothing to verify it is refused:
row Visit { id: id, subject: string(255) }
unit Visits {
state visits: Visit[8];
contract note(visit: id, who: Identity) => Noted: visits += [Visit { id: visit, subject: who.subject }];
}
flow visit() uses caller, identity atomic { Visits.note(caller, identity); }
book/refusals/signing-in-no-trust.vish:6:27: error: `uses identity` binds the identity a verified login token carries, and this program trusts no issuer; declare one: `trust jwt issuer "https://…" audience env "NAME";` (docs/identity.md)
flow visit() uses caller, identity atomic { Visits.note(caller, identity); }
^^^^^^^^
In a scenario, and in the verifier
scenario two_people
sign_in() => Joined
sign_in() => Returned
me() => Value([Member { id: 7, subject: "ada", issuer: "https://login.example", email: Some("ada@new.example"), verified: false }])
sign_in() => Joined
me() => Value([Member { id: 8, subject: "8", issuer: "", email: None, verified: false }])
sign_in() => Mismatch
Members = Members { members: [Member { id: 7, subject: "ada", issuer: "https://login.example", email: Some("ada@new.example"), verified: false }, Member { id: 8, subject: "8", issuer: "", email: None, verified: false }] }
6 step(s), all as expected
as 7 identity "ada" "ada@example.com" gives the step a login identity: the subject, then an email, verified unless unverified follows it, and the first trust line’s issuer unless issuer "…" names another. as 8 alone is a caller with a minted token. The last step brings caller 7 back with another subject, which the door never sends, and the contract refuses it rather than trusts it.
verify: 3 examples, 6 scenario steps and 3000 sampled checks passed
No token exists in verification. The verifier draws the caller as it does for uses caller, and the identity from the caller, so one caller id is always one identity: some callers hold minted tokens, the others tokens from each trusted issuer, and the email is drawn absent, verified or unverified on every call. Over a run every kind reaches every flow that uses identity. That is sampling, not a proof.
At the door
The check that keeps this guide true calls the first POST route with no token:
POST /sign-in
{"error":"unauthorized: a bearer token is required","flow":"sign_in"}
A route to a flow that uses caller or identity takes Authorization: Bearer <token>. A token the store minted (token 7 on the service’s command line, --mint 7 at the development door) is looked up in the store. A login token must name a trusted issuer, carry a signature that issuer’s key verifies, hold the audience, and be neither expired nor not yet valid, with 60 seconds of allowance for clocks. A refused token answers 401 with the class of the reason, such as unauthorized: the token has expired or unauthorized: the token is for another audience, and never echoes the token. The door fetches every provider’s keys when it starts and keeps them for up to an hour; while a provider’s keys have not been fetched, its tokens answer 503. docs/identity.md gives every check and every reason, and tests/check_identity.sh holds both doors to them with a stand-in provider.
The OpenAPI description marks the route, so a client team sees that it needs a token:
{
"POST": "/sign-in",
"requestBody": "(no body)",
"responses": {
"200": "the flow succeeded",
"400": "a missing or malformed argument",
"401": "no valid bearer token",
"409": "the flow failed with a declared outcome; nothing was written"
}
}
It marks a route whose flow or view uses caller. A route to a flow that uses identity without caller is not marked today, although the door asks it for a token too.
One more record keeps caller ids honest. Before anyone signs in, the command line may already have acted for ids: token <caller> mints a token for one, and a flow run with VISHY_CALLER acts for one. Both first record the id in the store, in every program, and both refuse an id a signed-in identity holds, so no person who signs in later is given an id whose rows are someone else’s. A program keyed by caller ids therefore needs no change when it adds the trust line. A store written before that record existed is the one exception, and docs/deploy.md, “Sign-in”, gives the operator’s step.
What is not here
Sign-up, passwords, sessions, refresh tokens and cookies belong to the identity provider: an application obtains a token there and sends it. Tokens are trusted until they expire; the provider decides how long that is, and a program can refuse a user it has blocked in its own state. Encrypted tokens, opaque tokens that need a call to the provider, and shared-secret signatures are not accepted. A key the provider removes from its set is still accepted until the door next fetches the set, up to an hour later. Roles are application state, never part of the language.
Try
Take identity out of the flow’s uses, leaving flow sign_in() uses caller atomic { … }. The checker refuses the call that passes it on: unknown name `identity`: the verified identity of a request is bound by `uses identity` on a flow (docs/identity.md). A flow sees only what its header names, the identity of the person included.