Specify authentication with OpenApiAuthenticationExtension
See original GitHub issueI’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:
- Created 3 years ago
- Comments:12 (6 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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.
@arielkaluzhny that is the issue. The extension is not a auth class! Adding it to
DEFAULT_AUTHENTICATION_CLASSESwill break DRF. Just add your actualTokenAuthenticationclass 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