Skip to content

Commit f8b84e8

Browse files
committed
Fix 'state' usage and validation
1 parent 9f5a84c commit f8b84e8

File tree

1 file changed

+3
-5
lines changed

1 file changed

+3
-5
lines changed

fastapi_oauth2/base.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,6 @@
99
from starlette.responses import RedirectResponse
1010

1111

12-
class UnsetStateWarning(UserWarning):
13-
"""Warning about unset state parameter"""
14-
15-
1612
class SSOLoginError(HTTPException):
1713
"""Raised when any login-related error occurs
1814
(such as when user is not verified or if there was an attempt for fake login)
@@ -78,6 +74,7 @@ async def get_login_url(
7874
params: Optional[Dict[str, Any]] = None,
7975
state: Optional[str] = None,
8076
) -> Any:
77+
self.state = state
8178
params = params or {}
8279
redirect_uri = redirect_uri or self.redirect_uri
8380
if redirect_uri is None:
@@ -108,7 +105,8 @@ async def verify_and_process(
108105
code = request.query_params.get("code")
109106
if code is None:
110107
raise SSOLoginError(400, "'code' parameter was not found in callback request")
111-
self.state = request.query_params.get("state")
108+
if self.state != request.query_params.get("state"):
109+
raise SSOLoginError(400, "'state' parameter does not match")
112110
return await self.process_login(
113111
code, request, params=params, additional_headers=headers, redirect_uri=redirect_uri
114112
)

0 commit comments

Comments
 (0)