Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

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_at follows the server’s Auth::mounted_at; the default is /auth.
  • The page on another host: see start: Config origins and with_web_origins.
  • End-to-end encryption: signup(.., true) makes the identity secret and a recovery key. login, recover, add_passkey(.., Some(&secret)) and new_recovery_key keep the envelopes. A passkey that gives no PRF output at creation is asked once more.
  • Your own routes: call sends a request with the login to any path; call_with adds headers, for example If-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 in app.
  • Errors: Error::StepUp means: call step_up and try again. Error::Refused carries 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.