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

Adding a panel to an existing app

A panel mounts on the app’s own Topcoat router and reads the app’s toasty::Db, so an app with its own users and sign-in adds one without giving either up. This chapter covers what the panel needs from the app and the two ways to connect their sign-ins.

What the panel needs

  • A Topcoat router and a Toasty Db. Panel mounts on a RouterBuilder and its resources read Toasty models. An app on another framework runs the panel as a separate binary over the same database, or puts a Topcoat router in front of its existing service with TowerRoute.
  • Models over the app’s tables. A resource needs a toasty::Model. Name a table Toasty would not derive with #[table = "…"].
  • Tablo’s tables. Add AuthSession to the Db’s models, plus the others its features name, and generate a migration for them as Deployment shows. An app with a model named as one of Tablo’s, such as its own AdminUser, lists its models with toasty::models!.
  • A prefix of its own. The gate covers the panel’s prefix, and a #[layout("/")] would wrap the panel too: give the app’s layout a narrower path.

Mounting beside the app’s sessions

The panel keeps its sessions in its own __Host-tablo_session_{prefix} cookie. The router’s SessionTokenStore and SessionLifetime stay the app’s, so a sign-in, a sign-out or a session lifetime on one side never touches the other:

#![allow(unused)]
fn main() {
pub fn app_router(db: Db) -> Result<Router> {
    let mut builder = Router::builder()
        .discover() // the app's own pages and routes, and Tablo's
        .with(db)
        .layer(CookieLayer::new())
        .with(SessionLifetime(Duration::from_hours(24 * 14))); // the app's sessions, untouched by the panel
    builder.panel(Panel::new("admin").auth(Auth::external(AppSession)))?;
    Ok(builder.build())
}
}

Install the app’s own pathless layers before .panel(..), which installs Topcoat’s CookieLayer and RuntimeLayer.

Choosing who signs users in

The app hasUseThe user sees
a user table with passwordsAuth::customthe panel’s login form, a second sign-in
its own login page, SSO or magic linksAuth::externalthe app’s login page, once

Signing in through the app

Implement ExternalAuth over the app’s session and pass it to Auth::external:

#![allow(unused)]
fn main() {
/// The app's own sessions, as the panel reads them.
pub struct AppSession;

impl ExternalAuth for AppSession {
    type User = Employee;

    async fn current_user(&self, cx: &Cx) -> Result<Option<Employee>> {
        match session::token_hash(cx).await? {
            Some(hash) => employee_for(cx, &hash).await,
            None => Ok(None),
        }
    }

    fn login_url(&self, _cx: &Cx, next: &str) -> String {
        format!("/login?return_to={next}")
    }

    fn logout_url(&self, _cx: &Cx) -> String {
        "/sign-out".to_string()
    }
}
}

The user type implements PanelUser as with any authenticator, and its can_access_panel decides who among the app’s users enters: a user it refuses gets a 403 page and no panel session.

A visitor with no app session follows this path:

  1. A page under the prefix redirects to /admin/login?next=…, as on any panel.
  2. That route finds no app user and redirects to login_url, whose next is the route’s own URL.
  3. The app signs the user in and redirects to next.
  4. The route finds the app’s user, starts a panel session and lands on the page from step 1.

next reaches the app through the browser, so check it before following it:

#![allow(unused)]
fn main() {
/// Where the app's login page sends a user it just signed in.
pub fn landing(return_to: Option<&str>) -> &str {
    return_to.and_then(auth::safe_next).unwrap_or("/")
}
}

The gate asks current_user on every request, and serves a session only while it names the session’s user. Signing out of the app, or into another account, shuts the panel at its next request, with no call to auth::revoke_sessions_for_user. For that to reach a live connection, which keeps the cookies of its handshake, current_user must consult a session the server holds. A browser that returns to the same app user finds its panel session, and its tenant, as it left them. The app’s session cookie must reach a page opened from another site, as SameSite=Lax does; behind SameSite=Strict a followed link finds no user.

The shell’s Sign out ends the panel’s session alone, then sends the browser to logout_url by GET, on any origin. Point that at a route that ends the app’s session, an SSO provider’s end-session URL included, or at a page of the app’s that offers to. While the app’s session lives, the next visit to the panel signs the user in again.

A browser holds one session on each panel, so entering this panel leaves a session held on another as it is.

An app page outside every panel that calls a panel’s shard names no panel. It runs on the session the browser holds; with sessions on several panels it runs as anonymous, unless only one of them declares the shard.

The app owns the account, so mounting refuses Auth::registration, Auth::password_reset, Auth::email_verification and Auth::two_factor beside Auth::external. The panel’s tenant menu, profile page and notifications work as on any panel.

Keep the gate on

Auth::disabled() with a layer of the app’s over the prefix leaves the panel open: its search and other endpoints serve at /_topcoat/runtime, outside the prefix, and answer anyone. Auth::external puts the same check in front of both.