@@ -72,6 +72,7 @@ def user():
72
72
def __init__ (self , app = None ):
73
73
self ._before_request_funcs = []
74
74
self ._after_request_funcs = []
75
+ self ._exception_handler = None
75
76
self ._invalid_response = None
76
77
if app :
77
78
self .init_app (app )
@@ -85,6 +86,13 @@ def init_app(self, app):
85
86
app .extensions = getattr (app , 'extensions' , {})
86
87
app .extensions ['oauthlib.provider.oauth2' ] = self
87
88
89
+ def _on_exception (self , error , redirect_content = None ):
90
+
91
+ if self ._exception_handler :
92
+ return self ._exception_handler (error , redirect_content )
93
+ else :
94
+ return redirect (redirect_content )
95
+
88
96
@cached_property
89
97
def error_uri (self ):
90
98
"""The error page URI.
@@ -208,6 +216,34 @@ def valid_after_request(valid, oauth):
208
216
self ._after_request_funcs .append (f )
209
217
return f
210
218
219
+ def exception_handler (self , f ):
220
+ """Register a function as custom exception handler.
221
+
222
+ **As the default error handling is leaking error to the client, it is
223
+ STRONGLY RECOMMENDED to implement your own handler to mask
224
+ the server side errors in production environment.**
225
+
226
+ When an error occur during execution, we can
227
+ handle the error with with the registered function. The function
228
+ accepts two parameters:
229
+ - error: the error raised
230
+ - redirect_content: the content used in the redirect by default
231
+
232
+ usage with the flask error handler ::
233
+ @oauth.exception_handler
234
+ def custom_exception_handler(error, *args):
235
+ raise error
236
+
237
+ @app.errorhandler(Exception)
238
+ def all_exception_handler(*args):
239
+ # any treatment you need for the error
240
+ return "Server error", 500
241
+
242
+ If no function is registered, it will do a redirect with ``redirect_content`` as content.
243
+ """
244
+ self ._exception_handler = f
245
+ return f
246
+
211
247
def invalid_response (self , f ):
212
248
"""Register a function for responsing with invalid request.
213
249
@@ -391,13 +427,13 @@ def decorated(*args, **kwargs):
391
427
kwargs .update (credentials )
392
428
except oauth2 .FatalClientError as e :
393
429
log .debug ('Fatal client error %r' , e , exc_info = True )
394
- return redirect ( e .in_uri (self .error_uri ))
430
+ return self . _on_exception ( e , e .in_uri (self .error_uri ))
395
431
except oauth2 .OAuth2Error as e :
396
432
log .debug ('OAuth2Error: %r' , e , exc_info = True )
397
- return redirect ( e .in_uri (redirect_uri ))
433
+ return self . _on_exception ( e , e .in_uri (redirect_uri ))
398
434
except Exception as e :
399
435
log .exception (e )
400
- return redirect ( add_params_to_uri (
436
+ return self . _on_exception ( e , add_params_to_uri (
401
437
self .error_uri , {'error' : str (e )}
402
438
))
403
439
@@ -410,10 +446,10 @@ def decorated(*args, **kwargs):
410
446
rv = f (* args , ** kwargs )
411
447
except oauth2 .FatalClientError as e :
412
448
log .debug ('Fatal client error %r' , e , exc_info = True )
413
- return redirect ( e .in_uri (self .error_uri ))
449
+ return self . _on_exception ( e , e .in_uri (self .error_uri ))
414
450
except oauth2 .OAuth2Error as e :
415
451
log .debug ('OAuth2Error: %r' , e , exc_info = True )
416
- return redirect ( e .in_uri (redirect_uri ))
452
+ return self . _on_exception ( e , e .in_uri (redirect_uri ))
417
453
418
454
if not isinstance (rv , bool ):
419
455
# if is a response or redirect
@@ -422,7 +458,7 @@ def decorated(*args, **kwargs):
422
458
if not rv :
423
459
# denied by user
424
460
e = oauth2 .AccessDeniedError ()
425
- return redirect ( e .in_uri (redirect_uri ))
461
+ return self . _on_exception ( e , e .in_uri (redirect_uri ))
426
462
return self .confirm_authorization_request ()
427
463
return decorated
428
464
@@ -449,13 +485,13 @@ def confirm_authorization_request(self):
449
485
return create_response (* ret )
450
486
except oauth2 .FatalClientError as e :
451
487
log .debug ('Fatal client error %r' , e , exc_info = True )
452
- return redirect ( e .in_uri (self .error_uri ))
488
+ return self . _on_exception ( e , e .in_uri (self .error_uri ))
453
489
except oauth2 .OAuth2Error as e :
454
490
log .debug ('OAuth2Error: %r' , e , exc_info = True )
455
- return redirect ( e .in_uri (redirect_uri or self .error_uri ))
491
+ return self . _on_exception ( e , e .in_uri (redirect_uri or self .error_uri ))
456
492
except Exception as e :
457
493
log .exception (e )
458
- return redirect ( add_params_to_uri (
494
+ return self . _on_exception ( e , add_params_to_uri (
459
495
self .error_uri , {'error' : str (e )}
460
496
))
461
497
0 commit comments