Skip to content
This repository was archived by the owner on Jun 29, 2019. It is now read-only.

Commit 8a79230

Browse files
committed
Except AuthCodeNotFound error in AuthHandler
1 parent ef37932 commit 8a79230

File tree

2 files changed

+19
-16
lines changed

2 files changed

+19
-16
lines changed

oauth2/grant.py

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@
2828
2929
"""
3030
from oauth2.error import OAuthInvalidError, UserNotAuthenticated, \
31-
AccessTokenNotFound, UserIdentifierMissingError, RedirectUriUnknown
31+
AccessTokenNotFound, UserIdentifierMissingError, RedirectUriUnknown, \
32+
AuthCodeNotFound
3233
from oauth2.compatibility import urlencode, quote
3334
import json
3435
import time
@@ -527,25 +528,27 @@ def _read_params(self, request):
527528
explanation="Invalid redirect_uri parameter")
528529

529530
def _validate_code(self):
530-
stored_code = self.auth_code_store.fetch_by_code(self.code)
531-
532-
if stored_code is None:
533-
raise OAuthInvalidError(error="invalid_request",
534-
explanation="Invalid authorization code " \
535-
"parameter")
531+
try:
532+
stored_code = self.auth_code_store.fetch_by_code(self.code)
533+
except AuthCodeNotFound:
534+
raise OAuthInvalidError(
535+
error="invalid_request",
536+
explanation="Invalid authorization code parameter")
536537

537538
if stored_code.code != self.code:
538-
raise OAuthInvalidError(error="invalid_grant",
539-
explanation="Invalid code parameter in " \
540-
"request")
539+
raise OAuthInvalidError(
540+
error="invalid_grant",
541+
explanation="Invalid code parameter in request")
541542

542543
if stored_code.redirect_uri != self.redirect_uri:
543-
raise OAuthInvalidError(error="invalid_request",
544-
explanation="Invalid redirect_uri parameter")
544+
raise OAuthInvalidError(
545+
error="invalid_request",
546+
explanation="Invalid redirect_uri parameter")
545547

546548
if stored_code.is_expired():
547-
raise OAuthInvalidError(error="invalid_grant",
548-
explanation="Authorization code has expired")
549+
raise OAuthInvalidError(
550+
error="invalid_grant",
551+
explanation="Authorization code has expired")
549552

550553
self.data = stored_code.data
551554
self.scopes = stored_code.scopes

oauth2/test/test_grant.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
ClientCredentialsHandler, AuthorizeMixin
1111
from oauth2.store import ClientStore, AuthCodeStore, AccessTokenStore
1212
from oauth2.error import OAuthInvalidError, UserNotAuthenticated, \
13-
AccessTokenNotFound, UserIdentifierMissingError
13+
AccessTokenNotFound, UserIdentifierMissingError, AuthCodeNotFound
1414
from oauth2 import Provider
1515
from oauth2.datatype import Client, AuthorizationCode, AccessToken
1616
from oauth2.tokengenerator import TokenGenerator
@@ -410,7 +410,7 @@ def test_read_validate_params_no_auth_code_found(self):
410410
redirect_uri = "http://callback"
411411

412412
auth_code_store_mock = Mock(spec=AuthCodeStore)
413-
auth_code_store_mock.fetch_by_code.return_value = None
413+
auth_code_store_mock.fetch_by_code.side_effect = AuthCodeNotFound
414414

415415
client = Client(identifier=client_id, secret=client_secret,
416416
redirect_uris=[redirect_uri])

0 commit comments

Comments
 (0)