@@ -109,10 +109,13 @@ pub(crate) enum RouteError {
109109 MissingFormParams ,
110110
111111 #[ error( "Invalid response mode, expected '{expected}'" ) ]
112- InvalidParamsMode {
112+ InvalidResponseMode {
113113 expected : UpstreamOAuthProviderResponseMode ,
114114 } ,
115115
116+ #[ error( "Invalid request method '{method}'" ) ]
117+ InvalidReqMethod { method : Method } ,
118+
116119 #[ error( transparent) ]
117120 Internal ( Box < dyn std:: error:: Error + Send + Sync + ' static > ) ,
118121}
@@ -184,25 +187,46 @@ pub(crate) async fn handler(
184187 // The `Form` extractor will use the body of the request for POST requests and
185188 // the query parameters for GET requests. We need to then look at the method do
186189 // make sure it matches the expected `response_mode`
187- match ( provider. response_mode , method) {
188- ( UpstreamOAuthProviderResponseMode :: Query , Method :: GET ) => { }
189- ( UpstreamOAuthProviderResponseMode :: FormPost , 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- }
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 } ) ;
204229 }
205- ( expected, _) => return Err ( RouteError :: InvalidParamsMode { expected } ) ,
206230 }
207231
208232 let ( session_id, _post_auth_action) = sessions_cookie
0 commit comments