Skip to content

Commit

Permalink
Improvements on authentication documentation (#1420)
Browse files Browse the repository at this point in the history
* Use `conn` in `AuthenticationBackend` documentation

* Remove unused import in `AuthenticationBackend` documentation

* Add missing imports in authentication documentation

Co-authored-by: Marcelo Trylesinski <marcelotryle@gmail.com>
  • Loading branch information
mkniewallner and Kludex committed Jan 18, 2022
1 parent e06425b commit 3182495
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions docs/authentication.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ interfaces will be available in your endpoints.
```python
from starlette.applications import Starlette
from starlette.authentication import (
AuthenticationBackend, AuthenticationError, SimpleUser, UnauthenticatedUser,
AuthCredentials
AuthCredentials, AuthenticationBackend, AuthenticationError, SimpleUser
)
from starlette.middleware import Middleware
from starlette.middleware.authentication import AuthenticationMiddleware
Expand All @@ -19,11 +18,11 @@ import binascii


class BasicAuthBackend(AuthenticationBackend):
async def authenticate(self, request):
if "Authorization" not in request.headers:
async def authenticate(self, conn):
if "Authorization" not in conn.headers:
return

auth = request.headers["Authorization"]
auth = conn.headers["Authorization"]
try:
scheme, credentials = auth.split()
if scheme.lower() != 'basic':
Expand Down Expand Up @@ -136,6 +135,10 @@ For class-based endpoints, you should wrap the decorator
around a method on the class.

```python
from starlette.authentication import requires
from starlette.endpoints import HTTPEndpoint


class Dashboard(HTTPEndpoint):
@requires("authenticated")
async def get(self, request):
Expand All @@ -148,6 +151,11 @@ You can customise the error response sent when a `AuthenticationError` is
raised by an auth backend:

```python
from starlette.middleware.authentication import AuthenticationMiddleware
from starlette.requests import Request
from starlette.responses import JSONResponse


def on_auth_error(request: Request, exc: Exception):
return JSONResponse({"error": str(exc)}, status_code=401)

Expand Down

0 comments on commit 3182495

Please sign in to comment.