-
Notifications
You must be signed in to change notification settings - Fork 270
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add auth extension for django-rest-knox
- Loading branch information
1 parent
22b78d6
commit 624c9b9
Showing
7 changed files
with
107 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,4 +11,5 @@ | |
'rest_framework_recursive', | ||
'rest_framework_gis', | ||
'pydantic', | ||
'knox_auth_token', | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
from drf_spectacular.extensions import OpenApiAuthenticationExtension | ||
from drf_spectacular.plumbing import build_bearer_security_scheme_object | ||
|
||
|
||
class KnoxTokenScheme(OpenApiAuthenticationExtension): | ||
target_class = 'knox.auth.TokenAuthentication' | ||
name = 'knoxApiToken' | ||
|
||
def get_security_definition(self, auto_schema): | ||
return build_bearer_security_scheme_object( | ||
header_name='Authorization', | ||
token_prefix=self.target.authenticate_header(""), | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
from unittest import mock | ||
|
||
import pytest | ||
from rest_framework import mixins, routers, serializers, viewsets | ||
|
||
from tests import assert_schema, generate_schema | ||
|
||
try: | ||
from knox.auth import TokenAuthentication | ||
except ImportError: | ||
TokenAuthentication = None | ||
|
||
|
||
class XSerializer(serializers.Serializer): | ||
uuid = serializers.UUIDField() | ||
|
||
|
||
class XViewset(mixins.ListModelMixin, viewsets.GenericViewSet): | ||
serializer_class = XSerializer | ||
authentication_classes = [TokenAuthentication] | ||
required_scopes = ['x:read', 'x:write'] | ||
|
||
|
||
@pytest.mark.contrib('knox_auth_token') | ||
def test_knox_auth_token(no_warnings): | ||
router = routers.SimpleRouter() | ||
router.register('x', XViewset, basename="x") | ||
|
||
urlpatterns = [ | ||
*router.urls | ||
] | ||
|
||
schema = generate_schema(None, patterns=urlpatterns) | ||
|
||
assert_schema(schema, 'tests/contrib/test_knox_auth_token.yml') | ||
|
||
|
||
@pytest.mark.contrib('knox_auth_token') | ||
@mock.patch('knox.settings.knox_settings.AUTH_HEADER_PREFIX', 'CustomPrefix') | ||
def test_knox_auth_token_non_default_prefix(no_warnings): | ||
schema = generate_schema('/x', XViewset) | ||
assert schema['components']['securitySchemes'] == { | ||
'knoxApiToken': { | ||
'type': 'apiKey', | ||
'in': 'header', | ||
'name': 'Authorization', | ||
'description': 'Token-based authentication with required prefix "CustomPrefix"' | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
openapi: 3.0.3 | ||
info: | ||
title: '' | ||
version: 0.0.0 | ||
paths: | ||
/x/: | ||
get: | ||
operationId: x_list | ||
tags: | ||
- x | ||
security: | ||
- knoxApiToken: [] | ||
- {} | ||
responses: | ||
'200': | ||
content: | ||
application/json: | ||
schema: | ||
type: array | ||
items: | ||
$ref: '#/components/schemas/X' | ||
description: '' | ||
components: | ||
schemas: | ||
X: | ||
type: object | ||
properties: | ||
uuid: | ||
type: string | ||
format: uuid | ||
required: | ||
- uuid | ||
securitySchemes: | ||
knoxApiToken: | ||
type: apiKey | ||
in: header | ||
name: Authorization | ||
description: Token-based authentication with required prefix "Token" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters