drf-oauth2
OAuth2 implementation for Django Rest Framework.
Installation
To install the package, use pip:
pip install drf-oauth2
Requirements
- Python >= 3.7
- Django >= 4.0
- djangorestframework >= 3.12
Setup
Add oauth2 to your INSTALLED_APPS in your Django settings:
INSTALLED_APPS = [
# Your apps
'oauth2',
]
Include the oauth2 URLs in your project's urls.py:
from django.urls import path, include
urlpatterns = [
# Your URLs
path('auth/', include('oauth2.urls')),
]
Configuration
Configure your OAuth2 providers in your Django settings:
import os
SOCIAL_AUTH_CONFIG = {
"google": {
"client_id": os.getenv("GOOGLE_CLIENT_ID", ""),
"client_secret": os.getenv("GOOGLE_CLIENT_SECRET", ""),
"redirect_uri": os.getenv("GOOGLE_REDIRECT_URI", ""),
},
"github": {
"client_id": os.getenv("GITHUB_CLIENT_ID", ""),
"client_secret": os.getenv("GITHUB_CLIENT_SECRET", ""),
"redirect_uri": os.getenv("GITHUB_REDIRECT_URI", ""),
},
"facebook": {
"client_id": os.getenv("FACEBOOK_CLIENT_ID", ""),
"client_secret": os.getenv("FACEBOOK_CLIENT_SECRET", ""),
"redirect_uri": os.getenv("FACEBOOK_REDIRECT_URI", ""),
"api_version": os.getenv("FACEBOOK_API_VERSION", "v21.0"),
},
}
Usage
Endpoints
POST /auth/social/register/- Authenticate with a social provider.GET /auth/social/list/- List available OAuth2 providers.
Example Request
Authenticate with a social provider
GET /auth/social/register/
Content-Type: application/json
Request body:
{
"provider": "google"
}
Response body:
{
"success": true,
"message": "Redirecting to provider",
"data": {
"url": "https://github.com/login/oauth/authorize?client_id=CLIENTID&redirect_uri=http://localhost:3001/auth/github/redirect/&scope=user:email"
}
}
POST /auth/social/register/
Content-Type: application/json
Request body:
{
"provider": "google",
"code": "authorization-code"
}
Response body:
{
"success": true,
"message": "Authentication successful",
"data": {
"refresh": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ0b2tlbl90eXBlIjoicmVmcmVzaCIsImV4cCI6MTczODI0MTc0MCwiaWF0IjoxNzM4MTU1MzQwLCJqdGkiOiJiNzU3NWQzNzM1MTg0NDIxYmUzZWVjNmUxZmQwZGJkZiIsInVzZXJfaWQiOjJ9.ZaOaoYgXdaoyyPNWHnywkd97kVA6NwGHTLL2BnIrhQA",
"access": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ0b2tlbl90eXBlIjoiYWNjZXNzIiwiZXhwIjoxNzM4MTU1NjQwLCJpYXQiOjE3MzgxNTUzNDAsImp0aSI6IjUzNzBhODQ4MDk4MzRmMzA5ZjdiZmE0ODgzNzFiZGI1IiwidXNlcl9pZCI6Mn0.4CimC7CL25EFeZyiWVTgqSE-KxTsnnjl_CXSCmPDITc",
"user": 2,
"expired_at": "2025-02-28T12:55:40.058390Z"
}
}
List available providers
GET /auth/social/list/
Response body:
{
"success": true,
"message": "Available providers",
"data": [
"github"
]
}
Development
To install development dependencies, use:
pip install -e .[dev]
Release files for drf-oauth2 0.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| drf_oauth2-0.3.tar.gz | 10.5 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| drf_oauth2-0.3-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 25.1 kB
Release files / drf_oauth2-0.3.tar.gz
| Download URL | drf_oauth2-0.3.tar.gz |
|---|---|
| Size | 10.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
7c845e05b0adcc664a82fb374e9fb218b8f739b11636429c76841756011f7509
|
|
BLAKE2b-256 checksum How to use checksums |
d616eca1c8634d66bc0fa13bc5a508c869b3b2f2a6a6fe83c4c3ea5274be84c9
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.12.6
|
Release files / drf_oauth2-0.3-py3-none-any.whl
| Download URL | drf_oauth2-0.3-py3-none-any.whl |
|---|---|
| Size | 14.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
8729a54fce091329319adfc28ce4283873d30d6604341aca7fe4f0467b59a931
|
|
BLAKE2b-256 checksum How to use checksums |
e93015436693270e25b499f55584d7e49c84860487017d491b88664d93008b56
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.12.6
|