A Rust client: CLI or wasm in a browser
auth_chad::client::Client talks to the account API. It proves its own key with DPoP on each call, renews its token,
and with end-to-end encryption seals and opens the identity key. It sends requests through your Http, so the same
code runs in a CLI and in a browser.
use auth_chad::client::{Client, Error, Http, Request, Response, Signup};
use auth_chad::core::{Clock, Draws, UnixTime};
use auth_chad::keys::SigningKey;
struct Net; // your HTTP library: send the method, URL, headers and body, return status, `dpop-nonce` and body
impl Http for Net {
async fn send(&self, _request: Request) -> Result<Response, Error> {
Err(Error::Network)
}
}
async fn device_login<C: Clock, D: Draws + Clone>(clock: C, draws: D, key: SigningKey) -> Result<(), Error> {
let mut client = Client::new(Net, clock, draws, "https://api.example.com", "myapp/v1", key).mounted_at("/v1/auth");
let waiting = client.request_device("laptop", "user", "").await?;
println!("Open {} and enter {}", waiting.verification_uri, waiting.user_code);
// every `waiting.interval` seconds, until it says Approved:
client.poll(&waiting).await?;
let _sessions: serde_json::Value = client.get("/sessions").await?; // renews the token when needed
Ok(())
}
fn keep(client: &Client<Net, impl Clock, impl Draws + Clone>) -> Option<(String, UnixTime)> {
client.token().map(|(t, end)| (t.to_owned(), end)) // store it with the key; `resume` continues
}
In a browser, add the crate with the browser feature and build for wasm32-unknown-unknown. It gives Fetch (no
cookies, no redirects), WebClock, WebDraws and Passkey (WebAuthn with the PRF extension). Call passkey methods from
a click:
use auth_chad_client::browser::{Fetch, Passkey, WebClock, WebDraws};
let mut client = Client::new(Fetch, WebClock, WebDraws, "https://api.example.com", "myapp/v1", key);
let (_session, keys) = client.signup(&Passkey, Signup { display_name: "Jane", ..Signup::default() }, true).await?;
let keys = keys.expect("with e2ee"); // show keys.recovery.display() once; keep keys.secret in memory only
let back = client.login(&Passkey, true).await?; // back.secret: the identity secret, opened with the passkey
- Where the API is:
mounted_atfollows the server’sAuth::mounted_at; the default is/auth. - The page on another host: see start:
Configorigins andwith_web_origins. - End-to-end encryption:
signup(.., true)makes the identity secret and a recovery key.login,recover,add_passkey(.., Some(&secret))andnew_recovery_keykeep the envelopes. A passkey that gives no PRF output at creation is asked once more. - Your own routes:
callsends a request with the login to any path;call_withadds headers, for exampleIf-Match, and takes a body that is already JSON. - Data from the secret in the signup: make the secret first and pass it in
Signup::secret, for example to send public keys that come from it inapp. - Errors:
Error::StepUpmeans: callstep_upand try again.Error::Refusedcarries the server’s code. - The text of a user in envelopes is the user ID. An application with envelopes from before can set its own with
with_label.