added session check + updates check fixed

This commit is contained in:
Shiroyasha 2024-07-23 23:35:09 +03:00
parent 63f0b0d732
commit 584983810b
Signed by: shiroyashik
GPG key ID: E4953D3940D7860A
4 changed files with 36 additions and 14 deletions

View file

@ -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(),
}
}