(
req: hyper::Request<hyper::body::Incoming>,
state: Arc<CallbackState>,
)
| 377 | } |
| 378 | |
| 379 | fn handle_oidc_callback( |
| 380 | req: hyper::Request<hyper::body::Incoming>, |
| 381 | state: Arc<CallbackState>, |
| 382 | ) -> Response<Full<Bytes>> { |
| 383 | if req.method() != Method::GET || !req.uri().path().starts_with("/callback") { |
| 384 | return Response::builder() |
| 385 | .status(StatusCode::NOT_FOUND) |
| 386 | .body(Full::new(Bytes::from("not found"))) |
| 387 | .expect("response"); |
| 388 | } |
| 389 | |
| 390 | let query = req.uri().query().unwrap_or(""); |
| 391 | let params: std::collections::HashMap<String, String> = query |
| 392 | .split('&') |
| 393 | .filter_map(|pair| { |
| 394 | let mut parts = pair.splitn(2, '='); |
| 395 | let key = percent_decode(parts.next()?); |
| 396 | let value = percent_decode(parts.next().unwrap_or("")); |
| 397 | Some((key, value)) |
| 398 | }) |
| 399 | .collect(); |
| 400 | |
| 401 | // Check for error response from the IdP. |
| 402 | if let Some(error) = params.get("error") { |
| 403 | let desc = params.get("error_description").map_or("", String::as_str); |
| 404 | debug!(error = %error, description = %desc, "OIDC auth error"); |
| 405 | let _ = state.take_sender(); |
| 406 | return html_response( |
| 407 | StatusCode::BAD_REQUEST, |
| 408 | &format!("Authentication failed: {error}. {desc}"), |
| 409 | ); |
| 410 | } |
| 411 | |
| 412 | let code = match params.get("code") { |
| 413 | Some(c) if !c.is_empty() => c, |
| 414 | _ => { |
| 415 | let _ = state.take_sender(); |
| 416 | return html_response(StatusCode::BAD_REQUEST, "Missing authorization code."); |
| 417 | } |
| 418 | }; |
| 419 | |
| 420 | let received_state = params.get("state").map_or("", String::as_str); |
| 421 | if received_state != state.expected_state { |
| 422 | debug!("OIDC state mismatch"); |
| 423 | let _ = state.take_sender(); |
| 424 | return html_response(StatusCode::FORBIDDEN, "State parameter mismatch."); |
| 425 | } |
| 426 | |
| 427 | if let Some(sender) = state.take_sender() { |
| 428 | let _ = sender.send(code.clone()); |
| 429 | } |
| 430 | |
| 431 | html_response( |
| 432 | StatusCode::OK, |
| 433 | "Authentication successful! You can close this tab and return to the terminal.", |
| 434 | ) |
| 435 | } |
| 436 |
no test coverage detected