def __init__(self, sdk_id=None, pem_file_path=None): self.sdk_id = sdk_id or environ.get('YOTI_CLIENT_SDK_ID') pem_file_path_env = environ.get('YOTI_KEY_FILE_PATH') if pem_file_path is not None: error_source = 'argument specified in Client()' pem = self.__read_pem_file(pem_file_path, error_source) elif pem_file_path_env is not None: error_source = 'specified by the YOTI_KEY_FILE_PATH env variable' pem = self.__read_pem_file(pem_file_path_env, error_source) else: raise RuntimeError(NO_KEY_FILE_SPECIFIED_ERROR) self.__crypto = Crypto(pem)
def __init__(self, sdk_id=None, pem_file_path=None, request_handler=None): self.sdk_id = sdk_id or environ.get("YOTI_CLIENT_SDK_ID") pem_file_path_env = environ.get("YOTI_KEY_FILE_PATH", pem_file_path) if pem_file_path is not None: error_source = "argument specified in Client()" self.__crypto = Crypto.read_pem_file(pem_file_path, error_source) elif pem_file_path_env is not None: error_source = "specified by the YOTI_KEY_FILE_PATH env variable" self.__crypto = Crypto.read_pem_file(pem_file_path_env, error_source) else: raise RuntimeError(NO_KEY_FILE_SPECIFIED_ERROR) self.__endpoint = Endpoint(sdk_id) self.__request_handler = request_handler
def test_create_signed_request_with_crypto_object(valid_base_url, valid_endpoint): http_method = "GET" crypto = Crypto.read_pem_file(conftest.PEM_FILE_PATH) (SignedRequest.builder().with_pem_file(crypto).with_base_url( valid_base_url).with_endpoint(valid_endpoint).with_http_method( http_method).build())
def __init__(self, sdk_id, pem_file, sandbox_url): self.sdk_id = sdk_id self.__endpoint = SandboxEndpoint(sdk_id) self.__sandbox_url = sandbox_url pem_data = SandboxClient.__read_pem_file( pem_file, "failed in SandboxClient __init__") self.__crypto = Crypto(pem_data)
def with_pem_file(self, pem_file): """ Sets the PEM file to be used for signing the request. Can be an instance of yoti_python_sdk.crypto.Crypto or a path to a PEM file """ if isinstance(pem_file, Crypto): self.__crypto = pem_file else: self.__crypto = Crypto.read_pem_file(pem_file) return self
def __init__(self, sdk_id, pem_file, sandbox_url=None): if sandbox_url is None: sandbox_url = yoti_python_sandbox.DEFAULT_SANDBOX_URL self.sdk_id = sdk_id self.__endpoint = SandboxEndpoint(sdk_id) self.__sandbox_url = sandbox_url if isinstance(pem_file, Crypto): self.__crypto = pem_file else: self.__crypto = Crypto.read_pem_file(pem_file, "SandboxClient.__init__")
def crypto(): with open(PEM_FILE_PATH, 'rb') as pem_file: return Crypto(pem_file.read())
def test_strip_pkcs5_padding(with_padding, stripped): assert Crypto.strip_pkcs5_padding(with_padding) == stripped
class Client(object): def __init__(self, sdk_id=None, pem_file_path=None): self.sdk_id = sdk_id or environ.get('YOTI_CLIENT_SDK_ID') pem_file_path_env = environ.get('YOTI_KEY_FILE_PATH') if pem_file_path is not None: error_source = 'argument specified in Client()' pem = self.__read_pem_file(pem_file_path, error_source) elif pem_file_path_env is not None: error_source = 'specified by the YOTI_KEY_FILE_PATH env variable' pem = self.__read_pem_file(pem_file_path_env, error_source) else: raise RuntimeError(NO_KEY_FILE_SPECIFIED_ERROR) self.__crypto = Crypto(pem) @staticmethod def __read_pem_file(key_file_path, error_source): try: key_file_path = expanduser(key_file_path) if not isinstance(key_file_path, basestring) or not isfile(key_file_path): raise IOError('File not found: {0}'.format(key_file_path)) with open(key_file_path, 'rb') as pem_file: return pem_file.read().strip() except (AttributeError, IOError, TypeError, OSError) as exc: error = 'Could not read private key file: "{0}", passed as: {1} '.format( key_file_path, error_source) exception = '{0}: {1}'.format(type(exc).__name__, exc) raise RuntimeError('{0}: {1}'.format(error, exception)) def get_activity_details(self, encrypted_request_token): response = self.__make_request(encrypted_request_token) receipt = json.loads(response.text).get('receipt') encrypted_data = protobuf.Protobuf().current_user(receipt) if not encrypted_data: return ActivityDetails(receipt) unwrapped_key = self.__crypto.decrypt_token( receipt['wrapped_receipt_key']) decrypted_data = self.__crypto.decipher(unwrapped_key, encrypted_data.iv, encrypted_data.cipher_text) attribute_list = protobuf.Protobuf().attribute_list(decrypted_data) return ActivityDetails(receipt, attribute_list) def __make_request(self, encrypted_request_token): path = self.__get_request_path(encrypted_request_token) url = yoti_python_sdk.YOTI_API_ENDPOINT + path headers = self.__get_request_headers(path) response = requests.get(url=url, headers=headers) if not response.status_code == 200: raise RuntimeError('Unsuccessful Yoti API call: {0}'.format( response.text)) return response def __get_request_path(self, encrypted_request_token): token = self.__crypto.decrypt_token(encrypted_request_token).decode( 'utf-8') nonce = uuid.uuid4() timestamp = int(time.time() * 1000) return '/profile/{0}?nonce={1}×tamp={2}&appId={3}'.format( token, nonce, timestamp, self.sdk_id) def __get_request_headers(self, path): return { 'X-Yoti-Auth-Key': self.__crypto.get_public_key(), 'X-Yoti-Auth-Digest': self.__crypto.sign('GET&' + path), 'X-Yoti-SDK': SDK_IDENTIFIER, 'Content-Type': 'application/json', 'Accept': 'application/json' }