Tenants
A tenant is an organization or a customer: a boundary for sessions, with members. Rules: spec 14. How to grow: the wiki. Load balancer settings for both kinds of address: load-balancer.
use std::sync::Arc;
use auth_chad::{Authenticated, Store, Tenant};
use auth_chad::axum::{Auth, FirstParty, Here, Hooks, Member, Tenancy};
use auth_chad::core::member;
auth_chad::core::member_columns! {
pub struct Membership { role: String } // your columns; `auth-chad memberships` writes their migration
}
#[derive(Clone)]
struct App;
impl<S: Store> Hooks<S> for App {
async fn may_create_tenant(&self, who: &Authenticated) -> bool {
who.is_first_party() // replace with your rule
}
async fn may_manage(&self, store: &S, who: &Authenticated, tenant: Tenant) -> bool {
let Ok(user) = who.user() else { return false };
let found = member::read::<Membership>(store, tenant, user).await;
matches!(found, Ok(Some(m)) if m.columns.role == "owner")
}
async fn may_invite(&self, store: &S, who: &Authenticated, tenant: Tenant) -> bool {
tenant != Tenant::ROOT && self.may_manage(store, who, tenant).await
}
}
fn setup<S: Store + 'static>(auth: Auth<S>) -> Arc<impl auth_chad::axum::Service> {
Arc::new(auth.with_hooks(App).with_members::<Membership>().with_tenancy(Tenancy::Host { parent: "example.com".into() }))
}
async fn report(who: FirstParty, here: Here, member: Member<Membership>) -> String {
format!("{} in {:?} as {}", who.0.tenant(), here.name, member.0.columns.role)
}
- Where the tenant is in a request:
Tenancy::Host(acme.example.com: each tenant has its own web origin; needs a wildcard DNS record and certificate) orTenancy::Path(/t/acme/: one host and certificate, one cookie for each tenant). The hosts of your configured origins are the root. - Tables: tenants are an optional feature. Migrate with it (
SqliteStore::create(…, &[auth_chad::Feature::Tenants]), orauth-chad feature sqlite tenants), and callauth.check_tables()at start: it fails if they are missing. - Members: the table is in the database of the users. Your columns need a default:
auth-chad memberships sqlite role:text=memberprints the migration and the declaration.Member<Membership>reads them with each request in a tenant. - Routes, all at the login origin with the root session:
GET/POST /auth/tenants,POST /auth/tenants/{name}/enter,/members,/members/{user}(PATCH {"state":"suspended"},DELETE),/invites,/accept. - Entering on a tenant’s host: send the browser to
https://acme.example.com/auth/enter/start?return=/. Without a session, the login page ofwith_login_pagecomes first. Key-bound clientsPOST /auth/tenants/{name}/enterwith their root token. - Signup with an invite of a tenant: add
"tenant": "<name>"to both signup calls. The new user is a member. - Ending a membership ends the person’s sessions, tokens, devices, grants and API keys in that tenant at once.
Machines stay. Add your own changes to the same commit with
tenant::endandcommit_with.