Python Social Auth Core

4.8.5 · active · verified Thu Apr 09

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

Install

Imports

Quickstart

This quickstart demonstrates how to define a custom OAuth2 backend using `social-auth-core`. This core component abstracts provider specifics. For actual usage in a web application, this backend definition must be integrated with a framework-specific package (e.g., `social-auth-django`) and configured in your project's settings to provide the necessary client ID, secret, and URLs.

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.

view raw JSON →