mirror of
https://github.com/danbulant/Portfolio
synced 2026-06-17 21:41:20 +00:00
feat: session auth logging
This commit is contained in:
parent
a499dc5cba
commit
593d04a106
3 changed files with 31 additions and 12 deletions
|
|
@ -1,10 +1,12 @@
|
|||
use entity::admin::Model as Admin;
|
||||
use log::info;
|
||||
use portfolio_core::sea_orm::prelude::Uuid;
|
||||
use portfolio_core::services::admin_service::AdminService;
|
||||
use rocket::http::Status;
|
||||
use rocket::outcome::Outcome;
|
||||
use rocket::request::{FromRequest, Request};
|
||||
|
||||
use crate::logging::format_request;
|
||||
use crate::pool::Db;
|
||||
|
||||
pub struct AdminAuth(Admin, String);
|
||||
|
|
@ -49,10 +51,14 @@ impl<'r> FromRequest<'r> for AdminAuth {
|
|||
let session = AdminService::auth(conn, uuid).await;
|
||||
|
||||
match session {
|
||||
Ok(model) => Outcome::Success(AdminAuth(model, private_key.to_string())),
|
||||
Err(e) => Outcome::Failure(
|
||||
(Status::from_code(e.code()).unwrap_or(Status::Unauthorized), None)
|
||||
),
|
||||
Ok(model) => {
|
||||
warn!("{}: ADMIN {} AUTHENTICATED", format_request(req), model.id);
|
||||
Outcome::Success(AdminAuth(model, private_key.to_string()))
|
||||
},
|
||||
Err(e) => {
|
||||
info!("{}: ADMIN AUTHENTICATION FAILED: {}", format_request(req), e);
|
||||
Outcome::Failure((Status::Unauthorized, None))
|
||||
},
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@ use rocket::http::Status;
|
|||
use rocket::outcome::Outcome;
|
||||
use rocket::request::{FromRequest, Request};
|
||||
|
||||
use crate::logging::format_request;
|
||||
use crate::pool::Db;
|
||||
|
||||
pub struct CandidateAuth(Candidate, String);
|
||||
|
|
@ -51,8 +52,14 @@ impl<'r> FromRequest<'r> for CandidateAuth {
|
|||
let session = CandidateService::auth(conn, uuid).await;
|
||||
|
||||
match session {
|
||||
Ok(model) => Outcome::Success(CandidateAuth(model, private_key.to_string().to_string())),
|
||||
Err(_) => Outcome::Failure((Status::Unauthorized, None)),
|
||||
Ok(model) => {
|
||||
info!("{}: CANDIDATE {} AUTHENTICATED", format_request(req), model.application);
|
||||
Outcome::Success(CandidateAuth(model, private_key.to_string().to_string()))
|
||||
},
|
||||
Err(e) => {
|
||||
info!("{}: CANDIDATE {} AUTHENTICATION FAILED", format_request(req), e);
|
||||
Outcome::Failure((Status::Unauthorized, None))
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
use std::net::Ipv4Addr;
|
||||
|
||||
use log::info;
|
||||
use rocket::{fairing::{Fairing, Info, Kind}, Request, Data};
|
||||
|
||||
pub struct Logging;
|
||||
|
|
@ -13,8 +14,15 @@ impl Fairing for Logging {
|
|||
}
|
||||
}
|
||||
|
||||
async fn on_request(&self, request: &mut Request<'_>, d: &mut Data<'_>) {
|
||||
let client_ip = request.client_ip().unwrap_or(std::net::IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1))).to_string();
|
||||
async fn on_request(&self, request: &mut Request<'_>, _: &mut Data<'_>) {
|
||||
let s = format_request(request);
|
||||
info!("> {}", s);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
pub fn format_request(request: &Request<'_>) -> String {
|
||||
let client_ip = request.client_ip().unwrap_or(std::net::IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1))).to_string();
|
||||
|
||||
let method = request.method().to_string();
|
||||
|
||||
|
|
@ -23,14 +31,12 @@ impl Fairing for Logging {
|
|||
let user_agent = request.headers().get_one("User-Agent").unwrap_or("").to_string();
|
||||
let content_length = request.headers().get_one("Content-Length").unwrap_or("").to_string();
|
||||
|
||||
info!("[{}] {} {} (User-Agent: {}, Content-Length: {}, Host: {})",
|
||||
format!("[{}] {} {} (User-Agent: {}, Content-Length: {}, Host: {})",
|
||||
client_ip,
|
||||
method,
|
||||
uri,
|
||||
user_agent,
|
||||
content_length,
|
||||
host,
|
||||
);
|
||||
|
||||
}
|
||||
)
|
||||
}
|
||||
Loading…
Reference in a new issue