From 6713da8e87ef5e7226477ab869aa0d05be507ffa Mon Sep 17 00:00:00 2001 From: Daryna Ishchenko Date: Mon, 12 Dec 2022 11:51:37 +0200 Subject: [PATCH] added login_url and validate_session_url to config --- .../sources/declarative/auth/token.py | 15 ++++--- .../auth/test_session_token_auth.py | 42 ++++++++++++++----- 2 files changed, 41 insertions(+), 16 deletions(-) diff --git a/airbyte-cdk/python/airbyte_cdk/sources/declarative/auth/token.py b/airbyte-cdk/python/airbyte_cdk/sources/declarative/auth/token.py index 7e97452ed617..c79e0d94751b 100644 --- a/airbyte-cdk/python/airbyte_cdk/sources/declarative/auth/token.py +++ b/airbyte-cdk/python/airbyte_cdk/sources/declarative/auth/token.py @@ -175,6 +175,8 @@ class SessionTokenAuthenticator(AbstractHeaderAuthenticator, DeclarativeAuthenti options (Mapping[str, Any]): Additional runtime parameters to be used for string interpolation session_token (Union[InterpolatedString, str]): Session token generated by user session_token_response_key (Union[InterpolatedString, str]): Key for retrieving session token from api response + login_url (Union[InterpolatedString, str]): Url fot getting a specific session token + validate_session_url (Union[InterpolatedString, str]): Url to validate passed session token """ api_url: Union[InterpolatedString, str] @@ -184,6 +186,8 @@ class SessionTokenAuthenticator(AbstractHeaderAuthenticator, DeclarativeAuthenti username: Union[InterpolatedString, str] config: Config options: InitVar[Mapping[str, Any]] + login_url: Union[InterpolatedString, str] + validate_session_url: Union[InterpolatedString, str] password: Union[InterpolatedString, str] = "" def __post_init__(self, options): @@ -193,6 +197,8 @@ def __post_init__(self, options): self._header = InterpolatedString.create(self.header, options=options) self._session_token = InterpolatedString.create(self.session_token, options=options) self._session_token_response_key = InterpolatedString.create(self.session_token_response_key, options=options) + self._login_url = InterpolatedString.create(self.login_url, options=options) + self._validate_session_url = InterpolatedString.create(self.validate_session_url, options=options) self.logger = logging.getLogger("airbyte") @@ -209,7 +215,7 @@ def token(self) -> str: username = self._username.eval(self.config) password = self._password.eval(self.config) session_token_response_key = self._session_token_response_key.eval(self.config) - api_url = f"{self._api_url.eval(self.config)}session" + api_url = f"{self._api_url.eval(self.config)}{self._login_url.eval(self.config)}" self.logger.info("Using generated session token by username and password") return get_new_session_token(api_url, username, password, session_token_response_key) @@ -219,7 +225,7 @@ def token(self) -> str: def is_valid_session_token(self) -> bool: try: response = requests.get( - f"{self._api_url.eval(self.config)}user/current", headers={self.auth_header: self._session_token.eval(self.config)} + f"{self._api_url.eval(self.config)}{self._validate_session_url.eval(self.config)}", headers={self.auth_header: self._session_token.eval(self.config)} ) response.raise_for_status() except requests.exceptions.HTTPError as e: @@ -229,10 +235,7 @@ def is_valid_session_token(self) -> bool: else: raise ConnectionError(f"Error while validating session token: {e}") if response.ok: - json_response = response.json() - self.logger.info( - f"Connection check for source successful for {json_response['common_name']} login at {json_response['last_login']}" - ) + self.logger.info(f"Connection check for source is successful.") return True else: raise ConnectionError(f"Failed to retrieve new session token, response code {response.status_code} because {response.reason}") diff --git a/airbyte-cdk/python/unit_tests/sources/declarative/auth/test_session_token_auth.py b/airbyte-cdk/python/unit_tests/sources/declarative/auth/test_session_token_auth.py index 51b390afafa1..47334cbdfafd 100644 --- a/airbyte-cdk/python/unit_tests/sources/declarative/auth/test_session_token_auth.py +++ b/airbyte-cdk/python/unit_tests/sources/declarative/auth/test_session_token_auth.py @@ -13,6 +13,8 @@ session_token = "session_token" header = "X-App-Session" session_token_response_key = "id" +login_url = "session" +validate_session_url = "user/current" input_instance_api_url = "{{ config['instance_api_url'] }}" input_username = "{{ config['username'] }}" @@ -25,7 +27,9 @@ "password": password, "session_token": session_token, "header": header, - "session_token_response_key": session_token_response_key + "session_token_response_key": session_token_response_key, + "login_url": login_url, + "validate_session_url": validate_session_url } config_session_token = { @@ -34,7 +38,9 @@ "password": "", "session_token": session_token, "header": header, - "session_token_response_key": session_token_response_key + "session_token_response_key": session_token_response_key, + "login_url": login_url, + "validate_session_url": validate_session_url } config_username_password = { @@ -43,7 +49,9 @@ "password": password, "session_token": "", "header": header, - "session_token_response_key": session_token_response_key + "session_token_response_key": session_token_response_key, + "login_url": login_url, + "validate_session_url": validate_session_url } @@ -56,7 +64,9 @@ def test_auth_header(): password=input_password, session_token=input_session_token, header=header, - session_token_response_key=session_token_response_key + session_token_response_key=session_token_response_key, + login_url=login_url, + validate_session_url=validate_session_url ).auth_header assert auth_header == "X-App-Session" @@ -75,7 +85,9 @@ def test_get_token_valid_session(requests_mock): password=input_password, session_token=input_session_token, header=header, - session_token_response_key=session_token_response_key + session_token_response_key=session_token_response_key, + login_url=login_url, + validate_session_url=validate_session_url ).token assert token == "session_token" @@ -90,7 +102,9 @@ def test_get_token_invalid_session_unauthorized(): password=input_password, session_token=input_session_token, header=header, - session_token_response_key=session_token_response_key + session_token_response_key=session_token_response_key, + login_url=login_url, + validate_session_url=validate_session_url ).token @@ -104,7 +118,9 @@ def test_get_token_invalid_username_password_unauthorized(): password=input_password, session_token=input_session_token, header=header, - session_token_response_key=session_token_response_key + session_token_response_key=session_token_response_key, + validate_session_url=validate_session_url, + login_url=login_url ).token @@ -119,7 +135,9 @@ def test_get_token_username_password(requests_mock): password=input_password, session_token=input_session_token, header=header, - session_token_response_key=session_token_response_key + session_token_response_key=session_token_response_key, + login_url=login_url, + validate_session_url=validate_session_url ).token assert token == "some session id" @@ -136,7 +154,9 @@ def test_check_is_valid_session_token(requests_mock): password=input_password, session_token=input_session_token, header=header, - session_token_response_key=session_token_response_key + session_token_response_key=session_token_response_key, + validate_session_url=validate_session_url, + login_url=login_url ).is_valid_session_token() @@ -149,7 +169,9 @@ def test_check_is_valid_session_token_unauthorized(): password=input_password, session_token=input_session_token, header=header, - session_token_response_key=session_token_response_key + session_token_response_key=session_token_response_key, + login_url=login_url, + validate_session_url=validate_session_url ).is_valid_session_token()