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

Uploaded Python 3

File details

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

File metadata

  • Download URL: drf_oauth2-0.1.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.1.tar.gz
Algorithm Hash digest
SHA256 dc859eff05aa14d9c142a6f0dabb506ef20a1b97fc8461525bec3264d76962d3
MD5 9447018f73ba806a5897800891e750f1
BLAKE2b-256 e10443bb18c41aa05c86ad70587e3a453f63411aef0df3c4bf8c5369a1eccafb

See more details on using hashes here.

File details

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

File metadata

  • Download URL: drf_oauth2-0.1-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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 cd3dc98f225330572c005542712f31cee986dd3f66c60224f1ba10cc7f0f8642
MD5 717093a11b5c5017d77b33dcef37382f
BLAKE2b-256 a6977787144cf9283cc6083a74168e9e35939998cf58181ba378121af9622ab3

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