-
Notifications
You must be signed in to change notification settings - Fork 4.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[low-code CDK] Enable runtime string interpolation in authenticators (#…
…14914) * interpolatedauth * fix tests * fix import * no need for default * Bump version * Missing docstrings * example * missing example * more docstrings * interpolated types
- Loading branch information
Showing
11 changed files
with
311 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
airbyte-cdk/python/airbyte_cdk/sources/declarative/auth/token.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
# | ||
# Copyright (c) 2022 Airbyte, Inc., all rights reserved. | ||
# | ||
|
||
import base64 | ||
from typing import Union | ||
|
||
from airbyte_cdk.sources.declarative.interpolation.interpolated_string import InterpolatedString | ||
from airbyte_cdk.sources.declarative.types import Config | ||
from airbyte_cdk.sources.streams.http.requests_native_auth.abtract_token import AbstractHeaderAuthenticator | ||
|
||
|
||
class ApiKeyAuthenticator(AbstractHeaderAuthenticator): | ||
""" | ||
ApiKeyAuth sets a request header on the HTTP requests sent. | ||
The header is of the form: | ||
`"<header>": "<token>"` | ||
For example, | ||
`ApiKeyAuthenticator("Authorization", "Bearer hello")` | ||
will result in the following header set on the HTTP request | ||
`"Authorization": "Bearer hello"` | ||
""" | ||
|
||
def __init__(self, header: Union[InterpolatedString, str], token: Union[InterpolatedString, str], config: Config): | ||
""" | ||
:param header: Header key to set on the HTTP requests | ||
:param token: Header value to set on the HTTP requests | ||
:param config: The user-provided configuration as specified by the source's spec | ||
""" | ||
self._header = InterpolatedString.create(header) | ||
self._token = InterpolatedString.create(token) | ||
self._config = config | ||
|
||
@property | ||
def auth_header(self) -> str: | ||
return self._header.eval(self._config) | ||
|
||
@property | ||
def token(self) -> str: | ||
return self._token.eval(self._config) | ||
|
||
|
||
class BearerAuthenticator(AbstractHeaderAuthenticator): | ||
""" | ||
Authenticator that sets the Authorization header on the HTTP requests sent. | ||
The header is of the form: | ||
`"Authorization": "Bearer <token>"` | ||
""" | ||
|
||
def __init__(self, token: Union[InterpolatedString, str], config: Config): | ||
""" | ||
:param token: The bearer token | ||
:param config: The user-provided configuration as specified by the source's spec | ||
""" | ||
self._token = InterpolatedString.create(token) | ||
self._config = config | ||
|
||
@property | ||
def auth_header(self) -> str: | ||
return "Authorization" | ||
|
||
@property | ||
def token(self) -> str: | ||
return f"Bearer {self._token.eval(self._config)}" | ||
|
||
|
||
class BasicHttpAuthenticator(AbstractHeaderAuthenticator): | ||
""" | ||
Builds auth based off the basic authentication scheme as defined by RFC 7617, which transmits credentials as USER ID/password pairs, encoded using bas64 | ||
https://developer.mozilla.org/en-US/docs/Web/HTTP/Authentication#basic_authentication_scheme | ||
The header is of the form | ||
`"Authorization": "Basic <encoded_credentials>"` | ||
""" | ||
|
||
def __init__(self, username: Union[InterpolatedString, str], config: Config, password: Union[InterpolatedString, str] = ""): | ||
""" | ||
:param username: The username | ||
:param config: The user-provided configuration as specified by the source's spec | ||
:param password: The password | ||
""" | ||
self._username = InterpolatedString.create(username) | ||
self._password = InterpolatedString.create(password) | ||
self._config = config | ||
|
||
@property | ||
def auth_header(self) -> str: | ||
return "Authorization" | ||
|
||
@property | ||
def token(self) -> str: | ||
auth_string = f"{self._username.eval(self._config)}:{self._password.eval(self._config)}".encode("utf8") | ||
b64_encoded = base64.b64encode(auth_string).decode("utf8") | ||
return f"Basic {b64_encoded}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 3 additions & 1 deletion
4
airbyte-cdk/python/airbyte_cdk/sources/streams/http/requests_native_auth/__init__.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
airbyte-cdk/python/airbyte_cdk/sources/streams/http/requests_native_auth/abtract_token.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# | ||
# Copyright (c) 2022 Airbyte, Inc., all rights reserved. | ||
# | ||
|
||
from abc import abstractmethod | ||
from typing import Any, Mapping | ||
|
||
from requests.auth import AuthBase | ||
|
||
|
||
class AbstractHeaderAuthenticator(AuthBase): | ||
""" | ||
Abstract class for header-based authenticators that set a key-value pair in outgoing HTTP headers | ||
""" | ||
|
||
def __call__(self, request): | ||
"""Attach the HTTP headers required to authenticate on the HTTP request""" | ||
request.headers.update(self.get_auth_header()) | ||
return request | ||
|
||
def get_auth_header(self) -> Mapping[str, Any]: | ||
"""HTTP header to set on the requests""" | ||
return {self.auth_header: self.token} | ||
|
||
@property | ||
@abstractmethod | ||
def auth_header(self) -> str: | ||
"""HTTP header to set on the requests""" | ||
|
||
@property | ||
@abstractmethod | ||
def token(self) -> str: | ||
"""Value of the HTTP header to set on the requests""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
airbyte-cdk/python/unit_tests/sources/declarative/auth/test_token_auth.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
# | ||
# Copyright (c) 2022 Airbyte, Inc., all rights reserved. | ||
# | ||
|
||
import logging | ||
|
||
import pytest | ||
import requests | ||
from airbyte_cdk.sources.declarative.auth.token import ApiKeyAuthenticator, BasicHttpAuthenticator, BearerAuthenticator | ||
from requests import Response | ||
|
||
LOGGER = logging.getLogger(__name__) | ||
|
||
resp = Response() | ||
config = {"username": "user", "password": "password", "header": "header"} | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"test_name, token, expected_header_value", | ||
[ | ||
("test_static_token", "test-token", "Bearer test-token"), | ||
("test_token_from_config", "{{ config.username }}", "Bearer user"), | ||
], | ||
) | ||
def test_bearer_token_authenticator(test_name, token, expected_header_value): | ||
""" | ||
Should match passed in token, no matter how many times token is retrieved. | ||
""" | ||
token_auth = BearerAuthenticator(token, config) | ||
header1 = token_auth.get_auth_header() | ||
header2 = token_auth.get_auth_header() | ||
|
||
prepared_request = requests.PreparedRequest() | ||
prepared_request.headers = {} | ||
token_auth(prepared_request) | ||
|
||
assert {"Authorization": expected_header_value} == prepared_request.headers | ||
assert {"Authorization": expected_header_value} == header1 | ||
assert {"Authorization": expected_header_value} == header2 | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"test_name, username, password, expected_header_value", | ||
[ | ||
("test_static_creds", "user", "password", "Basic dXNlcjpwYXNzd29yZA=="), | ||
("test_creds_from_config", "{{ config.username }}", "{{ config.password }}", "Basic dXNlcjpwYXNzd29yZA=="), | ||
], | ||
) | ||
def test_basic_authenticator(test_name, username, password, expected_header_value): | ||
""" | ||
Should match passed in token, no matter how many times token is retrieved. | ||
""" | ||
token_auth = BasicHttpAuthenticator(username=username, password=password, config=config) | ||
header1 = token_auth.get_auth_header() | ||
header2 = token_auth.get_auth_header() | ||
|
||
prepared_request = requests.PreparedRequest() | ||
prepared_request.headers = {} | ||
token_auth(prepared_request) | ||
|
||
assert {"Authorization": expected_header_value} == prepared_request.headers | ||
assert {"Authorization": expected_header_value} == header1 | ||
assert {"Authorization": expected_header_value} == header2 | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"test_name, header, token, expected_header, expected_header_value", | ||
[ | ||
("test_static_token", "Authorization", "test-token", "Authorization", "test-token"), | ||
("test_token_from_config", "{{ config.header }}", "{{ config.username }}", "header", "user"), | ||
], | ||
) | ||
def test_api_key_authenticator(test_name, header, token, expected_header, expected_header_value): | ||
""" | ||
Should match passed in token, no matter how many times token is retrieved. | ||
""" | ||
token_auth = ApiKeyAuthenticator(header, token, config) | ||
header1 = token_auth.get_auth_header() | ||
header2 = token_auth.get_auth_header() | ||
|
||
prepared_request = requests.PreparedRequest() | ||
prepared_request.headers = {} | ||
token_auth(prepared_request) | ||
|
||
assert {expected_header: expected_header_value} == prepared_request.headers | ||
assert {expected_header: expected_header_value} == header1 | ||
assert {expected_header: expected_header_value} == header2 |
Oops, something went wrong.