Skip to main content

Python Social Auth support for Django GraphQL

Project description

Python Social Auth support for Django GraphQL

🚀 Production-Ready social authentication for GraphQL APIs with comprehensive error handling, logging, and security features.

Features

  • 🔐 Session & JWT Authentication - Support for both session-based and JWT token authentication

  • 🛡️ Enhanced Security - Built-in rate limiting, input validation, and comprehensive error handling

  • 📊 Production Monitoring - Detailed logging and error tracking for production environments

  • 🔧 Easy Integration - Simple GraphQL mutations with extensive documentation

  • 🌐 Multiple Providers - Support for Google, Facebook, GitHub, Twitter, LinkedIn, Apple, and more

  • 📚 Comprehensive Docs - Complete setup guides and troubleshooting documentation

Dependencies

  • Python ≥ 3.8

  • Django ≥ 3.2

  • graphene-django ≥ 3.0.0

  • social-auth-app-django ≥ 5.0.0

Installation

Install from PyPI:

pip install django-graphene-social-auth

Quick Start

  1. Add to Django settings:

INSTALLED_APPS = [
    # ... your apps
    'social_django',
    'graphene_django',
    # ... your apps
]

AUTHENTICATION_BACKENDS = [
    'social_core.backends.google.GoogleOAuth2',
    'social_core.backends.facebook.FacebookOAuth2',
    # ... other backends
    'django.contrib.auth.backends.ModelBackend',
]

# Configure your social providers
SOCIAL_AUTH_GOOGLE_OAUTH2_KEY = 'your-google-client-id'
SOCIAL_AUTH_GOOGLE_OAUTH2_SECRET = 'your-google-client-secret'
  1. Run migrations:

python manage.py migrate
  1. Add to your GraphQL schema:

import graphene
import graphql_social_auth

class Mutations(graphene.ObjectType):
    # For session-based authentication
    social_auth = graphql_social_auth.SocialAuth.Field()

    # For JWT authentication (requires django-graphql-jwt)
    # social_auth = graphql_social_auth.SocialAuthJWT.Field()

Usage Examples

GraphQL Mutation (Session Authentication):

mutation SocialAuth($provider: String!, $accessToken: String!) {
  socialAuth(provider: $provider, accessToken: $accessToken) {
    success
    errors
    social {
      uid
      extraData
    }
    user {
      id
      username
      email
    }
  }
}

GraphQL Mutation (JWT Authentication):

mutation SocialAuthJWT($provider: String!, $accessToken: String!) {
  socialAuth(provider: $provider, accessToken: $accessToken) {
    success
    errors
    token
    refreshToken
    social {
      uid
    }
  }
}

Variables:

{
  "provider": "google-oauth2",
  "accessToken": "your-oauth-access-token"
}

Supported Providers

  • Google - google-oauth2

  • Facebook - facebook

  • GitHub - github

  • Twitter - twitter

  • LinkedIn - linkedin-oauth2

  • Apple - apple-id

  • Discord - discord

  • Microsoft - microsoft-graph

For complete provider setup instructions, see the Authentication backend list.

Production Setup

For production deployment with security best practices, monitoring, and troubleshooting guides, see:

Error Handling

The package provides comprehensive error handling with specific error types:

# Example error response
{
  "data": {
    "socialAuth": {
      "success": false,
      "errors": ["Provider 'invalid-provider' not found or not configured"],
      "social": null,
      "user": null
    }
  }
}

Common error types:

  • PROVIDER_NOT_FOUND - Invalid or unconfigured provider

  • INVALID_TOKEN - Expired or invalid access token

  • AUTH_FAILED - Authentication process failed

  • RATE_LIMIT_EXCEEDED - Too many requests

  • USER_CREATION_FAILED - User creation error

JWT Authentication

For JSON Web Token (JWT) authentication, install the JWT extension:

pip install 'django-graphene-social-auth[jwt]'

Configure JWT in your settings (see example_settings.py for complete configuration):

import datetime

GRAPHQL_JWT = {
    'JWT_EXPIRATION_DELTA': datetime.timedelta(minutes=60),
    'JWT_REFRESH_EXPIRATION_DELTA': datetime.timedelta(days=7),
    'JWT_LONG_RUNNING_REFRESH_TOKEN': True,
}

Use SocialAuthJWT instead of SocialAuth:

import graphene
import graphql_social_auth

class Mutations(graphene.ObjectType):
    social_auth = graphql_social_auth.SocialAuthJWT.Field()

Relay Support

Complete support for Relay:

import graphene
import graphql_social_auth

class Mutations(graphene.ObjectType):
    social_auth = graphql_social_auth.relay.SocialAuth.Field()

Relay mutations accept input arguments:

mutation SocialAuth($input: SocialAuthInput!) {
  socialAuth(input: $input) {
    social {
      uid
    }
  }
}

Customization

Customize the SocialAuth behavior by subclassing SocialAuthMutation:

import graphene
import graphql_social_auth
from myapp.types import UserType

class CustomSocialAuth(graphql_social_auth.SocialAuthMutation):
    user = graphene.Field(UserType)

    @classmethod
    def resolve(cls, root, info, social, **kwargs):
        # Custom logic here
        return cls(
            social=social,
            user=social.user,
            success=True,
            errors=[]
        )

Contributing

We welcome contributions! Please see our GitHub repository for:

License

This project is licensed under the MIT License - see the LICENSE file for details.

Acknowledgments

This package is a maintained fork of the original django-graphql-social-auth by @flavors.

Special thanks to @omab for Python Social Auth.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_graphene_social_auth-1.1.0.tar.gz (21.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_graphene_social_auth-1.1.0-py2.py3-none-any.whl (14.8 kB view details)

Uploaded Python 2Python 3

File details

Details for the file django_graphene_social_auth-1.1.0.tar.gz.

File metadata

File hashes

Hashes for django_graphene_social_auth-1.1.0.tar.gz
Algorithm Hash digest
SHA256 2428cf6d7b9b0b8740c3cbb3562eed618d5dda14a6572f6cb33e5eae65d7459f
MD5 1d66447a704473e4bdcf345c2ba81475
BLAKE2b-256 c2c13918e0d52cd00cbee7c5201b95ec3a46cd44260487de7a32b3bc58696ff0

See more details on using hashes here.

File details

Details for the file django_graphene_social_auth-1.1.0-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for django_graphene_social_auth-1.1.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 7d46ef9072c578750961cb891cdc0f146ac30ff7391c69b143aeceb559db25f0
MD5 713d58bcab5a02685aa8f2a8acca2079
BLAKE2b-256 2d431d24fcf1300d5a86d50e609be408814a55e3976f1d7a9700fdb4ec191b29

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page