moq-auth
Everything a party needs to ask for or answer an authorization on moq-relay, as a library. Use it in an auth server that answers the relay, in a service that mints tokens for clients, or in your own accept loop that decides in process.
- Request and grant:
Requestis the JSON a relay POSTs per session event (connect,revalidate,end) with everything it knows: id, node, transport, addresses, SNI and ALPN, the raw path and query, the moq-transport SETUPtoken(its Token Type and bytes, base64url on the wire), the declared role, and the verified certificate facts.Grantis the answer:publishandsubscribepattern unions, an optionalrootalias,mountsthat read a subtree from elsewhere on the origin,expires,revalidate,tier, andpeer, which marks the session as a cluster peer so the routes it announces reportSource::Peer.Grant::validaterefuses a grant that names nothing, asks to be revalidated without a bound or at no interval, or has already expired; expiry is exact, with no grace for clock skew. - Lease:
lease::Producerandlease::Consumerare the handle a session holds for its grant. The consumer reads the current grant, waits for a change, and learns why the lease ended; the producer updates and revokes. Either side's terminal call returns the reason the lease actually ended with, so whichever got there first is what both report.Consumer::fixedis a grant nobody drives.Consumer::revalidatenudges a re-check now and the producer observes it viapoll_revalidateorrevalidate_requested, which is how the relay's session push lands. Whoever runs the accept loop builds the producer, so an embedder decides in process with no trait and no HTTP. Enforcingexpiresis the holder's job; theClientdriver also revokes at expiry so itsendevent goes out.Grant::deadline()(featuretokio, also enabled byclientandserve) snapshots expiry on Tokio's clock. Call it once per accepted grant and retain the deadline; an expiry already past is now. - Client:
Client::new(url, tls)andClient::connect(request)drive a lease against an auth server overhttps://,unix://, or loopbackhttp://: revalidate on cadence with jittered backoff through an outage untilexpires, revoke on a 401/403 or an invalid grant, and POSTendwith the reason, duration, and byte totals the session reported throughlease::Consumer::closewhen it ended. Dropping the consumer reports zero bytes.end.reasonisdropped,expired,refused,invalid,narrowed,shutdown, or the session's own classification. - Server:
serve::Policyandserve::Server(featureserve) are the reference auth server behindmoq auth serve: a JWT from thejwtquery or a type-0 SETUP token, verified against a key file, a{kid}.jwkdirectory, or a JWK Set file (a token of another type, or two different ones, is refused), an explicit grant for verified certificates, the anonymous permissions, a tier, the revalidation cadence, a defaultexpires, and live session caps per token and per remote address. A token is authorized at the dialed path withClaims::authorize; residuals become the grant.Server::routeris an axumPOST /you can mount in your own service. - Keys: generate HS256/384/512, RS256/384/512, PS256/384/512, ES256/384, or EdDSA keys as JWKs, with a
kidfor rotation and an optional immutable scope that caps every token the key signs. - Claims:
root,publish,subscribe,exp,iat. Grants arePatternunions:foois one broadcast,foo/**is a subtree,**is everything.Key::signandKey::verifyhandle the signature and expiry;Key::decode::<T>checks only signature, algorithm, and key ID before returning an application payload. Legacyput/getprefix claims and scopes read as subtrees (pisp/**), and grants that are all subtrees are written that way so older verifiers accept them. - Authorization:
Claims::authorize(path)scopes verified claims to the path a client dialed and returns the publish and subscribe patterns relative to it, exactly asmoq auth servedoes. The relay forwards the raw path and enforces the grant it gets.
bash
cargo add moq-authFor command-line use, install the moq CLI and run moq auth generate|sign|verify, moq auth serve for the auth server, or moq auth sessions|revalidate to list live sessions and push a re-check through the relay's internal listener. Examples: basic.rs and asymmetric.rs. The TypeScript twin, @moq/auth, reads the same request, builds the same grant, and mints identical tokens. API: docs.rs/moq-auth.