[relay] F20: axum + SQLite relay storing exactly PROTOCOL.md section 5, tokens as hashes, no request log
This commit is contained in:
@@ -0,0 +1,162 @@
|
||||
//! The HTTP face of the store. No logging, no connection info: a handler never sees the client's
|
||||
//! address, and nothing here writes a request anywhere.
|
||||
|
||||
use crate::store::{Refused, Store};
|
||||
use axum::body::Bytes;
|
||||
use axum::extract::{DefaultBodyLimit, Path, State};
|
||||
use axum::http::{header, HeaderMap, StatusCode};
|
||||
use axum::response::{IntoResponse, Response};
|
||||
use axum::routing::{delete, get, post, put};
|
||||
use axum::{Json, Router};
|
||||
use serde::de::DeserializeOwned;
|
||||
use std::sync::Arc;
|
||||
use tabellarius_protocol::{
|
||||
CreateInvite, CreateMailbox, FetchedBlobs, InviteId, MailboxId, RotatePostToken, Sha256Hash,
|
||||
Token, VersionInfo, MAX_BLOB_BYTES, VERSION,
|
||||
};
|
||||
|
||||
/// Unix seconds. The server passes the system clock; tests pass their own.
|
||||
pub type Clock = Arc<dyn Fn() -> i64 + Send + Sync>;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct AppState {
|
||||
pub store: Arc<Store>,
|
||||
pub clock: Clock,
|
||||
}
|
||||
|
||||
/// JSON bodies are small: the largest is an invite with a 16 KiB KeyPackage in base64url.
|
||||
const MAX_JSON_BYTES: usize = 32 * 1024;
|
||||
|
||||
pub fn router(state: AppState) -> Router {
|
||||
let blob_routes = Router::new()
|
||||
.route("/v0/mailboxes/{id}/blobs", post(post_blob).get(fetch_blobs))
|
||||
.layer(DefaultBodyLimit::max(MAX_BLOB_BYTES));
|
||||
Router::new()
|
||||
.route("/v0/mailboxes", post(create_mailbox))
|
||||
.route("/v0/mailboxes/{id}/blobs/{hash}", delete(acknowledge))
|
||||
.route("/v0/mailboxes/{id}/post-token", put(rotate_post_token))
|
||||
.route("/v0/invites", post(create_invite))
|
||||
.route("/v0/invites/{id}", get(claim_invite))
|
||||
.route("/version", get(version))
|
||||
.layer(DefaultBodyLimit::max(MAX_JSON_BYTES))
|
||||
.merge(blob_routes)
|
||||
.with_state(state)
|
||||
}
|
||||
|
||||
/// Every refusal is a bare status code: no body that could echo the request back.
|
||||
struct Status(StatusCode);
|
||||
|
||||
impl IntoResponse for Status {
|
||||
fn into_response(self) -> Response {
|
||||
self.0.into_response()
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Refused> for Status {
|
||||
fn from(r: Refused) -> Self {
|
||||
Status(match r {
|
||||
Refused::NotFound => StatusCode::NOT_FOUND,
|
||||
Refused::Exists => StatusCode::CONFLICT,
|
||||
Refused::TooLarge => StatusCode::PAYLOAD_TOO_LARGE,
|
||||
Refused::Storage => StatusCode::INTERNAL_SERVER_ERROR,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// Parses a JSON body with the protocol's strict types; any failure is a plain 400.
|
||||
fn json<T: DeserializeOwned>(body: &Bytes) -> Result<T, Status> {
|
||||
serde_json::from_slice(body).map_err(|_| Status(StatusCode::BAD_REQUEST))
|
||||
}
|
||||
|
||||
/// A malformed id in the path is treated like an unknown one.
|
||||
fn mailbox(id: &str) -> Result<MailboxId, Status> {
|
||||
MailboxId::parse(id).map_err(|_| Status(StatusCode::NOT_FOUND))
|
||||
}
|
||||
|
||||
/// A missing or malformed bearer token is treated like a wrong one.
|
||||
fn bearer(headers: &HeaderMap) -> Result<Token, Status> {
|
||||
headers
|
||||
.get(header::AUTHORIZATION)
|
||||
.and_then(|v| v.to_str().ok())
|
||||
.and_then(|v| Token::from_bearer(v).ok())
|
||||
.ok_or(Status(StatusCode::NOT_FOUND))
|
||||
}
|
||||
|
||||
async fn create_mailbox(State(s): State<AppState>, body: Bytes) -> Result<StatusCode, Status> {
|
||||
let req: CreateMailbox = json(&body)?;
|
||||
s.store.create_mailbox(&req, (s.clock)())?;
|
||||
Ok(StatusCode::CREATED)
|
||||
}
|
||||
|
||||
async fn post_blob(
|
||||
State(s): State<AppState>,
|
||||
Path(id): Path<String>,
|
||||
headers: HeaderMap,
|
||||
body: Bytes,
|
||||
) -> Result<StatusCode, Status> {
|
||||
let id = mailbox(&id)?;
|
||||
let token = bearer(&headers)?;
|
||||
s.store.post(&id, &token, &body, (s.clock)())?;
|
||||
Ok(StatusCode::CREATED)
|
||||
}
|
||||
|
||||
async fn fetch_blobs(
|
||||
State(s): State<AppState>,
|
||||
Path(id): Path<String>,
|
||||
headers: HeaderMap,
|
||||
) -> Result<Json<FetchedBlobs>, Status> {
|
||||
let id = mailbox(&id)?;
|
||||
let token = bearer(&headers)?;
|
||||
let blobs = s.store.fetch(&id, &token)?;
|
||||
Ok(Json(FetchedBlobs { blobs }))
|
||||
}
|
||||
|
||||
async fn acknowledge(
|
||||
State(s): State<AppState>,
|
||||
Path((id, hash)): Path<(String, String)>,
|
||||
headers: HeaderMap,
|
||||
) -> Result<StatusCode, Status> {
|
||||
let id = mailbox(&id)?;
|
||||
let token = bearer(&headers)?;
|
||||
let hash = Sha256Hash::parse(&hash).map_err(|_| Status(StatusCode::NOT_FOUND))?;
|
||||
s.store.acknowledge(&id, &token, &hash)?;
|
||||
Ok(StatusCode::NO_CONTENT)
|
||||
}
|
||||
|
||||
async fn rotate_post_token(
|
||||
State(s): State<AppState>,
|
||||
Path(id): Path<String>,
|
||||
headers: HeaderMap,
|
||||
body: Bytes,
|
||||
) -> Result<StatusCode, Status> {
|
||||
let id = mailbox(&id)?;
|
||||
let token = bearer(&headers)?;
|
||||
let req: RotatePostToken = json(&body)?;
|
||||
s.store
|
||||
.rotate_post_token(&id, &token, &req.post_token_hash)?;
|
||||
Ok(StatusCode::NO_CONTENT)
|
||||
}
|
||||
|
||||
async fn create_invite(State(s): State<AppState>, body: Bytes) -> Result<StatusCode, Status> {
|
||||
let req: CreateInvite = json(&body)?;
|
||||
s.store.create_invite(&req, (s.clock)())?;
|
||||
Ok(StatusCode::CREATED)
|
||||
}
|
||||
|
||||
async fn claim_invite(
|
||||
State(s): State<AppState>,
|
||||
Path(id): Path<String>,
|
||||
headers: HeaderMap,
|
||||
) -> Result<Response, Status> {
|
||||
let id = InviteId::parse(&id).map_err(|_| Status(StatusCode::NOT_FOUND))?;
|
||||
let secret = bearer(&headers)?;
|
||||
let claimed = s.store.claim_invite(&id, &secret, (s.clock)())?;
|
||||
Ok(Json(claimed).into_response())
|
||||
}
|
||||
|
||||
async fn version() -> Json<VersionInfo> {
|
||||
Json(VersionInfo {
|
||||
protocol: VERSION.to_string(),
|
||||
relay: env!("CARGO_PKG_VERSION").to_string(),
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user