refactor loose files

This commit is contained in:
2025-03-07 13:04:10 -06:00
parent 8597ba1090
commit 88b533c27b
6 changed files with 152 additions and 108 deletions

View File

@@ -1,6 +1,6 @@
from jinja2 import Environment, FileSystemLoader from jinja2 import Environment, FileSystemLoader
import os import os
from nkode_api import NKodeAPI from src.nkode_api import NKodeAPI
from src.models import NKodePolicy, KeypadSize, EncipheredNKode from src.models import NKodePolicy, KeypadSize, EncipheredNKode
from src.user_cipher_keys import UserCipherKeys from src.user_cipher_keys import UserCipherKeys
from src.utils import list_to_matrix, matrix_transpose, xor_lists from src.utils import list_to_matrix, matrix_transpose, xor_lists
@@ -29,7 +29,7 @@ def keypad_view(interface: list[int], attrs_per_key: int):
def render_nkode_authentication(data: dict): def render_nkode_authentication(data: dict):
# Set up the Jinja2 environment and template loader # Set up the Jinja2 environment and template loader
file_loader = FileSystemLoader('./docs') file_loader = FileSystemLoader('')
env = Environment(loader=file_loader) env = Environment(loader=file_loader)
# Load the template # Load the template

View File

@@ -3,7 +3,7 @@
{ {
"cell_type": "code", "cell_type": "code",
"source": [ "source": [
"from nkode_api import NKodeAPI\n", "from src.nkode_api import NKodeAPI\n",
"from src.models import NKodePolicy, KeypadSize\n", "from src.models import NKodePolicy, KeypadSize\n",
"from src.utils import list_to_matrix, matrix_transpose\n", "from src.utils import list_to_matrix, matrix_transpose\n",
"from secrets import choice\n", "from secrets import choice\n",
@@ -12,12 +12,12 @@
"metadata": { "metadata": {
"collapsed": false, "collapsed": false,
"ExecuteTime": { "ExecuteTime": {
"end_time": "2024-08-05T17:40:17.475149Z", "end_time": "2025-03-07T19:01:08.154404Z",
"start_time": "2024-08-05T17:40:17.473025Z" "start_time": "2025-03-07T19:01:08.149432Z"
} }
}, },
"outputs": [], "outputs": [],
"execution_count": 12 "execution_count": 58
}, },
{ {
"cell_type": "code", "cell_type": "code",
@@ -38,12 +38,12 @@
"metadata": { "metadata": {
"collapsed": false, "collapsed": false,
"ExecuteTime": { "ExecuteTime": {
"end_time": "2024-08-05T17:40:17.556128Z", "end_time": "2025-03-07T19:01:08.165511Z",
"start_time": "2024-08-05T17:40:17.552985Z" "start_time": "2025-03-07T19:01:08.162339Z"
} }
}, },
"outputs": [], "outputs": [],
"execution_count": 13 "execution_count": 59
}, },
{ {
"cell_type": "code", "cell_type": "code",
@@ -53,12 +53,12 @@
"metadata": { "metadata": {
"collapsed": false, "collapsed": false,
"ExecuteTime": { "ExecuteTime": {
"end_time": "2024-08-05T17:40:17.559851Z", "end_time": "2025-03-07T19:01:08.174320Z",
"start_time": "2024-08-05T17:40:17.557794Z" "start_time": "2025-03-07T19:01:08.172724Z"
} }
}, },
"outputs": [], "outputs": [],
"execution_count": 14 "execution_count": 60
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
@@ -98,12 +98,12 @@
"metadata": { "metadata": {
"collapsed": false, "collapsed": false,
"ExecuteTime": { "ExecuteTime": {
"end_time": "2024-08-05T17:40:17.740656Z", "end_time": "2025-03-07T19:01:08.378578Z",
"start_time": "2024-08-05T17:40:17.570041Z" "start_time": "2025-03-07T19:01:08.188114Z"
} }
}, },
"outputs": [], "outputs": [],
"execution_count": 15 "execution_count": 61
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
@@ -140,8 +140,8 @@
"metadata": { "metadata": {
"collapsed": false, "collapsed": false,
"ExecuteTime": { "ExecuteTime": {
"end_time": "2024-08-05T17:40:17.744240Z", "end_time": "2025-03-07T19:01:08.387613Z",
"start_time": "2024-08-05T17:40:17.741773Z" "start_time": "2025-03-07T19:01:08.385501Z"
} }
}, },
"outputs": [ "outputs": [
@@ -149,17 +149,17 @@
"name": "stdout", "name": "stdout",
"output_type": "stream", "output_type": "stream",
"text": [ "text": [
"Customer Sets: [41463, 23817, 10115, 59621, 17701, 39232]\n", "Customer Sets: [14533, 13441, 52408, 39610, 49828, 11300]\n",
"Customer Attributes:\n", "Customer Attributes:\n",
"[15101, 42072, 1835, 15413, 46991, 41978]\n", "[50415, 3350, 62907, 34493, 46982, 55292]\n",
"[47133, 25183, 43097, 17581, 20893, 52753]\n", "[23503, 57678, 17001, 8963, 4893, 53685]\n",
"[14863, 31428, 41789, 18633, 3962, 50372]\n", "[25912, 9324, 2770, 57761, 57056, 5837]\n",
"[53357, 51786, 31149, 59745, 30907, 16058]\n", "[49831, 16518, 53473, 35853, 12433, 20763]\n",
"[60389, 36053, 8246, 53494, 23730, 15317]\n" "[60930, 19614, 2083, 2879, 58781, 13705]\n"
] ]
} }
], ],
"execution_count": 16 "execution_count": 62
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
@@ -183,8 +183,8 @@
"metadata": { "metadata": {
"collapsed": false, "collapsed": false,
"ExecuteTime": { "ExecuteTime": {
"end_time": "2024-08-05T17:40:17.747131Z", "end_time": "2025-03-07T19:01:08.412394Z",
"start_time": "2024-08-05T17:40:17.744937Z" "start_time": "2025-03-07T19:01:08.410114Z"
} }
}, },
"outputs": [ "outputs": [
@@ -193,16 +193,16 @@
"output_type": "stream", "output_type": "stream",
"text": [ "text": [
"Set to Attribute Map:\n", "Set to Attribute Map:\n",
"41463: [15101, 47133, 14863, 53357, 60389]\n", "14533: [50415, 23503, 25912, 49831, 60930]\n",
"23817: [42072, 25183, 31428, 51786, 36053]\n", "13441: [3350, 57678, 9324, 16518, 19614]\n",
"10115: [1835, 43097, 41789, 31149, 8246]\n", "52408: [62907, 17001, 2770, 53473, 2083]\n",
"59621: [15413, 17581, 18633, 59745, 53494]\n", "39610: [34493, 8963, 57761, 35853, 2879]\n",
"17701: [46991, 20893, 3962, 30907, 23730]\n", "49828: [46982, 4893, 57056, 12433, 58781]\n",
"39232: [41978, 52753, 50372, 16058, 15317]\n" "11300: [55292, 53685, 5837, 20763, 13705]\n"
] ]
} }
], ],
"execution_count": 17 "execution_count": 63
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
@@ -232,27 +232,27 @@
"metadata": { "metadata": {
"collapsed": false, "collapsed": false,
"ExecuteTime": { "ExecuteTime": {
"end_time": "2024-08-05T17:40:17.751757Z", "end_time": "2025-03-07T19:01:08.439639Z",
"start_time": "2024-08-05T17:40:17.748531Z" "start_time": "2025-03-07T19:01:08.436629Z"
} }
}, },
"outputs": [ "outputs": [
{ {
"data": { "data": {
"text/plain": [ "text/plain": [
"[[29, 3, 19, 12, 26],\n", "[[3, 20, 10, 6, 29],\n",
" [5, 27, 1, 0, 8],\n", " [27, 26, 4, 24, 11],\n",
" [23, 9, 13, 24, 20],\n", " [15, 2, 22, 0, 5],\n",
" [11, 21, 25, 18, 14],\n", " [9, 8, 16, 12, 23],\n",
" [17, 15, 7, 6, 2]]" " [21, 14, 28, 18, 17]]"
] ]
}, },
"execution_count": 18, "execution_count": 64,
"metadata": {}, "metadata": {},
"output_type": "execute_result" "output_type": "execute_result"
} }
], ],
"execution_count": 18 "execution_count": 64
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
@@ -280,8 +280,8 @@
"metadata": { "metadata": {
"collapsed": false, "collapsed": false,
"ExecuteTime": { "ExecuteTime": {
"end_time": "2024-08-05T17:40:17.755453Z", "end_time": "2025-03-07T19:01:08.474579Z",
"start_time": "2024-08-05T17:40:17.752507Z" "start_time": "2025-03-07T19:01:08.471813Z"
} }
}, },
"outputs": [ "outputs": [
@@ -290,19 +290,19 @@
"output_type": "stream", "output_type": "stream",
"text": [ "text": [
"Keypad View\n", "Keypad View\n",
"Key 0: [29, 3, 19, 12, 26]\n", "Key 0: [3, 20, 10, 6, 29]\n",
"Key 1: [5, 27, 1, 0, 8]\n", "Key 1: [27, 26, 4, 24, 11]\n",
"Key 2: [23, 9, 13, 24, 20]\n", "Key 2: [15, 2, 22, 0, 5]\n",
"Key 3: [11, 21, 25, 18, 14]\n", "Key 3: [9, 8, 16, 12, 23]\n",
"Key 4: [17, 15, 7, 6, 2]\n", "Key 4: [21, 14, 28, 18, 17]\n",
"User Passcode: [29, 3, 19, 12]\n", "User Passcode: [3, 20, 10, 6]\n",
"Selected Keys\n", "Selected Keys\n",
"[0, 0, 0, 0]\n", "[0, 0, 0, 0]\n",
"User Passcode Server-side Attributes: [15317, 15413, 51786, 14863]\n" "User Passcode Server-side Attributes: [34493, 53473, 4893, 23503]\n"
] ]
} }
], ],
"execution_count": 19 "execution_count": 65
}, },
{ {
"metadata": {}, "metadata": {},
@@ -320,8 +320,8 @@
"metadata": { "metadata": {
"collapsed": false, "collapsed": false,
"ExecuteTime": { "ExecuteTime": {
"end_time": "2024-08-05T17:40:17.760514Z", "end_time": "2025-03-07T19:01:08.496620Z",
"start_time": "2024-08-05T17:40:17.758171Z" "start_time": "2025-03-07T19:01:08.494090Z"
} }
}, },
"outputs": [ "outputs": [
@@ -330,17 +330,17 @@
"output_type": "stream", "output_type": "stream",
"text": [ "text": [
"Keypad View\n", "Keypad View\n",
"Key 0: [23, 3, 25, 6, 8]\n", "Key 0: [15, 8, 28, 24, 29]\n",
"Key 1: [17, 9, 19, 0, 14]\n", "Key 1: [27, 20, 22, 12, 17]\n",
"Key 2: [5, 15, 13, 18, 26]\n", "Key 2: [9, 14, 4, 6, 5]\n",
"Key 3: [11, 27, 7, 12, 20]\n", "Key 3: [3, 2, 16, 18, 11]\n",
"Key 4: [29, 21, 1, 24, 2]\n", "Key 4: [21, 26, 10, 0, 23]\n",
"Selected Keys\n", "Selected Keys\n",
"[4, 0, 1, 3]\n" "[3, 1, 4, 2]\n"
] ]
} }
], ],
"execution_count": 20 "execution_count": 66
}, },
{ {
"cell_type": "code", "cell_type": "code",
@@ -352,8 +352,8 @@
"metadata": { "metadata": {
"collapsed": false, "collapsed": false,
"ExecuteTime": { "ExecuteTime": {
"end_time": "2024-08-05T17:40:18.401946Z", "end_time": "2025-03-07T19:01:09.173784Z",
"start_time": "2024-08-05T17:40:17.761154Z" "start_time": "2025-03-07T19:01:08.522178Z"
} }
}, },
"outputs": [ "outputs": [
@@ -365,7 +365,7 @@
] ]
} }
], ],
"execution_count": 21 "execution_count": 67
}, },
{ {
"metadata": {}, "metadata": {},
@@ -384,8 +384,8 @@
{ {
"metadata": { "metadata": {
"ExecuteTime": { "ExecuteTime": {
"end_time": "2024-08-05T17:54:37.484873Z", "end_time": "2025-03-07T19:01:09.193871Z",
"start_time": "2024-08-05T17:54:37.480227Z" "start_time": "2025-03-07T19:01:09.190856Z"
} }
}, },
"cell_type": "code", "cell_type": "code",
@@ -422,12 +422,12 @@
"name": "stdout", "name": "stdout",
"output_type": "stream", "output_type": "stream",
"text": [ "text": [
"Passcode Set Vals: [17701, 39232, 10115, 23817]\n", "Passcode Set Vals: [39610, 52408, 49828, 14533]\n",
"Passcode Attr Vals: [20893, 16058, 41789, 25183]\n" "Passcode Attr Vals: [34493, 53473, 4893, 23503]\n"
] ]
} }
], ],
"execution_count": 36 "execution_count": 68
}, },
{ {
"metadata": {}, "metadata": {},
@@ -449,8 +449,8 @@
{ {
"metadata": { "metadata": {
"ExecuteTime": { "ExecuteTime": {
"end_time": "2024-08-05T17:52:29.553010Z", "end_time": "2025-03-07T19:01:09.223843Z",
"start_time": "2024-08-05T17:52:29.549861Z" "start_time": "2025-03-07T19:01:09.221595Z"
} }
}, },
"cell_type": "code", "cell_type": "code",
@@ -466,7 +466,7 @@
"mask = user_keys.encode_base64_str(ciphered_mask)" "mask = user_keys.encode_base64_str(ciphered_mask)"
], ],
"outputs": [], "outputs": [],
"execution_count": 28 "execution_count": 69
}, },
{ {
"metadata": {}, "metadata": {},
@@ -483,8 +483,8 @@
{ {
"metadata": { "metadata": {
"ExecuteTime": { "ExecuteTime": {
"end_time": "2024-08-05T17:52:34.104235Z", "end_time": "2025-03-07T19:01:09.479816Z",
"start_time": "2024-08-05T17:52:33.794884Z" "start_time": "2025-03-07T19:01:09.236822Z"
} }
}, },
"cell_type": "code", "cell_type": "code",
@@ -495,7 +495,7 @@
"from src.utils import int_array_to_bytes\n", "from src.utils import int_array_to_bytes\n",
"\n", "\n",
"ciphered_customer_attrs = xor_lists(customer.attributes.attr_vals, user_keys.alpha_key)\n", "ciphered_customer_attrs = xor_lists(customer.attributes.attr_vals, user_keys.alpha_key)\n",
"passcode_ciphered_attrs = [ciphered_customer_attrs[idx] for idx in passcode]\n", "passcode_ciphered_attrs = [ciphered_customer_attrs[idx] for idx in user_passcode]\n",
"pad_len = customer.nkode_policy.max_nkode_len - passcode_len\n", "pad_len = customer.nkode_policy.max_nkode_len - passcode_len\n",
"\n", "\n",
"passcode_ciphered_attrs.extend([0 for _ in range(pad_len)])\n", "passcode_ciphered_attrs.extend([0 for _ in range(pad_len)])\n",
@@ -508,13 +508,13 @@
"code = hashed_data.decode(\"utf-8\")" "code = hashed_data.decode(\"utf-8\")"
], ],
"outputs": [], "outputs": [],
"execution_count": 29 "execution_count": 70
}, },
{ {
"metadata": { "metadata": {
"ExecuteTime": { "ExecuteTime": {
"end_time": "2024-08-05T17:52:34.205433Z", "end_time": "2025-03-07T19:01:09.495420Z",
"start_time": "2024-08-05T17:52:34.203278Z" "start_time": "2025-03-07T19:01:09.488943Z"
} }
}, },
"cell_type": "code", "cell_type": "code",
@@ -527,7 +527,7 @@
")" ")"
], ],
"outputs": [], "outputs": [],
"execution_count": 30 "execution_count": 71
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
@@ -553,8 +553,8 @@
"metadata": { "metadata": {
"collapsed": false, "collapsed": false,
"ExecuteTime": { "ExecuteTime": {
"end_time": "2024-08-05T17:52:38.297363Z", "end_time": "2025-03-07T19:01:09.736403Z",
"start_time": "2024-08-05T17:52:37.990052Z" "start_time": "2025-03-07T19:01:09.500580Z"
} }
}, },
"outputs": [ "outputs": [
@@ -563,17 +563,17 @@
"output_type": "stream", "output_type": "stream",
"text": [ "text": [
"Keypad View\n", "Keypad View\n",
"Key 0: [6, 19, 14, 15, 4, 17]\n", "Key 0: [24, 25, 8, 15, 4, 29]\n",
"Key 1: [24, 13, 26, 3, 28, 23]\n", "Key 1: [12, 19, 2, 3, 16, 23]\n",
"Key 2: [0, 7, 2, 9, 16, 5]\n", "Key 2: [18, 7, 26, 21, 28, 5]\n",
"Key 3: [18, 1, 8, 27, 22, 11]\n", "Key 3: [6, 1, 14, 9, 10, 11]\n",
"Key 4: [12, 25, 20, 21, 10, 29]\n", "Key 4: [0, 13, 20, 27, 22, 17]\n",
"Selected Keys: [4, 1, 0, 4]\n", "Selected Keys: [1, 4, 3, 3]\n",
"True\n" "True\n"
] ]
} }
], ],
"execution_count": 31 "execution_count": 72
}, },
{ {
"metadata": {}, "metadata": {},
@@ -605,8 +605,8 @@
{ {
"metadata": { "metadata": {
"ExecuteTime": { "ExecuteTime": {
"end_time": "2024-08-05T17:59:58.753205Z", "end_time": "2025-03-07T19:01:09.774425Z",
"start_time": "2024-08-05T17:59:58.749714Z" "start_time": "2025-03-07T19:01:09.772200Z"
} }
}, },
"cell_type": "code", "cell_type": "code",
@@ -629,11 +629,11 @@
"name": "stdout", "name": "stdout",
"output_type": "stream", "output_type": "stream",
"text": [ "text": [
"[39232, 59621, 23817, 41463]\n" "[39610, 52408, 49828, 14533]\n"
] ]
} }
], ],
"execution_count": 38 "execution_count": 73
}, },
{ {
"metadata": {}, "metadata": {},
@@ -643,8 +643,8 @@
{ {
"metadata": { "metadata": {
"ExecuteTime": { "ExecuteTime": {
"end_time": "2024-08-05T17:54:03.252644Z", "end_time": "2025-03-07T19:01:09.795190Z",
"start_time": "2024-08-05T17:54:03.249271Z" "start_time": "2025-03-07T19:01:09.792579Z"
} }
}, },
"cell_type": "code", "cell_type": "code",
@@ -669,7 +669,7 @@
] ]
} }
], ],
"execution_count": 35 "execution_count": 74
}, },
{ {
"metadata": {}, "metadata": {},
@@ -677,15 +677,28 @@
"source": "### Compare Enciphered Passcodes" "source": "### Compare Enciphered Passcodes"
}, },
{ {
"metadata": {}, "metadata": {
"ExecuteTime": {
"end_time": "2025-03-07T19:01:10.057899Z",
"start_time": "2025-03-07T19:01:09.819860Z"
}
},
"cell_type": "code", "cell_type": "code",
"source": [ "source": [
"enciphered_nkode = user_keys.encipher_salt_hash_code(presumed_selected_attributes_idx, customer.attributes)\n", "enciphered_nkode = user_keys.encipher_salt_hash_code(presumed_selected_attributes_idx, customer.attributes)\n",
"\n", "\n",
"print(enciphered_nkode == user.enciphered_passcode.code)\n" "print(enciphered_nkode == user.enciphered_passcode.code)\n"
], ],
"outputs": [], "outputs": [
"execution_count": null {
"name": "stdout",
"output_type": "stream",
"text": [
"True\n"
]
}
],
"execution_count": 75
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
@@ -701,7 +714,12 @@
} }
}, },
{ {
"metadata": {}, "metadata": {
"ExecuteTime": {
"end_time": "2025-03-07T19:01:11.147274Z",
"start_time": "2025-03-07T19:01:10.066784Z"
}
},
"cell_type": "code", "cell_type": "code",
"source": [ "source": [
"def print_user_enciphered_code():\n", "def print_user_enciphered_code():\n",
@@ -719,8 +737,19 @@
"print(success)\n", "print(success)\n",
"print_user_enciphered_code()" "print_user_enciphered_code()"
], ],
"outputs": [], "outputs": [
"execution_count": null {
"name": "stdout",
"output_type": "stream",
"text": [
"mask: xYa4LE04KVq0jRcaFG9HA8BZHqo=, code: $2b$12$GdGDlzHY6rt.It6xq.EBIud5PgIWKObJdMms3AaYz1JWK1nf4Jj5G\n",
"mask: xYa4LE04KVq0jRcaFG9HA8BZHqo=, code: $2b$12$GdGDlzHY6rt.It6xq.EBIud5PgIWKObJdMms3AaYz1JWK1nf4Jj5G\n",
"True\n",
"mask: jTZF7KTs3zviTwTt7yqXk7BEK7I=, code: $2b$12$BIJdJ/77MAKNtETz8WSB9uWpJo4.jisH/HsN5o55ez294T1a40rxm\n"
]
}
],
"execution_count": 76
}, },
{ {
"metadata": {}, "metadata": {},
@@ -732,7 +761,12 @@
] ]
}, },
{ {
"metadata": {}, "metadata": {
"ExecuteTime": {
"end_time": "2025-03-07T19:01:11.355505Z",
"start_time": "2025-03-07T19:01:11.153001Z"
}
},
"cell_type": "code", "cell_type": "code",
"source": [ "source": [
"old_attrs = customer.attributes.attr_vals.copy()\n", "old_attrs = customer.attributes.attr_vals.copy()\n",
@@ -742,7 +776,7 @@
"new_sets = customer.attributes.set_vals" "new_sets = customer.attributes.set_vals"
], ],
"outputs": [], "outputs": [],
"execution_count": null "execution_count": 77
}, },
{ {
"metadata": {}, "metadata": {},
@@ -753,7 +787,12 @@
] ]
}, },
{ {
"metadata": {}, "metadata": {
"ExecuteTime": {
"end_time": "2025-03-07T19:01:11.363643Z",
"start_time": "2025-03-07T19:01:11.361690Z"
}
},
"cell_type": "code", "cell_type": "code",
"source": [ "source": [
"attrs_xor = xor_lists(new_attrs, old_attrs)\n", "attrs_xor = xor_lists(new_attrs, old_attrs)\n",
@@ -764,7 +803,7 @@
" user.user_keys.alpha_key = xor_lists(user.user_keys.alpha_key, attrs_xor)" " user.user_keys.alpha_key = xor_lists(user.user_keys.alpha_key, attrs_xor)"
], ],
"outputs": [], "outputs": [],
"execution_count": null "execution_count": 78
}, },
{ {
"metadata": {}, "metadata": {},
@@ -772,7 +811,12 @@
"source": "### Refresh User Keys" "source": "### Refresh User Keys"
}, },
{ {
"metadata": {}, "metadata": {
"ExecuteTime": {
"end_time": "2025-03-07T19:01:11.989265Z",
"start_time": "2025-03-07T19:01:11.369911Z"
}
},
"cell_type": "code", "cell_type": "code",
"source": [ "source": [
"user.user_keys = UserCipherKeys.new(\n", "user.user_keys = UserCipherKeys.new(\n",
@@ -784,7 +828,7 @@
"user.renew = False" "user.renew = False"
], ],
"outputs": [], "outputs": [],
"execution_count": null "execution_count": 79
} }
], ],
"metadata": { "metadata": {

View File

@@ -1,5 +1,5 @@
import pytest import pytest
from nkode_api import NKodeAPI from src.nkode_api import NKodeAPI
from src.models import NKodePolicy, KeypadSize from src.models import NKodePolicy, KeypadSize