Skip to content

Commit

Permalink
Add MissingStateError
Browse files Browse the repository at this point in the history
This commit adds a new `MissingStateError` that will be raised before a
`MismatchingStateError` when there is no `state` value in the session.
  • Loading branch information
cburmeister committed Oct 6, 2020
1 parent f8f8df5 commit 3f0bb79
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 2 deletions.
4 changes: 2 additions & 2 deletions authlib/integrations/base_client/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
from .errors import (
OAuthError, MissingRequestTokenError, MissingTokenError,
TokenExpiredError, InvalidTokenError, UnsupportedTokenTypeError,
MismatchingStateError,
MismatchingStateError, MissingStateError,
)

__all__ = [
'BaseOAuth', 'BaseApp', 'RemoteApp', 'FrameworkIntegration',
'OAuthError', 'MissingRequestTokenError', 'MissingTokenError',
'TokenExpiredError', 'InvalidTokenError', 'UnsupportedTokenTypeError',
'MismatchingStateError',
'MismatchingStateError', 'MissingStateError',
]
3 changes: 3 additions & 0 deletions authlib/integrations/base_client/base_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from authlib.consts import default_user_agent
from .errors import (
MismatchingStateError,
MissingStateError,
)

__all__ = ['BaseApp']
Expand Down Expand Up @@ -122,6 +123,8 @@ def _get_oauth_client(self, **kwargs):
def _retrieve_oauth2_access_token_params(self, request, params):
request_state = params.pop('state', None)
state = self.framework.get_session_data(request, 'state')
if not state:
raise MissingStateError()
if state != request_state:
raise MismatchingStateError()
if state:
Expand Down
5 changes: 5 additions & 0 deletions authlib/integrations/base_client/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,8 @@ class UnsupportedTokenTypeError(OAuthError):
class MismatchingStateError(OAuthError):
error = 'mismatching_state'
description = 'CSRF Warning! State not equal in request and response.'


class MissingStateError(OAuthError):
error = 'missing_state'
description = 'CSRF Warning! State missing in request.'

0 comments on commit 3f0bb79

Please sign in to comment.