Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 36 additions & 11 deletions social_core/backends/linkedin.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,13 @@
name = "linkedin-oauth2"
AUTHORIZATION_URL = "https://www.linkedin.com/oauth/v2/authorization"
ACCESS_TOKEN_URL = "https://www.linkedin.com/oauth/v2/accessToken"
USER_DETAILS_URL = "https://api.linkedin.com/v2/userinfo?projection=({projection})"
USER_DETAILS_URL = "https://api.linkedin.com/v2/me?projection=({projection})"
USER_EMAILS_URL = (
"https://api.linkedin.com/v2/emailAddress"
"?q=members&projection=(elements*(handle~))"
)
REDIRECT_STATE = False
DEFAULT_SCOPE = ["email", "profile", "openid"]
DEFAULT_SCOPE = ["r_liteprofile"]
EXTRA_DATA = [
("id", "id"),
("expires_in", "expires"),
Expand All @@ -65,7 +65,14 @@
]

def user_details_url(self):
return self.USER_DETAILS_URL
# use set() since LinkedIn fails when values are duplicated
fields_selectors = list(
{"id", "firstName", "lastName", *self.setting("FIELD_SELECTORS", [])}
)
# user sort to ease the tests URL mocking
fields_selectors.sort()
fields_selectors = ",".join(fields_selectors)
return self.USER_DETAILS_URL.format(projection=fields_selectors)

def user_emails_url(self):
return self.USER_EMAILS_URL
Expand All @@ -75,10 +82,10 @@
self.user_details_url(), headers=self.user_data_headers(access_token)
)

if "email" in set(self.setting("FIELD_SELECTORS", [])):
if "emailAddress" in set(self.setting("FIELD_SELECTORS", [])):
emails = self.email_data(access_token, *args, **kwargs)
if emails:
response["email"] = emails[0]
response["emailAddress"] = emails[0]

Check warning on line 88 in social_core/backends/linkedin.py

View check run for this annotation

Codecov / codecov/patch

social_core/backends/linkedin.py#L88

Added line #L88 was not covered by tests

return response

Expand All @@ -88,20 +95,38 @@
)
email_addresses = []
for element in response.get("elements", []):
email_address = element.get("handle~", {}).get("email")
email_address = element.get("handle~", {}).get("emailAddress")

Check warning on line 98 in social_core/backends/linkedin.py

View check run for this annotation

Codecov / codecov/patch

social_core/backends/linkedin.py#L98

Added line #L98 was not covered by tests
email_addresses.append(email_address)
return list(filter(None, email_addresses))

def get_user_details(self, response):
"""Return user details from Linkedin account"""
response = self.user_data(access_token=response["access_token"])

def get_localized_name(name):
"""
FirstName & Last Name object
{
'localized': {
'en_US': 'Smith'
},
'preferredLocale': {
'country': 'US',
'language': 'en'
}
}
:return the localizedName from the lastName object
"""
locale = "{}_{}".format(
name["preferredLocale"]["language"], name["preferredLocale"]["country"]
)
return name["localized"].get(locale, "")

fullname, first_name, last_name = self.get_user_names(
first_name=response["given_name"],
last_name=response["family_name"],
first_name=get_localized_name(response["firstName"]),
last_name=get_localized_name(response["lastName"]),
)
email = response.get("email", "")
email = response.get("emailAddress", "")
return {
"id": response.get("sub", ""),
"username": first_name + last_name,
"fullname": fullname,
"first_name": first_name,
Expand Down
23 changes: 12 additions & 11 deletions social_core/tests/backends/test_linkedin.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,23 +41,24 @@ def test_invalid_nonce(self):


class BaseLinkedinTest:
user_data_url = "https://api.linkedin.com/v2/userinfo"
user_data_url = "https://api.linkedin.com/v2/me?projection=(firstName,id,lastName)"
expected_username = "FooBar"
access_token_body = json.dumps({"access_token": "foobar", "token_type": "bearer"})

# Reference:
# https://learn.microsoft.com/en-us/linkedin/consumer/integrations/self
# -serve/sign-in-with-linkedin-v2#response-body-schema
# https://docs.microsoft.com/en-us/linkedin/consumer/integrations/self
# -serve/sign-in-with-linkedin?context=linkedin/consumer/context#api-request
user_data_body = json.dumps(
{
"sub": "782bbtaQ",
"name": "FooBar",
"given_name": "Foo",
"family_name": "Bar",
"picture": "https://media.licdn-ei.com/dms/image/C5F03AQHqK8v7tB1HCQ/profile-displayphoto-shrink_100_100/0/",
"locale": "en-US",
"email": "doe@email.com",
"email_verified": True,
"id": "1010101010",
"firstName": {
"localized": {"en_US": "Foo"},
"preferredLocale": {"country": "US", "language": "en"},
},
"lastName": {
"localized": {"en_US": "Bar"},
"preferredLocale": {"country": "US", "language": "en"},
},
}
)

Expand Down