diff options
author | Jonathan Huot <jonathan.huot@thomsonreuters.com> | 2018-08-02 00:54:54 +0200 |
---|---|---|
committer | Jonathan Huot <jonathan.huot@thomsonreuters.com> | 2018-09-10 22:41:49 +0200 |
commit | e81ae772e4f260cc02ce07a7396470821ac63b1e (patch) | |
tree | 06e602897fe52814353faaa5af9a5cbbc304d8a5 | |
parent | 36e7f50049f3333db72ebcb82677b465ec09f84b (diff) | |
download | oauthlib-e81ae772e4f260cc02ce07a7396470821ac63b1e.tar.gz |
Add support of custom errors coming from providers
Fix #431. The inherent function "raise_from_error" is called when "error=" is found in the payload. So it MUST raise something, and until now, only RFC errors were raised.
-rw-r--r-- | oauthlib/oauth2/rfc6749/errors.py | 12 | ||||
-rw-r--r-- | tests/oauth2/rfc6749/test_parameters.py | 4 |
2 files changed, 16 insertions, 0 deletions
diff --git a/oauthlib/oauth2/rfc6749/errors.py b/oauthlib/oauth2/rfc6749/errors.py index 5a0cca2..8882ab2 100644 --- a/oauthlib/oauth2/rfc6749/errors.py +++ b/oauthlib/oauth2/rfc6749/errors.py @@ -313,6 +313,7 @@ class ConsentRequired(OAuth2Error): error = 'consent_required' status_code = 401 + class LoginRequired(OAuth2Error): """ The Authorization Server requires End-User authentication. @@ -325,6 +326,16 @@ class LoginRequired(OAuth2Error): status_code = 401 +class CustomOAuth2Error(OAuth2Error): + """ + This error is a placeholder for all custom errors not described by the RFC. + Some of the popular OAuth2 providers are using custom errors. + """ + def __init__(self, error, *args, **kwargs): + self.error = error + super().__init__(*args, **kwargs) + + def raise_from_error(error, params=None): import inspect import sys @@ -336,3 +347,4 @@ def raise_from_error(error, params=None): for _, cls in inspect.getmembers(sys.modules[__name__], inspect.isclass): if cls.error == error: raise cls(**kwargs) + raise CustomOAuth2Error(error=error, **kwargs) diff --git a/tests/oauth2/rfc6749/test_parameters.py b/tests/oauth2/rfc6749/test_parameters.py index b211d1e..c42f516 100644 --- a/tests/oauth2/rfc6749/test_parameters.py +++ b/tests/oauth2/rfc6749/test_parameters.py @@ -103,6 +103,7 @@ class ParameterTests(TestCase): ' "refresh_token": "tGzv3JOkF0XG5Qx2TlKWIA",' ' "example_parameter": "example_value" }') + json_custom_error = '{ "error": "incorrect_client_credentials" }' json_error = '{ "error": "access_denied" }' json_notoken = ('{ "token_type": "example",' @@ -197,6 +198,9 @@ class ParameterTests(TestCase): self.assertRaises(ValueError, parse_implicit_response, self.implicit_wrongstate, state=self.state) + def test_custom_json_error(self): + self.assertRaises(CustomOAuth2Error, parse_token_response, self.json_custom_error) + def test_json_token_response(self): """Verify correct parameter parsing and validation for token responses. """ self.assertEqual(parse_token_response(self.json_response), self.json_dict) |