Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolve the package version in all http calls. #39

Merged
merged 1 commit into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions authress/api/token_verifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@
from urllib.parse import urlparse

from authress import rest
from authress.utils import service_client_token_provider
from authress.utils import service_client_token_provider, PackageVersionProvider

class TokenVerifier(object):
def __init__(self):
self.keyMap = dict()
self.package_version_provider = PackageVersionProvider()

def verify_token(self, authressCustomDomain, token, options=None):
sanitized_domain = re.sub(r"https?://", "", authressCustomDomain)
Expand Down Expand Up @@ -73,7 +74,12 @@ def get_public_key(self, jwkKeyListUrl, kid):

def get_key_uncached(self, jwkKeyListUrl, kid):
rest_client = rest.RESTClientObject()
result = rest_client.get_request(jwkKeyListUrl)

version = self.package_version_provider.get_version()
headers = {
'User-Agent': f'Authress SDK; Python; {version};'
}
result = rest_client.get_request(jwkKeyListUrl, headers=headers)

for index, key in enumerate(json.loads(result.data)['keys']):
if key['kid'] == kid:
Expand Down
12 changes: 5 additions & 7 deletions authress/http_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from urllib.parse import quote

from authress.api_response import ApiResponse
from authress.utils import service_client_token_provider
from authress.utils import service_client_token_provider, PackageVersionProvider
import authress.models
from authress import rest
from authress.exceptions import ApiValueError, ApiException, ServiceException
Expand Down Expand Up @@ -58,11 +58,9 @@ def __init__(self, host=None, access_key=None):
self.client_side_validation = False

self.service_client_token_provider = service_client_token_provider.ServiceClientTokenProvider(self.access_key, self.host)

this_directory = os.path.abspath(os.path.dirname(__file__))
with open(os.path.join(this_directory, 'VERSION')) as version_file:
VERSION = version_file.read().strip()
self.default_headers['User-Agent'] = f'Authress SDK; Python; {VERSION};'

version = PackageVersionProvider().get_version()
self.default_headers['User-Agent'] = f'Authress SDK; Python; {version};'

def set_token(self, token):
self.default_headers['Authorization'] = f'Bearer {token.replace("Bearer", "").strip()}'
Expand Down Expand Up @@ -204,7 +202,7 @@ def __call_api(

self.last_response = response_data

return_data = None # assuming derialization is not needed
return_data = None # assuming deserialization is not needed
# data needs deserialization or returns HTTP data (deserialized) only
if _preload_content or _return_http_data_only:
response_type = response_types_map.get(str(response_data.status), None)
Expand Down
1 change: 1 addition & 0 deletions authress/utils/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# flake8: noqa

from authress.utils.jwt_manager import JwtManager
from authress.utils.package_version_provider import PackageVersionProvider
from authress.utils.service_client_token_provider import ServiceClientTokenProvider
17 changes: 17 additions & 0 deletions authress/utils/package_version_provider.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import os

class PackageVersionProvider:
def __init__(self):
self.version = None

def get_version(self):
if self.version is None:
try:
this_directory = os.path.abspath(os.path.dirname(__file__))
with open(os.path.join(this_directory, '..', 'VERSION')) as version_file:
self.version = version_file.read().strip()
except Exception as e:
return 'InvalidPackageVersion'

return self.version

Loading