Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ Watari is a Rust/Axum web GUI that sits in front of [rustypaste](https://github.
- "Admin" (for gating `/admin/tokens`) is defined as *the resolved token binding has the `delete` permission* — the spec doesn't define it more precisely.
- Encrypted URL-shortening: rustypaste's `url` field is a redirect target, so it can't hold ciphertext. When password-protection is on, `/api/shorten` uploads the encrypted URL as a `.enc` **file** instead of calling rustypaste's shorten endpoint (still logged with `kind: "url"` in `upload_log` for display purposes).
- Text paste has no filename/extension field (§8): only the content matters. `/api/paste` ignores any `filename` field and sends rustypaste a generated unique `paste-<8 hex>.txt` (unique so deployments without `random_url` don't 409 on name collisions). The dashboard label (`display_name`) is the paste's first non-blank line, or rustypaste's resulting filename for encrypted pastes, whose content the server can't read.
- Public pages beyond `/decrypt` (§8): `/home` is public too, and `/` (the dashboard) redirects anonymous visitors to `/home` instead of `/auth/login`. Both live in `routes::public_router()` and take `Option<UserSession>`; anonymous visitors get a reduced sidebar (Home, Decrypt, "Log in"). Every other page and `/api/*` route still takes `UserSession` and requires SSO.
- CSP has no nonce — every template ended up with zero inline `<script>`/`<style>` (HTMX config goes through the `hx-headers` body attribute, encryption logic lives in `static/app.js`), so a static policy is enough.

## Architecture invariants worth preserving
Expand Down
9 changes: 1 addition & 8 deletions src/routes/decrypt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,7 @@ pub async fn page(
csrf_token: CsrfToken,
Query(query): Query<DecryptPageQuery>,
) -> Response {
let layout = match &session {
Some(session) => super::pages::layout_for(&state, &csrf_token, session),
None => csrf_token
.authenticity_token()
.map(|csrf| Layout::anonymous(csrf, state.config.pbkdf2_iterations))
.map_err(|e| anyhow::anyhow!("csrf token error: {e}")),
};
let layout = match layout {
let layout = match super::pages::public_layout(&state, &csrf_token, session.as_ref()) {
Ok(l) => l,
Err(e) => return AppError::Internal(e).into_response(),
};
Expand Down
14 changes: 9 additions & 5 deletions src/routes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,22 @@ use axum::routing::{delete, get, post};
use crate::ratelimit;
use crate::state::AppState;

/// Public routes: no session required (watari.md §9.3 — `/decrypt` is
/// intentionally reachable by recipients without an SSO account).
/// Public routes: no session required. `/decrypt` is reachable by recipients
/// without an SSO account (watari.md §9.3); `/home` is the public landing page,
/// and `/` sends anonymous visitors there (signed-in users get the dashboard).
/// These handlers take `Option<UserSession>` so signed-in users keep their
/// sidebar; everything in `router()` below requires SSO.
pub fn public_router() -> Router<AppState> {
Router::new()
.route("/", get(pages::dashboard))
.route("/home", get(pages::home_page))
.route("/decrypt", get(decrypt::page))
.route("/decrypt/fetch", get(decrypt::fetch))
}

/// Session-guarded application routes (watari.md §3's route table, minus
/// `/auth/*` and `/decrypt`, which are public — see main.rs).
/// `/auth/*` and the public routes above — see main.rs). Each handler takes
/// `UserSession`, which redirects to `/auth/login` without a valid session.
pub fn router(max_body_bytes: usize) -> Router<AppState> {
let mutating_api = Router::new()
.route("/api/upload", post(api::upload))
Expand All @@ -29,8 +35,6 @@ pub fn router(max_body_bytes: usize) -> Router<AppState> {
.layer(ratelimit::api_governor_layer());

Router::new()
.route("/", get(pages::dashboard))
.route("/home", get(pages::home_page))
.route("/upload", get(pages::upload_page))
.route("/paste", get(pages::paste_page))
.route("/shorten", get(pages::shorten_page))
Expand Down
32 changes: 27 additions & 5 deletions src/routes/pages.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use askama::Template;
use axum::extract::State;
use axum::response::IntoResponse;
use axum::response::{IntoResponse, Redirect};
use axum_csrf::CsrfToken;
use time::OffsetDateTime;
use time::format_description::well_known::Rfc3339;
Expand Down Expand Up @@ -158,7 +158,7 @@ fn can_delete(state: &AppState, token_id: &str) -> bool {
.unwrap_or(false)
}

pub(crate) fn layout_for(
fn layout_for(
state: &AppState,
token: &CsrfToken,
session: &UserSession,
Expand All @@ -174,6 +174,22 @@ pub(crate) fn layout_for(
))
}

/// Layout for public pages (`/home`, `/decrypt`): the signed-in sidebar when a
/// session exists, the anonymous one (Home, Decrypt, "Log in") otherwise.
pub(crate) fn public_layout(
state: &AppState,
token: &CsrfToken,
session: Option<&UserSession>,
) -> anyhow::Result<Layout> {
match session {
Some(session) => layout_for(state, token, session),
None => token
.authenticity_token()
.map(|csrf| Layout::anonymous(csrf, state.config.pbkdf2_iterations))
.map_err(|e| anyhow::anyhow!("csrf token error: {e}")),
}
}

struct DashboardSection {
kind: db::UploadKind,
page: SectionPage,
Expand All @@ -187,11 +203,16 @@ struct DashboardTemplate {
can_delete: bool,
}

/// `GET /` — the dashboard for signed-in users. Anonymous visitors land on the
/// public Home page instead of being sent straight to the SSO login.
pub async fn dashboard(
State(state): State<AppState>,
session: UserSession,
session: Option<UserSession>,
csrf_token: CsrfToken,
) -> axum::response::Response {
let Some(session) = session else {
return Redirect::to("/home").into_response();
};
let layout = match layout_for(&state, &csrf_token, &session) {
Ok(l) => l,
Err(e) => return crate::error::AppError::Internal(e).into_response(),
Expand Down Expand Up @@ -220,12 +241,13 @@ struct HomeTemplate {
layout: Layout,
}

/// `GET /home` — public.
pub async fn home_page(
State(state): State<AppState>,
session: UserSession,
session: Option<UserSession>,
csrf_token: CsrfToken,
) -> axum::response::Response {
match layout_for(&state, &csrf_token, &session) {
match public_layout(&state, &csrf_token, session.as_ref()) {
Ok(layout) => (csrf_token, Tpl(HomeTemplate { layout })).into_response(),
Err(e) => crate::error::AppError::Internal(e).into_response(),
}
Expand Down
15 changes: 15 additions & 0 deletions static/app.css
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,8 @@ h1 { font-size: 1.4rem; }
.home ul { max-width: 42rem; line-height: 1.55; }
.home .lead { font-size: 1.05rem; opacity: 0.8; margin-top: -0.25rem; }
.home h2 { font-size: 1.1rem; margin: 2rem 0 0.5rem; }
.home-cta { display: flex; flex-wrap: wrap; align-items: center; gap: 0.5rem 0.9rem; margin-top: 1.25rem; }
.home-cta .hint { margin: 0; flex: 1 1 18rem; }
.feature-list { padding-left: 1.25rem; }
.feature-list li { margin-bottom: 0.4rem; }

Expand Down Expand Up @@ -277,6 +279,19 @@ button.copy-btn {
color: var(--accent);
}

/* A link that looks like the primary button (e.g. "Log in with SSO"). */
a.button-link {
display: inline-block;
padding: 0.5rem 1.1rem;
border-radius: 0.4rem;
border: 1px solid var(--accent);
background: var(--accent);
color: white;
text-decoration: none;
}

a.icon-btn { text-decoration: none; }

.hint { font-size: 0.85rem; opacity: 0.7; }

.dropzone {
Expand Down
8 changes: 8 additions & 0 deletions templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round"><path d="M3 10.5 12 3l9 7.5"/><path d="M5 9v10a2 2 0 0 0 2 2h10a2 2 0 0 0 2-2V9"/><path d="M10 21v-6h4v6"/></svg>
<span>Home</span>
</a>
{# Signed-in only: these routes require SSO. Anonymous visitors get Home + Decrypt. #}
{% if layout.user_email.is_some() %}
<a href="/">
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round"><rect x="3" y="3" width="7" height="9" rx="1.5"/><rect x="14" y="3" width="7" height="5" rx="1.5"/><rect x="14" y="12" width="7" height="9" rx="1.5"/><rect x="3" y="16" width="7" height="5" rx="1.5"/></svg>
<span>Dashboard</span>
Expand All @@ -38,6 +40,7 @@
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round"><path d="M9 15 15 9"/><path d="M10.5 6.5 12 5a3.54 3.54 0 1 1 5 5l-1.5 1.5"/><path d="M13.5 17.5 12 19a3.54 3.54 0 1 1-5-5l1.5-1.5"/></svg>
<span>Shorten</span>
</a>
{% endif %}
<a href="/decrypt">
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round"><rect x="4" y="11" width="16" height="10" rx="2"/><path d="M8 11V7a4 4 0 0 1 7.75-1.4"/><path d="M12 15v2"/></svg>
<span>Decrypt</span>
Expand All @@ -63,6 +66,11 @@
<span>Log out</span>
</button>
</form>
{% else %}
<a class="icon-btn" href="/auth/login" title="Log in with SSO">
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round"><path d="M15 3h3a2 2 0 0 1 2 2v14a2 2 0 0 1-2 2h-3"/><path d="M10 17l5-5-5-5"/><path d="M15 12H3"/></svg>
<span>Log in</span>
</a>
{% endif %}
</div>
</aside>
Expand Down
6 changes: 6 additions & 0 deletions templates/home.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@
<div class="home">
<h1>Watari</h1>
<p class="lead">A web front end for <a href="https://github.com/orhun/rustypaste" target="_blank" rel="noopener">rustypaste</a> that gives every teammate their own login.</p>
{% if layout.user_email.is_none() %}
<div class="home-cta">
<a class="button-link" href="/auth/login">Log in with SSO</a>
<p class="hint">to upload files, paste text and shorten links. Opening password-protected content needs no account: use the <a href="/decrypt">Decrypt</a> tab.</p>
</div>
{% endif %}

<h2>Why Watari?</h2>
<p>rustypaste is a minimal self-hosted pastebin and file-sharing server. It only offers an HTTP API protected by shared tokens and has no notion of users, so everyone who wants to use it ends up holding the same raw token.</p>
Expand Down
Loading