-
Notifications
You must be signed in to change notification settings - Fork 270
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
Specify authentication with OpenApiAuthenticationExtension #264
Comments
https://drf-spectacular.readthedocs.io/en/latest/blueprints.html the info box describes this. maybe i should also put that info box on the customization page. point is that the interpreter needs to load it. import that file ( |
and by the way, if you are happy with your extensions i would appreciate a PR for that lib as it has been on the short list for a while. |
Thanks - that helped. Let me do a bit more analysis, and present my findings and we can decide if a PR is required. I will reply back soon. |
I got an error pasting the example
|
@vdoma any updates here or should i close the issue? |
Sorry about the delayed response. # 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"
} |
Hello! I am trying to do something very similar and have basically copied this code but am getting the error:
Anyone have any thoughts on this? Thank you in advance! |
not sure how that is even possible with above drf-spectacular/drf_spectacular/plumbing.py Line 696 in 01b23b9
|
@tfranzel - Nope. Here's the code: # scheme.py
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": "Knox Authorization",
"description": "Token-based authentication with required prefix 'Token'",
} # settings.py
REST_FRAMEWORK = {
"DEFAULT_RENDERER_CLASSES": ["rest_framework.renderers.JSONRenderer"],
"DEFAULT_AUTHENTICATION_CLASSES": [
"backend.scheme.KnoxTokenScheme",
"rest_framework.authentication.SessionAuthentication", # For the swagger API
],
'DEFAULT_SCHEMA_CLASS': 'drf_spectacular.openapi.AutoSchema',
} Hypothetically, I would actually use my own Custom Token Authentication class which subclasses I assume it's something to do with using it in |
@arielkaluzhny that is the issue. The extension is not a auth class! Adding it to 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 |
Thank you @tfranzel, it appears to be working (or at least not failing & giving errors)! I'm not convinced I am actually doing what I want here though. It appears that the It may be that I just need to play around with this more and dig into the documentation. Maybe I need to change what |
means that the extension class
no, that is not how this works. The do this: # settings.py
REST_FRAMEWORK = {
"DEFAULT_AUTHENTICATION_CLASSES": ["knox.auth.TokenAuthentication"],
'DEFAULT_SCHEMA_CLASS': 'drf_spectacular.openapi.AutoSchema',
}
# schema.py
# put your extensions here as is. no fiddling with get_security_definition required
# YOURAPP/apps.py
# only add the ready function with the import.
class YOURAPPConfig(AppConfig):
default_auto_field = "django.db.models.BigAutoField"
name = "YOURAPP"
def ready(self):
# import above schema.py
import YOURAPP.schema # noqa: E402 The last part will make sure your extensions are properly loaded. This is a natural thing for Celery users, but I learned the hard way that not every Django/DRF user is familiar with this idiom. Will add this example to the doc also. |
Not sure exactly what happened here, but in my case, using drf_spectacular: # settings.py
REST_FRAMEWORK = {
"DEFAULT_AUTHENTICATION_CLASSES": ("knox.auth.TokenAuthentication",),
"DEFAULT_SCHEMA_CLASS": "drf_spectacular.openapi.AutoSchema",
# ...
}
SPECTACULAR_SETTINGS = {
"COMPONENT_SPLIT_REQUEST": True,
"SCHEMA_COERCE_PATH_PK_SUFFIX": False,
"SCHEMA_PATH_PREFIX": r"(/api/admin/)|(/api/)",
"SWAGGER_UI_SETTINGS": {
"persistAuthorization": True,
},
# other than that - only name, description
} # scheme.py
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": "Knox Authorization", # <----------- (??)
"description": "Token-based authentication with required prefix 'Token'",
} Did not work until I have changed |
@janlis-ff I believe in that context |
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:
Code:
The text was updated successfully, but these errors were encountered: