Skip to content

Commit

Permalink
Add methods to retrieve user details via access token
Browse files Browse the repository at this point in the history
Two new methods have been introduced in the views.py file of the drf-social-oauth2 module. The 'get_user' method retrieves the user associated with an access token. The 'prepare_response' method, on the other hand, adds user detailed info such as email, first name, and last name into the response data. The returned response from the 'post' method has been updated to utilize these changes.
  • Loading branch information
wagnerdelima committed Jul 19, 2024
1 parent 73dcac4 commit b8950ac
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion drf_social_oauth2/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,20 @@ class ConvertTokenView(CsrfExemptMixin, OAuthLibMixin, APIView):
oauthlib_backend_class = KeepRequestCore
permission_classes = (AllowAny,)

def get_user(self, access_token: str):
token = AccessToken.objects.filter(token=access_token).first()
return token.user if token else None

def prepare_response(self, data: dict):
user = self.get_user(data.get('access_token'))
if user:
data['user'] = {
'email': user.email,
'first_name': user.first_name,
'last_name': user.last_name,
}
return data

def post(self, request: Request, *args, **kwargs):
if 'client_secret' in request.data:
# Log a warning
Expand Down Expand Up @@ -192,7 +206,8 @@ def post(self, request: Request, *args, **kwargs):
status=HTTP_500_INTERNAL_SERVER_ERROR,
)

return Response(data=json_loads(body), status=status)
data = self.prepare_response(json_loads(body))
return Response(data, status=status)


class RevokeTokenView(CsrfExemptMixin, OAuthLibMixin, APIView):
Expand Down

0 comments on commit b8950ac

Please sign in to comment.