diff --git a/Config.example.toml b/Config.example.toml index 9fe93b3..9124de6 100644 --- a/Config.example.toml +++ b/Config.example.toml @@ -48,11 +48,13 @@ motd = """ maxAvatarSize = 100000 # 100 KB maxAvatars = 10 -[advancedUsers.66004548-4de5-49de-bade-9c3933d8eb97] -username = "Shiroyashik" -authSystem = "elyby" -special = [0,0,0,1,0,0] # 6 -pride = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0] # 25 +[advancedUsers] + +# [advancedUsers.66004548-4de5-49de-bade-9c3933d8eb97] +# username = "Shiroyashik" +# authSystem = "elyby" +# special = [0,0,0,1,0,0] # 6 +# pride = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0] # 25 ## With advancedUsers you can set additional parameters # [advancedUsers.your uuid here] diff --git a/src/auth/auth.rs b/src/auth/auth.rs index a6ee9ec..bce67f2 100644 --- a/src/auth/auth.rs +++ b/src/auth/auth.rs @@ -2,12 +2,14 @@ use std::sync::Arc; use anyhow::anyhow; use axum::{ - async_trait, extract::FromRequestParts, http::{request::Parts, StatusCode} + async_trait, extract::{FromRequestParts, State}, http::{request::Parts, StatusCode}, response::{IntoResponse, Response} }; use dashmap::DashMap; use tracing::{debug, trace}; use uuid::Uuid; +use crate::AppState; + use super::types::*; // It's an extractor that pulls a token from the Header. @@ -137,7 +139,7 @@ impl UManager { ) -> Option> { self.registered.get(uuid) } - pub fn _is_authenticated(&self, token: &String) -> bool { + pub fn is_authenticated(&self, token: &String) -> bool { self.authenticated.contains_key(token) } pub fn _is_registered(&self, uuid: &Uuid) -> bool { @@ -148,4 +150,21 @@ impl UManager { self.authenticated.remove(&token); } } -// End of User manager \ No newline at end of file +// End of User manager + +pub async fn check_auth( + Token(token): Token, + State(state): State, +) -> Response { + + match token { + Some(token) => { + if state.user_manager.is_authenticated(&token) { + (StatusCode::OK, "ok".to_string()).into_response() + } else { + (StatusCode::UNAUTHORIZED, "unauthorized".to_string()).into_response() + } + }, + None => (StatusCode::BAD_REQUEST, "bad request".to_string()).into_response(), + } +} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 19a7632..43309c9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -34,7 +34,7 @@ use api::{ // Auth mod auth; -use auth::UManager; +use auth::{UManager, check_auth}; // Config mod state; @@ -112,6 +112,7 @@ async fn main() -> Result<()> { let api = Router::new() .nest("//auth", api_auth::router()) .nest("/v1", api::v1::router()) + .route("/", get(check_auth)) .route("/limits", get(api_info::limits)) .route("/version", get(api_info::version)) .route("/motd", get(api_info::motd)) diff --git a/src/utils/check_updates.rs b/src/utils/check_updates.rs index 39da7e7..a5c731b 100644 --- a/src/utils/check_updates.rs +++ b/src/utils/check_updates.rs @@ -12,7 +12,7 @@ struct Tag { async fn get_latest_version(repo: &str, current_version: Version) -> anyhow::Result> { let url = format!("https://api.github.com/repos/{repo}/tags"); let client = Client::new(); - let response = client.get(&url).send().await?; + let response = client.get(&url).header("User-Agent", "reqwest").send().await?; if response.status().is_success() { let tags: Vec = response.json().await?; @@ -27,9 +27,9 @@ async fn get_latest_version(repo: &str, current_version: Version) -> anyhow::Res .max(); if let Some(latest_version) = latest_tag { if latest_version > current_version { - Ok(Some(latest_version.to_string())) + Ok(Some(format!("Available new v{latest_version}"))) } else { - Ok(None) + Ok(Some("Up to date".to_string())) } } else { Err(anyhow!("Can't find version tags!")) @@ -43,8 +43,8 @@ pub async fn check_updates(repo: &str, current_version: &str) -> anyhow::Result< let current_version = semver::Version::parse(¤t_version)?; match get_latest_version(repo, current_version).await { - Ok(d) => if let Some(latest_version) = d { - Ok(format!(" - Available new v{latest_version}!")) + Ok(d) => if let Some(text) = d { + Ok(format!(" - {text}!")) } else { Ok(String::new()) },