Skip to main content

OAuth2 implementation for Django Rest Framework

Project description

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]

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

drf_oauth2-0.3.tar.gz (10.5 kB view details)

Uploaded Source

Built Distribution

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

drf_oauth2-0.3-py3-none-any.whl (14.6 kB view details)

Uploaded Python 3

File details

Details for the file drf_oauth2-0.3.tar.gz.

File metadata

  • Download URL: drf_oauth2-0.3.tar.gz
  • Upload date:
  • Size: 10.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.6

File hashes

Hashes for drf_oauth2-0.3.tar.gz
Algorithm Hash digest
SHA256 7c845e05b0adcc664a82fb374e9fb218b8f739b11636429c76841756011f7509
MD5 953957793d0d6a91ce5ab48e9e8406c6
BLAKE2b-256 d616eca1c8634d66bc0fa13bc5a508c869b3b2f2a6a6fe83c4c3ea5274be84c9

See more details on using hashes here.

File details

Details for the file drf_oauth2-0.3-py3-none-any.whl.

File metadata

  • Download URL: drf_oauth2-0.3-py3-none-any.whl
  • Upload date:
  • Size: 14.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.6

File hashes

Hashes for drf_oauth2-0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 8729a54fce091329319adfc28ce4283873d30d6604341aca7fe4f0467b59a931
MD5 d3a776d823e7bdd48959524de5551ae5
BLAKE2b-256 e93015436693270e25b499f55584d7e49c84860487017d491b88664d93008b56

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