fight unuseds, sessions endpoint

This commit is contained in:
2026-02-24 01:42:19 +01:00
parent ee7ed48144
commit 1e7866a293
11 changed files with 59 additions and 13 deletions

View File

@@ -4,18 +4,19 @@ use axum::{
routing::get,
};
use crate::{
api::users::{get_by_id, get_me},
users::{UserError, auth::AuthError, sessions::SessionError},
};
use crate::users::{UserError, auth::AuthError, sessions::SessionError};
mod sessions;
mod users;
// TODO: PERMISSIONS FOR ENDPOINTS & ACTIONS
pub fn api_router() -> Router {
Router::new()
.route("/api/live", get(async || "Mnemosyne lives"))
.route("/api/users/me", get(get_me))
.route("/api/users/{id}", get(get_by_id))
.route("/api/users/me", get(users::get_me))
.route("/api/users/{id}", get(users::get_by_id))
.route("/api/users/@{handle}", get(users::get_by_handle))
.route("/api/sessions/{id}", get(sessions::get_by_id))
}
pub struct CompositeError(Response);

24
src/api/sessions.rs Normal file
View File

@@ -0,0 +1,24 @@
use axum::{
Json,
extract::Path,
http::HeaderMap,
response::{IntoResponse, Response},
};
use uuid::Uuid;
use crate::{
api::CompositeError,
users::{
User,
auth::{UserAuthRequired, UserAuthenticate},
sessions::Session,
},
};
pub async fn get_by_id(
Path(id): Path<Uuid>,
headers: HeaderMap,
) -> Result<Response, CompositeError> {
User::authenticate(&headers)?.required()?;
Ok(Json(Session::get_by_id(id)?).into_response())
}

View File

@@ -1,7 +1,7 @@
use axum::{
Json,
extract::Path,
http::{HeaderMap, StatusCode},
http::HeaderMap,
response::{IntoResponse, Response},
};
use uuid::Uuid;
@@ -11,14 +11,26 @@ use crate::{
users::{
User,
auth::{UserAuthRequired, UserAuthenticate},
handle::UserHandle,
},
};
pub async fn get_me(h: HeaderMap) -> Result<Response, CompositeError> {
Ok(Json(User::authenticate(&h)?.required()?).into_response())
pub async fn get_me(headers: HeaderMap) -> Result<Response, CompositeError> {
Ok(Json(User::authenticate(&headers)?.required()?).into_response())
}
pub async fn get_by_id(Path(id): Path<Uuid>, h: HeaderMap) -> Result<Response, CompositeError> {
User::authenticate(&h)?.required()?;
pub async fn get_by_id(
Path(id): Path<Uuid>,
headers: HeaderMap,
) -> Result<Response, CompositeError> {
User::authenticate(&headers)?.required()?;
Ok(Json(User::get_by_id(id)?).into_response())
}
pub async fn get_by_handle(
Path(handle): Path<UserHandle>,
headers: HeaderMap,
) -> Result<Response, CompositeError> {
User::authenticate(&headers)?.required()?;
Ok(Json(User::get_by_handle(handle)?).into_response())
}

View File

@@ -36,7 +36,6 @@ async fn main() -> Result<(), Box<dyn Error>> {
let r = api::api_router();
let l = TcpListener::bind(format!("0.0.0.0:{port}")).await?;
println!("Listener bound to {}", l.local_addr()?);
let port = l.local_addr()?.port();
axum::serve(l, r).await?;
Ok(())

View File

@@ -1,3 +1,4 @@
pub mod names;
#[allow(unused)]
pub struct Person;

View File

@@ -1 +1,2 @@
#[allow(unused)]
pub struct Name;

View File

@@ -1 +1,2 @@
#[allow(unused)]
pub struct QuoteLine;

View File

@@ -1,3 +1,4 @@
pub mod lines;
#[allow(unused)]
pub struct Quote;

View File

@@ -1 +1,2 @@
#[allow(unused)]
pub struct Tag;

View File

@@ -10,7 +10,7 @@ use crate::{
ISE_MSG,
database::{self},
users::{
auth::{AuthError, UserPasswordHashing},
auth::UserPasswordHashing,
handle::{UserHandle, UserHandleError},
},
};
@@ -122,6 +122,7 @@ impl User {
/// to do everything and probably should not be used as a regular account
/// due to the ramifications of compromise. But it could be used for that,
/// and have its name changed.
#[allow(unused)]
pub fn is_infradmin(&self) -> bool {
self.id == Uuid::max()
}
@@ -167,6 +168,7 @@ impl User {
/// for actions performed by Mnemosyne internally.
/// It shall not be available for log-in.
/// It should not have its name changed, and should be protected from that.
#[allow(unused)]
pub fn is_systemuser(&self) -> bool {
self.id == Uuid::nil()
}

View File

@@ -104,6 +104,7 @@ impl Session {
None => Err(SessionError::NoSessionWithToken(token.to_string())),
}
}
#[allow(unused)]
pub fn new_for_user(user: &User) -> Result<(Session, String), SessionError> {
let id = Uuid::now_v7();
let token = auth::generate_token(auth::TokenSize::Char64);
@@ -139,6 +140,7 @@ impl Session {
Ok(())
}
#[allow(unused)]
pub fn revoke(&mut self, actor: Option<&User>) -> Result<(), SessionError> {
let now = Utc::now();
let id = actor.map(|u| u.id).unwrap_or(Uuid::nil());
@@ -154,6 +156,7 @@ impl Session {
Ok(())
}
#[allow(unused)]
pub fn issued(&self) -> DateTime<Utc> {
// unwrapping here since we use UUIDv7
// and since we assume we're not in 10k CE