Repository: CS-SI/eodag (python)

Issue:
TokenAuth should raise AuthenticationError
In `TokenAuth`, catch `RequestException` the same way as it was done for https://github.com/CS-SI/eodag/pull/555 and redirect to an `AuthenticationError`

Pull request:
fix: update auth plugins docs and error handling
Fixes #583 

Updates authentication plugins documentation and `TokenAuth` plugin requests error handling

Patch:
diff --git a/eodag/plugins/authentication/header.py b/eodag/plugins/authentication/header.py
index 48d27cf9..0913296d 100644
--- a/eodag/plugins/authentication/header.py
+++ b/eodag/plugins/authentication/header.py
@@ -22,7 +22,7 @@ from eodag.plugins.authentication import Authentication
 
 
 class HTTPHeaderAuth(Authentication):
-    """A Generic Authentication plugin.
+    """HTTPHeaderAuth Authentication plugin.
 
     This plugin enables implementation of custom HTTP authentication scheme (other than Basic, Digest, Token
     negotiation et al.) using HTTP headers.
@@ -65,7 +65,7 @@ class HTTPHeaderAuth(Authentication):
 
 
 class HeaderAuth(requests.auth.AuthBase):
-    """HeaderAuth authentication plugin"""
+    """HeaderAuth custom authentication class to be used with requests module"""
 
     def __init__(self, authentication_headers):
         self.auth_headers = authentication_headers
diff --git a/eodag/plugins/authentication/openid_connect.py b/eodag/plugins/authentication/openid_connect.py
index 73f53c8a..effbea6e 100644
--- a/eodag/plugins/authentication/openid_connect.py
+++ b/eodag/plugins/authentication/openid_connect.py
@@ -279,7 +279,7 @@ class OIDCAuthorizationCodeFlowAuth(Authentication):
 
 
 class CodeAuthorizedAuth(AuthBase):
-    """CodeAuthorizedAuth authentication plugin"""
+    """CodeAuthorizedAuth custom authentication class to be used with requests module"""
 
     def __init__(self, token, where, key=None):
         self.token = token
diff --git a/eodag/plugins/authentication/token.py b/eodag/plugins/authentication/token.py
index a9e8f1cd..9419f85c 100644
--- a/eodag/plugins/authentication/token.py
+++ b/eodag/plugins/authentication/token.py
@@ -21,7 +21,7 @@ from requests import RequestException
 
 from eodag.plugins.authentication.base import Authentication
 from eodag.utils import RequestsTokenAuth
-from eodag.utils.exceptions import MisconfiguredError
+from eodag.utils.exceptions import AuthenticationError, MisconfiguredError
 from eodag.utils.stac_reader import HTTP_REQ_TIMEOUT
 
 
@@ -49,18 +49,17 @@ class TokenAuth(Authentication):
         req_kwargs = (
             {"headers": self.config.headers} if hasattr(self.config, "headers") else {}
         )
-
-        # First get the token
-        response = requests.post(
-            self.config.auth_uri,
-            data=self.config.credentials,
-            timeout=HTTP_REQ_TIMEOUT,
-            **req_kwargs,
-        )
         try:
+            # First get the token
+            response = requests.post(
+                self.config.auth_uri,
+                data=self.config.credentials,
+                timeout=HTTP_REQ_TIMEOUT,
+                **req_kwargs,
+            )
             response.raise_for_status()
         except RequestException as e:
-            raise e
+            raise AuthenticationError(f"Could no get authentication token: {str(e)}")
         else:
             if getattr(self.config, "token_type", "text") == "json":
                 token = response.json()[self.config.token_key]


Test patch:
diff --git a/tests/units/test_auth_plugins.py b/tests/units/test_auth_plugins.py
index a1ad599b..60c093b1 100644
--- a/tests/units/test_auth_plugins.py
+++ b/tests/units/test_auth_plugins.py
@@ -20,9 +20,10 @@ import unittest
 from unittest import mock
 
 from requests.auth import AuthBase
+from requests.exceptions import RequestException
 
 from eodag.config import override_config_from_mapping
-from tests.context import MisconfiguredError, PluginManager
+from tests.context import AuthenticationError, MisconfiguredError, PluginManager
 
 
 class BaseAuthPluginTest(unittest.TestCase):
@@ -167,3 +168,18 @@ class TestAuthPluginTokenAuth(BaseAuthPluginTest):
         req = mock.Mock(headers={})
         auth(req)
         assert req.headers["Authorization"] == "Bearer this_is_test_token"
+
+    @mock.patch("eodag.plugins.authentication.token.requests.post", autospec=True)
+    def test_plugins_auth_tokenauth_request_error(self, mock_requests_post):
+        """TokenAuth.authenticate must raise an AuthenticationError if a request error occurs"""
+        auth_plugin = self.get_auth_plugin("provider_json_token_simple_url")
+
+        auth_plugin.config.credentials = {"foo": "bar"}
+
+        # mock token post request response
+        mock_requests_post.side_effect = RequestException
+
+        self.assertRaises(
+            AuthenticationError,
+            auth_plugin.authenticate,
+        )