🧹 Remove unused legacy modules and functions
- Deleted quota.rs module (quota management not needed via HTTP) - Removed legacy get_id_by_token GraphQL function - Removed unused set_user_quota and increase_user_quota methods - Cleaned up unused imports and legacy structs - Simplified handlers/mod.rs to only expose universal_handler Architecture now focused on core functionality: - GET / (user info) - GET /<filename> (file serving) - POST / (file upload)
This commit is contained in:
78
src/auth.rs
78
src/auth.rs
@@ -2,33 +2,8 @@ use actix_web::error::ErrorInternalServerError;
|
||||
use jsonwebtoken::{Algorithm, DecodingKey, Validation, decode};
|
||||
use log::{info, warn};
|
||||
use redis::{AsyncCommands, aio::MultiplexedConnection};
|
||||
use reqwest::Client as HTTPClient;
|
||||
use reqwest::header::{CONTENT_TYPE, HeaderMap, HeaderValue};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::json;
|
||||
use std::{collections::HashMap, env, error::Error};
|
||||
|
||||
// Старые структуры для совместимости с get_id_by_token
|
||||
#[derive(Deserialize)]
|
||||
struct AuthResponse {
|
||||
data: Option<AuthData>,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct AuthData {
|
||||
validate_jwt_token: Option<ValidateJWTToken>,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct ValidateJWTToken {
|
||||
is_valid: bool,
|
||||
claims: Option<Claims>,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct Claims {
|
||||
sub: Option<String>,
|
||||
}
|
||||
use std::error::Error;
|
||||
|
||||
// Структуры для JWT токенов
|
||||
#[derive(Debug, Deserialize)]
|
||||
@@ -51,57 +26,6 @@ pub struct Author {
|
||||
pub device_info: Option<String>,
|
||||
}
|
||||
|
||||
/// Получает айди пользователя из токена в заголовке
|
||||
#[allow(clippy::collapsible_if)]
|
||||
pub async fn get_id_by_token(token: &str) -> Result<String, Box<dyn Error>> {
|
||||
let auth_api_base = env::var("CORE_URL")?;
|
||||
let query_name = "validate_jwt_token";
|
||||
let operation = "ValidateToken";
|
||||
let mut headers = HeaderMap::new();
|
||||
headers.insert(CONTENT_TYPE, HeaderValue::from_static("application/json"));
|
||||
|
||||
let mut variables = HashMap::<String, HashMap<String, String>>::new();
|
||||
let mut params = HashMap::<String, String>::new();
|
||||
params.insert("token".to_string(), token.to_string());
|
||||
params.insert("token_type".to_string(), "access_token".to_string());
|
||||
variables.insert("params".to_string(), params);
|
||||
|
||||
let gql = json!({
|
||||
"query": format!("query {}($params: ValidateJWTTokenInput!) {{ {}(params: $params) {{ is_valid claims }} }}", operation, query_name),
|
||||
"operationName": operation,
|
||||
"variables": variables
|
||||
});
|
||||
|
||||
let client = HTTPClient::new();
|
||||
let response = client
|
||||
.post(&auth_api_base)
|
||||
.headers(headers)
|
||||
.json(&gql)
|
||||
.send()
|
||||
.await?;
|
||||
|
||||
if response.status().is_success() {
|
||||
let auth_response: AuthResponse = response.json().await?;
|
||||
if let Some(auth_data) = auth_response.data {
|
||||
if let Some(validate_jwt_token) = auth_data.validate_jwt_token {
|
||||
if validate_jwt_token.is_valid {
|
||||
if let Some(claims) = validate_jwt_token.claims {
|
||||
if let Some(sub) = claims.sub {
|
||||
return Ok(sub);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(Box::new(std::io::Error::other("Invalid token response")))
|
||||
} else {
|
||||
Err(Box::new(std::io::Error::other(format!(
|
||||
"Request failed with status: {}",
|
||||
response.status()
|
||||
))))
|
||||
}
|
||||
}
|
||||
|
||||
/// Декодирует JWT токен и извлекает claims с проверкой истечения
|
||||
fn decode_jwt_token(token: &str) -> Result<TokenClaims, Box<dyn Error>> {
|
||||
// В реальном приложении здесь должен быть настоящий секретный ключ
|
||||
|
||||
Reference in New Issue
Block a user