@@ -2,9 +2,12 @@ use std::{collections::HashMap, fmt::Debug, sync::Arc};
2
2
3
3
use crate :: about:: current;
4
4
use crate :: handlers:: http:: modal:: utils:: rbac_utils:: get_metadata;
5
+ use crate :: rbac:: map:: SessionKey ;
6
+ use crate :: rbac:: Users ;
5
7
6
8
use super :: option:: CONFIG ;
7
9
use actix_web:: dev:: ServiceRequest ;
10
+ use actix_web_httpauth:: extractors:: basic:: BasicAuth ;
8
11
use chrono:: { DateTime , Utc } ;
9
12
use reqwest:: Client ;
10
13
use serde:: Serialize ;
@@ -194,9 +197,10 @@ impl AuditLogBuilder {
194
197
self . stream = stream;
195
198
}
196
199
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 ) ;
199
202
203
+ let conn = req. connection_info ( ) ;
200
204
self . request = RequestLog {
201
205
method : req. method ( ) . to_string ( ) ,
202
206
path : req. path ( ) . to_string ( ) ,
@@ -225,11 +229,33 @@ impl AuditLogBuilder {
225
229
. and_then ( |a| a. to_str ( ) . ok ( ) )
226
230
. unwrap_or_default ( )
227
231
. to_owned ( ) ,
228
- ..Default :: default ( )
232
+ username,
233
+ authorization_method,
229
234
}
230
235
}
231
236
}
232
237
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
+
233
259
impl Drop for AuditLogBuilder {
234
260
fn drop ( & mut self ) {
235
261
let audit_json = json ! ( {
0 commit comments