Skip to content

Commit 67ce0ce

Browse files
committed
Address comments
1 parent 8c59ebe commit 67ce0ce

File tree

2 files changed

+24
-54
lines changed

2 files changed

+24
-54
lines changed

crates/handlers/src/oauth2/authorization/mod.rs

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -106,25 +106,18 @@ fn resolve_response_mode(
106106
) -> Result<ResponseMode, RouteError> {
107107
use ResponseMode as M;
108108

109-
if response_type.has_token() || response_type.has_id_token() {
110109
// If the response type includes either "token" or "id_token", the default
111110
// response mode is "fragment" and the response mode "query" must not be
112111
// used
113-
if let Some(suggested_response_mode) = suggested_response_mode {
112+
if response_type.has_token() || response_type.has_id_token() {
114113
match suggested_response_mode {
115-
M::Query => Err(RouteError::InvalidResponseMode),
116-
mode => Ok(mode),
117-
}
118-
} else {
119-
Ok(M::Fragment)
114+
None => Ok(M::Fragment),
115+
Some(M::Query) => Err(RouteError::InvalidResponseMode),
116+
Some(mode) => Ok(mode),
120117
}
121118
} else {
122119
// In other cases, all response modes are allowed, defaulting to "query"
123-
if let Some(suggested_response_mode) = suggested_response_mode {
124-
Ok(suggested_response_mode)
125-
} else {
126-
Ok(M::Query)
127-
}
120+
Ok(suggested_response_mode.unwrap_or(M::Query))
128121
}
129122
}
130123

crates/handlers/src/upstream_oauth2/callback.rs

Lines changed: 19 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,6 @@ pub(crate) enum RouteError {
113113
expected: UpstreamOAuthProviderResponseMode,
114114
},
115115

116-
#[error("Invalid request method '{method}'")]
117-
InvalidReqMethod { method: Method },
118-
119116
#[error(transparent)]
120117
Internal(Box<dyn std::error::Error + Send + Sync + 'static>),
121118
}
@@ -187,46 +184,26 @@ pub(crate) async fn handler(
187184
// The `Form` extractor will use the body of the request for POST requests and
188185
// the query parameters for GET requests. We need to then look at the method do
189186
// make sure it matches the expected `response_mode`
190-
match method {
191-
Method::GET => {
192-
match provider.response_mode {
193-
Some(UpstreamOAuthProviderResponseMode::Query) | None => {}
194-
Some(UpstreamOAuthProviderResponseMode::FormPost) => {
195-
return Err(RouteError::InvalidResponseMode {
196-
expected: UpstreamOAuthProviderResponseMode::Query,
197-
})
198-
}
199-
};
200-
}
201-
Method::POST => {
202-
match provider.response_mode {
203-
Some(UpstreamOAuthProviderResponseMode::FormPost) => {
204-
// We set the cookies with a `Same-Site` policy set to `Lax`, so because this is
205-
// usually a cross-site form POST, we need to render a form with the
206-
// same values, which posts back to the same URL. However, there are
207-
// other valid reasons for the cookie to be missing, so to track whether we did
208-
// this POST ourselves, we set a flag.
209-
if sessions_cookie.is_empty() && !params.did_mas_repost_to_itself {
210-
let params = Params {
211-
did_mas_repost_to_itself: true,
212-
..params
213-
};
214-
let context =
215-
FormPostContext::new_for_current_url(params).with_language(&locale);
216-
let html = templates.render_form_post(&context)?;
217-
return Ok(Html(html).into_response());
218-
}
219-
}
220-
Some(UpstreamOAuthProviderResponseMode::Query) | None => {
221-
return Err(RouteError::InvalidResponseMode {
222-
expected: UpstreamOAuthProviderResponseMode::FormPost,
223-
})
224-
}
225-
};
226-
}
227-
method => {
228-
return Err(RouteError::InvalidReqMethod { method });
187+
match (provider.response_mode, method) {
188+
(Some(UpstreamOAuthProviderResponseMode::Query) | None, Method::GET) => {}
189+
(Some(UpstreamOAuthProviderResponseMode::FormPost) | None, Method::POST) => {
190+
// We set the cookies with a `Same-Site` policy set to `Lax`, so because this is
191+
// usually a cross-site form POST, we need to render a form with the
192+
// same values, which posts back to the same URL. However, there are
193+
// other valid reasons for the cookie to be missing, so to track whether we did
194+
// this POST ourselves, we set a flag.
195+
if sessions_cookie.is_empty() && !params.did_mas_repost_to_itself {
196+
let params = Params {
197+
did_mas_repost_to_itself: true,
198+
..params
199+
};
200+
let context = FormPostContext::new_for_current_url(params).with_language(&locale);
201+
let html = templates.render_form_post(&context)?;
202+
return Ok(Html(html).into_response());
203+
}
229204
}
205+
(None, _) => {}
206+
(Some(expected), _) => return Err(RouteError::InvalidResponseMode { expected }),
230207
}
231208

232209
let (session_id, _post_auth_action) = sessions_cookie

0 commit comments

Comments
 (0)