Skip to content

Commit 1da2a0a

Browse files
committed
corrected get_or_create
1 parent ee55818 commit 1da2a0a

File tree

1 file changed

+15
-14
lines changed

1 file changed

+15
-14
lines changed

oauth2_provider/oauth2_validators.py

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -332,20 +332,21 @@ def _get_token_from_authentication_server(
332332
scope = content.get("scope", "")
333333
expires = make_aware(expires)
334334

335-
try:
336-
access_token = AccessToken.objects.select_related("application", "user").get(token=token)
337-
except AccessToken.DoesNotExist:
338-
access_token = AccessToken.objects.create(
339-
token=token,
340-
user=user,
341-
application=None,
342-
scope=scope,
343-
expires=expires
344-
)
345-
else:
346-
access_token.expires = expires
347-
access_token.scope = scope
348-
access_token.save()
335+
with transaction.atomic():
336+
access_token, _created = AccessToken\
337+
.objects.select_related("application", "user")\
338+
.select_for_update()\
339+
.get_or_create(token=token,
340+
defaults={
341+
"user": user,
342+
"application": None,
343+
"scope": scope,
344+
"expires": expires,
345+
})
346+
if not _created:
347+
access_token.scope = scope
348+
access_token.expires = expires
349+
access_token.save()
349350

350351
return access_token
351352

0 commit comments

Comments
 (0)