Files
pynkode/nkode_tutorial.ipynb
2024-07-19 10:39:05 -05:00

499 lines
14 KiB
Plaintext

{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"outputs": [],
"source": [
"from nkode_api import NKodeAPI\n",
"from src.models import NKodePolicy, KeypadSize\n",
"from src.utils import list_to_matrix, matrix_transpose\n",
"from secrets import choice\n",
"from string import ascii_lowercase"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-07-19T15:38:32.636706Z",
"start_time": "2024-07-19T15:38:32.541836Z"
}
}
},
{
"cell_type": "code",
"execution_count": 2,
"outputs": [],
"source": [
"def random_username() -> str:\n",
" return \"test_username\" + \"\".join([choice(ascii_lowercase) for _ in range(6)])\n",
"\n",
"\n",
"def select_keys_with_passcode_values(user_passcode: list[int], interface: list[int], attrs_per_key: int) -> list[int]:\n",
" return [interface.index(attr) // attrs_per_key for attr in user_passcode]\n",
"\n",
"def keypad_view(interface: list[int], attrs_per_key: int):\n",
" print(\"Keypad View\")\n",
" interface_keypad = list_to_matrix(interface, attrs_per_key)\n",
" for idx, key_vals in enumerate(interface_keypad):\n",
" print(f\"Key {idx}: {key_vals}\")\n"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-07-19T15:38:32.636920Z",
"start_time": "2024-07-19T15:38:32.606667Z"
}
}
},
{
"cell_type": "code",
"execution_count": 3,
"outputs": [],
"source": [
"api = NKodeAPI()"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-07-19T15:38:32.636991Z",
"start_time": "2024-07-19T15:38:32.608558Z"
}
}
},
{
"cell_type": "markdown",
"source": [
"# NKode API\n",
"### Customer NKode Policy and Interface\n",
"A customer defines their NKode Policy and their interface. Below we've set:\n",
"- max nkode length = 10\n",
"- min nkode length = 4\n",
"- distinct attributes = 4\n",
"- distinct set = 0\n",
"\n",
"This customer also has an interface with 10 keys and 7 attributes per key"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": 4,
"outputs": [],
"source": [
"policy = NKodePolicy(\n",
" max_nkode_len=10,\n",
" min_nkode_len=4,\n",
" distinct_sets=0,\n",
" distinct_attributes=4\n",
")\n",
"keypad_size = KeypadSize(\n",
" numb_of_keys = 10,\n",
" attrs_per_key = 7 # aka number of sets\n",
")\n",
"customer_id = api.create_new_customer(keypad_size, policy)\n",
"customer = api.customers[customer_id]"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-07-19T15:38:32.817740Z",
"start_time": "2024-07-19T15:38:32.612174Z"
}
}
},
{
"cell_type": "markdown",
"source": [
"### NKode Customer\n",
"A customer has users and defines the attributes and set values for all its users.\n",
"Since our customer has 10 keys and 7 attributes per key, this gives a customer interface of 70 distinct attributes and 7 distinct attribute sets.\n",
"Each attribute belongs to one of the 7 sets."
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "markdown",
"source": [
"#### Customer and Attribute Values"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": 5,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Customer Sets: [63949, 41634, 17125, 52762, 13459, 5935, 727]\n",
"Customer Attributes: [52222, 57050, 17039, 29896, 31069, 35859, 36843, 2137, 24380, 34643, 29053, 46327, 39534, 64877, 53617, 14001, 39532, 19103, 38549, 42982, 63429, 11337, 42520, 23186, 49849, 6237, 49291, 44219, 34592, 26781, 25410, 34134, 51292, 40084, 49002, 1221, 30724, 62975, 33074, 22158, 18970, 54242, 15906, 19581, 10796, 6147, 36402, 44152, 43216, 16186, 571, 32657, 20955, 10260, 51931, 46321, 40259, 1905, 14430, 48389, 35797, 48564, 32173, 50142, 65402, 11082, 39195, 46929, 18113, 52721]\n"
]
}
],
"source": [
"set_vals = customer.interface.set_vals\n",
"attr_vals = customer.interface.attr_vals\n",
"print(f\"Customer Sets: {set_vals}\")\n",
"print(f\"Customer Attributes: {attr_vals}\")"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-07-19T15:38:32.818026Z",
"start_time": "2024-07-19T15:38:32.781094Z"
}
}
},
{
"cell_type": "markdown",
"source": [
"#### Customer Set To Attribute Map"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": 6,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Set to Attribute Map:\n",
"63949: [52222, 2137, 53617, 11337, 34592, 1221, 15906, 16186, 40259, 50142]\n",
"41634: [57050, 24380, 14001, 42520, 26781, 30724, 19581, 571, 1905, 65402]\n",
"17125: [17039, 34643, 39532, 23186, 25410, 62975, 10796, 32657, 14430, 11082]\n",
"52762: [29896, 29053, 19103, 49849, 34134, 33074, 6147, 20955, 48389, 39195]\n",
"13459: [31069, 46327, 38549, 6237, 51292, 22158, 36402, 10260, 35797, 46929]\n",
"5935: [35859, 39534, 42982, 49291, 40084, 18970, 44152, 51931, 48564, 18113]\n",
"727: [36843, 64877, 63429, 44219, 49002, 54242, 43216, 46321, 32173, 52721]\n"
]
}
],
"source": [
"attr_keypad_view = list_to_matrix(attr_vals, keypad_size.attrs_per_key)\n",
"attr_set_view = matrix_transpose(attr_keypad_view)\n",
"set_attribute_dict = dict(zip(set_vals, attr_set_view))\n",
"print(f\"Set to Attribute Map:\")\n",
"for set_val, attrs in set_attribute_dict.items():\n",
" print(f\"{set_val}: {attrs}\")"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-07-19T15:38:32.818150Z",
"start_time": "2024-07-19T15:38:32.784103Z"
}
}
},
{
"cell_type": "markdown",
"source": [
"### User Signup\n",
"To create a new must call this endpoints in order:\n",
"1. Generate Index Interface\n",
"2. Set User NKode\n",
"3. Confirm User NKode\n",
"\n",
"#### Generate Index Interface\n",
"- random interface is generated. Run the cell below over and over to see it change. Notice that values never move out of their columns jus their rows.\n",
"- each value in the interface is the index value of an attribute in the customer interface\n",
"- the user never learns what their \"real\" attribute is. All they do is specify an index in the customer interface\n"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": 7,
"outputs": [],
"source": [
"session_id, signup_interface = api.generate_index_interface(customer_id)"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-07-19T15:38:32.818223Z",
"start_time": "2024-07-19T15:38:32.787316Z"
}
}
},
{
"cell_type": "markdown",
"source": [
"#### Set NKode\n",
"The user identifies attributes in the interface they want in their nkode. Each attribute in the gui has an index value. Below the user has selected 12, 2, 52, 28. Graphiclly represent with anything. The only requirment is that the graphical must be associated with the same number everytime the user goes to login. If the user wants to change anything about their interface, they must also change their nkode."
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": 8,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Keypad View\n",
"Key 0: [14, 50, 16, 59, 39, 26, 6]\n",
"Key 1: [56, 36, 37, 52, 18, 47, 41]\n",
"Key 2: [49, 43, 2, 66, 60, 54, 69]\n",
"Key 3: [7, 8, 44, 3, 11, 68, 13]\n",
"Key 4: [63, 57, 65, 45, 53, 12, 48]\n",
"Key 5: [0, 22, 23, 10, 4, 40, 20]\n",
"Key 6: [35, 15, 51, 31, 67, 5, 55]\n",
"Key 7: [42, 29, 9, 38, 32, 33, 62]\n",
"Key 8: [21, 1, 58, 24, 46, 19, 34]\n",
"Key 9: [28, 64, 30, 17, 25, 61, 27]\n",
"Selected Keys\n",
"[4, 2, 1, 9]\n"
]
}
],
"source": [
"keypad_view(signup_interface, keypad_size.attrs_per_key)\n",
"username = random_username()\n",
"user_passcode = [12, 2, 52, 28]\n",
"selected_keys_set = select_keys_with_passcode_values(user_passcode, signup_interface, keypad_size.attrs_per_key)\n",
"print(f\"Selected Keys\\n{selected_keys_set}\")"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-07-19T15:38:32.818320Z",
"start_time": "2024-07-19T15:38:32.791137Z"
}
}
},
{
"cell_type": "code",
"execution_count": 9,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Keypad View\n",
"Key 0: [7, 29, 51, 66, 53, 47, 27]\n",
"Key 1: [21, 15, 2, 59, 18, 33, 20]\n",
"Key 2: [63, 43, 16, 17, 32, 5, 13]\n",
"Key 3: [56, 50, 30, 10, 67, 54, 34]\n",
"Key 4: [42, 64, 23, 3, 60, 26, 48]\n",
"Key 5: [35, 22, 44, 24, 39, 61, 41]\n",
"Key 6: [49, 8, 58, 45, 25, 40, 62]\n",
"Key 7: [14, 1, 65, 52, 4, 68, 55]\n",
"Key 8: [28, 57, 37, 38, 11, 19, 69]\n",
"Key 9: [0, 36, 9, 31, 46, 12, 6]\n",
"Selected Keys\n",
"[9, 1, 7, 8]\n"
]
}
],
"source": [
"confirm_interface = api.set_nkode(username, customer_id, selected_keys_set, session_id)\n",
"keypad_view(confirm_interface, keypad_size.attrs_per_key)\n",
"selected_keys_confirm = select_keys_with_passcode_values(user_passcode, confirm_interface, keypad_size.attrs_per_key)\n",
"print(f\"Selected Keys\\n{selected_keys_confirm}\")"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-07-19T15:38:32.818413Z",
"start_time": "2024-07-19T15:38:32.794201Z"
}
}
},
{
"cell_type": "markdown",
"source": [
"### Attribute Dispersion\n",
"In order for the api to determine what the users attributes are, it must run a dispersion algorithm.\n",
"This means that none of the attributes in the set interface share a key with any of the attributes in the confirm interface.\n",
"Take a look at them next to each other."
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": 10,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Set Interface\n",
"Keypad View\n",
"Key 0: [14, 50, 16, 59, 39, 26, 6]\n",
"Key 1: [56, 36, 37, 52, 18, 47, 41]\n",
"Key 2: [49, 43, 2, 66, 60, 54, 69]\n",
"Key 3: [7, 8, 44, 3, 11, 68, 13]\n",
"Key 4: [63, 57, 65, 45, 53, 12, 48]\n",
"Key 5: [0, 22, 23, 10, 4, 40, 20]\n",
"Key 6: [35, 15, 51, 31, 67, 5, 55]\n",
"Key 7: [42, 29, 9, 38, 32, 33, 62]\n",
"Key 8: [21, 1, 58, 24, 46, 19, 34]\n",
"Key 9: [28, 64, 30, 17, 25, 61, 27]\n"
]
}
],
"source": [
"print(\"Set Interface\")\n",
"keypad_view(signup_interface, keypad_size.attrs_per_key)"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-07-19T15:38:32.818504Z",
"start_time": "2024-07-19T15:38:32.797246Z"
}
}
},
{
"cell_type": "code",
"execution_count": 11,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Confirm Interface\n",
"Keypad View\n",
"Key 0: [7, 29, 51, 66, 53, 47, 27]\n",
"Key 1: [21, 15, 2, 59, 18, 33, 20]\n",
"Key 2: [63, 43, 16, 17, 32, 5, 13]\n",
"Key 3: [56, 50, 30, 10, 67, 54, 34]\n",
"Key 4: [42, 64, 23, 3, 60, 26, 48]\n",
"Key 5: [35, 22, 44, 24, 39, 61, 41]\n",
"Key 6: [49, 8, 58, 45, 25, 40, 62]\n",
"Key 7: [14, 1, 65, 52, 4, 68, 55]\n",
"Key 8: [28, 57, 37, 38, 11, 19, 69]\n",
"Key 9: [0, 36, 9, 31, 46, 12, 6]\n"
]
}
],
"source": [
"print(\"Confirm Interface\")\n",
"keypad_view(confirm_interface, keypad_size.attrs_per_key)"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-07-19T15:38:32.818596Z",
"start_time": "2024-07-19T15:38:32.799553Z"
}
}
},
{
"cell_type": "code",
"execution_count": 12,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Success\n"
]
}
],
"source": [
"# the session is deleted after the nkode is confirmed. To rerun this cell, rerun the cells above starting with cell 8 where the username is created\n",
"success = api.confirm_nkode(username, customer_id, selected_keys_confirm, session_id)\n",
"if success:\n",
" print(\"Success\")\n",
"else:\n",
" print(\"Failed\")"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-07-19T15:38:33.435016Z",
"start_time": "2024-07-19T15:38:32.803552Z"
}
}
},
{
"cell_type": "markdown",
"source": [
"### User Login\n",
"1. Get login interface\n",
"2. Login\n"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": 12,
"outputs": [],
"source": [],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-07-19T15:38:33.451250Z",
"start_time": "2024-07-19T15:38:33.435255Z"
}
}
},
{
"cell_type": "markdown",
"source": [
"#### Renew Keys\n",
"1. Renew Customer Keys\n",
"2. Renew User Keys"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": 12,
"outputs": [],
"source": [],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-07-19T15:38:33.451441Z",
"start_time": "2024-07-19T15:38:33.438804Z"
}
}
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.6"
}
},
"nbformat": 4,
"nbformat_minor": 0
}