Skip to content

Commit a1dcd37

Browse files
madprimeauvipy
authored andcommitted
Auto-authorize if valid refresh tokens exist (#754)
* Auto-authorize if valid refresh tokens exist * Add test for auto auth from refresh token
1 parent 1499048 commit a1dcd37

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

oauth2_provider/views/base.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from ..exceptions import OAuthToolkitError
1313
from ..forms import AllowForm
1414
from ..http import OAuth2ResponseRedirect
15-
from ..models import get_access_token_model, get_application_model
15+
from ..models import get_access_token_model, get_application_model, get_refresh_token_model
1616
from ..scopes import get_scopes_backend
1717
from ..settings import oauth2_settings
1818
from ..signals import app_authorized
@@ -191,6 +191,12 @@ def get(self, request, *args, **kwargs):
191191
expires__gt=timezone.now()
192192
).all()
193193

194+
refresh_tokens = get_refresh_token_model().objects.filter(
195+
user=request.user,
196+
application=kwargs["application"]
197+
).exclude(revoked__lt=timezone.now()).all()
198+
tokens = list(tokens) + [r.access_token for r in refresh_tokens]
199+
194200
# check past authorizations regarded the same scopes as the current one
195201
for token in tokens:
196202
if token.allow_scopes(scopes):

tests/test_authorization_code.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,16 @@ def test_pre_auth_approval_prompt(self):
197197
url = "{url}?{qs}".format(url=reverse("oauth2_provider:authorize"), qs=query_string)
198198
response = self.client.get(url)
199199
self.assertEqual(response.status_code, 302)
200+
# access token expired but valid refresh token exists
201+
tok.expires = timezone.now() - datetime.timedelta(days=1)
202+
tok.save()
203+
reftok = RefreshToken.objects.create(
204+
user=self.test_user, token="0123456789",
205+
application=self.application,
206+
access_token=tok
207+
)
208+
response = self.client.get(url)
209+
self.assertEqual(response.status_code, 302)
200210
# user already authorized the application, but with different scopes: prompt them.
201211
tok.scope = "read"
202212
tok.save()

0 commit comments

Comments
 (0)