```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
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':
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):
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)