-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* skip manual config step * better ux * eslint * cleanup * update protobufs (#88) * feat: config polling (#86) * CI: fix re-creating manifests * chore: log version with git commit hash on startup (#89) * update protobufs (#90) * Rework instance config fetching (#91) * instance config fetching rework * update protobufs * add teonite link (#92) * add link * noreferrer * add defguard link * Basic nix flake without rust * Flake update * enable ARMv7 build (#93) Co-authored-by: Maciej Wójcik <[email protected]> * Make a pre-release and release docker build workflow (#94) * split builds * fix vergen * add flavor to build-docker workflow * bump version to 1.0.0 (#95) * OpenID via Proxy (#97) * Handle auth info * Use AuthInfoRequest * Handle AuthCallback * Use Url crate for URL option * add frontend * translations, id_token -> code * more translations, cleanup * cleanup * move to enterprise folder --------- Co-authored-by: Aleksander <[email protected]> * Change nonce and csrf cookie names (#99) * change cookies name * bump version * fix cargo lock --------- Co-authored-by: Robert Olejnik <[email protected]> Co-authored-by: Jacek Chmielewski <[email protected]> Co-authored-by: Adam Ciarciński <[email protected]> Co-authored-by: Maciek <[email protected]> Co-authored-by: Maciej Wójcik <[email protected]>
- Loading branch information
1 parent
44d968f
commit 00c6b41
Showing
32 changed files
with
1,773 additions
and
454 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub mod openid_login; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
use axum::{ | ||
extract::State, | ||
routing::{get, post}, | ||
Json, Router, | ||
}; | ||
use axum_extra::extract::{ | ||
cookie::{Cookie, SameSite}, | ||
PrivateCookieJar, | ||
}; | ||
use serde::{Deserialize, Serialize}; | ||
use time::Duration; | ||
|
||
use crate::{ | ||
error::ApiError, | ||
handlers::get_core_response, | ||
http::AppState, | ||
proto::{ | ||
core_request, core_response, AuthCallbackRequest, AuthCallbackResponse, AuthInfoRequest, | ||
}, | ||
}; | ||
|
||
const COOKIE_MAX_AGE: Duration = Duration::days(1); | ||
static CSRF_COOKIE_NAME: &str = "csrf_proxy"; | ||
static NONCE_COOKIE_NAME: &str = "nonce_proxy"; | ||
|
||
pub(crate) fn router() -> Router<AppState> { | ||
Router::new() | ||
.route("/auth_info", get(auth_info)) | ||
.route("/callback", post(auth_callback)) | ||
} | ||
|
||
#[derive(Serialize)] | ||
struct AuthInfo { | ||
url: String, | ||
button_display_name: Option<String>, | ||
} | ||
|
||
impl AuthInfo { | ||
#[must_use] | ||
fn new(url: String, button_display_name: Option<String>) -> Self { | ||
Self { | ||
url, | ||
button_display_name, | ||
} | ||
} | ||
} | ||
|
||
/// Request external OAuth2/OpenID provider details from Defguard Core. | ||
#[instrument(level = "debug", skip(state))] | ||
async fn auth_info( | ||
State(state): State<AppState>, | ||
private_cookies: PrivateCookieJar, | ||
) -> Result<(PrivateCookieJar, Json<AuthInfo>), ApiError> { | ||
debug!("Getting auth info for OAuth2/OpenID login"); | ||
|
||
let request = AuthInfoRequest { | ||
redirect_url: state.callback_url().to_string(), | ||
}; | ||
|
||
let rx = state | ||
.grpc_server | ||
.send(Some(core_request::Payload::AuthInfo(request)), None)?; | ||
let payload = get_core_response(rx).await?; | ||
if let core_response::Payload::AuthInfo(response) = payload { | ||
debug!("Received auth info {response:?}"); | ||
|
||
let nonce_cookie = Cookie::build((NONCE_COOKIE_NAME, response.nonce)) | ||
// .domain(cookie_domain) | ||
.path("/api/v1/openid/callback") | ||
.http_only(true) | ||
.same_site(SameSite::Strict) | ||
.secure(true) | ||
.max_age(COOKIE_MAX_AGE) | ||
.build(); | ||
let csrf_cookie = Cookie::build((CSRF_COOKIE_NAME, response.csrf_token)) | ||
// .domain(cookie_domain) | ||
.path("/api/v1/openid/callback") | ||
.http_only(true) | ||
.same_site(SameSite::Strict) | ||
.secure(true) | ||
.max_age(COOKIE_MAX_AGE) | ||
.build(); | ||
let private_cookies = private_cookies.add(nonce_cookie).add(csrf_cookie); | ||
|
||
let auth_info = AuthInfo::new(response.url, response.button_display_name); | ||
Ok((private_cookies, Json(auth_info))) | ||
} else { | ||
error!("Received invalid gRPC response type: {payload:#?}"); | ||
Err(ApiError::InvalidResponseType) | ||
} | ||
} | ||
|
||
#[derive(Debug, Deserialize)] | ||
pub struct AuthenticationResponse { | ||
code: String, | ||
state: String, | ||
} | ||
|
||
#[derive(Serialize)] | ||
struct CallbackResponseData { | ||
url: String, | ||
token: String, | ||
} | ||
|
||
#[instrument(level = "debug", skip(state))] | ||
async fn auth_callback( | ||
State(state): State<AppState>, | ||
mut private_cookies: PrivateCookieJar, | ||
Json(payload): Json<AuthenticationResponse>, | ||
) -> Result<(PrivateCookieJar, Json<CallbackResponseData>), ApiError> { | ||
let nonce = private_cookies | ||
.get(NONCE_COOKIE_NAME) | ||
.ok_or(ApiError::Unauthorized("Nonce cookie not found".into()))? | ||
.value_trimmed() | ||
.to_string(); | ||
let csrf = private_cookies | ||
.get(CSRF_COOKIE_NAME) | ||
.ok_or(ApiError::Unauthorized("CSRF cookie not found".into()))? | ||
.value_trimmed() | ||
.to_string(); | ||
|
||
if payload.state != csrf { | ||
return Err(ApiError::Unauthorized("CSRF token mismatch".into())); | ||
} | ||
|
||
private_cookies = private_cookies | ||
.remove(Cookie::from(NONCE_COOKIE_NAME)) | ||
.remove(Cookie::from(CSRF_COOKIE_NAME)); | ||
|
||
let request = AuthCallbackRequest { | ||
code: payload.code, | ||
nonce, | ||
callback_url: state.callback_url().to_string(), | ||
}; | ||
|
||
let rx = state | ||
.grpc_server | ||
.send(Some(core_request::Payload::AuthCallback(request)), None)?; | ||
let payload = get_core_response(rx).await?; | ||
if let core_response::Payload::AuthCallback(AuthCallbackResponse { url, token }) = payload { | ||
debug!("Received auth callback response {url:?} {token:?}"); | ||
Ok((private_cookies, Json(CallbackResponseData { url, token }))) | ||
} else { | ||
error!("Received invalid gRPC response type during handling the OpenID authentication callback: {payload:#?}"); | ||
Err(ApiError::InvalidResponseType) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub mod handlers; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.