Skip to content

Commit

Permalink
Add User's Info to Conver-Token response (#238)
Browse files Browse the repository at this point in the history
* Add LinkedIn OpenID support

A new class, LinkedInOpenIDUserInfo, has been added to the backends file. This class queries LinkedIn's userinfo API, processes the response for errors, and maps response fields to user details. The new functionality extends the social authentication support to include LinkedIn's OpenID Connect service.

* Update version to 3.1.0

The version number of the package drf-social-oauth2 has been incremented to '3.1.0' from '3.0.1'. Also, the documentation and installation instructions have been updated to reflect this new version.

* Update the authentication class for LinledIn setup.

 Removed unnecessary config fields since LinkedIn deprecated them since Aug, 2024.

* Add methods to retrieve user details via access token

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 authored Jul 19, 2024
1 parent e86ec0e commit b2a33a9
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 13 deletions.
2 changes: 1 addition & 1 deletion docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
project = 'drf-social-oauth2'
copyright = '2024, Wagner de Lima'
author = 'Wagner de Lima'
release = '3.0.1'
release = '3.1.0'

# -- General configuration ---------------------------------------------------
# https://www.sphinx-doc.org/en/master/usage/configuration.html#general-configuration
Expand Down
2 changes: 1 addition & 1 deletion docs/source/installation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ This framework is published at the PyPI, install it with pip:

.. code-block:: console
$ pip install drf_social_oauth2==3.0.1
$ pip install drf_social_oauth2==3.1.0
To enable OAuth2 social authentication support for your Django REST Framework application, you need to install
Expand Down
10 changes: 1 addition & 9 deletions docs/source/integration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ Configure your settings.py as follows:
...
# Linked OpenID
'social_core.backends.linkedin.LinkedinOpenIdConnect',
'drf_social_oauth2.backends.LinkedInOpenIDUserInfo',
# drf-social-oauth2
'drf_social_oauth2.backends.DjangoOAuth2',
Expand All @@ -369,14 +369,6 @@ Configure your settings.py as follows:
SOCIAL_AUTH_LINKEDIN_OPENIDCONNECT_KEY = 'key goes here'
SOCIAL_AUTH_LINKEDIN_OPENIDCONNECT_SECRET = 'secret goes here'
SOCIAL_AUTH_LINKEDIN_OAUTH2_SCOPE = ['r_liteprofile', 'r_emailaddress']
# Add the fields so they will be requested from linkedin.
SOCIAL_AUTH_LINKEDIN_OAUTH2_FIELD_SELECTORS = ['emailAddress']
# Arrange to add the fields to UserSocialAuth.extra_data
SOCIAL_AUTH_LINKEDIN_OAUTH2_EXTRA_DATA = [('id', 'id'),
('firstName', 'first_name'),
('lastName', 'last_name'),
('emailAddress', 'email_address')]
Other Backend Integration
Expand Down
2 changes: 1 addition & 1 deletion drf_social_oauth2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
and a ton more!
"""

__version__ = '3.0.1'
__version__ = '3.1.0'

try:
from secrets import SystemRandom
Expand Down
22 changes: 22 additions & 0 deletions drf_social_oauth2/backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from social_core.backends.oauth import BaseOAuth2
from social_core.backends.google import GooglePlusAuth
from social_core.backends.linkedin import LinkedinOpenIdConnect

from drf_social_oauth2.settings import (
DRFSO2_PROPRIETARY_BACKEND_NAME,
Expand Down Expand Up @@ -39,3 +40,24 @@ def user_data(self, access_token, *args, **kwargs):
)
self.process_error(response)
return response


class LinkedInOpenIDUserInfo(LinkedinOpenIdConnect):
def user_data(self, access_token, *args, **kwargs):
response = self.get_json(
"https://api.linkedin.com/v2/userinfo",
headers={"Authorization": f"Bearer {access_token}"},
)
self.process_error(response)
return response

def get_user_details(self, response):
username_key = self.setting("USERNAME_KEY", self.USERNAME_KEY)
return {
"username": response.get(username_key),
"email": response.get("email"),
"fullname": response.get("name"),
"first_name": response.get("given_name"),
"last_name": response.get("family_name"),
"picture": response.get("picture"),
}
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 b2a33a9

Please sign in to comment.