You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Improved the reusability of the middleware by passing all headers (#3)
* Improved the reusability of the middleware by passing all headers instead of Authorization
* fix tests
* fix signatures
Co-authored-by: Yannic Schröer <yannicschroer@Yannics-MBP.fritz.box>
user = FastAPIUser(first_name="Code", last_name="Specialist", user_id=1) # Usually you would decode the JWT here and verify its signature to extract the 'sub'
21
21
scopes = ["admin"] # You could for instance use the scopes provided in the JWT or request them by looking up the scopes with the 'sub' somewhere
22
22
return scopes, user
23
23
24
24
25
25
users_app = FastAPI()
26
-
users_app.add_middleware(AuthMiddleware, verify_authorization_header=verify_authorization_header) # Add the middleware with your verification method to the whole application
26
+
users_app.add_middleware(AuthMiddleware, verify_header=verify_header) # Add the middleware with your verification method to the whole application
user = FastAPIUser(first_name="Code", last_name="Specialist", user_id=1) # Usually you would decode the JWT here and verify its signature to extract the 'sub'
16
16
scopes = ["admin"] # You could for instance use the scopes provided in the JWT or request them by looking up the scopes with the 'sub' somewhere
17
17
return scopes, user
18
18
19
19
20
20
app = FastAPI()
21
-
app.add_middleware(AuthMiddleware, verify_authorization_header=verify_authorization_header) # Add the middleware with your verification method to the whole application
21
+
app.add_middleware(AuthMiddleware, verify_header=verify_header) # Add the middleware with your verification method to the whole application
Copy file name to clipboardExpand all lines: docs/docs/index.md
+2-2Lines changed: 2 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -38,7 +38,7 @@ from starlette.authentication import BaseUser
38
38
39
39
...
40
40
# Takes a string that will look like 'Bearer eyJhbGc...'
41
-
defverify_authorization_header(auth_header: str) -> Tuple[List[str], BaseUser]: # Returns a Tuple of a List of scopes (string) and a BaseUser
41
+
defverify_header(headers: List[str]) -> Tuple[List[str], BaseUser]: # Returns a Tuple of a List of scopes (string) and a BaseUser
42
42
user = FastAPIUser(first_name="Code", last_name="Specialist", user_id=1) # Usually you would decode the JWT here and verify its signature to extract the 'sub'
43
43
scopes = [] # You could for instance use the scopes provided in the JWT or request them by looking up the scopes with the 'sub' somewhere
44
44
return scopes, user
@@ -53,7 +53,7 @@ from fastapi_auth_middleware import AuthMiddleware
After adding this middleware, all requests will pass the `verify_authorization_header` function and contain the **scopes** as well as the **user object** as injected dependencies.
user=FastAPIUser(first_name="Code", last_name="Specialist", user_id=1) # Usually you would decode the JWT here and verify its signature to extract the 'sub'
12
12
scopes= [] # You could for instance use the scopes provided in the JWT or request them by looking up the scopes with the 'sub' somewhere
13
13
returnscopes, user
14
14
15
15
16
16
app=FastAPI()
17
-
app.add_middleware(AuthMiddleware, verify_authorization_header=verify_authorization_header) # Add the middleware with your verification method to the whole application
17
+
app.add_middleware(AuthMiddleware, verify_header=verify_header) # Add the middleware with your verification method to the whole application
user=FastAPIUser(first_name="Code", last_name="Specialist", user_id=1) # Usually you would decode the JWT here and verify its signature to extract the 'sub'
13
13
scopes= ["admin"] # You could for instance use the scopes provided in the JWT or request them by looking up the scopes with the 'sub' somewhere
14
14
returnscopes, user
15
15
16
16
17
17
app=FastAPI()
18
-
app.add_middleware(AuthMiddleware, verify_authorization_header=verify_authorization_header) # Add the middleware with your verification method to the whole application
18
+
app.add_middleware(AuthMiddleware, verify_header=verify_header) # Add the middleware with your verification method to the whole application
""" Factory method, returning an AuthenticationMiddleware
89
88
Intentionally not named with lower snake case convention as this is a factory method returning a class. Should feel like a class.
90
89
91
90
Args:
92
91
app (FastAPI): The FastAPI instance the middleware should be applied to. The `add_middleware` function of FastAPI adds the app as first argument by default.
93
-
verify_authorization_header (Callable[[str], Tuple[List[str], BaseUser]]): A function handle that returns a list of scopes and a BaseUser
92
+
verify_header (Callable[[str], Tuple[List[str], BaseUser]]): A function handle that returns a list of scopes and a BaseUser
94
93
auth_error_handler (Callable[[Request, Exception], JSONResponse]): Optional error handler for creating responses when an exception was raised in verify_authorization_header
0 commit comments