Skip to content

Commit 970e5cf

Browse files
authored
Implemented #103 (#105)
* Closing #103
1 parent 6ee9528 commit 970e5cf

File tree

13 files changed

+109
-80
lines changed

13 files changed

+109
-80
lines changed

.github/FUNDING.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22

33
patreon: akopachov
44
ko_fi: akopachov
5-
custom: ["https://buycoffee.to/akopachov"]
5+
custom: ["https://donorbox.org/flipper-authenticator", "https://buycoffee.to/akopachov"]

totp/cli/cli_helpers.c

+4-2
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,16 @@ bool totp_cli_ensure_authenticated(const PluginState* plugin_state, Cli* cli) {
77
if(plugin_state->current_scene == TotpSceneAuthentication) {
88
TOTP_CLI_PRINTF("Pleases enter PIN on your flipper device\r\n");
99

10-
while(plugin_state->current_scene == TotpSceneAuthentication &&
10+
while((plugin_state->current_scene == TotpSceneAuthentication ||
11+
plugin_state->current_scene == TotpSceneNone) &&
1112
!cli_cmd_interrupt_received(cli)) {
1213
furi_delay_ms(100);
1314
}
1415

1516
TOTP_CLI_DELETE_LAST_LINE();
1617

17-
if(plugin_state->current_scene == TotpSceneAuthentication) { //-V547
18+
if(plugin_state->current_scene == TotpSceneAuthentication || //-V560
19+
plugin_state->current_scene == TotpSceneNone) { //-V560
1820
return false;
1921
}
2022
}

totp/cli/commands/add/add.c

+3
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,15 @@ void totp_cli_command_add_docopt_options() {
5151
DOCOPT_ARGUMENT(
5252
TOTP_CLI_COMMAND_ARG_AUTOMATION_FEATURE)) " Token automation features to be enabled. Must be one of: " TOTP_TOKEN_AUTOMATION_FEATURE_NONE_NAME
5353
", " TOTP_TOKEN_AUTOMATION_FEATURE_ENTER_AT_THE_END_NAME
54+
", " TOTP_TOKEN_AUTOMATION_FEATURE_TAB_AT_THE_END_NAME
5455
" " DOCOPT_DEFAULT(
5556
TOTP_TOKEN_AUTOMATION_FEATURE_NONE_NAME) "\r\n");
5657
TOTP_CLI_PRINTF(" # " TOTP_TOKEN_AUTOMATION_FEATURE_NONE_NAME
5758
" - No features\r\n");
5859
TOTP_CLI_PRINTF(" # " TOTP_TOKEN_AUTOMATION_FEATURE_ENTER_AT_THE_END_NAME
5960
" - Type <Enter> key at the end of token input automation\r\n");
61+
TOTP_CLI_PRINTF(" # " TOTP_TOKEN_AUTOMATION_FEATURE_TAB_AT_THE_END_NAME
62+
" - Type <Tab> key at the end of token input automation\r\n");
6063
}
6164

6265
void totp_cli_command_add_handle(PluginState* plugin_state, FuriString* args, Cli* cli) {

totp/cli/commands/details/details.c

+15-1
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,29 @@
77
#include "../../cli_helpers.h"
88
#include "../../common_command_arguments.h"
99

10+
#define AUTOMATION_FEATURES_PROPERTY_HEADER "Automation features"
11+
1012
static void print_automation_features(const TokenInfo* token_info) {
1113
if(token_info->automation_features == TOKEN_AUTOMATION_FEATURE_NONE) {
1214
TOTP_CLI_PRINTF("| %-20s | %-28.28s |\r\n", "Automation features", "None");
1315
return;
1416
}
1517

18+
bool header_printed = false;
1619
if(token_info->automation_features & TOKEN_AUTOMATION_FEATURE_ENTER_AT_THE_END) {
1720
TOTP_CLI_PRINTF(
18-
"| %-20s | %-28.28s |\r\n", "Automation features", "Type <Enter> key at the end");
21+
"| %-20s | %-28.28s |\r\n",
22+
AUTOMATION_FEATURES_PROPERTY_HEADER,
23+
"Type <Enter> key at the end");
24+
header_printed = true;
25+
}
26+
27+
if(token_info->automation_features & TOKEN_AUTOMATION_FEATURE_TAB_AT_THE_END) {
28+
TOTP_CLI_PRINTF(
29+
"| %-20s | %-28.28s |\r\n",
30+
header_printed ? "" : AUTOMATION_FEATURES_PROPERTY_HEADER,
31+
"Type <Tab> key at the end");
32+
header_printed = true;
1933
}
2034
}
2135

totp/cli/commands/move/move.c

+9-19
Original file line numberDiff line numberDiff line change
@@ -54,28 +54,18 @@ void totp_cli_command_move_handle(PluginState* plugin_state, FuriString* args, C
5454
activate_generate_token_scene = true;
5555
}
5656

57-
bool token_updated = false;
5857
TokenInfo* token_info = NULL;
59-
if(new_token_index > 0) {
60-
plugin_state->tokens_list =
61-
list_remove_at(plugin_state->tokens_list, token_index - 1, (void**)&token_info);
62-
furi_check(token_info != NULL);
63-
plugin_state->tokens_list =
64-
list_insert_at(plugin_state->tokens_list, new_token_index - 1, token_info);
65-
token_updated = true;
66-
} else {
67-
token_info = list_element_at(plugin_state->tokens_list, token_index - 1)->data;
68-
}
58+
plugin_state->tokens_list =
59+
list_remove_at(plugin_state->tokens_list, token_index - 1, (void**)&token_info);
60+
furi_check(token_info != NULL);
61+
plugin_state->tokens_list =
62+
list_insert_at(plugin_state->tokens_list, new_token_index - 1, token_info);
6963

70-
if(token_updated) {
71-
if(totp_full_save_config_file(plugin_state) == TotpConfigFileUpdateSuccess) {
72-
TOTP_CLI_PRINTF_SUCCESS(
73-
"Token \"%s\" has been successfully updated\r\n", token_info->name);
74-
} else {
75-
TOTP_CLI_PRINT_ERROR_UPDATING_CONFIG_FILE();
76-
}
64+
if(totp_full_save_config_file(plugin_state) == TotpConfigFileUpdateSuccess) {
65+
TOTP_CLI_PRINTF_SUCCESS(
66+
"Token \"%s\" has been successfully updated\r\n", token_info->name);
7767
} else {
78-
TOTP_CLI_PRINT_INVALID_ARGUMENTS();
68+
TOTP_CLI_PRINT_ERROR_UPDATING_CONFIG_FILE();
7969
}
8070

8171
if(activate_generate_token_scene) {

totp/types/token_info.c

+5
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,11 @@ bool token_info_set_automation_feature_from_str(TokenInfo* token_info, const Fur
113113
return true;
114114
}
115115

116+
if(furi_string_cmpi_str(str, TOTP_TOKEN_AUTOMATION_FEATURE_TAB_AT_THE_END_NAME) == 0) {
117+
token_info->automation_features |= TOKEN_AUTOMATION_FEATURE_TAB_AT_THE_END;
118+
return true;
119+
}
120+
116121
if(furi_string_cmpi_str(str, TOTP_TOKEN_AUTOMATION_FEATURE_NONE_NAME) == 0) {
117122
token_info->automation_features = TOKEN_AUTOMATION_FEATURE_NONE;
118123
return true;

totp/types/token_info.h

+7-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
#define TOTP_TOKEN_AUTOMATION_FEATURE_NONE_NAME "none"
1515
#define TOTP_TOKEN_AUTOMATION_FEATURE_ENTER_AT_THE_END_NAME "enter"
16+
#define TOTP_TOKEN_AUTOMATION_FEATURE_TAB_AT_THE_END_NAME "tab"
1617

1718
typedef uint8_t TokenHashAlgo;
1819
typedef uint8_t TokenDigitsCount;
@@ -65,7 +66,12 @@ enum TokenAutomationFeatures {
6566
/**
6667
* @brief Press "Enter" key at the end as a part of token input automation
6768
*/
68-
TOKEN_AUTOMATION_FEATURE_ENTER_AT_THE_END = 0b01
69+
TOKEN_AUTOMATION_FEATURE_ENTER_AT_THE_END = 0b01,
70+
71+
/**
72+
* @brief Press "Tab" key at the end as a part of token input automation
73+
*/
74+
TOKEN_AUTOMATION_FEATURE_TAB_AT_THE_END = 0b10
6975
};
7076

7177
#define TOTP_TOKEN_DIGITS_MAX_COUNT 8

totp/workers/bt_type_code/bt_type_code.c

+6-18
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
#include <storage/storage.h>
44
#include "../../types/common.h"
55
#include "../../types/token_info.h"
6-
#include "../../services/convert/convert.h"
7-
#include "../constants.h"
6+
#include "../common.h"
87

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

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

2120
static void totp_type_code_worker_type_code(TotpBtTypeCodeWorkerContext* context) {
22-
TokenAutomationFeature features = context->flags;
2321
uint8_t i = 0;
2422
do {
2523
furi_delay_ms(500);
2624
i++;
2725
} while(!context->is_connected && i < 100 && !totp_type_code_worker_stop_requested());
2826

2927
if(context->is_connected && furi_mutex_acquire(context->string_sync, 500) == FuriStatusOk) {
30-
furi_delay_ms(500);
31-
i = 0;
32-
while(i < context->string_length && context->string[i] != 0) {
33-
uint8_t digit = CONVERT_CHAR_TO_DIGIT(context->string[i]);
34-
if(digit > 9) break;
35-
uint8_t hid_kb_key = hid_number_keys[digit];
36-
totp_type_code_worker_press_key(hid_kb_key);
37-
i++;
38-
}
39-
40-
if(features & TOKEN_AUTOMATION_FEATURE_ENTER_AT_THE_END) {
41-
furi_delay_ms(30);
42-
totp_type_code_worker_press_key(hid_enter_key);
43-
}
44-
28+
totp_type_code_worker_execute_automation(
29+
&totp_type_code_worker_press_key,
30+
context->string,
31+
context->string_length,
32+
context->flags);
4533
furi_mutex_release(context->string_sync);
4634
}
4735
}

totp/workers/common.c

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#include "common.h"
2+
#include <furi/furi.h>
3+
#include <furi_hal.h>
4+
#include "../../services/convert/convert.h"
5+
6+
static const uint8_t hid_number_keys[10] = {
7+
HID_KEYBOARD_0,
8+
HID_KEYBOARD_1,
9+
HID_KEYBOARD_2,
10+
HID_KEYBOARD_3,
11+
HID_KEYBOARD_4,
12+
HID_KEYBOARD_5,
13+
HID_KEYBOARD_6,
14+
HID_KEYBOARD_7,
15+
HID_KEYBOARD_8,
16+
HID_KEYBOARD_9};
17+
18+
void totp_type_code_worker_execute_automation(
19+
TOTP_AUTOMATION_PRESS_KEY key_press_fn,
20+
const char* string,
21+
uint8_t string_length,
22+
TokenAutomationFeature features) {
23+
furi_delay_ms(500);
24+
uint8_t i = 0;
25+
while(i < string_length && string[i] != 0) {
26+
uint8_t digit = CONVERT_CHAR_TO_DIGIT(string[i]);
27+
if(digit > 9) break;
28+
uint8_t hid_kb_key = hid_number_keys[digit];
29+
(*key_press_fn)(hid_kb_key);
30+
i++;
31+
}
32+
33+
if(features & TOKEN_AUTOMATION_FEATURE_ENTER_AT_THE_END) {
34+
furi_delay_ms(30);
35+
(*key_press_fn)(HID_KEYBOARD_RETURN);
36+
}
37+
38+
if(features & TOKEN_AUTOMATION_FEATURE_TAB_AT_THE_END) {
39+
furi_delay_ms(30);
40+
(*key_press_fn)(HID_KEYBOARD_TAB);
41+
}
42+
}

totp/workers/common.h

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#pragma once
2+
#include <stdint.h>
3+
#include "../types/token_info.h"
4+
5+
typedef void (*TOTP_AUTOMATION_PRESS_KEY)(uint8_t key);
6+
7+
void totp_type_code_worker_execute_automation(
8+
TOTP_AUTOMATION_PRESS_KEY key_press_fn,
9+
const char* string,
10+
uint8_t string_length,
11+
TokenAutomationFeature features);

totp/workers/constants.c

-16
This file was deleted.

totp/workers/constants.h

-5
This file was deleted.

totp/workers/usb_type_code/usb_type_code.c

+6-17
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#include "usb_type_code.h"
22
#include "../../services/convert/convert.h"
33
#include "../../types/token_info.h"
4-
#include "../constants.h"
4+
#include "../common.h"
55

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

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

3433
if(furi_hal_hid_is_connected() &&
3534
furi_mutex_acquire(context->string_sync, 500) == FuriStatusOk) {
36-
furi_delay_ms(500);
37-
i = 0;
38-
while(i < context->string_length && context->string[i] != 0) {
39-
uint8_t digit = CONVERT_CHAR_TO_DIGIT(context->string[i]);
40-
if(digit > 9) break;
41-
uint8_t hid_kb_key = hid_number_keys[digit];
42-
totp_type_code_worker_press_key(hid_kb_key);
43-
i++;
44-
}
45-
46-
if(features & TOKEN_AUTOMATION_FEATURE_ENTER_AT_THE_END) {
47-
furi_delay_ms(30);
48-
totp_type_code_worker_press_key(hid_enter_key);
49-
}
50-
35+
totp_type_code_worker_execute_automation(
36+
&totp_type_code_worker_press_key,
37+
context->string,
38+
context->string_length,
39+
context->flags);
5140
furi_mutex_release(context->string_sync);
5241

5342
furi_delay_ms(100);

0 commit comments

Comments
 (0)