Check basic auth if `--ui-auth` is configured. Returns true if OK.
(
state: &web::Data<Arc<RwLock<Master<M>>>>,
req: &HttpRequest,
)
| 336 | |
| 337 | /// Check basic auth if `--ui-auth` is configured. Returns true if OK. |
| 338 | async fn check_ui_auth<M: Model>( |
| 339 | state: &web::Data<Arc<RwLock<Master<M>>>>, |
| 340 | req: &HttpRequest, |
| 341 | ) -> bool { |
| 342 | let master = state.read().await; |
| 343 | let expected = match &master.ctx.args.ui_auth { |
| 344 | Some(cred) => cred.clone(), |
| 345 | None => return true, // no auth configured |
| 346 | }; |
| 347 | |
| 348 | let header = match req.headers().get("Authorization") { |
| 349 | Some(h) => h.to_str().unwrap_or(""), |
| 350 | None => return false, |
| 351 | }; |
| 352 | |
| 353 | if let Some(encoded) = header.strip_prefix("Basic ") { |
| 354 | if let Ok(decoded) = base64::Engine::decode( |
| 355 | &base64::engine::general_purpose::STANDARD, |
| 356 | encoded.trim(), |
| 357 | ) { |
| 358 | if let Ok(cred_str) = String::from_utf8(decoded) { |
| 359 | return cred_str == expected; |
| 360 | } |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | false |
| 365 | } |