|
| 1 | +use axum::{ |
| 2 | + extract::State, |
| 3 | + routing::{get, post}, |
| 4 | + Json, Router, |
| 5 | +}; |
| 6 | +use axum_extra::extract::{ |
| 7 | + cookie::{Cookie, SameSite}, |
| 8 | + PrivateCookieJar, |
| 9 | +}; |
| 10 | +use serde::{Deserialize, Serialize}; |
| 11 | +use time::Duration; |
| 12 | + |
| 13 | +use crate::{ |
| 14 | + error::ApiError, |
| 15 | + handlers::get_core_response, |
| 16 | + http::AppState, |
| 17 | + proto::{ |
| 18 | + core_request, core_response, AuthCallbackRequest, AuthCallbackResponse, AuthInfoRequest, |
| 19 | + }, |
| 20 | +}; |
| 21 | + |
| 22 | +const COOKIE_MAX_AGE: Duration = Duration::days(1); |
| 23 | +static CSRF_COOKIE_NAME: &str = "csrf_proxy"; |
| 24 | +static NONCE_COOKIE_NAME: &str = "nonce_proxy"; |
| 25 | + |
| 26 | +pub(crate) fn router() -> Router<AppState> { |
| 27 | + Router::new() |
| 28 | + .route("/auth_info", get(auth_info)) |
| 29 | + .route("/callback", post(auth_callback)) |
| 30 | +} |
| 31 | + |
| 32 | +#[derive(Serialize)] |
| 33 | +struct AuthInfo { |
| 34 | + url: String, |
| 35 | + button_display_name: Option<String>, |
| 36 | +} |
| 37 | + |
| 38 | +impl AuthInfo { |
| 39 | + #[must_use] |
| 40 | + fn new(url: String, button_display_name: Option<String>) -> Self { |
| 41 | + Self { |
| 42 | + url, |
| 43 | + button_display_name, |
| 44 | + } |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +/// Request external OAuth2/OpenID provider details from Defguard Core. |
| 49 | +#[instrument(level = "debug", skip(state))] |
| 50 | +async fn auth_info( |
| 51 | + State(state): State<AppState>, |
| 52 | + private_cookies: PrivateCookieJar, |
| 53 | +) -> Result<(PrivateCookieJar, Json<AuthInfo>), ApiError> { |
| 54 | + debug!("Getting auth info for OAuth2/OpenID login"); |
| 55 | + |
| 56 | + let request = AuthInfoRequest { |
| 57 | + redirect_url: state.callback_url().to_string(), |
| 58 | + }; |
| 59 | + |
| 60 | + let rx = state |
| 61 | + .grpc_server |
| 62 | + .send(Some(core_request::Payload::AuthInfo(request)), None)?; |
| 63 | + let payload = get_core_response(rx).await?; |
| 64 | + if let core_response::Payload::AuthInfo(response) = payload { |
| 65 | + debug!("Received auth info {response:?}"); |
| 66 | + |
| 67 | + let nonce_cookie = Cookie::build((NONCE_COOKIE_NAME, response.nonce)) |
| 68 | + // .domain(cookie_domain) |
| 69 | + .path("/api/v1/openid/callback") |
| 70 | + .http_only(true) |
| 71 | + .same_site(SameSite::Strict) |
| 72 | + .secure(true) |
| 73 | + .max_age(COOKIE_MAX_AGE) |
| 74 | + .build(); |
| 75 | + let csrf_cookie = Cookie::build((CSRF_COOKIE_NAME, response.csrf_token)) |
| 76 | + // .domain(cookie_domain) |
| 77 | + .path("/api/v1/openid/callback") |
| 78 | + .http_only(true) |
| 79 | + .same_site(SameSite::Strict) |
| 80 | + .secure(true) |
| 81 | + .max_age(COOKIE_MAX_AGE) |
| 82 | + .build(); |
| 83 | + let private_cookies = private_cookies.add(nonce_cookie).add(csrf_cookie); |
| 84 | + |
| 85 | + let auth_info = AuthInfo::new(response.url, response.button_display_name); |
| 86 | + Ok((private_cookies, Json(auth_info))) |
| 87 | + } else { |
| 88 | + error!("Received invalid gRPC response type: {payload:#?}"); |
| 89 | + Err(ApiError::InvalidResponseType) |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +#[derive(Debug, Deserialize)] |
| 94 | +pub struct AuthenticationResponse { |
| 95 | + code: String, |
| 96 | + state: String, |
| 97 | +} |
| 98 | + |
| 99 | +#[derive(Serialize)] |
| 100 | +struct CallbackResponseData { |
| 101 | + url: String, |
| 102 | + token: String, |
| 103 | +} |
| 104 | + |
| 105 | +#[instrument(level = "debug", skip(state))] |
| 106 | +async fn auth_callback( |
| 107 | + State(state): State<AppState>, |
| 108 | + mut private_cookies: PrivateCookieJar, |
| 109 | + Json(payload): Json<AuthenticationResponse>, |
| 110 | +) -> Result<(PrivateCookieJar, Json<CallbackResponseData>), ApiError> { |
| 111 | + let nonce = private_cookies |
| 112 | + .get(NONCE_COOKIE_NAME) |
| 113 | + .ok_or(ApiError::Unauthorized("Nonce cookie not found".into()))? |
| 114 | + .value_trimmed() |
| 115 | + .to_string(); |
| 116 | + let csrf = private_cookies |
| 117 | + .get(CSRF_COOKIE_NAME) |
| 118 | + .ok_or(ApiError::Unauthorized("CSRF cookie not found".into()))? |
| 119 | + .value_trimmed() |
| 120 | + .to_string(); |
| 121 | + |
| 122 | + if payload.state != csrf { |
| 123 | + return Err(ApiError::Unauthorized("CSRF token mismatch".into())); |
| 124 | + } |
| 125 | + |
| 126 | + private_cookies = private_cookies |
| 127 | + .remove(Cookie::from(NONCE_COOKIE_NAME)) |
| 128 | + .remove(Cookie::from(CSRF_COOKIE_NAME)); |
| 129 | + |
| 130 | + let request = AuthCallbackRequest { |
| 131 | + code: payload.code, |
| 132 | + nonce, |
| 133 | + callback_url: state.callback_url().to_string(), |
| 134 | + }; |
| 135 | + |
| 136 | + let rx = state |
| 137 | + .grpc_server |
| 138 | + .send(Some(core_request::Payload::AuthCallback(request)), None)?; |
| 139 | + let payload = get_core_response(rx).await?; |
| 140 | + if let core_response::Payload::AuthCallback(AuthCallbackResponse { url, token }) = payload { |
| 141 | + debug!("Received auth callback response {url:?} {token:?}"); |
| 142 | + Ok((private_cookies, Json(CallbackResponseData { url, token }))) |
| 143 | + } else { |
| 144 | + error!("Received invalid gRPC response type during handling the OpenID authentication callback: {payload:#?}"); |
| 145 | + Err(ApiError::InvalidResponseType) |
| 146 | + } |
| 147 | +} |
0 commit comments