Skip to content
This repository was archived by the owner on Mar 3, 2020. It is now read-only.

Commit 887f3f6

Browse files
committed
fixup! De-coupled refresh token creation from access token creation
1 parent 4fbced8 commit 887f3f6

File tree

1 file changed

+59
-26
lines changed

1 file changed

+59
-26
lines changed

provider/views.py

Lines changed: 59 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,25 @@ def access_token_response_data(self, access_token, response_type=None):
105105

106106
return response_data
107107

108+
def get_access_and_refresh_tokens(self, request, user, scope, client, reuse_existing_access_token=False, create_refresh_token=True):
109+
"""
110+
Returns an AccessToken and RefreshToken for the given user, scope, and client combination.
111+
112+
Returns:
113+
(AccessToken, RefreshToken)
114+
If create_refresh_token is False, the second element of the tuple will be None.
115+
"""
116+
if reuse_existing_access_token:
117+
at = self.get_access_token(request, user, scope, client)
118+
else:
119+
at = self.create_access_token(request, user, scope, client)
120+
121+
rt = None
122+
if create_refresh_token and not reuse_existing_access_token:
123+
rt = self.create_refresh_token(request, user, scope, at, client)
124+
125+
return at, rt
126+
108127

109128
class OAuthView(TemplateView):
110129
"""
@@ -333,15 +352,14 @@ def get_implicit_response(self, request, client):
333352
data = self.get_data(request)
334353

335354
lookup_kwargs = {
336-
"user": request.user,
337-
"client": client,
338-
"scope": scope.to_int(*data.get('scope', constants.SCOPES[0][1]).split())
355+
'user': request.user,
356+
'client': client,
357+
'scope': scope.to_int(*data.get('scope', constants.SCOPES[0][1]).split()),
358+
'reuse_existing_access_token': constants.SINGLE_ACCESS_TOKEN,
359+
'create_refresh_token': False
339360
}
340361

341-
if constants.SINGLE_ACCESS_TOKEN:
342-
token = self.get_access_token(request, **lookup_kwargs)
343-
else:
344-
token = self.create_access_token(request, **lookup_kwargs)
362+
token, __ = self.get_access_and_refresh_tokens(request, **lookup_kwargs)
345363

346364
response_data = self.access_token_response_data(token, data['response_type'])
347365

@@ -572,13 +590,16 @@ def authorization_code(self, request, data, client):
572590
Handle ``grant_type=authorization_code`` requests as defined in
573591
:rfc:`4.1.3`.
574592
"""
575-
grant = self.get_authorization_code_grant(request, request.POST,
576-
client)
577-
if constants.SINGLE_ACCESS_TOKEN:
578-
at = self.get_access_token(request, grant.user, grant.scope, client)
579-
else:
580-
at = self.create_access_token(request, grant.user, grant.scope, client)
581-
rt = self.create_refresh_token(request, grant.user, grant.scope, at, client)
593+
grant = self.get_authorization_code_grant(request, request.POST, client)
594+
595+
kwargs = {
596+
'request': request,
597+
'user': grant.user,
598+
'scope': grant.scope,
599+
'client': client,
600+
'reuse_existing_access_token': constants.SINGLE_ACCESS_TOKEN,
601+
}
602+
at, rt = self.get_access_and_refresh_tokens(**kwargs)
582603

583604
self.invalidate_grant(grant)
584605

@@ -594,8 +615,13 @@ def refresh_token(self, request, data, client):
594615
self.invalidate_refresh_token(rt)
595616
self.invalidate_access_token(rt.access_token)
596617

597-
at = self.create_access_token(request, rt.user, rt.access_token.scope, client)
598-
rt = self.create_refresh_token(request, at.user, at.scope, at, client)
618+
kwargs = {
619+
'request': request,
620+
'user': rt.user,
621+
'scope': rt.access_token.scope,
622+
'client': client,
623+
}
624+
at, rt = self.get_access_and_refresh_tokens(**kwargs)
599625

600626
return self.access_token_response(at)
601627

@@ -605,24 +631,31 @@ def password(self, request, data, client):
605631
"""
606632

607633
data = self.get_password_grant(request, data, client)
608-
user = data.get('user')
609-
scope = data.get('scope')
634+
kwargs = {
635+
'request': request,
636+
'user': data.get('user'),
637+
'scope': data.get('scope'),
638+
'client': client,
639+
'reuse_existing_access_token': constants.SINGLE_ACCESS_TOKEN,
610640

611-
if constants.SINGLE_ACCESS_TOKEN:
612-
at = self.get_access_token(request, user, scope, client)
613-
else:
614-
at = self.create_access_token(request, user, scope, client)
615641
# Public clients don't get refresh tokens
616-
if client.client_type == constants.CONFIDENTIAL:
617-
rt = self.create_refresh_token(request, user, scope, at, client)
642+
'create_refresh_token': client.client_type == constants.CONFIDENTIAL
643+
}
644+
at, rt = self.get_access_and_refresh_tokens(**kwargs)
618645

619646
return self.access_token_response(at)
620647

621648
def client_credentials(self, request, data, client):
622649
""" Handle ``grant_type=client_credentials`` requests as defined in :rfc:`4.4`. """
623650
data = self.get_client_credentials_grant(request, data, client)
624-
scope = data.get('scope')
625-
at = self.get_access_token(request, client.user, scope, client)
651+
kwargs = {
652+
'request': request,
653+
'user': client.user,
654+
'scope': data.get('scope'),
655+
'client': client,
656+
'create_refresh_token': False,
657+
}
658+
at, rt = self.get_access_and_refresh_tokens(**kwargs)
626659

627660
return self.access_token_response(at)
628661

0 commit comments

Comments
 (0)