Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Autoload drf simple access key in drf spectacular #6

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 10 additions & 15 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,20 @@ jobs:
fail-fast: false
matrix:
python-version:
- '3.6'
- '3.7'
- '3.8'
- '3.9'
- '3.10'
- 'pypy3'
django-version:
- '2.2'
- '3.1'
- '3.2'
djangorestframework-version:
- '3.10'
- '3.11'
- '3.12'
django-version:
- '4.2'
- '5.0'
djangorestframework-version:
- '3.15'
drf-spectacular-version:
- '0.27.2'
exclude:
- django-version: '3.1'
djangorestframework-version: '3.10'
- django-version: '3.2'
djangorestframework-version: '3.10'

- django-version: '5.0'
python-version: '3.9'
steps:
- uses: actions/checkout@v2

Expand All @@ -42,6 +36,7 @@ jobs:
pip install -r requirements.txt
pip install django~=${{ matrix.django-version }}.0
pip install djangorestframework~=${{ matrix.djangorestframework-version }}.0
pip install drf-spectacular~=${{ matrix.drf-spectacular-version }}

- name: Lint with flake8
run: |
Expand Down
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
repos:
- repo: https://github.com/pycqa/isort
rev: 5.10.1
rev: 5.12.0
hooks:
- id: isort
args: [ "--profile", "black", "--filter-files" ]

- repo: https://github.com/psf/black
rev: 22.6.0 # Replace by any tag/version: https://github.com/psf/black/tags
rev: 23.3.0 # Replace by any tag/version: https://github.com/psf/black/tags
hooks:
- id: black
language_version: python3 # Should be a command that runs python3.6.2+
39 changes: 39 additions & 0 deletions drf_simple_access_key/authentication.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from django.conf import settings
from django.contrib.auth.models import AnonymousUser
from rest_framework.authentication import BaseAuthentication


class SimpleAccessKeyAuthentication(BaseAuthentication):
"""
This is an authentication that is assigned to endpoint for OpenAPI generation when using the
SimpleAccessKey permission.

The authentication returns an AnonymousUser, as the checks are done in the permission class.
"""

def authenticate(self, request):
return AnonymousUser(), None


try:
from drf_spectacular.extensions import OpenApiAuthenticationExtension
from drf_spectacular.plumbing import build_bearer_security_scheme_object

class SimpleAccessKeyAuthenticationScheme(OpenApiAuthenticationExtension):
target_class = (
"drf_simple_access_key.authentication.SimpleAccessKeyAuthentication"
)
name = "SimpleAccessKey"

def get_security_definition(self, auto_schema):
return build_bearer_security_scheme_object(
header_name=settings.SIMPLE_ACCESS_KEY_SETTINGS[
"HTTP_AUTHORIZATION_HEADER"
],
token_prefix=settings.SIMPLE_ACCESS_KEY_SETTINGS[
"HTTP_AUTHORIZATION_SCHEME"
],
)

except ImportError:
pass
4 changes: 2 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
-e .

# Development dependencies
flake8>=3.9,<3.10
codecov>=2.1,<2.2
flake8>=5.0
codecov>=2.1
setuptools>=42
wheel>=0.37
twine>=3.4
Expand Down
14 changes: 7 additions & 7 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,22 @@
author="Harald Nezbeda",
author_email="[email protected]",
install_requires=[
"django>=2.2",
"djangorestframework>=3.10",
"django>=4.2",
"djangorestframework>=3.11",
],
classifiers=[
"License :: OSI Approved :: MIT License",
"Framework :: Django",
"Framework :: Django :: 2.2",
"Framework :: Django :: 3.1",
"Framework :: Django :: 3.2",
"Framework :: Django :: 4.2",
"Framework :: Django :: 5.0",
"Framework :: Django :: 5.1",
"Programming Language :: Python",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.6",
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
"Programming Language :: Python :: Implementation :: CPython",
"Programming Language :: Python :: Implementation :: PyPy",
],
Expand Down
23 changes: 23 additions & 0 deletions tests/core/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,26 @@
"drf_simple_access_key.SimpleAccessKey",
],
}


# NOTE: at this moment drf_spectacular isn't a requirement of drf_simple_access_key.
# thus in a try/except block.
try:
import drf_spectacular

INSTALLED_APPS.append("drf_spectacular")

REST_FRAMEWORK["DEFAULT_SCHEMA_CLASS"] = "drf_spectacular.openapi.AutoSchema"

SPECTACULAR_SETTINGS = {
"TITLE": "Your Project API",
"DESCRIPTION": "Your project description",
"VERSION": "1.0.0",
"SERVE_INCLUDE_SCHEMA": False,
"AUTHENTICATION_WHITELIST": [
"drf_simple_access_key.authentication.SimpleAccessKeyAuthentication",
],
}

except ImportError:
pass
2 changes: 2 additions & 0 deletions tests/core/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""

from django.urls import include, path
from rest_framework import routers
from testapp import views

router = routers.DefaultRouter()
router.register(r"books", views.BookViewSet)
router.register(r"private-books", views.PrivateBookViewSet)

urlpatterns = [
path("api/", include(router.urls)),
Expand Down
27 changes: 27 additions & 0 deletions tests/testapp/migrations/0002_privatebook.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Generated by Django 5.1.2 on 2024-10-11 13:01

from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("testapp", "0001_initial"),
]

operations = [
migrations.CreateModel(
name="PrivateBook",
fields=[
(
"id",
models.AutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
("title", models.CharField(max_length=50)),
],
),
]
7 changes: 7 additions & 0 deletions tests/testapp/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,14 @@


class Book(models.Model):
title = models.CharField(
max_length=50,
blank=False,
null=False,
)


class PrivateBook(models.Model):
title = models.CharField(
max_length=50,
blank=False,
Expand Down
11 changes: 10 additions & 1 deletion tests/testapp/serializers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from rest_framework import serializers

from .models import Book
from .models import Book, PrivateBook


class BookSerializer(serializers.ModelSerializer):
Expand All @@ -10,3 +10,12 @@ class Meta:
"pk",
"title",
]


class PrivateBookSerializer(serializers.ModelSerializer):
class Meta:
model = PrivateBook
fields = [
"pk",
"title",
]
14 changes: 12 additions & 2 deletions tests/testapp/views.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
from rest_framework import viewsets

from .models import Book
from .serializers import BookSerializer
from drf_simple_access_key import SimpleAccessKey
from drf_simple_access_key.authentication import SimpleAccessKeyAuthentication

from .models import Book, PrivateBook
from .serializers import BookSerializer, PrivateBookSerializer


class BookViewSet(viewsets.ModelViewSet):
queryset = Book.objects.all()
serializer_class = BookSerializer


class PrivateBookViewSet(viewsets.ModelViewSet):
queryset = PrivateBook.objects.all()
serializer_class = PrivateBookSerializer
authentication_classes = [SimpleAccessKeyAuthentication]
permission_classes = [SimpleAccessKey]
Loading