-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1284e30
commit 294178c
Showing
8 changed files
with
1,494 additions
and
68 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
Large diffs are not rendered by default.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,63 @@ | ||
"""Sunwave Authentication.""" | ||
|
||
from __future__ import annotations | ||
|
||
import base64 | ||
from datetime import datetime, timezone | ||
import hashlib | ||
import hmac | ||
import uuid | ||
import typing as t | ||
import uuid | ||
from datetime import datetime, timezone | ||
|
||
import requests | ||
from singer_sdk.authenticators import APIAuthenticatorBase, SingletonMeta | ||
|
||
if t.TYPE_CHECKING: | ||
import requests | ||
|
||
from tap_sunwave.client import SunwaveStream | ||
|
||
|
||
class SunwaveAuthenticator(APIAuthenticatorBase, metaclass=SingletonMeta): | ||
"""Authenticator class for Sunwave.""" | ||
|
||
|
||
def authenticate_request( | ||
self, | ||
request: requests.PreparedRequest, | ||
) -> requests.PreparedRequest: | ||
|
||
request_body = request.body if request.body else "" | ||
|
||
date_calc = datetime.now(timezone.utc).strftime("%a, %d %b %Y %H:%M:%S %z") | ||
dateTimeBase64 = base64.b64encode(date_calc.encode('utf-8')).decode('utf-8') | ||
datetime_base_64 = base64.b64encode(date_calc.encode("utf-8")).decode("utf-8") | ||
unique_transaction_id = str(uuid.uuid4()) | ||
|
||
md5_payload = hashlib.md5(request_body.encode('utf-8')).hexdigest() # For GET requests this isn't needed, but an empty string md5'd works | ||
base64md5Payload_bytes = base64.b64encode(md5_payload.encode('utf-8')) | ||
base64md5Payload = base64md5Payload_bytes.decode('utf-8').replace('/', '_').replace('+', '-') | ||
|
||
seed_string = f"{self.config['user_id']}:{self.config['client_id']}:{dateTimeBase64}:{self.config['clinic_id']}:{unique_transaction_id}:{base64md5Payload}" | ||
seed_bytes = seed_string.encode('utf-8') | ||
|
||
hmac_digest = hmac.new(self.config['client_secret'].encode('utf-8'), seed_bytes, hashlib.sha512).digest() | ||
hmacBase64_bytes = base64.b64encode(hmac_digest) | ||
hmacBase64 = hmacBase64_bytes.decode('utf-8').replace('/', '_').replace('+', '-') | ||
|
||
request.headers.update({"Authorization": f"Digest {seed_string}:{hmacBase64}"}) | ||
# For GET requests this isn't needed, but an empty string md5'd works | ||
md5_payload = hashlib.md5( # noqa: S324 | ||
request_body.encode("utf-8") | ||
).hexdigest() | ||
base64_md5_payload_bytes = base64.b64encode(md5_payload.encode("utf-8")) | ||
base64_md5_payload = ( | ||
base64_md5_payload_bytes.decode("utf-8").replace("/", "_").replace("+", "-") | ||
) | ||
|
||
seed_string = ( | ||
f"{self.config['user_id']}:{self.config['client_id']}:{datetime_base_64}:" | ||
f"{self.config['clinic_id']}:{unique_transaction_id}:{base64_md5_payload}" | ||
) | ||
seed_bytes = seed_string.encode("utf-8") | ||
|
||
hmac_digest = hmac.new( | ||
self.config["client_secret"].encode("utf-8"), seed_bytes, hashlib.sha512 | ||
).digest() | ||
hmac_base64_bytes = base64.b64encode(hmac_digest) | ||
hmac_base64 = ( | ||
hmac_base64_bytes.decode("utf-8").replace("/", "_").replace("+", "-") | ||
) | ||
|
||
request.headers.update({"Authorization": f"Digest {seed_string}:{hmac_base64}"}) | ||
|
||
return request | ||
|
||
@classmethod | ||
def create_for_stream(cls, stream: SunwaveStream) -> SunwaveAuthenticator: # noqa: ANN001 | ||
def create_for_stream(cls, stream: SunwaveStream) -> SunwaveAuthenticator: | ||
return cls(stream=stream) |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,5 @@ | ||
"""Tests standard tap features using the built-in SDK tests library.""" | ||
|
||
import datetime | ||
|
||
from singer_sdk.testing import get_tap_test_class | ||
|
||
from tap_sunwave.tap import TapSunwave | ||
|
||
def test_tap_sunwave() -> None: | ||
pass |