Skip to main content

A Django app extension of django-oauth-toolkit that implements scope based applications.

Project description

Mad Oauth2

Mad Oauth2 app is an extension of django-oauth-toolkit that implements scopes and schemes in detail

Quick start

  1. Add "mad_oauth2" to your INSTALLED_APPS setting like this:

    INSTALLED_APPS = [
        ...
        'oauth2_provider',
        'mad_oauth2',
        ...
    ]
    
    REST_FRAMEWORK = {
        ...
        "DEFAULT_PERMISSION_CLASSES": (
            "oauth2_provider.contrib.rest_framework.TokenMatchesOASRequirements",
            # OR
            "oauth2_provider.contrib.rest_framework.TokenHasResourceScope",
        ),
        'DEFAULT_THROTTLE_CLASSES': [
            'mad_oauth2.throttling.BaseScopedRateThrottle'
        ],
        ...
    }
    
    OAUTH2_PROVIDER_APPLICATION_MODEL="mad_oauth2.Application"
    OAUTH2_PROVIDER = {
        "SCOPES_BACKEND_CLASS": "mad_oauth2.oauth2.ApplicationScopes",
        "APPLICATION_ADMIN_CLASS": "mad_oauth2.admin.ApplicationAdminClass",
    }
    
  2. Run python manage.py migrate to create mad_oauth2 models.

Important Note

First add scopes and throttles from the admin, then mark the view with the respective scope. Changes will take effect once the code is deployed(redeployed/rebuild).

Clearing Expired Tokens

Run celery periodic task to clear expired tokens mad_oauth2.tasks.removeExpiredTokens

Restricting Views with Scopes

this_view = "user"
throttle_scope = this_view
required_alternate_scopes = mad_oauth2.utils.requiredScopesForView(this_view)

Visit the official documentation for django-oauth-toolkit for more details on this:

https://django-oauth-toolkit.readthedocs.io/en/latest/rest-framework/permissions.html

Throttling Requests

REST_FRAMEWORK = {
    ...
    'DEFAULT_THROTTLE_CLASSES': [
        'mad_oauth2.throttling.BaseScopedRateThrottle',
    ],
    ...
}

Visit the official documentation for djangorestframework for more details this:

https://www.django-rest-framework.org/api-guide/throttling/#scopedratethrottle

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

mad_oauth2-4.0.0.tar.gz (9.2 kB view details)

Uploaded Source

Built Distribution

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

mad_oauth2-4.0.0-py3-none-any.whl (10.6 kB view details)

Uploaded Python 3

File details

Details for the file mad_oauth2-4.0.0.tar.gz.

File metadata

  • Download URL: mad_oauth2-4.0.0.tar.gz
  • Upload date:
  • Size: 9.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.5

File hashes

Hashes for mad_oauth2-4.0.0.tar.gz
Algorithm Hash digest
SHA256 d4dc32520970147ec6221f15b92d91848d6f8b08086422aba1e34bdba28eae98
MD5 6379b9af0315fb9d0b17ac10b0758132
BLAKE2b-256 fc3cb38ea2d1a68311379944744b2b20aae0aa78b2769dca2600c12f6af11433

See more details on using hashes here.

File details

Details for the file mad_oauth2-4.0.0-py3-none-any.whl.

File metadata

  • Download URL: mad_oauth2-4.0.0-py3-none-any.whl
  • Upload date:
  • Size: 10.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.5

File hashes

Hashes for mad_oauth2-4.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3a0e8f46f39427a7ac4cb215e60ff0cfe35193655b4931c20631a75ff9d18cfd
MD5 fd556fcd686ee7f8dabf84a6bfc279ca
BLAKE2b-256 960e876cc5e741bedeba3e9f3e0f6e6434aad3689f58e31e2b8f05a9e8e539a2

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