Skip to content

Commit 252c00e

Browse files
committed
- Fix typing for lower versions of Python interpreter
- Use `default_on_error` instead of defining new one
1 parent 0041465 commit 252c00e

File tree

1 file changed

+4
-10
lines changed

1 file changed

+4
-10
lines changed

src/fastapi_oauth2/middleware.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,8 @@
1919
from starlette.authentication import AuthenticationError
2020
from starlette.authentication import BaseUser
2121
from starlette.middleware.authentication import AuthenticationMiddleware
22-
from starlette.requests import Request
2322
from starlette.requests import HTTPConnection
24-
from starlette.responses import PlainTextResponse
23+
from starlette.requests import Request
2524
from starlette.responses import Response
2625
from starlette.types import ASGIApp
2726
from starlette.types import Receive
@@ -31,7 +30,6 @@
3130
from .claims import Claims
3231
from .config import OAuth2Config
3332
from .core import OAuth2Core
34-
from .exceptions import OAuth2AuthenticationError
3533

3634

3735
class 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

Comments
 (0)