Skip to content

Commit b427e12

Browse files
authored
Handle not found error (#175)
1 parent c5b3360 commit b427e12

File tree

7 files changed

+79
-92
lines changed

7 files changed

+79
-92
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@
55
.envrc
66
/node_modules
77
.env
8+
.DS_Store

Cargo.lock

Lines changed: 36 additions & 38 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/enterprise/handlers/desktop_client_mfa.rs

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,23 +21,24 @@ pub(super) async fn mfa_auth_callback(
2121
) -> Result<PrivateCookieJar, ApiError> {
2222
info!("Processing MFA authentication callback");
2323
debug!(
24-
"Received payload: state={}, flow_type={}",
24+
"Received payload: state={}, flow_type={:?}",
2525
payload.state, payload.flow_type
2626
);
2727

28-
let flow_type = payload.flow_type.parse::<FlowType>().map_err(|err| {
29-
warn!("Failed to parse flow type '{}': {err:?}", payload.flow_type);
30-
ApiError::BadRequest("Invalid flow type".into())
31-
})?;
32-
33-
if flow_type != FlowType::Mfa {
34-
warn!("Invalid flow type for MFA callback: {flow_type:?}");
35-
return Err(ApiError::BadRequest(
36-
"Invalid flow type for MFA callback".into(),
37-
));
28+
match payload.flow_type {
29+
FlowType::Mfa => (),
30+
FlowType::Enrollment => {
31+
warn!(
32+
"Invalid flow type for MFA callback: {:?}",
33+
payload.flow_type
34+
);
35+
return Err(ApiError::BadRequest(
36+
"Invalid flow type for MFA callback".into(),
37+
));
38+
}
3839
}
3940

40-
debug!("Flow type validation passed: {flow_type:?}");
41+
debug!("Flow type validation passed: {:?}", payload.flow_type);
4142

4243
let nonce = private_cookies
4344
.get(NONCE_COOKIE_NAME)
@@ -78,7 +79,7 @@ pub(super) async fn mfa_auth_callback(
7879
let request = ClientMfaOidcAuthenticateRequest {
7980
code: payload.code,
8081
nonce,
81-
callback_url: state.callback_url(&flow_type).to_string(),
82+
callback_url: state.callback_url(&payload.flow_type).to_string(),
8283
state: payload.state,
8384
};
8485

src/enterprise/handlers/openid_login.rs

Lines changed: 14 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -44,29 +44,18 @@ impl AuthInfo {
4444
}
4545
}
4646

47-
#[derive(Deserialize, Debug, PartialEq, Eq)]
47+
#[derive(Deserialize, Debug)]
48+
#[serde(rename_all = "lowercase")]
4849
pub(crate) enum FlowType {
4950
Enrollment,
5051
Mfa,
5152
}
5253

53-
impl std::str::FromStr for FlowType {
54-
type Err = ();
55-
56-
fn from_str(s: &str) -> Result<Self, Self::Err> {
57-
match s.to_lowercase().as_str() {
58-
"enrollment" => Ok(FlowType::Enrollment),
59-
"mfa" => Ok(FlowType::Mfa),
60-
_ => Err(()),
61-
}
62-
}
63-
}
64-
6554
#[derive(Deserialize, Debug)]
66-
struct RequestData {
55+
pub(crate) struct RequestData {
6756
state: Option<String>,
6857
#[serde(rename = "type")]
69-
flow_type: String,
58+
flow_type: FlowType,
7059
}
7160

7261
/// Request external OAuth2/OpenID provider details from Defguard Core.
@@ -79,13 +68,8 @@ async fn auth_info(
7968
) -> Result<(PrivateCookieJar, Json<AuthInfo>), ApiError> {
8069
debug!("Getting auth info for OAuth2/OpenID login");
8170

82-
let flow_type = request_data
83-
.flow_type
84-
.parse::<FlowType>()
85-
.map_err(|()| ApiError::BadRequest("Invalid flow type".into()))?;
86-
8771
let request = AuthInfoRequest {
88-
redirect_url: state.callback_url(&flow_type).to_string(),
72+
redirect_url: state.callback_url(&request_data.flow_type).to_string(),
8973
state: request_data.state,
9074
};
9175

@@ -127,7 +111,7 @@ pub(super) struct AuthenticationResponse {
127111
pub(super) code: String,
128112
pub(super) state: String,
129113
#[serde(rename = "type")]
130-
pub(super) flow_type: String,
114+
pub(super) flow_type: FlowType,
131115
}
132116

133117
#[derive(Serialize)]
@@ -143,15 +127,13 @@ async fn auth_callback(
143127
mut private_cookies: PrivateCookieJar,
144128
Json(payload): Json<AuthenticationResponse>,
145129
) -> Result<(PrivateCookieJar, Json<CallbackResponseData>), ApiError> {
146-
let flow_type = payload
147-
.flow_type
148-
.parse::<FlowType>()
149-
.map_err(|()| ApiError::BadRequest("Invalid flow type".into()))?;
150-
151-
if flow_type != FlowType::Enrollment {
152-
return Err(ApiError::BadRequest(
153-
"Invalid flow type for OpenID enrollment callback".into(),
154-
));
130+
match payload.flow_type {
131+
FlowType::Enrollment => (),
132+
FlowType::Mfa => {
133+
return Err(ApiError::BadRequest(
134+
"Invalid flow type for OpenID enrollment callback".into(),
135+
));
136+
}
155137
}
156138

157139
let nonce = private_cookies
@@ -176,7 +158,7 @@ async fn auth_callback(
176158
let request = AuthCallbackRequest {
177159
code: payload.code,
178160
nonce,
179-
callback_url: state.callback_url(&flow_type).to_string(),
161+
callback_url: state.callback_url(&payload.flow_type).to_string(),
180162
};
181163

182164
let rx = state

0 commit comments

Comments
 (0)