Skip to content

Commit a889332

Browse files
author
Devdutt Shenoi
committed
fix: get auth details
1 parent 306b4be commit a889332

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed

src/audit.rs

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@ use std::{collections::HashMap, fmt::Debug, sync::Arc};
22

33
use crate::about::current;
44
use crate::handlers::http::modal::utils::rbac_utils::get_metadata;
5+
use crate::rbac::map::SessionKey;
6+
use crate::rbac::Users;
57

68
use super::option::CONFIG;
79
use actix_web::dev::ServiceRequest;
10+
use actix_web_httpauth::extractors::basic::BasicAuth;
811
use chrono::{DateTime, Utc};
912
use reqwest::Client;
1013
use serde::Serialize;
@@ -194,9 +197,10 @@ impl AuditLogBuilder {
194197
self.stream = stream;
195198
}
196199

197-
pub fn update_from_http(&mut self, req: &ServiceRequest) {
198-
let conn = req.connection_info();
200+
pub fn update_from_http(&mut self, req: &mut ServiceRequest) {
201+
let (username, authorization_method) = get_auth_details(req);
199202

203+
let conn = req.connection_info();
200204
self.request = RequestLog {
201205
method: req.method().to_string(),
202206
path: req.path().to_string(),
@@ -225,11 +229,33 @@ impl AuditLogBuilder {
225229
.and_then(|a| a.to_str().ok())
226230
.unwrap_or_default()
227231
.to_owned(),
228-
..Default::default()
232+
username,
233+
authorization_method,
229234
}
230235
}
231236
}
232237

238+
fn get_auth_details(req: &mut ServiceRequest) -> (String, String) {
239+
let mut username = "Unknown".to_owned();
240+
let mut auth_method = "None".to_owned();
241+
242+
if let Ok(creds) = req.extract::<BasicAuth>().into_inner() {
243+
return (creds.user_id().trim().to_owned(), "Basic Auth".to_owned());
244+
}
245+
246+
if let Some(cookie) = req.cookie("session") {
247+
auth_method = "Session Cookie".to_owned();
248+
if let Some(user_id) = Ulid::from_string(cookie.value())
249+
.ok()
250+
.and_then(|ulid| Users.get_username_from_session(&SessionKey::SessionId(ulid)))
251+
{
252+
username = user_id;
253+
}
254+
}
255+
256+
(username, auth_method)
257+
}
258+
233259
impl Drop for AuditLogBuilder {
234260
fn drop(&mut self) {
235261
let audit_json = json!({

src/handlers/http/middleware.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ where
177177
// Ensures that log will be pushed to subscriber on drop
178178
let mut log_builder = AuditLogBuilder::default();
179179
log_builder.set_stream_name(stream_name.unwrap_or_default());
180-
log_builder.update_from_http(&req);
180+
log_builder.update_from_http(&mut req);
181181
let fut = self.service.call(req);
182182
Box::pin(async move {
183183
log_builder.set_deployment_id().await;

0 commit comments

Comments
 (0)