Skip to content

Commit

Permalink
Implemented #103 (#105)
Browse files Browse the repository at this point in the history
* Closing #103
  • Loading branch information
akopachov authored Mar 31, 2023
1 parent 5b6aedb commit 2901da8
Show file tree
Hide file tree
Showing 12 changed files with 108 additions and 79 deletions.
6 changes: 4 additions & 2 deletions cli/cli_helpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@ bool totp_cli_ensure_authenticated(const PluginState* plugin_state, Cli* cli) {
if(plugin_state->current_scene == TotpSceneAuthentication) {
TOTP_CLI_PRINTF("Pleases enter PIN on your flipper device\r\n");

while(plugin_state->current_scene == TotpSceneAuthentication &&
while((plugin_state->current_scene == TotpSceneAuthentication ||
plugin_state->current_scene == TotpSceneNone) &&
!cli_cmd_interrupt_received(cli)) {
furi_delay_ms(100);
}

TOTP_CLI_DELETE_LAST_LINE();

if(plugin_state->current_scene == TotpSceneAuthentication) { //-V547
if(plugin_state->current_scene == TotpSceneAuthentication || //-V560
plugin_state->current_scene == TotpSceneNone) { //-V560
return false;
}
}
Expand Down
3 changes: 3 additions & 0 deletions cli/commands/add/add.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,15 @@ void totp_cli_command_add_docopt_options() {
DOCOPT_ARGUMENT(
TOTP_CLI_COMMAND_ARG_AUTOMATION_FEATURE)) " Token automation features to be enabled. Must be one of: " TOTP_TOKEN_AUTOMATION_FEATURE_NONE_NAME
", " TOTP_TOKEN_AUTOMATION_FEATURE_ENTER_AT_THE_END_NAME
", " TOTP_TOKEN_AUTOMATION_FEATURE_TAB_AT_THE_END_NAME
" " DOCOPT_DEFAULT(
TOTP_TOKEN_AUTOMATION_FEATURE_NONE_NAME) "\r\n");
TOTP_CLI_PRINTF(" # " TOTP_TOKEN_AUTOMATION_FEATURE_NONE_NAME
" - No features\r\n");
TOTP_CLI_PRINTF(" # " TOTP_TOKEN_AUTOMATION_FEATURE_ENTER_AT_THE_END_NAME
" - Type <Enter> key at the end of token input automation\r\n");
TOTP_CLI_PRINTF(" # " TOTP_TOKEN_AUTOMATION_FEATURE_TAB_AT_THE_END_NAME
" - Type <Tab> key at the end of token input automation\r\n");
}

void totp_cli_command_add_handle(PluginState* plugin_state, FuriString* args, Cli* cli) {
Expand Down
16 changes: 15 additions & 1 deletion cli/commands/details/details.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,29 @@
#include "../../cli_helpers.h"
#include "../../common_command_arguments.h"

#define AUTOMATION_FEATURES_PROPERTY_HEADER "Automation features"

static void print_automation_features(const TokenInfo* token_info) {
if(token_info->automation_features == TOKEN_AUTOMATION_FEATURE_NONE) {
TOTP_CLI_PRINTF("| %-20s | %-28.28s |\r\n", "Automation features", "None");
return;
}

bool header_printed = false;
if(token_info->automation_features & TOKEN_AUTOMATION_FEATURE_ENTER_AT_THE_END) {
TOTP_CLI_PRINTF(
"| %-20s | %-28.28s |\r\n", "Automation features", "Type <Enter> key at the end");
"| %-20s | %-28.28s |\r\n",
AUTOMATION_FEATURES_PROPERTY_HEADER,
"Type <Enter> key at the end");
header_printed = true;
}

if(token_info->automation_features & TOKEN_AUTOMATION_FEATURE_TAB_AT_THE_END) {
TOTP_CLI_PRINTF(
"| %-20s | %-28.28s |\r\n",
header_printed ? "" : AUTOMATION_FEATURES_PROPERTY_HEADER,
"Type <Tab> key at the end");
header_printed = true;
}
}

Expand Down
28 changes: 9 additions & 19 deletions cli/commands/move/move.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,28 +54,18 @@ void totp_cli_command_move_handle(PluginState* plugin_state, FuriString* args, C
activate_generate_token_scene = true;
}

bool token_updated = false;
TokenInfo* token_info = NULL;
if(new_token_index > 0) {
plugin_state->tokens_list =
list_remove_at(plugin_state->tokens_list, token_index - 1, (void**)&token_info);
furi_check(token_info != NULL);
plugin_state->tokens_list =
list_insert_at(plugin_state->tokens_list, new_token_index - 1, token_info);
token_updated = true;
} else {
token_info = list_element_at(plugin_state->tokens_list, token_index - 1)->data;
}
plugin_state->tokens_list =
list_remove_at(plugin_state->tokens_list, token_index - 1, (void**)&token_info);
furi_check(token_info != NULL);
plugin_state->tokens_list =
list_insert_at(plugin_state->tokens_list, new_token_index - 1, token_info);

if(token_updated) {
if(totp_full_save_config_file(plugin_state) == TotpConfigFileUpdateSuccess) {
TOTP_CLI_PRINTF_SUCCESS(
"Token \"%s\" has been successfully updated\r\n", token_info->name);
} else {
TOTP_CLI_PRINT_ERROR_UPDATING_CONFIG_FILE();
}
if(totp_full_save_config_file(plugin_state) == TotpConfigFileUpdateSuccess) {
TOTP_CLI_PRINTF_SUCCESS(
"Token \"%s\" has been successfully updated\r\n", token_info->name);
} else {
TOTP_CLI_PRINT_INVALID_ARGUMENTS();
TOTP_CLI_PRINT_ERROR_UPDATING_CONFIG_FILE();
}

if(activate_generate_token_scene) {
Expand Down
5 changes: 5 additions & 0 deletions types/token_info.c
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,11 @@ bool token_info_set_automation_feature_from_str(TokenInfo* token_info, const Fur
return true;
}

if(furi_string_cmpi_str(str, TOTP_TOKEN_AUTOMATION_FEATURE_TAB_AT_THE_END_NAME) == 0) {
token_info->automation_features |= TOKEN_AUTOMATION_FEATURE_TAB_AT_THE_END;
return true;
}

if(furi_string_cmpi_str(str, TOTP_TOKEN_AUTOMATION_FEATURE_NONE_NAME) == 0) {
token_info->automation_features = TOKEN_AUTOMATION_FEATURE_NONE;
return true;
Expand Down
8 changes: 7 additions & 1 deletion types/token_info.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

#define TOTP_TOKEN_AUTOMATION_FEATURE_NONE_NAME "none"
#define TOTP_TOKEN_AUTOMATION_FEATURE_ENTER_AT_THE_END_NAME "enter"
#define TOTP_TOKEN_AUTOMATION_FEATURE_TAB_AT_THE_END_NAME "tab"

typedef uint8_t TokenHashAlgo;
typedef uint8_t TokenDigitsCount;
Expand Down Expand Up @@ -65,7 +66,12 @@ enum TokenAutomationFeatures {
/**
* @brief Press "Enter" key at the end as a part of token input automation
*/
TOKEN_AUTOMATION_FEATURE_ENTER_AT_THE_END = 0b01
TOKEN_AUTOMATION_FEATURE_ENTER_AT_THE_END = 0b01,

/**
* @brief Press "Tab" key at the end as a part of token input automation
*/
TOKEN_AUTOMATION_FEATURE_TAB_AT_THE_END = 0b10
};

#define TOTP_TOKEN_DIGITS_MAX_COUNT 8
Expand Down
24 changes: 6 additions & 18 deletions workers/bt_type_code/bt_type_code.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
#include <storage/storage.h>
#include "../../types/common.h"
#include "../../types/token_info.h"
#include "../../services/convert/convert.h"
#include "../constants.h"
#include "../common.h"

#define HID_BT_KEYS_STORAGE_PATH EXT_PATH("authenticator/.bt_hid.keys")

Expand All @@ -19,29 +18,18 @@ static void totp_type_code_worker_press_key(uint8_t key) {
}

static void totp_type_code_worker_type_code(TotpBtTypeCodeWorkerContext* context) {
TokenAutomationFeature features = context->flags;
uint8_t i = 0;
do {
furi_delay_ms(500);
i++;
} while(!context->is_connected && i < 100 && !totp_type_code_worker_stop_requested());

if(context->is_connected && furi_mutex_acquire(context->string_sync, 500) == FuriStatusOk) {
furi_delay_ms(500);
i = 0;
while(i < context->string_length && context->string[i] != 0) {
uint8_t digit = CONVERT_CHAR_TO_DIGIT(context->string[i]);
if(digit > 9) break;
uint8_t hid_kb_key = hid_number_keys[digit];
totp_type_code_worker_press_key(hid_kb_key);
i++;
}

if(features & TOKEN_AUTOMATION_FEATURE_ENTER_AT_THE_END) {
furi_delay_ms(30);
totp_type_code_worker_press_key(hid_enter_key);
}

totp_type_code_worker_execute_automation(
&totp_type_code_worker_press_key,
context->string,
context->string_length,
context->flags);
furi_mutex_release(context->string_sync);
}
}
Expand Down
42 changes: 42 additions & 0 deletions workers/common.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include "common.h"
#include <furi/furi.h>
#include <furi_hal.h>
#include "../../services/convert/convert.h"

static const uint8_t hid_number_keys[10] = {
HID_KEYBOARD_0,
HID_KEYBOARD_1,
HID_KEYBOARD_2,
HID_KEYBOARD_3,
HID_KEYBOARD_4,
HID_KEYBOARD_5,
HID_KEYBOARD_6,
HID_KEYBOARD_7,
HID_KEYBOARD_8,
HID_KEYBOARD_9};

void totp_type_code_worker_execute_automation(
TOTP_AUTOMATION_PRESS_KEY key_press_fn,
const char* string,
uint8_t string_length,
TokenAutomationFeature features) {
furi_delay_ms(500);
uint8_t i = 0;
while(i < string_length && string[i] != 0) {
uint8_t digit = CONVERT_CHAR_TO_DIGIT(string[i]);
if(digit > 9) break;
uint8_t hid_kb_key = hid_number_keys[digit];
(*key_press_fn)(hid_kb_key);
i++;
}

if(features & TOKEN_AUTOMATION_FEATURE_ENTER_AT_THE_END) {
furi_delay_ms(30);
(*key_press_fn)(HID_KEYBOARD_RETURN);
}

if(features & TOKEN_AUTOMATION_FEATURE_TAB_AT_THE_END) {
furi_delay_ms(30);
(*key_press_fn)(HID_KEYBOARD_TAB);
}
}
11 changes: 11 additions & 0 deletions workers/common.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#pragma once
#include <stdint.h>
#include "../types/token_info.h"

typedef void (*TOTP_AUTOMATION_PRESS_KEY)(uint8_t key);

void totp_type_code_worker_execute_automation(
TOTP_AUTOMATION_PRESS_KEY key_press_fn,
const char* string,
uint8_t string_length,
TokenAutomationFeature features);
16 changes: 0 additions & 16 deletions workers/constants.c

This file was deleted.

5 changes: 0 additions & 5 deletions workers/constants.h

This file was deleted.

23 changes: 6 additions & 17 deletions workers/usb_type_code/usb_type_code.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "usb_type_code.h"
#include "../../services/convert/convert.h"
#include "../../types/token_info.h"
#include "../constants.h"
#include "../common.h"

static void totp_type_code_worker_restore_usb_mode(TotpUsbTypeCodeWorkerContext* context) {
if(context->usb_mode_prev != NULL) {
Expand All @@ -21,7 +21,6 @@ static void totp_type_code_worker_press_key(uint8_t key) {
}

static void totp_type_code_worker_type_code(TotpUsbTypeCodeWorkerContext* context) {
TokenAutomationFeature features = context->flags;
context->usb_mode_prev = furi_hal_usb_get_config();
furi_hal_usb_unlock();
furi_check(furi_hal_usb_set_config(&usb_hid, NULL) == true);
Expand All @@ -33,21 +32,11 @@ static void totp_type_code_worker_type_code(TotpUsbTypeCodeWorkerContext* contex

if(furi_hal_hid_is_connected() &&
furi_mutex_acquire(context->string_sync, 500) == FuriStatusOk) {
furi_delay_ms(500);
i = 0;
while(i < context->string_length && context->string[i] != 0) {
uint8_t digit = CONVERT_CHAR_TO_DIGIT(context->string[i]);
if(digit > 9) break;
uint8_t hid_kb_key = hid_number_keys[digit];
totp_type_code_worker_press_key(hid_kb_key);
i++;
}

if(features & TOKEN_AUTOMATION_FEATURE_ENTER_AT_THE_END) {
furi_delay_ms(30);
totp_type_code_worker_press_key(hid_enter_key);
}

totp_type_code_worker_execute_automation(
&totp_type_code_worker_press_key,
context->string,
context->string_length,
context->flags);
furi_mutex_release(context->string_sync);

furi_delay_ms(100);
Expand Down

0 comments on commit 2901da8

Please sign in to comment.