1919from starlette .authentication import AuthenticationError
2020from starlette .authentication import BaseUser
2121from starlette .middleware .authentication import AuthenticationMiddleware
22- from starlette .requests import Request
2322from starlette .requests import HTTPConnection
24- from starlette .responses import PlainTextResponse
23+ from starlette .requests import Request
2524from starlette .responses import Response
2625from starlette .types import ASGIApp
2726from starlette .types import Receive
3130from .claims import Claims
3231from .config import OAuth2Config
3332from .core import OAuth2Core
34- from .exceptions import OAuth2AuthenticationError
3533
3634
3735class Auth (AuthCredentials ):
@@ -141,8 +139,7 @@ def __init__(
141139 app : ASGIApp ,
142140 config : Union [OAuth2Config , dict ],
143141 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 ,
146143 ) -> None :
147144 """Initiates the middleware with the given configuration.
148145
@@ -155,13 +152,10 @@ def __init__(
155152 elif not isinstance (config , OAuth2Config ):
156153 raise TypeError ("config is not a valid type" )
157154 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 )
159157
160158 async def __call__ (self , scope : Scope , receive : Receive , send : Send ) -> None :
161159 if scope ["type" ] == "http" :
162160 return await self .auth_middleware (scope , receive , send )
163161 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