]> git.ipfire.org Git - thirdparty/starlette.git/commitdiff
Improvements on authentication documentation (#1420)
authorMathieu Kniewallner <mathieu.kniewallner@gmail.com>
Tue, 18 Jan 2022 14:48:48 +0000 (15:48 +0100)
committerGitHub <noreply@github.com>
Tue, 18 Jan 2022 14:48:48 +0000 (15:48 +0100)
* 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>
docs/authentication.md

index d4af5b21644a2f0d4a6bd8390d452d43328603a8..48eba6ca2f0575b816cfc6663ff7f2559ebc7032 100644 (file)
@@ -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
@@ -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':
@@ -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):
@@ -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)