@@ -147,19 +147,18 @@ async def send_wrapper(message: Message):
147
147
except :
148
148
user = None
149
149
if user :
150
- log [' user' ] = user
151
- log [' status' ] = message ["status" ]
150
+ log [" user" ] = user
151
+ log [" status" ] = message ["status" ]
152
152
153
153
if self .headers :
154
- log [' res.headers' ] = message ["headers" ]
154
+ log [" res.headers" ] = message ["headers" ]
155
155
156
156
self .logger .debug (log )
157
157
await send (message )
158
158
159
159
await self .app (scope , receive , send_wrapper )
160
160
161
161
162
-
163
162
class LowerCaseQueryStringMiddleware :
164
163
"""Middleware to make URL parameters case-insensitive.
165
164
taken from: https://github.com/tiangolo/fastapi/issues/826
@@ -212,9 +211,11 @@ async def __call__(self, scope: Scope, receive: Receive, send: Send):
212
211
213
212
214
213
class JWTAuthenticationMiddleware :
215
- """Middleware to authentication with jwt"""
214
+ """Middleware for authentication with jwt"""
216
215
217
- def __init__ (self , app : ASGIApp , secret : str , user_key = "user" , algorithms : List [str ]= None ) -> None :
216
+ def __init__ (
217
+ self , app : ASGIApp , secret : str , user_key = "user" , algorithms : List [str ] = None
218
+ ) -> None :
218
219
"""Init Middleware.
219
220
220
221
Args:
@@ -224,35 +225,42 @@ def __init__(self, app: ASGIApp, secret: str, user_key="user", algorithms: List[
224
225
algorithms (List[str]): algorithms for decode jwt. default ["HS512"]
225
226
"""
226
227
if algorithms is None :
227
- algorithms = ["HS512 " ]
228
+ algorithms = ["HS256 " ]
228
229
from fastapi .security import HTTPBearer
230
+
229
231
self .app = app
230
232
self .secret = secret
231
233
self .http_bearer = HTTPBearer (bearerFormat = "jwt" , auto_error = False )
232
234
self .algorithms = algorithms
233
235
self .user_key = user_key
234
236
235
237
async def __call__ (self , scope : Scope , receive : Receive , send : Send ):
236
- async def response401 (message : str = "Not authenticated" ):
237
- response = JSONResponse (content = {"detail" : message },
238
- status_code = starlette .status .HTTP_401_UNAUTHORIZED )
238
+ async def response401 (message : str = "Not authenticated" ):
239
+ response = JSONResponse (
240
+ content = {"detail" : message },
241
+ status_code = starlette .status .HTTP_401_UNAUTHORIZED ,
242
+ )
239
243
await response (scope , receive , send )
244
+
240
245
"""Handle call."""
241
246
if scope ["type" ] == "http" :
242
247
request = Request (scope )
243
- credentials = await self .http_bearer (request )
244
- if not credentials :
248
+
249
+ access_token = request .query_params .get ("access_token" )
250
+ if not access_token :
245
251
await response401 ("access token is required" )
246
252
return
247
253
try :
248
- payload = jwt .decode (credentials .credentials , self .secret , algorithms = self .algorithms )
254
+ payload = jwt .decode (
255
+ access_token , self .secret , algorithms = self .algorithms
256
+ )
249
257
except jwt .DecodeError as e :
250
258
await response401 ("unsupported token" )
251
259
except jwt .InvalidTokenError as e :
252
260
await response401 ("invalid token" )
253
261
return
254
262
user = payload [self .user_key ]
255
- scope [' auth' ] = credentials . credentials
256
- scope [' user' ] = user
263
+ scope [" auth" ] = access_token
264
+ scope [" user" ] = user
257
265
258
266
await self .app (scope , receive , send )
0 commit comments