Specify authentication with OpenApiAuthenticationExtension

See original GitHub issue

I’m using TokenAuthentication provided by django-rest-knox and am trying to get it to work with drf-spectacular. I created an OpenApiAuthenticationExtension subclass using your documentation, but am unsure how to register it. Where would I register it so that drf-spectacular finds it? Thanks.

Error:

Warning #0: could not resolve authenticator <class 'knox.auth.TokenAuthentication'>. There was no OpenApiAuthenticationExtension registered for that class. Try creating one by subclassing it. Ignoring for now.

Code:

    # in local file: scheme.py 
    class KnoxTokenScheme(OpenApiAuthenticationExtension):
    target_class = 'knox.auth.TokenAuthentication'
    name = 'knoxTokenAuth'
    match_subclasses = True
    priority = 1

    def get_security_definition(self, auto_schema):
        if self.target.keyword == 'Token':
            return {
                'type': 'http',
                'scheme': 'bearer',
            }
        else:
            return {
                'type': 'apiKey',
                'in': 'header',
                'name': 'Authorization',
                'description': _(
                    'Token-based authentication with required prefix "%s"'
                ) % self.target.keyword
            }

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:12 (6 by maintainers)

github_iconTop GitHub Comments

6reactions
vdomacommented, Mar 20, 2021

Sorry about the delayed response. Here’s what worked for me. I created a file scheme.py (below) in my authentication app and then as suggested, imported it in my views.py. Let me know if you have any questions.

# views.py
from authentication.scheme import KnoxTokenScheme
# scheme.py
from django.utils.translation import gettext_lazy as _
from drf_spectacular.extensions import OpenApiAuthenticationExtension

class KnoxTokenScheme(OpenApiAuthenticationExtension):
    target_class = 'knox.auth.TokenAuthentication'
    name = 'knoxTokenAuth'

    def get_security_definition(self, auto_schema):        
        return {
            'type': 'apiKey',
            'in': 'header',
            'name': 'Authorization',
            'description': _(
                'Token-based authentication with required prefix "%s"'
            ) % "Token"
        }
1reaction
tfranzelcommented, Nov 16, 2022

@arielkaluzhny that is the issue. The extension is not a auth class! Adding it to DEFAULT_AUTHENTICATION_CLASSES will break DRF. Just add your actual TokenAuthentication class there. The extension is not an auth class, merely a description of what to do when the actual auth class (it is referring to) is encountered.

The extension itself does not need to be added anywhere. All you have to do is making sure the python interpreter loads it.

https://drf-spectacular.readthedocs.io/en/latest/customization.html#step-5-extensions

Read more comments on GitHub >

github_iconTop Results From Across the Web

Workflow & schema customization - drf-spectacular
Specify authentication with OpenApiAuthenticationExtension. Authentication classes that do not have 3rd party support will emit warnings and be ignored.
Read more >
DRF spectacular not discovering custom auth extension classes
got to /swagger-ui/ and it works fine. then create a subclass of JWTAuthentication like below: from rest_framework_simplejwt.authentication ...
Read more >
Authentication - Django REST framework
Django, API, REST, Authentication. ... This can be done by specifying the WSGIPassAuthorization directive in the appropriate context and setting it to 'On' ......
Read more >
Authorization header is not sending when using ... - Django.fun
class MyAuthenticationScheme(OpenApiAuthenticationExtension): ... is a copy of the existing bearer/jwt token ones just with my custom prefix specified.
Read more >
flexible OpenAPI 3 schema generation for Django REST - kandi
Django drf-spectacular with FirebaseBackend auth. copy icon. Copy download icon ... Define component schema with drf-spectacular for django API. copy icon.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found