Smarter LinkedIn oAuth Library

LinkedIn OpenID Connect and OAuth2 backends for python-social-auth.

They adapt the backends that python-social-auth documents at https://python-social-auth.readthedocs.io/en/latest/backends/linkedin.html to LinkedIn’s current api.

class smarter.lib.social_core.backends.linkedin.LinkedinMobileOAuth2(strategy=None, redirect_uri=None)[source]

Bases: LinkedinOAuth2

LinkedIn backend for the mobile SDK, which adds the x-li-src: msdk header.

name = 'linkedin-mobile-oauth2'
user_data_headers(access_token)[source]

Return the parent’s headers, plus x-li-src: msdk.

Parameters:

access_token (str) – The OAuth2 access token.

Return type:

dict

Returns:

The headers.

class smarter.lib.social_core.backends.linkedin.LinkedinOAuth2(strategy=None, redirect_uri=None)[source]

Bases: BaseOAuth2

Python-social-auth backend for signing in with LinkedIn, by OAuth2 with the OpenID Connect scopes.

ACCESS_TOKEN_METHOD: Literal['GET', 'POST'] = 'POST'
ACCESS_TOKEN_URL = 'https://www.linkedin.com/oauth/v2/accessToken'
AUTHORIZATION_URL = 'https://www.linkedin.com/oauth/v2/authorization'
DEFAULT_SCOPE: list[str] | None = ['openid', 'profile', 'email']
EXTRA_DATA: list[str | tuple[str, str] | tuple[str, str, bool]] | None = [('id', 'id'), ('expires_in', 'expires'), ('firstName', 'first_name'), ('lastName', 'last_name'), ('refresh_token', 'refresh_token'), ('refresh_token_expires_in', 'refresh_expires_in')]
REDIRECT_STATE = False
USER_DETAILS_URL = 'https://api.linkedin.com/v2/userinfo?projection=({projection})'
USER_EMAILS_URL = 'https://api.linkedin.com/v2/emailAddress?q=members&projection=(elements*(handle~))'
email_data(access_token, *args, **kwargs)[source]

Fetch the user’s email addresses from LinkedIn.

Parameters:
  • access_token – The OAuth2 access token.

  • args – Unused.

  • kwargs – Unused.

Returns:

The non-empty email addresses.

get_user_details(response)[source]

Return the user’s details from their LinkedIn profile.

A profile without both a first and a last name gets unknown names, and one without an email address gets unknown@mail.com.

Parameters:

response (dict) – The profile, from user_data().

Return type:

dict

Returns:

A dict of username, fullname, first_name, last_name and email.

name = 'linkedin-oauth2'
process_error(data)[source]

Raise on an OAuth2 error, or on a LinkedIn serviceErrorCode.

Parameters:

data (dict) – The response data.

Raises:

AuthCanceled – If the response has a serviceErrorCode.

Return type:

None

request_access_token(*args, **kwargs)[source]

Request the access token, sending the form data as query parameters, as LinkedIn expects.

Parameters:
  • args – Passed to the base class.

  • kwargs – Passed to the base class; data is moved to params.

Returns:

The token response.

user_data(access_token, *args, **kwargs)[source]

Fetch the user’s profile from LinkedIn, adding the first email address if FIELD_SELECTORS asks for it.

Parameters:
Returns:

The profile dict.

user_data_headers(access_token)[source]

Return the headers of LinkedIn api requests: the bearer token, and Accept-Language if FORCE_PROFILE_LANGUAGE is set.

A FORCE_PROFILE_LANGUAGE of True means the strategy’s current language.

Parameters:

access_token (str) – The OAuth2 access token.

Return type:

dict

Returns:

The headers.

user_details_url()[source]

Return the userinfo url, projecting id, firstName, lastName and the FIELD_SELECTORS setting.

Return type:

str

Returns:

The url.

user_emails_url()[source]

Return the url of the user’s email addresses.

Return type:

str

Returns:

The url.

class smarter.lib.social_core.backends.linkedin.LinkedinOpenIdConnect(strategy=None, redirect_uri=None)[source]

Bases: OpenIdConnectAuth

Linkedin OpenID Connect backend.

Oauth2 has been deprecated as of August 1, 2023. https://learn.microsoft.com/en-us/linkedin/consumer/integrations/self-serve/sign-in-with-linkedin-v2?context=linkedin/consumer/context

OIDC_ENDPOINT: str | None = 'https://www.linkedin.com/oauth'
TOKEN_ENDPOINT_AUTH_METHOD = 'client_secret_post'
name = 'linkedin-openidconnect'
validate_claims(id_token)[source]

Validate the id token’s claims, like the base class, but without the nonce validation.

LinkedIn does not provide a nonce, so only the nbf (not before) and iat (issued at) claims are checked.

Parameters:

id_token (dict) – The decoded id token.

Raises:

AuthTokenError – If the token is not valid yet, or was issued more than ID_TOKEN_MAX_AGE seconds ago.

Return type:

None