Skip to main content

DRF OAuth Toolkit

CI Tests PyPI License: MIT Python Version

DRF OAuth Toolkit Logo

drf-oauth-toolkit is a flexible and lightweight OAuth2 integration library for Django Rest Framework (DRF), designed to simplify the process of adding OAuth2 support to your DRF projects.

🚀 Features

  • ✅ Plug-and-play OAuth2 integration for DRF
  • ✅ Supports multiple OAuth providers (Google, Facebook, etc.)
  • ✅ Built-in token management and CSRF protection
  • ✅ Customizable and extensible service classes

📦 Installation

pip install drf-oauth-toolkit

Add drf_oauth_toolkit to your settings.py:

INSTALLED_APPS = [
    ...
    "drf_oauth_toolkit",
]

📖 Usage

  1. Configure OAuth credentials in your Django settings:
OAUTH_CREDENTIALS = {
    "host": "http://127.0.0.1:8000",
    "encryption_key": "Xcub4xIr71H3VR_PxDQdXAT39H9lM9nE2A0GQBg38Xo=",
    "google": {
        "client_id": "your google client id",
        "client_secret": "your google client secert",
        "callback_url": "google-callback",
    },
}
  1. Extend the base service class to integrate a provider:
from drf_oauth_toolkit.views.google_views import GoogleOAuthCallbackApi, GoogleOAuthRedirectApi

urlpatterns = [
    ...
    path("oauth2/google/login", GoogleOAuthRedirectApi.as_view(), name="google-login"),
    path("oauth2/google/callback", GoogleOAuthCallbackApi.as_view(), name="google-callback"), # sure the url name matches the one in the settings
]

🎯 Inspiration

In my experience, OAuth integration has become a standard requirement across modern software projects. Whether you're integrating with Google, Facebook, or other OAuth providers, there are already some outstanding libraries available for Django and Django Rest Framework, such as:

However, the challenge I aim to solve with drf-oauth-toolkit is the ease of use and flexibility. Many existing solutions assume a rigid workflow for how you should handle tokens and user management, often leading to challenges when working with DRF or when the library doesn’t align with your token handling requirements.

✅ Key Problems Addressed:

  • Complex Setup: Some libraries require extensive setup with limited flexibility.
  • Token Management Assumptions: Many solutions assume how tokens should be stored and used, which may not fit every project.
  • DRF Integration: Some packages aren't well-optimized for Django Rest Framework out of the box.

🌟 Solution and Design Philosophy

drf-oauth-toolkit aims to simplify the OAuth integration process while maintaining full control and flexibility over how tokens are stored, validated, and extended. The core design principle is customizability — you can override and extend methods according to your project’s needs.

Example: Overriding Token Storage Logic

If you simply want to store tokens after a successful OAuth flow, you should not need to do anything, but in case you have a custom user model you can use something like below example:

from django.contrib.auth import get_user_model

from drf_oauth_toolkit.models import OAuth2Token, ServiceChoices
from drf_oauth_toolkit.services.google import GoogleOAuthService
from drf_oauth_toolkit.views.base import OAuthCallbackApiBase

User = get_user_model()

class GoogleOAuthCallbackApi(OAuthCallbackApiBase):
    oauth_service_class = GoogleOAuthService
    session_state_key = "google_oauth_state"
    user_info_email_field = "email"
    user_info_first_name_field = "given_name"
    user_info_last_name_field = "family_name"

    def update_account(self, user, oauth_tokens):
        """
        Update or create a user account with the given OAuth tokens.
        """
        if user is None:
            user_info = self.oauth_service_class().get_user_info(oauth_tokens=oauth_tokens)

            user = self.create_user_from_oauth(user_info)

        OAuth2Token.objects.update_or_create_token(
            user=user, service_name=ServiceChoices.GOOGLE, oauth_tokens=oauth_tokens
        )

    def create_user_from_oauth(self, user_info):
        """
        Create a new user based on the information retrieved from the OAuth service.
        """
        email = user_info.get(self.user_info_email_field)
        first_name = user_info.get(self.user_info_first_name_field, "")
        last_name = user_info.get(self.user_info_last_name_field, "")

        user, _ = User.objects.get_or_create(
            email=email,
            defaults={
                "first_name": first_name,
                "last_name": last_name,
                "username": email.split("@")[0],
            },
        )
        return user

🎯 Extending for Specialized Use Cases

You can easily extend the base service for different OAuth providers. For example, handling YouTube OAuth integration:

class YouTubeOAuthService(GoogleOAuthService):
    API_URI_NAME = "youtube_callback"
    SCOPES = [
        "https://www.googleapis.com/auth/youtube.readonly",
        "https://www.googleapis.com/auth/userinfo.email",
    ]

    def fetch_channel_info(self, oauth_tokens: OAuthTokens) -> Dict[str, Any]:
        self._ensure_valid_token(oauth_tokens)
        headers = {"Authorization": f"Bearer {oauth_tokens.access_token}"}
        response = requests.get(
            "https://www.googleapis.com/youtube/v3/channels",
            headers=headers,
            params={"part": "snippet,contentDetails,statistics", "mine": "true"},
        )
        response.raise_for_status()
        return response.json()

🎯 Next Steps and Enhancements

  • Improved Documentation: Adding more inline code documentation and examples for clarity.
  • Expanded Provider Support: Adding support for additional OAuth providers like Facebook and Microsoft.
  • Enhanced Token Management: Providing built-in support for token rotation and expiration handling.

✅ Running Tests

Run tests using pytest:

pytest

🤝 Contributing

Contributions are welcome! If you'd like to contribute:

  • Fork the repository
  • Create a feature branch (git checkout -b feature-branch)
  • Commit your changes (git commit -m 'Add feature')
  • Push the branch (git push origin feature-branch)
  • Open a pull request

For major changes, please open an issue first to discuss your ideas.


📄 License

This project is licensed under the MIT License. See the LICENSE file for more details.


📫 Contact

For questions and suggestions, feel free to reach out via GitHub Issues.

Metadata

Release files for drf-oauth-toolkit 0.1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for drf-oauth-toolkit 0.1.2
File Size Uploaded Provenance Actions
drf_oauth_toolkit-0.1.2.tar.gz 16.8 kB Without provenance Details for drf_oauth_toolkit-0.1.2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for drf-oauth-toolkit 0.1.2
File Interpreter ABI Platform Actions
drf_oauth_toolkit-0.1.2-py3-none-any.whl Python 3 none any Details for drf_oauth_toolkit-0.1.2-py3-none-any.whl

Total release size: 36.6 kB

Release files / drf_oauth_toolkit-0.1.2.tar.gz

Download URL drf_oauth_toolkit-0.1.2.tar.gz
Size 16.8 kB
Tags Source
SHA-256 checksum
How to use checksums
86af77fd545d45e5038c9eddf4753a11d706a5d1e7aec7db4d098e47f82b55d7
BLAKE2b-256 checksum
How to use checksums
002fca0750444a2b818e56aa84e436261c74345dfc2282f55c29d8c6f3fd6c5b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.5 CPython/3.13.1 Darwin/24.1.0

Release files / drf_oauth_toolkit-0.1.2-py3-none-any.whl

Download URL drf_oauth_toolkit-0.1.2-py3-none-any.whl
Size 19.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
cc77944992c9587e4df80ef88a9ff49b0ab97bf169fe7379825ccf6dc34a93e1
BLAKE2b-256 checksum
How to use checksums
af7635f4bc55e1813f9ad6e06e742282035af91a812228f3941365b171686fda
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.5 CPython/3.13.1 Darwin/24.1.0

Security for drf-oauth-toolkit 0.1.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.2 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page