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.2.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.2-py3-none-any.whl (14.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: drf_oauth2-0.2.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.2.tar.gz
Algorithm Hash digest
SHA256 6717e53fc3810e16060be9971875c131e08001f706372625ce8e05dd292963e8
MD5 f59919894384c5df6a7b364ac8b4f2d4
BLAKE2b-256 aa86b55a805cdbd31493e16df25073bf5e90f7c728a1941022a88a7f51aaefb4

See more details on using hashes here.

File details

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

File metadata

  • Download URL: drf_oauth2-0.2-py3-none-any.whl
  • Upload date:
  • Size: 14.5 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.2-py3-none-any.whl
Algorithm Hash digest
SHA256 6f69e40e444d2d19aff509b9070f0cc75a294ecab72635c147652e9a1b66e42f
MD5 f2ca75a6171611b9201b705fa1f36063
BLAKE2b-256 83c66a0b4af3ea9867aa51c86ec17e31c03e5a450c2d239a948559f991bd75d5

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