From a4f00214324e36ba705addca9bedf42b3eb46795 Mon Sep 17 00:00:00 2001 From: Alex Burt Date: Tue, 10 Sep 2019 10:30:09 +0100 Subject: [PATCH] SDK-1076: Move create_nonce and create_timestamp into a utils module, and update references in the rest of the SDK --- sonar-project.properties | 2 +- yoti_python_sdk/endpoint.py | 25 ++++--------------------- yoti_python_sdk/http.py | 16 ++-------------- yoti_python_sdk/sandbox/endpoint.py | 15 ++------------- yoti_python_sdk/utils.py | 20 ++++++++++++++++++++ 5 files changed, 29 insertions(+), 49 deletions(-) create mode 100644 yoti_python_sdk/utils.py diff --git a/sonar-project.properties b/sonar-project.properties index d1e324ae..20048c32 100644 --- a/sonar-project.properties +++ b/sonar-project.properties @@ -1,6 +1,6 @@ sonar.projectKey = yoti-web-sdk:python sonar.projectName = python-sdk -sonar.projectVersion = 2.8.2 +sonar.projectVersion = 2.9.0 sonar.exclusions=yoti_python_sdk/tests/**,examples/** sonar.python.pylint.reportPath=coverage.out diff --git a/yoti_python_sdk/endpoint.py b/yoti_python_sdk/endpoint.py index 381ba721..0244d128 100644 --- a/yoti_python_sdk/endpoint.py +++ b/yoti_python_sdk/endpoint.py @@ -1,6 +1,4 @@ -from deprecated import deprecated -import time -import uuid +from yoti_python_sdk.utils import create_timestamp, create_nonce class Endpoint(object): @@ -14,10 +12,7 @@ def get_activity_details_request_path( return "/profile/{0}".format(decrypted_request_token) return "/profile/{0}?nonce={1}×tamp={2}&appId={3}".format( - decrypted_request_token, - self.__create_nonce(), - self.__create_timestamp(), - self.sdk_id, + decrypted_request_token, create_nonce(), create_timestamp(), self.sdk_id ) def get_aml_request_url(self, no_params=False): @@ -25,7 +20,7 @@ def get_aml_request_url(self, no_params=False): return "/aml-check" return "/aml-check?appId={0}×tamp={1}&nonce={2}".format( - self.sdk_id, self.__create_timestamp(), self.__create_nonce() + self.sdk_id, create_timestamp(), create_nonce() ) def get_dynamic_share_request_url(self, no_params=False): @@ -33,17 +28,5 @@ def get_dynamic_share_request_url(self, no_params=False): return "/qrcodes/app/{appid}".format(appid=self.sdk_id) return "/qrcodes/apps/{appid}?nonce={nonce}×tamp={timestamp}".format( - appid=self.sdk_id, - nonce=self.__create_nonce(), - timestamp=self.__create_timestamp(), + appid=self.sdk_id, nonce=create_nonce(), timestamp=create_timestamp() ) - - @staticmethod - @deprecated - def __create_nonce(): - return uuid.uuid4() - - @staticmethod - @deprecated - def __create_timestamp(): - return int(time.time() * 1000) diff --git a/yoti_python_sdk/http.py b/yoti_python_sdk/http.py index 7d883db6..9db9ad4a 100644 --- a/yoti_python_sdk/http.py +++ b/yoti_python_sdk/http.py @@ -1,4 +1,5 @@ from yoti_python_sdk.crypto import Crypto +from yoti_python_sdk.utils import create_nonce, create_timestamp from yoti_python_sdk.config import ( X_YOTI_AUTH_KEY, X_YOTI_AUTH_DIGEST, @@ -12,8 +13,6 @@ import yoti_python_sdk import requests -import uuid -import time try: # pragma: no cover from urllib.parse import urlencode @@ -230,10 +229,7 @@ def __append_query_params(self, query_params=None): Appends supplied query params in a dict to default query params. Returns a url encoded query param string """ - required = { - "nonce": self.__create_nonce(), - "timestamp": self.__create_timestamp(), - } + required = {"nonce": create_nonce(), "timestamp": create_timestamp()} query_params = self.__merge_dictionary(query_params, required) return "?{}".format(urlencode(query_params)) @@ -304,14 +300,6 @@ def __validate_request(self): if self.__http_method is None: raise ValueError("HTTP method must be specified") - @staticmethod - def __create_nonce(): - return uuid.uuid4() - - @staticmethod - def __create_timestamp(): - return int(time.time() * 1000) - def build(self): """ Builds a SignedRequest object with the supplied values diff --git a/yoti_python_sdk/sandbox/endpoint.py b/yoti_python_sdk/sandbox/endpoint.py index e671777a..194dd812 100644 --- a/yoti_python_sdk/sandbox/endpoint.py +++ b/yoti_python_sdk/sandbox/endpoint.py @@ -1,6 +1,5 @@ from yoti_python_sdk.endpoint import Endpoint -import time -import uuid +from yoti_python_sdk.utils import create_timestamp, create_nonce class SandboxEndpoint(Endpoint): @@ -9,15 +8,5 @@ def __init__(self, sdk_id): def get_sandbox_path(self): return "/apps/{sdk_id}/tokens?timestamp={timestamp}&nonce={nonce}".format( - sdk_id=self.sdk_id, - nonce=self.__create_nonce(), - timestamp=self.__create_timestamp(), + sdk_id=self.sdk_id, nonce=create_nonce(), timestamp=create_timestamp() ) - - @staticmethod - def __create_nonce(): - return uuid.uuid4() - - @staticmethod - def __create_timestamp(): - return int(time.time() * 1000) diff --git a/yoti_python_sdk/utils.py b/yoti_python_sdk/utils.py new file mode 100644 index 00000000..773b4112 --- /dev/null +++ b/yoti_python_sdk/utils.py @@ -0,0 +1,20 @@ +import uuid +import time + + +def create_nonce(): + """ + Create and return a nonce + \ + :return: the nonce + """ + return uuid.uuid4() + + +def create_timestamp(): + """ + Create and return a timestamp + + :return: the timestamp as a int + """ + return int(time.time() * 1000)