Comprehensive Django REST Framework library for OAuth social login
Project description
README.md
DRF Social OAuth
A highly extensible Django REST Framework library for OAuth social login with customizable providers and handlers.
✨ Features
- 🔐 Multiple OAuth Providers: Google, Facebook, GitHub, Twitter out of the box
- 🎛️ Highly Customizable: Custom providers, handlers, and response formats
- 🏗️ DRF Native: Built with ViewSets, Serializers, and proper REST patterns
- 🔑 Multiple Auth Types: JWT, Sessions, DRF Tokens supported
- 🛡️ Security First: CSRF protection, proper error handling, secure defaults
- 📊 Admin Integration: Django admin interface for social accounts
- 🧪 Well Tested: Comprehensive test suite with high coverage
- 📚 Great Documentation: Detailed docs with examples
🚀 Quick Start
Installation
pip install drf-oauth2-tools
Basic Setup
- Add to your
INSTALLED_APPS:
INSTALLED_APPS = [
# ... other apps
'rest_framework',
'drf_oauth2',
]
- Configure OAuth providers in
settings.py:
OAUTH_PROVIDERS = {
'GOOGLE': {
'CLIENT_ID': 'your-google-client-id',
'CLIENT_SECRET': 'your-google-client-secret',
},
'GITHUB': {
'CLIENT_ID': 'your-github-client-id',
'CLIENT_SECRET': 'your-github-client-secret',
},
}
- Add URLs to your
urls.py:
from django.urls import path, include
urlpatterns = [
path('api/auth/', include('drf_oauth2.urls')),
]
- Run migrations:
python manage.py migrate
🎯 Usage
API Endpoints
GET /api/auth/oauth/login/google/ # Initiate Google OAuth
GET /api/auth/oauth/callback/google/ # Handle OAuth callback
Frontend Integration
// Get authorization URL
const response = await fetch('/api/auth/oauth/login/google/');
const data = await response.json();
// Redirect user to OAuth provider
window.location.href = data.authorization_url;
// After callback, you'll receive JWT tokens
🔧 Advanced Configuration
Custom Callback Handler
from drf_oauth2.handlers import BaseCallbackHandler
class CustomHandler(BaseCallbackHandler):
def handle_callback(self, user_info, tokens, provider, request=None):
user = self.get_or_create_user(user_info, provider)
return {
'success': True,
'user_id': user.id,
'custom_data': 'your custom response'
}
# Configure in settings
OAUTH_PROVIDERS = {
'GOOGLE': {
'CLIENT_ID': 'your-client-id',
'CLIENT_SECRET': 'your-client-secret',
},
"CALLBACK_HANDLER_CLASS": 'myapp.handlers.CustomHandler',
}
Custom OAuth Provider
from drf_oauth2.providers import BaseOAuthProvider, register_provider
class LinkedInProvider(BaseOAuthProvider):
PROVIDER = "linkedin"
AUTHORIZATION_URL = "https://www.linkedin.com/oauth/v2/authorization"
# ... implement other required methods
# Configure in settings
OAUTH_PROVIDERS = {
'LINKEDIN': {
'CLIENT_ID': 'your-client-id',
'CLIENT_SECRET': 'your-client-secret',
"PROVIDER_CLASS": "myapp.providers.linkedin.LinkedInProvider'
},
}
📋 Supported Providers
- Google -
google - Facebook -
facebook - GitHub -
github - Twitter -
twitter - Custom providers - Easy to add
🔐 Supported Authentication Types
- JWT Tokens (via
djangorestframework-simplejwt) (DEFAULT) - Django Sessions
- DRF Tokens
- Custom handlers
📝 License
This project is licensed under the MIT License - see the LICENSE file for details.
🙏 Acknowledgments
- Django REST Framework team
- OAuth provider documentation
- Contributors and users
Made with ❤️ by AstralMortem
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file drf_oauth2_tools-1.0.1.tar.gz.
File metadata
- Download URL: drf_oauth2_tools-1.0.1.tar.gz
- Upload date:
- Size: 10.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bbcc2d3b8ff7d6c0779989a8fb823dfba8aa443091f0c64a13915a37b7a4fc7f
|
|
| MD5 |
f969e200f818fc3e3c3b40cc005c3eac
|
|
| BLAKE2b-256 |
bccff22289b55576927880b32c976ac514a44eaf2f7e9fd97a640dcfa9dccc0c
|
Provenance
The following attestation bundles were made for drf_oauth2_tools-1.0.1.tar.gz:
Publisher:
release.yaml on AstralMortem/drf-oauth2-tools
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
drf_oauth2_tools-1.0.1.tar.gz -
Subject digest:
bbcc2d3b8ff7d6c0779989a8fb823dfba8aa443091f0c64a13915a37b7a4fc7f - Sigstore transparency entry: 336255705
- Sigstore integration time:
-
Permalink:
AstralMortem/drf-oauth2-tools@1ff68993e6ce5b2b8fbbeabb4a6a29c1a103de56 -
Branch / Tag:
refs/tags/v1.0.1 - Owner: https://github.com/AstralMortem
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yaml@1ff68993e6ce5b2b8fbbeabb4a6a29c1a103de56 -
Trigger Event:
push
-
Statement type:
File details
Details for the file drf_oauth2_tools-1.0.1-py3-none-any.whl.
File metadata
- Download URL: drf_oauth2_tools-1.0.1-py3-none-any.whl
- Upload date:
- Size: 14.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4952bb7c46f59f4b80018b9bf1f3f32078ef4b1a0abc5aadfd8f6d7739a399e8
|
|
| MD5 |
44ea492045be6b5dd0af12769bde53bf
|
|
| BLAKE2b-256 |
a8332d40a96ee83ab3a8e0b5e70093adeab53630114347dc462213618755bfa2
|
Provenance
The following attestation bundles were made for drf_oauth2_tools-1.0.1-py3-none-any.whl:
Publisher:
release.yaml on AstralMortem/drf-oauth2-tools
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
drf_oauth2_tools-1.0.1-py3-none-any.whl -
Subject digest:
4952bb7c46f59f4b80018b9bf1f3f32078ef4b1a0abc5aadfd8f6d7739a399e8 - Sigstore transparency entry: 336255708
- Sigstore integration time:
-
Permalink:
AstralMortem/drf-oauth2-tools@1ff68993e6ce5b2b8fbbeabb4a6a29c1a103de56 -
Branch / Tag:
refs/tags/v1.0.1 - Owner: https://github.com/AstralMortem
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yaml@1ff68993e6ce5b2b8fbbeabb4a6a29c1a103de56 -
Trigger Event:
push
-
Statement type: