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.1.tar.gz (31.0 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.1-py2.py3-none-any.whl (15.4 kB view details)

Uploaded Python 2Python 3

File details

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

File metadata

File hashes

Hashes for django_graphene_social_auth-1.1.1.tar.gz
Algorithm Hash digest
SHA256 b8694643e913476a732b40a07328e69e2ee33b6fca16991b62e86038d3a7a042
MD5 c5d2559b535dc67cca8e9bf1347be518
BLAKE2b-256 a362ebf44754ed1fb8d8f9f14f67922d6e57c6fd371a965a0d9fa412307a9350

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for django_graphene_social_auth-1.1.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 5b2a6d524bfd8803bf0e28edcd84809dde840e2510faccef9c320c081efceed1
MD5 03c7264242dc683ea47f6ebe1ab6d24b
BLAKE2b-256 d5eeaca8e90965da591fd89b2ce031ef552c9b12275bb7b515dcee01fb758e49

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