Skip to content

Commit

Permalink
added login_url and validate_session_url to config
Browse files Browse the repository at this point in the history
  • Loading branch information
darynaishchenko committed Dec 12, 2022
1 parent 4f3c260 commit 6713da8
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 16 deletions.
15 changes: 9 additions & 6 deletions airbyte-cdk/python/airbyte_cdk/sources/declarative/auth/token.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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):
Expand All @@ -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")

Expand All @@ -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)
Expand All @@ -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:
Expand All @@ -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}")
Original file line number Diff line number Diff line change
Expand Up @@ -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'] }}"
Expand All @@ -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 = {
Expand All @@ -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 = {
Expand All @@ -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
}


Expand All @@ -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"

Expand All @@ -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"

Expand All @@ -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


Expand All @@ -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


Expand All @@ -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"

Expand All @@ -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()


Expand All @@ -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()


Expand Down

0 comments on commit 6713da8

Please sign in to comment.