@@ -65,6 +65,14 @@ def authenticate(self, request, token):
65
65
raise AuthorizationError
66
66
67
67
68
+ class AsyncBearerAuth (HttpBearer ):
69
+ async def authenticate (self , request , token ):
70
+ if token == "bearertoken" :
71
+ return token
72
+ if token == "nottherightone" :
73
+ raise AuthorizationError
74
+
75
+
68
76
def demo_operation (request ):
69
77
return {"auth" : request .auth }
70
78
@@ -87,6 +95,7 @@ def on_custom_error(request, exc):
87
95
("apikeycookie" , KeyCookie ()),
88
96
("basic" , BasicAuth ()),
89
97
("bearer" , BearerAuth ()),
98
+ ("async_bearer" , AsyncBearerAuth ()),
90
99
("customexception" , KeyHeaderCustomException ()),
91
100
]:
92
101
api .get (f"/{ path } " , auth = auth , operation_id = path )(demo_operation )
@@ -216,6 +225,18 @@ class MockStaffUser(str):
216
225
401 ,
217
226
BODY_UNAUTHORIZED_DEFAULT ,
218
227
),
228
+ (
229
+ "/async_bearer" ,
230
+ dict (headers = {"Authorization" : "Bearer nonexistingtoken" }),
231
+ 401 ,
232
+ BODY_UNAUTHORIZED_DEFAULT ,
233
+ ),
234
+ (
235
+ "/async_bearer" ,
236
+ dict (headers = {}),
237
+ 401 ,
238
+ BODY_UNAUTHORIZED_DEFAULT ,
239
+ ),
219
240
(
220
241
"/bearer" ,
221
242
dict (headers = {"Authorization" : "Bearer nottherightone" }),
@@ -244,6 +265,7 @@ def test_schema():
244
265
assert schema ["components" ]["securitySchemes" ] == {
245
266
"BasicAuth" : {"scheme" : "basic" , "type" : "http" },
246
267
"BearerAuth" : {"scheme" : "bearer" , "type" : "http" },
268
+ "AsyncBearerAuth" : {"scheme" : "bearer" , "type" : "http" },
247
269
"KeyCookie" : {"in" : "cookie" , "name" : "key" , "type" : "apiKey" },
248
270
"KeyHeader" : {"in" : "header" , "name" : "key" , "type" : "apiKey" },
249
271
"KeyHeaderCustomException" : {"in" : "header" , "name" : "key" , "type" : "apiKey" },
0 commit comments