From c907f159abf9db0877910240dd8bef76709bd104 Mon Sep 17 00:00:00 2001 From: Donovan Date: Wed, 17 Jul 2024 08:59:59 -0500 Subject: [PATCH] rename functions and attributes --- nkode_api.py | 22 +++++++++++----------- src/customer.py | 6 +++--- src/session_cache.py | 2 +- src/{user_db_model.py => user.py} | 6 +++--- src/user_cipher_keys.py | 9 +++++---- src/user_interface.py | 16 ++++++++-------- test/test_nkode_interface.py | 2 +- test/test_user_cipher_keys.py | 15 ++++++++------- test/test_user_interface.py | 4 ++-- 9 files changed, 42 insertions(+), 40 deletions(-) rename src/{user_db_model.py => user.py} (90%) diff --git a/nkode_api.py b/nkode_api.py index cee8e98..33dea25 100644 --- a/nkode_api.py +++ b/nkode_api.py @@ -3,24 +3,24 @@ from pydantic import BaseModel from src.customer import Customer from src.models import NKodePolicy -from src.session_cache import SessionCacheModel +from src.session_cache import SessionCache from src.user_cipher_keys import UserCipherKeys -from src.user_db_model import UserDBModel +from src.user import User from src.user_interface import UserInterface from src.customer_interface import CustomerInterface class NKodeAPI(BaseModel): customers: dict[UUID, Customer] = {} - sessions: dict[UUID, SessionCacheModel] = {} + sessions: dict[UUID, SessionCache] = {} def generate_index_interface(self, customer_id: UUID) -> tuple[UUID, list[int]]: assert (customer_id in self.customers.keys()) customer = self.customers[customer_id] set_interface = UserInterface.new(customer.interface.attrs_per_key, customer.interface.numb_of_keys) - new_session = SessionCacheModel( + new_session = SessionCache( session_id=uuid4(), - set_interface=set_interface.interface_index, + set_interface=set_interface.attr_indices, customer_id=customer_id, ) self.sessions[new_session.session_id] = new_session @@ -35,7 +35,7 @@ class NKodeAPI(BaseModel): assert (username in customer.users.keys()) user = customer.users[username] user.user_interface.shuffle_interface() - return user.user_interface.interface_index + return user.user_interface.attr_indices def set_nkode( self, username: str, customer_id: UUID, @@ -50,14 +50,14 @@ class NKodeAPI(BaseModel): attrs_per_key = customer.interface.attrs_per_key assert (all(0 <= key <= numb_of_keys for key in key_selection)) set_interface = UserInterface( - interface_index=session.set_interface, + attr_indices=session.set_interface, attrs_per_key=attrs_per_key, numb_of_keys=numb_of_keys, ) set_interface.disperse_interface() session.username = username session.set_key_entry = key_selection - session.confirm_interface = set_interface.interface_index + session.confirm_interface = set_interface.attr_indices self.sessions[session_id] = session return self.sessions[session_id].confirm_interface @@ -76,14 +76,14 @@ class NKodeAPI(BaseModel): set_values = customer.interface.set_vals if not customer.valid_new_nkode(passcode): return False - new_user_keys = UserCipherKeys.new(numb_of_keys, attrs_per_key, set_values) + new_user_keys = UserCipherKeys.new(numb_of_keys, attrs_per_key, set_values, customer.nkode_policy.max_nkode_len) enciphered_passcode = new_user_keys.encipher_nkode(passcode, customer.interface) - new_user = UserDBModel( + new_user = User( username=username, enciphered_passcode=enciphered_passcode, user_keys=new_user_keys, user_interface=UserInterface( - interface_index=self.sessions[session_id].confirm_interface, + attr_indices=self.sessions[session_id].confirm_interface, attrs_per_key=attrs_per_key, numb_of_keys=numb_of_keys, ), diff --git a/src/customer.py b/src/customer.py index 6ab6a0d..48cd6e4 100644 --- a/src/customer.py +++ b/src/customer.py @@ -2,7 +2,7 @@ from uuid import UUID from pydantic import BaseModel from src.customer_interface import CustomerInterface from src.models import NKodePolicy -from src.user_db_model import UserDBModel +from src.user import User from src.utils import xor_lists @@ -10,9 +10,9 @@ class Customer(BaseModel): customer_id: UUID nkode_policy: NKodePolicy interface: CustomerInterface - users: dict[str, UserDBModel] + users: dict[str, User] - def add_new_user(self, user: UserDBModel): + def add_new_user(self, user: User): self.users[user.username] = user def valid_key_entry(self, username, selected_keys) -> bool: diff --git a/src/session_cache.py b/src/session_cache.py index 8ca5891..df72b9e 100644 --- a/src/session_cache.py +++ b/src/session_cache.py @@ -3,7 +3,7 @@ from uuid import UUID from pydantic import BaseModel -class SessionCacheModel(BaseModel): +class SessionCache(BaseModel): session_id: UUID set_interface: list[int] | None = None confirm_interface: list[int] | None = None diff --git a/src/user_db_model.py b/src/user.py similarity index 90% rename from src/user_db_model.py rename to src/user.py index 4bfd3eb..9ef2c82 100644 --- a/src/user_db_model.py +++ b/src/user.py @@ -7,7 +7,7 @@ from src.user_interface import UserInterface from src.utils import xor_lists -class UserDBModel(BaseModel): +class User(BaseModel): username: str enciphered_passcode: EncipheredNKode user_keys: UserCipherKeys @@ -23,8 +23,8 @@ class UserDBModel(BaseModel): self.user_keys = UserCipherKeys.new( customer_interface.numb_of_keys, customer_interface.attrs_per_key, - customer_interface.set_vals - + customer_interface.set_vals, + self.user_keys.max_nkode_len ) self.enciphered_passcode = self.user_keys.encipher_nkode(passcode_attr_idx, customer_interface) self.renew = False diff --git a/src/user_cipher_keys.py b/src/user_cipher_keys.py index 4653c59..779cf89 100644 --- a/src/user_cipher_keys.py +++ b/src/user_cipher_keys.py @@ -15,10 +15,10 @@ class UserCipherKeys(BaseModel): pass_key: list[int] mask_key: list[int] salt: bytes - max_nkode_len: int = 10 + max_nkode_len: int @staticmethod - def new(numb_of_keys: int, attrs_per_key: int, set_values: list[int]): + def new(numb_of_keys: int, attrs_per_key: int, set_values: list[int], max_nkode_len: int): assert len(set_values) == attrs_per_key set_key = generate_random_nonrepeating_list(attrs_per_key) @@ -26,10 +26,11 @@ class UserCipherKeys(BaseModel): return UserCipherKeys( alpha_key=generate_random_nonrepeating_list(attrs_per_key * numb_of_keys), - pass_key=generate_random_nonrepeating_list(numb_of_keys), - mask_key=generate_random_nonrepeating_list(numb_of_keys), + pass_key=generate_random_nonrepeating_list(max_nkode_len), + mask_key=generate_random_nonrepeating_list(max_nkode_len), set_key=set_key, salt=bcrypt.gensalt(), + max_nkode_len=max_nkode_len ) def pad_user_mask(self, user_mask: list[int], set_vals: list[int]) -> list[int]: diff --git a/src/user_interface.py b/src/user_interface.py index c67fa1b..e1d0455 100644 --- a/src/user_interface.py +++ b/src/user_interface.py @@ -4,7 +4,7 @@ from src.utils import list_to_matrix, secure_fisher_yates_shuffle, matrix_to_lis class UserInterface(BaseModel): - interface_index: list[int] + attr_indices: list[int] attrs_per_key: int numb_of_keys: int @@ -12,7 +12,7 @@ class UserInterface(BaseModel): def new(attrs_per_key: int, numb_of_keys: int): # Todo: this a hack do a proper random interface interface = UserInterface( - interface_index=list(range(attrs_per_key * numb_of_keys)), + attr_indices=list(range(attrs_per_key * numb_of_keys)), attrs_per_key=attrs_per_key, numb_of_keys=numb_of_keys, ) @@ -22,10 +22,10 @@ class UserInterface(BaseModel): return interface def disperse_interface(self): - user_interface_matrix = list_to_matrix(self.interface_index, self.attrs_per_key) + user_interface_matrix = list_to_matrix(self.attr_indices, self.attrs_per_key) shuffled_keys = secure_fisher_yates_shuffle(user_interface_matrix) dispersed_interface = self._random_attribute_rotation(shuffled_keys, list(range(self.attrs_per_key))) - self.interface_index = matrix_to_list(dispersed_interface) + self.attr_indices = matrix_to_list(dispersed_interface) @staticmethod def matrix_transpose(interface: list[list[int]]) -> list[list[int]]: @@ -37,7 +37,7 @@ class UserInterface(BaseModel): # randomly shuffle half the sets. if attrs_per_key is odd, randomly add one 50% of the time numb_of_selected_sets += choice([0, 1]) if (self.attrs_per_key & 1) == 1 else 0 selected_sets = secure_fisher_yates_shuffle(list(range(self.attrs_per_key)))[:numb_of_selected_sets] - user_interface_matrix = list_to_matrix(self.interface_index, self.attrs_per_key) + user_interface_matrix = list_to_matrix(self.attr_indices, self.attrs_per_key) shuffled_keys = secure_fisher_yates_shuffle(user_interface_matrix) interface_by_sets = [] for idx, attrs in enumerate(self.matrix_transpose(shuffled_keys)): @@ -45,7 +45,7 @@ class UserInterface(BaseModel): interface_by_sets.append(secure_fisher_yates_shuffle(attrs)) else: interface_by_sets.append(attrs) - self.interface_index = matrix_to_list(self.matrix_transpose(interface_by_sets)) + self.attr_indices = matrix_to_list(self.matrix_transpose(interface_by_sets)) def _random_attribute_rotation(self, user_interface: list[list[int]], selected_sets: list[int]) -> list[list[int]]: attr_rotation = secure_fisher_yates_shuffle(list(range(self.numb_of_keys)))[:self.attrs_per_key] @@ -59,7 +59,7 @@ class UserInterface(BaseModel): return self.matrix_transpose(transposed_user_interface) def attribute_adjacency_graph(self) -> dict[int, set[int]]: - user_interface_keypad = list_to_matrix(self.interface_index, self.attrs_per_key) + user_interface_keypad = list_to_matrix(self.attr_indices, self.attrs_per_key) graph = {} for key in user_interface_keypad: for attr in key: @@ -70,5 +70,5 @@ class UserInterface(BaseModel): def get_key_attr_idxs(self, key_numb: int) -> list[int]: assert (0 <= key_numb < self.numb_of_keys) - keypad_attr_idx = list_to_matrix(self.interface_index, self.attrs_per_key) + keypad_attr_idx = list_to_matrix(self.attr_indices, self.attrs_per_key) return keypad_attr_idx[key_numb] diff --git a/test/test_nkode_interface.py b/test/test_nkode_interface.py index 8b6d4bd..1519eef 100644 --- a/test/test_nkode_interface.py +++ b/test/test_nkode_interface.py @@ -9,6 +9,6 @@ from src.user_interface import UserInterface def test_attr_set_idx(numb_of_keys, attrs_per_key): user_interface = UserInterface.new(attrs_per_key, numb_of_keys) for attr_idx in range(70): - user_interface_idx = user_interface.interface_index[attr_idx] + user_interface_idx = user_interface.attr_indices[attr_idx] assert (attr_idx % attrs_per_key == user_interface_idx % attrs_per_key) diff --git a/test/test_user_cipher_keys.py b/test/test_user_cipher_keys.py index 3e76dcf..df420ec 100644 --- a/test/test_user_cipher_keys.py +++ b/test/test_user_cipher_keys.py @@ -16,20 +16,21 @@ def test_encode_decode_base64(passcode_len): assert (len(data) == len(decoded)) assert (all(data[idx] == decoded[idx] for idx in range(passcode_len))) + @pytest.mark.parametrize( - "numb_of_keys,attrs_per_key", + "numb_of_keys,attrs_per_key,max_nkode_len", [ - (10, 7,) + (10, 7,10) ]) -def test_decode_mask(numb_of_keys, attrs_per_key): +def test_decode_mask(numb_of_keys, attrs_per_key, max_nkode_len): customer = CustomerInterface.new(numb_of_keys, attrs_per_key) - passcode_entry = generate_random_nonrepeating_list(numb_of_keys*attrs_per_key, max_val=70)[:4] + passcode_entry = generate_random_nonrepeating_list(numb_of_keys * attrs_per_key, max_val=70)[:4] passcode_values = [customer.attr_vals[idx] for idx in passcode_entry] set_vals = customer.set_vals - user_keys = UserCipherKeys.new(numb_of_keys, attrs_per_key, set_vals) + user_keys = UserCipherKeys.new(numb_of_keys, attrs_per_key, set_vals, max_nkode_len) passcode = user_keys.encipher_nkode(passcode_entry, customer) orig_passcode_set_vals = [customer.get_attr_set_val(attr) for attr in passcode_values] passcode_set_vals = user_keys.decipher_mask(passcode.mask, set_vals, len(passcode_entry)) - assert(len(passcode_set_vals) == len(orig_passcode_set_vals)) - assert(all(orig_passcode_set_vals[idx] == passcode_set_vals[idx] for idx in range(len(passcode_set_vals)))) + assert (len(passcode_set_vals) == len(orig_passcode_set_vals)) + assert (all(orig_passcode_set_vals[idx] == passcode_set_vals[idx] for idx in range(len(passcode_set_vals)))) diff --git a/test/test_user_interface.py b/test/test_user_interface.py index a31635d..d240e52 100644 --- a/test/test_user_interface.py +++ b/test/test_user_interface.py @@ -24,9 +24,9 @@ def test_shuffle_attrs(user_interface): - every attribute is adjacent to every other attribute with uniform distribution - the order in which the attributes move from key to key is random (i.e. the distance traveled is uniform) """ - pre_shuffle_interface = user_interface.interface_index + pre_shuffle_interface = user_interface.attr_indices user_interface.shuffle_interface() - post_shuffle_interface = user_interface.interface_index + post_shuffle_interface = user_interface.attr_indices for i in range(1000): assert (not all( post_shuffle_interface[idx] == pre_shuffle_interface[idx] for idx in range(len(post_shuffle_interface))