Python Social Auth Core
social-auth-core is the foundational library for Python Social Auth, providing a flexible, decoupled mechanism for social authentication. It abstracts away the complexities of integrating OAuth, OpenID Connect, and SAML providers, enabling developers to add various social logins easily. The current version is 4.8.5, and it maintains a regular release cadence with frequent patch and minor updates to support new backends and fix issues.
Warnings
- breaking Python 3.9 support was dropped in version 4.8.0. Users on Python 3.9 or older must upgrade to Python 3.10+.
- breaking Numerous social authentication backends have been removed across recent versions (e.g., itembase, nk, AOL OpenID, BitBucket OAuth 1.0, khanacademy). Users relying on these backends will find them unavailable.
- breaking For the SAML backend, missing configured attributes now cause an `AuthMissingParameter` error. Previously, these might have been silently ignored.
- breaking OAuth2 backends now default to using the POST method for token exchange (e.g., getting access tokens). This change occurred in version 4.6.0.
- gotcha `social-auth-core` is a core library and requires a framework-specific integration package (e.g., `social-auth-django`, `social-auth-flask`, `social-auth-pyramid`) to be used in a web application context.
- gotcha The `ID_KEY` used for identifying users, which defaults to `id`, became configurable in version 4.8.2. If you have custom logic relying on the fixed 'id' key or are extending backends, this might impact your code.
Install
-
pip install social-auth-core -
pip install social-auth-core[sso]
Imports
- BaseOAuth2
from social_core.backends.oauth import BaseOAuth2
- BaseStrategy
from social_core.strategy import BaseStrategy
- AuthException
from social_core.exceptions import AuthException
Quickstart
import os
from social_core.backends.oauth import BaseOAuth2
# This example demonstrates how to define a custom OAuth2 backend using social-auth-core.
# To use this in a web application, you would also need a framework-specific integration
# (e.g., social-auth-django) and configure it in your project's settings.
class MyCustomOAuth2Backend(BaseOAuth2):
name = 'my-custom-oauth2'
AUTHORIZATION_URL = os.environ.get('MY_CUSTOM_OAUTH2_AUTHORIZATION_URL', 'https://example.com/oauth/authorize')
ACCESS_TOKEN_URL = os.environ.get('MY_CUSTOM_OAUTH2_ACCESS_TOKEN_URL', 'https://example.com/oauth/token')
SCOPE_SEPARATOR = ','
DEFAULT_SCOPE = ['email', 'profile']
EXTRA_DATA = [
('id', 'id'),
('expires_in', 'expires'),
('token_type', 'token_type'),
]
def get_user_details(self, response):
"""Return user details from example.com account."""
return {
'username': response.get('email') or response.get('name'),
'email': response.get('email'),
'fullname': response.get('name')
}
def user_data(self, access_token, *args, **kwargs):
"""Loads user data from the custom service using the access_token."""
# In a real backend, you'd make an API call to fetch user info.
# For this quickstart, we return mock data.
# Example of a real call:
# url = 'https://api.example.com/userinfo'
# headers = {'Authorization': f'Bearer {access_token}'}
# response = self.get_json(url, headers=headers)
# return response
return {'id': 'user123', 'email': 'user@example.com', 'name': 'Test User'}
# To integrate this, you would typically add 'my_app.backends.MyCustomOAuth2Backend'
# to your framework's SOCIAL_AUTH_AUTHENTICATION_BACKENDS setting.