diff --git a/src/api/mod.rs b/src/api/mod.rs index 17a4a00..ab616dc 100644 --- a/src/api/mod.rs +++ b/src/api/mod.rs @@ -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); diff --git a/src/api/sessions.rs b/src/api/sessions.rs new file mode 100644 index 0000000..214525e --- /dev/null +++ b/src/api/sessions.rs @@ -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, + headers: HeaderMap, +) -> Result { + User::authenticate(&headers)?.required()?; + Ok(Json(Session::get_by_id(id)?).into_response()) +} diff --git a/src/api/users.rs b/src/api/users.rs index fed6e94..99f1003 100644 --- a/src/api/users.rs +++ b/src/api/users.rs @@ -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 { - Ok(Json(User::authenticate(&h)?.required()?).into_response()) +pub async fn get_me(headers: HeaderMap) -> Result { + Ok(Json(User::authenticate(&headers)?.required()?).into_response()) } -pub async fn get_by_id(Path(id): Path, h: HeaderMap) -> Result { - User::authenticate(&h)?.required()?; +pub async fn get_by_id( + Path(id): Path, + headers: HeaderMap, +) -> Result { + User::authenticate(&headers)?.required()?; Ok(Json(User::get_by_id(id)?).into_response()) } + +pub async fn get_by_handle( + Path(handle): Path, + headers: HeaderMap, +) -> Result { + User::authenticate(&headers)?.required()?; + Ok(Json(User::get_by_handle(handle)?).into_response()) +} diff --git a/src/main.rs b/src/main.rs index a83fa51..5f64a2f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -36,7 +36,6 @@ async fn main() -> Result<(), Box> { 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(()) diff --git a/src/persons/mod.rs b/src/persons/mod.rs index b6fd5d9..1912359 100644 --- a/src/persons/mod.rs +++ b/src/persons/mod.rs @@ -1,3 +1,4 @@ pub mod names; +#[allow(unused)] pub struct Person; diff --git a/src/persons/names.rs b/src/persons/names.rs index a050f41..744e7c8 100644 --- a/src/persons/names.rs +++ b/src/persons/names.rs @@ -1 +1,2 @@ +#[allow(unused)] pub struct Name; diff --git a/src/quotes/lines.rs b/src/quotes/lines.rs index 64cd091..1f67f82 100644 --- a/src/quotes/lines.rs +++ b/src/quotes/lines.rs @@ -1 +1,2 @@ +#[allow(unused)] pub struct QuoteLine; diff --git a/src/quotes/mod.rs b/src/quotes/mod.rs index b22580d..e306c0a 100644 --- a/src/quotes/mod.rs +++ b/src/quotes/mod.rs @@ -1,3 +1,4 @@ pub mod lines; +#[allow(unused)] pub struct Quote; diff --git a/src/tags.rs b/src/tags.rs index 7fe1c9e..e6e4201 100644 --- a/src/tags.rs +++ b/src/tags.rs @@ -1 +1,2 @@ +#[allow(unused)] pub struct Tag; diff --git a/src/users/mod.rs b/src/users/mod.rs index 36f85ef..8cd7592 100644 --- a/src/users/mod.rs +++ b/src/users/mod.rs @@ -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() } diff --git a/src/users/sessions.rs b/src/users/sessions.rs index cd3f8be..938bd1e 100644 --- a/src/users/sessions.rs +++ b/src/users/sessions.rs @@ -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 { // unwrapping here since we use UUIDv7 // and since we assume we're not in 10k CE