Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix typo in scope check for 'websocket' #335

Merged
merged 6 commits into from
Jan 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions starlette/authentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import typing

from starlette.exceptions import HTTPException
from starlette.requests import Request
from starlette.requests import Request, HTTPConnection
from starlette.responses import RedirectResponse, Response


Expand Down Expand Up @@ -68,7 +68,7 @@ class AuthenticationError(Exception):

class AuthenticationBackend:
async def authenticate(
self, request: Request
self, conn: HTTPConnection
) -> typing.Optional[typing.Tuple["AuthCredentials", "BaseUser"]]:
raise NotImplemented() # pragma: no cover

Expand Down
18 changes: 10 additions & 8 deletions starlette/middleware/authentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
AuthenticationError,
UnauthenticatedUser,
)
from starlette.requests import Request
from starlette.requests import HTTPConnection
from starlette.responses import PlainTextResponse, Response
from starlette.types import ASGIApp, ASGIInstance, Receive, Scope, Send

Expand All @@ -17,25 +17,27 @@ def __init__(
self,
app: ASGIApp,
backend: AuthenticationBackend,
on_error: typing.Callable[[Request, AuthenticationError], Response] = None,
on_error: typing.Callable[
[HTTPConnection, AuthenticationError], Response
] = None,
) -> None:
self.app = app
self.backend = backend
self.on_error = (
on_error if on_error is not None else self.default_on_error
) # type: typing.Callable[[Request, AuthenticationError], Response]
) # type: typing.Callable[[HTTPConnection, AuthenticationError], Response]

def __call__(self, scope: Scope) -> ASGIInstance:
if scope["type"] in ["http", "websockets"]:
if scope["type"] in ["http", "websocket"]:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup.

return functools.partial(self.asgi, scope=scope)
return self.app(scope)

async def asgi(self, receive: Receive, send: Send, scope: Scope) -> None:
request = Request(scope, receive=receive)
conn = HTTPConnection(scope, receive=receive)
try:
auth_result = await self.backend.authenticate(request)
auth_result = await self.backend.authenticate(conn)
except AuthenticationError as exc:
response = self.on_error(request, exc)
response = self.on_error(conn, exc)
await response(receive, send)
return

Expand All @@ -46,5 +48,5 @@ async def asgi(self, receive: Receive, send: Send, scope: Scope) -> None:
await inner(receive, send)

@staticmethod
def default_on_error(request: Request, exc: Exception) -> Response:
def default_on_error(conn: HTTPConnection, exc: Exception) -> Response:
return PlainTextResponse(str(exc), status_code=400)