Skip to content

Commit 048a337

Browse files
committed
Redirect to /login on invalid grant
1 parent fe715e3 commit 048a337

File tree

4 files changed

+20
-7
lines changed

4 files changed

+20
-7
lines changed

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ deferring to an oauth2 provider.
77

88
# IMPORTANT SECURITY NOTE
99

10-
If you use this package, you should install the latest development version of `request_oauthlib` in order to get [an important commit that fixes a CSRF vulnerability](https://github.com/requests/requests-oauthlib/commit/c5cad15edc28040f85dba52ceebb18e11bd9e759)
10+
If you use this package, you should install the latest development version of `requests_oauthlib` in order to get [an important commit that fixes a CSRF vulnerability](https://github.com/requests/requests-oauthlib/commit/c5cad15edc28040f85dba52ceebb18e11bd9e759)
1111

1212
# Support
1313

@@ -81,7 +81,8 @@ and then run the tests with the provided script:
8181
```
8282

8383
## Changelog
84-
* 0.2.3: Redirect to the log if the state is mismatching
85-
* 0.2.2: Redirect to the log if the state goes missing (sometimes people bookmark the login url)
84+
* 0.2.4: Redirect to the login if the grant is invalid
85+
* 0.2.3: Redirect to the login if the state is mismatching
86+
* 0.2.2: Redirect to the login if the state goes missing (sometimes people bookmark the login url)
8687
* 0.2.1: Added tests for the ping function and fixed a bug with the session variable name for the ping timestamp.
8788
* 0.2.0: Added support for pinging the auth server to make sure the token is still valid

oauthadmin/views.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from time import time
22

33
from requests_oauthlib import OAuth2Session
4-
from oauthlib.oauth2.rfc6749.errors import MismatchingStateError
4+
from oauthlib.oauth2.rfc6749.errors import MismatchingStateError, InvalidGrantError
55
from urllib import quote_plus
66

77
from django.shortcuts import redirect
@@ -44,7 +44,7 @@ def callback(request):
4444
client_secret=app_setting('CLIENT_SECRET'),
4545
authorization_response=app_setting('AUTH_URL') + "?" + request.GET.urlencode()
4646
)
47-
except MismatchingStateError:
47+
except (MismatchingStateError, InvalidGrantError):
4848
return HttpResponseRedirect(request.build_absolute_uri(reverse('oauthadmin.views.login')))
4949

5050
user = import_by_path(app_setting('GET_USER'))(token)

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
setup(
1919
name='django-admin-oauth2',
20-
version='0.2.3',
20+
version='0.2.4',
2121
description='A django app that replaces the django admin authentication mechanism by deferring to an oauth2 provider',
2222
long_description=README,
2323
url='https://github.com/RealGeeks/django-admin-oauth2',

test/test_views.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import mock
22
import pytest
33
from oauthadmin.views import destroy_session, login, callback, logout
4-
from oauthlib.oauth2.rfc6749.errors import MismatchingStateError
4+
from oauthlib.oauth2.rfc6749.errors import MismatchingStateError, InvalidGrantError
55
from django.test.client import RequestFactory
66

77

@@ -73,6 +73,18 @@ def test_callback_with_missing_state(import_by_path, app_setting, OAuth2Session,
7373
assert resp.status_code == 302
7474
assert resp['location'] == 'http://testserver/login/'
7575

76+
@mock.patch('oauthadmin.views.OAuth2Session')
77+
@mock.patch('oauthadmin.views.app_setting')
78+
@mock.patch('oauthadmin.views.import_by_path')
79+
def test_callback_with_invalid_grant(import_by_path, app_setting, OAuth2Session, request_factory):
80+
request = request_factory.get('/')
81+
request.session = {'oauth_state':'foo'}
82+
app_setting.return_value = 'app-setting'
83+
OAuth2Session.return_value = mock.Mock(fetch_token = mock.Mock(side_effect=InvalidGrantError))
84+
resp = callback(request)
85+
assert resp.status_code == 302
86+
assert resp['location'] == 'http://testserver/login/'
87+
7688
@mock.patch('oauthadmin.views.OAuth2Session')
7789
@mock.patch('oauthadmin.views.app_setting')
7890
@mock.patch('oauthadmin.views.import_by_path')

0 commit comments

Comments
 (0)