Metadata-Version: 2.4
Name: mad_oauth2
Version: 3.0.4
Summary: A Django app extension of django-oauth-toolkit that implements scope based applications.
Home-page: https://www.madithouse.com/
Author: Haseeb Ur Rehman
Author-email: haseeb@madithouse.com
License: Other/Proprietary License
Classifier: Environment :: Web Environment
Classifier: Framework :: Django
Classifier: Intended Audience :: Developers
Classifier: License :: Other/Proprietary License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.6
Classifier: Topic :: Internet :: WWW/HTTP
Classifier: Topic :: Internet :: WWW/HTTP :: Dynamic Content
Requires-Python: >=3.6
Description-Content-Type: text/markdown
License-File: LICENSE.md
Requires-Dist: django>=4.2.6
Requires-Dist: djangorestframework>=3.14.0
Requires-Dist: celery>=5.3.4
Requires-Dist: django-celery-beat>=2.5.0
Requires-Dist: django-filter>=23.3
Requires-Dist: django-oauth-toolkit>=2.4.0
Requires-Dist: django-multiselectfield>=0.1.12
Dynamic: license-file

# 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:

    ```python
    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

```python
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

```python
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
