-
Notifications
You must be signed in to change notification settings - Fork 273
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Documentation for Authentication #99
Comments
Apologies - I had accidentally had the suggested |
Hi! that is weird. the As for you other auth class. you can easily write an extension. look at the customization documentation. take the other auth extensions as example. you can have the extension anywhere in your code. it autoregisters the moment the interpreter comes by. the try |
@tfranzel thanks! yes, by subclassing I was able to resolve the issue. I don't know why the I would propose adding an example to the documentation, inside the class MyAuthenticationScheme(OpenApiAuthenticationExtension):
target_class = "my.authentication.MyAuthentication" # full import path OR class ref
name = "MyAuthentication" # custom name for your auth scheme
def get_security_definition(self, auto_schema):
return {
"type": "apiKey",
"in": "header",
"name": "Authorization",
"description": "Value should be formatted: `Token <key>`"
} |
@savitmk it was indeed a bug... small typo 🤦 good idea with the example. i'll add it. thanks. please close if that solves it for you. |
thank you @tfranzel ! |
Hey, |
The extension class need not be added anywhere. you use your custom auth class as normal. The extension class gets auto-loaded once the interpreter parses it. just put it somewhere the interpreter will come by. this should be documented more prominently. there is a info box here though: https://drf-spectacular.readthedocs.io/en/latest/blueprints.html does that make sense? i should write a FAQ entry for this |
Sorry, I haven't understood it yet.
/# has @permission_classes([AllowAny]) to allow post to it without authenification. Problem: Problem 2: So I write a class like this:
It is correct? I don't no exactly what to return. How can I achieve correct lock symbols in my swagger view, and how can I add to each view, that a token in the header is required? |
I also have a similar problem. I want API key auth on by default but I have a few monitoring endpoints that don't require API key auth. How do I disable the lock icon from appearing? @api_view(["GET"])
@permission_classes([])
@extend_schema(auth=[{}])
def alive(request: Request) -> Response:
return Response(status=status.HTTP_200_OK) Still shows the lock icon. |
@revmischa @Wissperwind as far as i can tell, the outcome is correct and as expected. please tell me otherwise. here is the rationale behind it. there is probably a confusion on what the lock symbol means. here is what i understand:
these are the schema variations we may generate:
@revmischa your problem is 1. to fix it you could remove your @Wissperwind: this has very little to do with the extension. the extension merely provides the security definition. the auth classes and permissions on the views determine security section in the schema and thus the shape of the lock. you can test this in https://editor.swagger.io openapi: 3.0.3
info:
title: ''
version: 0.0.0
paths:
/doesitall/:
post:
operationId: customname_create
description: this weird endpoint needs some explaining
summary: short summary
requestBody:
content:
application/json:
schema:
type: string
required: true
security:
- basicAuth: []
- {}
responses:
'201':
description: ''
components:
securitySchemes:
basicAuth:
type: http
scheme: basic |
@tfranzel I am using:
There are no |
Ok, I now better understand the meaning of the lock symbols. Thanks!
Code like this was recommended for custom Authentification methods. But what is the effect? |
@Wissperwind this tells spectacular how your custom auth works and what is expected... header, cookie, etc. if a view has this auth, spectacular wires this with the specification section (at the end) and all is good. If you have a custom auth scheme, spectacular cannot know how it works without that additional info. @revmischa how does your security section look like now with these settings? |
I am following that advice, it works nicely. The problem is that I want to set security to |
ok, just as i suspected. now i understand your problem. let me think about it for a moment. it is a use case i have not anticipated because above advice in itself is already a "hack" and we would need to find a solution that works for all the cases. |
I have the same use case and developed this hack for it: empty = type('empty', (list,), {'__bool__': lambda _: True})()
empty == [] # True
bool(empty) # True You can use it like so: @extend_schema(auth=empty)
def alive(request: Request) -> Response:
return Response(status=status.HTTP_200_OK) Ideally though, |
sry, I kind of forgot about this. @ObserverOfTime the auth=None -> default behavior
auth=['foo'] -> explicit auth method override
auth=[] -> explicit empty list / remove auth methods override the last case is new and removes the security section, which should do the the trick as these 3 variations are functionally identical: I already prepared a commit. no need for a PR. just let me know if that makes sense to you. SIDENOTE: with the current version you could do |
Makes sense to me. |
I am attempting to set up
drf-spectacular
and am struggling to configure authentication.We have extended DRF's
TokenAuthentication
, and it is included in theauthentication_classes
list on the targetAPIView
. We also have another custom authentication class, which does not match anyAuthenticationExtensions
.If
authentication_classes=[SubclassedTokenAuthentication]
only (excluding our other authentication), then the "Authorize" button appears in Swagger. When we add the other authentication class, no "Authorize" button appears in Swagger.Ideally, if any of the list of
authentication_classes
matches one of theAuthenticationExtensions
, then that should be used in the resulting schema. Alternatively, being able to configure via theextend_schema(auth=?)
kwarg would work - but I cannot find documentation for this argument.Thanks for the great library!
The text was updated successfully, but these errors were encountered: