mirror of
https://github.com/shiroyashik/sculptor.git
synced 2025-12-06 13:01:12 +03:00
added session check + updates check fixed
This commit is contained in:
parent
63f0b0d732
commit
584983810b
4 changed files with 36 additions and 14 deletions
|
|
@ -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<dashmap::mapref::one::Ref<'_, Uuid, Userinfo>> {
|
||||
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
|
||||
// End of User manager
|
||||
|
||||
pub async fn check_auth(
|
||||
Token(token): Token,
|
||||
State(state): State<AppState>,
|
||||
) -> 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(),
|
||||
}
|
||||
}
|
||||
|
|
@ -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))
|
||||
|
|
|
|||
|
|
@ -12,7 +12,7 @@ struct Tag {
|
|||
async fn get_latest_version(repo: &str, current_version: Version) -> anyhow::Result<Option<String>> {
|
||||
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<Tag> = 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())
|
||||
},
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue