19
19
from starlette .authentication import AuthenticationError
20
20
from starlette .authentication import BaseUser
21
21
from starlette .middleware .authentication import AuthenticationMiddleware
22
- from starlette .requests import Request
23
22
from starlette .requests import HTTPConnection
24
- from starlette .responses import PlainTextResponse
23
+ from starlette .requests import Request
25
24
from starlette .responses import Response
26
25
from starlette .types import ASGIApp
27
26
from starlette .types import Receive
31
30
from .claims import Claims
32
31
from .config import OAuth2Config
33
32
from .core import OAuth2Core
34
- from .exceptions import OAuth2AuthenticationError
35
33
36
34
37
35
class Auth (AuthCredentials ):
@@ -141,8 +139,7 @@ def __init__(
141
139
app : ASGIApp ,
142
140
config : Union [OAuth2Config , dict ],
143
141
callback : Callable [[Auth , User ], Union [Awaitable [None ], None ]] = None ,
144
- on_error : Callable [[HTTPConnection , AuthenticationError ], Response ] | None = None ,
145
- ** kwargs , # AuthenticationMiddleware kwargs
142
+ on_error : Optional [Callable [[HTTPConnection , AuthenticationError ], Response ]] = None ,
146
143
) -> None :
147
144
"""Initiates the middleware with the given configuration.
148
145
@@ -155,13 +152,10 @@ def __init__(
155
152
elif not isinstance (config , OAuth2Config ):
156
153
raise TypeError ("config is not a valid type" )
157
154
self .default_application_middleware = app
158
- self .auth_middleware = AuthenticationMiddleware (app , backend = OAuth2Backend (config , callback ), on_error = on_error or self .on_error , ** kwargs )
155
+ on_error = on_error or AuthenticationMiddleware .default_on_error
156
+ self .auth_middleware = AuthenticationMiddleware (app , backend = OAuth2Backend (config , callback ), on_error = on_error )
159
157
160
158
async def __call__ (self , scope : Scope , receive : Receive , send : Send ) -> None :
161
159
if scope ["type" ] == "http" :
162
160
return await self .auth_middleware (scope , receive , send )
163
161
await self .default_application_middleware (scope , receive , send )
164
-
165
- @staticmethod
166
- def on_error (conn : HTTPConnection , exc : Exception ) -> Response :
167
- return PlainTextResponse (str (exc ), status_code = 401 )
0 commit comments