@@ -53,14 +53,15 @@ async def secure_endpoint(user: AuthenticatedUserDependency):
53
53
key_expired_handler ,
54
54
logout ,
55
55
on_auth_error ,
56
+ transform_auth_error ,
56
57
)
57
58
58
59
59
60
class SOUserWithGrants (SOUser ):
60
61
grants : set [str ] = Field (default_factory = set )
61
62
62
63
63
- def add_exception_handlers (app : FastAPI ) -> FastAPI :
64
+ def add_exception_handlers (app : FastAPI , allow_refresh : bool = True ) -> FastAPI :
64
65
"""
65
66
Adds exception handlers for authentication. To use these, you must:
66
67
@@ -73,6 +74,10 @@ def add_exception_handlers(app: FastAPI) -> FastAPI:
73
74
74
75
- Set `app.refresh_token_name` to change the cookie name for the refresh token
75
76
- Set `app.access_token_name` to change the cookie name for the access token
77
+
78
+ If you do not wish to allow automatic refreshing of the keys by the application
79
+ (e.g. you are running in 'consumer' mode where only 401 or 200s are allowed),
80
+ you can set allow_refresh = False.
76
81
"""
77
82
app .add_exception_handler (KeyDecodeError , key_decode_handler )
78
83
app .add_exception_handler (KeyExpiredError , key_expired_handler )
@@ -179,6 +184,8 @@ def global_setup(
179
184
public_key : str ,
180
185
key_pair_type : str ,
181
186
add_middleware : bool = True ,
187
+ handle_exceptions : bool = True ,
188
+ use_refresh_token : bool = True ,
182
189
) -> FastAPI :
183
190
"""
184
191
Transform the app such that it is ready for authentication. Can either add middleware
@@ -207,6 +214,13 @@ def global_setup(
207
214
This allows for the use of starlette's `@requries()` (against user grants), and
208
215
access to `request.user` and `request.auth.scopes`. Alternatively, you can use
209
216
the dependencies defined in this file.
217
+ handle_exceptions: bool = True,
218
+ If this is used, we automatically handle expired/broken credentials via the server
219
+ itself (including using refresh tokens). If it is not, you are left on your own
220
+ to handle the resulting 400-level errors.
221
+ use_refresh_token: bool = True,
222
+ Whether or not to set and use the refresh token cookie. Otherwise your users will
223
+ need to round-trip to the identity server every time the access token expires.
210
224
211
225
Example
212
226
-------
@@ -274,6 +288,7 @@ async def test(request: Request):
274
288
app .authentication_url = authentication_base_url
275
289
app .client_secret = client_secret
276
290
app .app_id = app_id
291
+ app .use_refresh_token = use_refresh_token
277
292
278
293
app .public_key = public_key .encode ("utf-8" )
279
294
app .key_pair_type = key_pair_type
@@ -289,9 +304,11 @@ async def test(request: Request):
289
304
app .add_middleware (
290
305
AuthenticationMiddleware ,
291
306
backend = SOAuthCookieBackend (
292
- public_key = app .public_key , key_pair_type = app .key_pair_type
307
+ public_key = app .public_key ,
308
+ key_pair_type = app .key_pair_type ,
309
+ use_refresh_token = use_refresh_token ,
293
310
),
294
- on_error = on_auth_error ,
311
+ on_error = on_auth_error if handle_exceptions else transform_auth_error ,
295
312
)
296
313
297
314
app .add_api_route (path = "/logout" , endpoint = logout )
0 commit comments