Celeris Rust server
This crate signs Celeris credentials on a trusted server. Your server authenticates its user. It decides what the user can do, and it signs those claims. Clients connect with the result through useceleris-client or a different Celeris SDK.
How it fits
- Your application asks your credential endpoint for credentials.
- The endpoint authenticates the user, decides the claims, and signs them with this crate.
- The application connects with the credentials. It never sees the signing secret.
A trusted backend that connects to realtime itself does not need the endpoint. It signs its own credentials. Refer to A backend that connects to realtime.
Install
[]
= "0.1"
= "0.1"
The crate needs Rust 1.88 or newer. It uses the client crate for Credentials, CredentialProvider and CelerisError. The client crate never uses this crate. Signing uses the RustCrypto hmac and sha2 crates and the base64 crate.
Sign credentials
use Duration;
use ;
sign is synchronous. It uses the current time as the timestamp. Sign again for each request. Do not cache, reuse or backdate credentials. A Signer is cheap to clone, and you can share it between threads.
Claims
Authenticate and authorize the user before you choose their claims. Never sign permissions that a client asked for. Claims has no default, so you must choose the channels and the permissions. Unrestricted access is always an explicit choice.
| Field | Values | Claims::new gives |
|---|---|---|
channels |
ChannelScope::All, or ChannelScope::Restricted(references): 1 to 255 ASCII letters, digits, - or _ |
(required) |
permissions |
SegmentPermissions::All { read, write }, or SegmentPermissions::Restricted(segments). No segments gives no access |
(required) |
reference |
The identity that peers see in deliveries and presence. It must not be empty and must not contain a colon, CR or LF | None |
replay |
Replay::None, Replay::Backlog for all the history that the server keeps, or Replay::Lookback(duration) in whole milliseconds, 0 to 4294967295 |
Replay::None |
allow_echo |
If true, a connection receives its own publishes |
false |
A subscription needs read, and a publish needs write. A token with write only can publish and cannot subscribe. A token with read only can subscribe and cannot publish. A publish does not join a segment.
The server replays history each time the connection joins a segment. Replay::Lookback(Duration::ZERO) is different from Replay::None: it replays nothing older than the join.
A credential endpoint
Each browser or mobile application needs a credential endpoint. This function, from examples/credential_endpoint.rs, does not need a web framework. It authorizes the channel, decides the claims, and signs them:
use Duration;
use ;
/// The user that your authentication found.
/// Gives the HTTP status and the JSON body for one credential request.
Your web framework finds the user from its session, reads a small request body, and calls the function. The user never comes from the request body. Limit the size of the request body, and set a deadline for the request headers.
A backend that connects to realtime
credential_provider changes a signer into the client's CredentialProvider, for a trusted server that connects itself. For each connection attempt, it calls your claims function again and signs with a new timestamp. Nothing in the request makes the claims wider.
use ;
use ;
examples/quickstart.rs shows the full flow as a program. If the claims function fails or gives invalid claims, Channel::connect gives a general CelerisError::Transport. To see which rule failed, sign the claims directly with Signer::sign.
Errors
Invalid options or claims give CelerisError::Configuration. The message names each field and rule that failed, but never the value:
use CelerisError;
use ;
let signer = new
.unwrap;
let error = signer
.sign
.unwrap_err;
assert_eq!;
Rust text is always valid UTF-8, so the signed bytes are always the text that you gave. For the same claims, the signed bytes are equal to the bytes of the JavaScript reference signer. Shared signing vectors prove this.
Keeping the secret
The signing secret is the trust boundary. Keep it on trusted servers. Never ship the secret, or this crate, in an application. A Signer keeps the secret only inside its HMAC state. Debug shows SignerOptions, Signer and Credentials as redacted. The crate does not print or log.
Versioning
Releases follow semantic versioning. Before version 1.0.0, a minor release can change the API.
More documentation
- Crate reference
- Authentication, for how credentials work in all SDKs
Development
make check runs rustfmt, clippy, the unit and integration tests, and the documentation build. The tests include the signing vectors, the layout and surface checks, and every Rust snippet in this README. make live runs the acceptance suites against a real Celeris stack. They read CELERIS_WS_URL, CELERIS_CLIENT_ID and CELERIS_SIGNING_SECRET from the environment or from a .env file that git ignores.
Read CONVENTIONS.md before you contribute. Read SECURITY.md before you report a vulnerability.