mirror of
https://github.com/Dasemu/Synapse.git
synced 2026-11-26 15:26:17 +00:00
feat(sync): op-log sync protocol, client and axum server
Adds synapse-sync (protocol types, Remote transport and read API, SyncClient with background loop, SSE realtime, resumable blobs) and synapse-server (multi-graph, bearer auth, blob streaming, read API, authenticated update endpoint), with e2e tests, docs/sync.md, docs/server.md, systemd unit and update script. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
fbcfcf678f
commit
4aa3c01a7f
25 files changed
+4299
-10
No files matched your search
@@ -0,0 +1,82 @@
|
||||
//! Bearer-token authentication. Tokens map to a [`Principal`] that may be restricted to some
|
||||
//! graphs, so per-user tokens (loaded from a file or DB) can replace the env list later
|
||||
//! without touching the handlers.
|
||||
|
||||
use crate::AppState;
|
||||
use crate::error::ApiError;
|
||||
use axum::extract::{Request, State};
|
||||
use axum::http::{StatusCode, header};
|
||||
use axum::middleware::Next;
|
||||
use axum::response::Response;
|
||||
use std::collections::HashSet;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Principal {
|
||||
pub name: String,
|
||||
/// `None` = every graph.
|
||||
pub graphs: Option<HashSet<String>>,
|
||||
}
|
||||
|
||||
impl Principal {
|
||||
pub fn can_access(&self, graph: &str) -> bool {
|
||||
self.graphs.as_ref().is_none_or(|g| g.contains(graph))
|
||||
}
|
||||
}
|
||||
|
||||
/// Stores only hashes of the tokens; compares in constant time.
|
||||
#[derive(Debug, Default)]
|
||||
pub struct Authenticator {
|
||||
entries: Vec<(blake3::Hash, Principal)>,
|
||||
}
|
||||
|
||||
impl Authenticator {
|
||||
pub fn from_shared_tokens(tokens: &[String]) -> Self {
|
||||
let entries = tokens
|
||||
.iter()
|
||||
.enumerate()
|
||||
.map(|(i, t)| {
|
||||
(
|
||||
blake3::hash(t.as_bytes()),
|
||||
Principal {
|
||||
name: format!("token-{}", i + 1),
|
||||
graphs: None,
|
||||
},
|
||||
)
|
||||
})
|
||||
.collect();
|
||||
Self { entries }
|
||||
}
|
||||
|
||||
pub fn authenticate(&self, token: &str) -> Option<Principal> {
|
||||
let h = blake3::hash(token.as_bytes());
|
||||
let mut found = None;
|
||||
// No early exit: the time taken does not depend on which token matched.
|
||||
for (stored, p) in &self.entries {
|
||||
if *stored == h {
|
||||
found = Some(p.clone());
|
||||
}
|
||||
}
|
||||
found
|
||||
}
|
||||
}
|
||||
|
||||
pub fn bearer(req: &Request) -> Option<&str> {
|
||||
req.headers()
|
||||
.get(header::AUTHORIZATION)?
|
||||
.to_str()
|
||||
.ok()?
|
||||
.strip_prefix("Bearer ")
|
||||
.map(str::trim)
|
||||
}
|
||||
|
||||
pub async fn require_auth(
|
||||
State(state): State<AppState>,
|
||||
mut req: Request,
|
||||
next: Next,
|
||||
) -> Result<Response, ApiError> {
|
||||
let principal = bearer(&req)
|
||||
.and_then(|t| state.auth.authenticate(t))
|
||||
.ok_or_else(|| ApiError::new(StatusCode::UNAUTHORIZED, "missing or invalid token"))?;
|
||||
req.extensions_mut().insert(principal);
|
||||
Ok(next.run(req).await)
|
||||
}
|
||||
Reference in new issue
Block a user