Skip to content

Commit

Permalink
Implemented #171 (#175)
Browse files Browse the repository at this point in the history
  • Loading branch information
akopachov committed Jul 31, 2023
1 parent 0604b90 commit bc5daa2
Show file tree
Hide file tree
Showing 21 changed files with 409 additions and 182 deletions.
9 changes: 9 additions & 0 deletions .ofwcatalog/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# Changelog

## v3.1.0 - 31 Jul 2023

* Fixed [#171](https://github.com/akopachov/flipper-zero_authenticator/issues/171)

## v3.0.2 - 28 Jul 2023

* Fixed [#169](https://github.com/akopachov/flipper-zero_authenticator/issues/169)
* Fixed [#172](https://github.com/akopachov/flipper-zero_authenticator/issues/172)

## v3.0.0 - 26 Jul 2023

* Implemented better encryption [#167](https://github.com/akopachov/flipper-zero_authenticator/issues/167)
Expand Down
2 changes: 1 addition & 1 deletion application.fam
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ App(
],
stack_size=2 * 1024,
order=20,
fap_version="3.2",
fap_version="3.10",
fap_author="Alexander Kopachov (@akopachov)",
fap_description="Software-based TOTP authenticator for Flipper Zero device",
fap_weburl="https://github.com/akopachov/flipper-zero_authenticator",
Expand Down
74 changes: 66 additions & 8 deletions cli/commands/automation/automation.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,20 @@
#ifdef TOTP_BADBT_TYPE_ENABLED
#define TOTP_CLI_COMMAND_AUTOMATION_METHOD_BT "bt"
#endif
#define TOTP_CLI_COMMAND_AUTOMATION_LAYOUT_QWERTY "QWERTY"
#define TOTP_CLI_COMMAND_AUTOMATION_LAYOUT_AZERTY "AZERTY"
#define TOTP_CLI_COMMAND_AUTOMATION_ARG_KB_LAYOUT_PREFIX "-k"
#define TOTP_CLI_COMMAND_AUTOMATION_ARG_KB_LAYOUT "layout"

void totp_cli_command_automation_docopt_commands() {
TOTP_CLI_PRINTF(" " TOTP_CLI_COMMAND_AUTOMATION " Get or set automation method\r\n");
TOTP_CLI_PRINTF(" " TOTP_CLI_COMMAND_AUTOMATION " Get or set automation settings\r\n");
}

void totp_cli_command_automation_docopt_usage() {
TOTP_CLI_PRINTF(" " TOTP_CLI_COMMAND_NAME " " TOTP_CLI_COMMAND_AUTOMATION " " DOCOPT_OPTIONAL(
DOCOPT_MULTIPLE(DOCOPT_ARGUMENT(TOTP_CLI_COMMAND_AUTOMATION_ARG_METHOD))) "\r\n");
TOTP_CLI_PRINTF(" " TOTP_CLI_COMMAND_NAME " " TOTP_CLI_COMMAND_AUTOMATION " " DOCOPT_OPTIONAL(DOCOPT_OPTION(
TOTP_CLI_COMMAND_AUTOMATION_ARG_KB_LAYOUT_PREFIX,
DOCOPT_ARGUMENT(
TOTP_CLI_COMMAND_AUTOMATION_ARG_KB_LAYOUT))) " " DOCOPT_OPTIONAL(DOCOPT_MULTIPLE(DOCOPT_ARGUMENT(TOTP_CLI_COMMAND_AUTOMATION_ARG_METHOD))) "\r\n");
}

void totp_cli_command_automation_docopt_arguments() {
Expand All @@ -31,7 +37,16 @@ void totp_cli_command_automation_docopt_arguments() {
"\r\n");
}

static void totp_cli_command_automation_print_method(AutomationMethod method, const char* color) {
void totp_cli_command_automation_docopt_options() {
TOTP_CLI_PRINTF(" " DOCOPT_OPTION(
TOTP_CLI_COMMAND_AUTOMATION_ARG_KB_LAYOUT_PREFIX,
DOCOPT_ARGUMENT(
TOTP_CLI_COMMAND_AUTOMATION_ARG_KB_LAYOUT)) " Automation keyboard layout. Must be one of: " TOTP_CLI_COMMAND_AUTOMATION_LAYOUT_QWERTY
", " TOTP_CLI_COMMAND_AUTOMATION_LAYOUT_AZERTY
"\r\n");
}

static void print_method(AutomationMethod method, const char* color) {
#ifdef TOTP_BADBT_TYPE_ENABLED
bool has_previous_method = false;
#endif
Expand All @@ -57,6 +72,36 @@ static void totp_cli_command_automation_print_method(AutomationMethod method, co
}
}

static void print_kb_layout(AutomationKeyboardLayout layout, const char* color) {
char* layoutToPrint;
switch(layout) {
case AutomationKeyboardLayoutQWERTY:
layoutToPrint = TOTP_CLI_COMMAND_AUTOMATION_LAYOUT_QWERTY;
break;
case AutomationKeyboardLayoutAZERTY:
layoutToPrint = TOTP_CLI_COMMAND_AUTOMATION_LAYOUT_AZERTY;
break;
default:
furi_crash("Unknown automation keyboard layout");
break;
}

TOTP_CLI_PRINTF_COLORFUL(color, "%s", layoutToPrint);
}

static bool parse_automation_keyboard_layout(const FuriString* str, AutomationKeyboardLayout* out) {
bool result = true;
if(furi_string_cmpi_str(str, TOTP_CLI_COMMAND_AUTOMATION_LAYOUT_QWERTY) == 0) {
*out = AutomationKeyboardLayoutQWERTY;
} else if(furi_string_cmpi_str(str, TOTP_CLI_COMMAND_AUTOMATION_LAYOUT_AZERTY) == 0) {
*out = AutomationKeyboardLayoutAZERTY;
} else {
result = false;
}

return result;
}

void totp_cli_command_automation_handle(PluginState* plugin_state, FuriString* args, Cli* cli) {
if(!totp_cli_ensure_authenticated(plugin_state, cli)) {
return;
Expand All @@ -65,6 +110,7 @@ void totp_cli_command_automation_handle(PluginState* plugin_state, FuriString* a
FuriString* temp_str = furi_string_alloc();
bool new_method_provided = false;
AutomationMethod new_method = AutomationMethodNone;
AutomationKeyboardLayout new_kb_layout = plugin_state->automation_kb_layout;
bool args_valid = true;
while(args_read_string_and_trim(args, temp_str)) {
if(furi_string_cmpi_str(temp_str, TOTP_CLI_COMMAND_AUTOMATION_METHOD_NONE) == 0) {
Expand All @@ -80,7 +126,13 @@ void totp_cli_command_automation_handle(PluginState* plugin_state, FuriString* a
new_method |= AutomationMethodBadBt;
}
#endif
else {
else if(furi_string_cmpi_str(temp_str, TOTP_CLI_COMMAND_AUTOMATION_ARG_KB_LAYOUT_PREFIX) == 0) {
if(!args_read_string_and_trim(args, temp_str) ||
!parse_automation_keyboard_layout(temp_str, &new_kb_layout)) {
args_valid = false;
break;
}
} else {
args_valid = false;
break;
}
Expand All @@ -96,9 +148,13 @@ void totp_cli_command_automation_handle(PluginState* plugin_state, FuriString* a
TOTP_CLI_LOCK_UI(plugin_state);

plugin_state->automation_method = new_method;
plugin_state->automation_kb_layout = new_kb_layout;
if(totp_config_file_update_automation_method(plugin_state)) {
TOTP_CLI_PRINTF_SUCCESS("Automation method is set to ");
totp_cli_command_automation_print_method(new_method, TOTP_CLI_COLOR_SUCCESS);
print_method(new_method, TOTP_CLI_COLOR_SUCCESS);
TOTP_CLI_PRINTF_SUCCESS(" (");
print_kb_layout(plugin_state->automation_kb_layout, TOTP_CLI_COLOR_SUCCESS);
TOTP_CLI_PRINTF_SUCCESS(")");
cli_nl();
} else {
totp_cli_print_error_updating_config_file();
Expand All @@ -115,8 +171,10 @@ void totp_cli_command_automation_handle(PluginState* plugin_state, FuriString* a
TOTP_CLI_UNLOCK_UI(plugin_state);
} else {
TOTP_CLI_PRINTF_INFO("Current automation method is ");
totp_cli_command_automation_print_method(
plugin_state->automation_method, TOTP_CLI_COLOR_INFO);
print_method(plugin_state->automation_method, TOTP_CLI_COLOR_INFO);
TOTP_CLI_PRINTF_INFO(" (");
print_kb_layout(plugin_state->automation_kb_layout, TOTP_CLI_COLOR_INFO);
TOTP_CLI_PRINTF_INFO(")");
cli_nl();
}
} while(false);
Expand Down
3 changes: 2 additions & 1 deletion cli/commands/automation/automation.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@
void totp_cli_command_automation_handle(PluginState* plugin_state, FuriString* args, Cli* cli);
void totp_cli_command_automation_docopt_commands();
void totp_cli_command_automation_docopt_usage();
void totp_cli_command_automation_docopt_arguments();
void totp_cli_command_automation_docopt_arguments();
void totp_cli_command_automation_docopt_options();
1 change: 1 addition & 0 deletions cli/commands/help/help.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,5 @@ void totp_cli_command_help_handle() {
totp_cli_command_update_docopt_options();
totp_cli_command_delete_docopt_options();
totp_cli_command_pin_docopt_options();
totp_cli_command_automation_docopt_options();
}
Binary file removed images/totp_arrow_bottom_10x5.png
Binary file not shown.
29 changes: 28 additions & 1 deletion services/config/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,11 @@ static bool totp_open_config_file(Storage* storage, FlipperFormat** file) {
flipper_format_write_uint32(
fff_data_file, TOTP_CONFIG_KEY_AUTOMATION_METHOD, &tmp_uint32, 1);

tmp_uint32 = 0;
tmp_uint32 = AutomationKeyboardLayoutQWERTY;
flipper_format_write_uint32(
fff_data_file, TOTP_CONFIG_KEY_AUTOMATION_KB_LAYOUT, &tmp_uint32, 1);

tmp_uint32 = 0; //-V1048
flipper_format_write_uint32(fff_data_file, TOTP_CONFIG_KEY_FONT, &tmp_uint32, 1);

if(!flipper_format_rewind(fff_data_file)) {
Expand Down Expand Up @@ -241,6 +245,12 @@ bool totp_config_file_update_automation_method(const PluginState* plugin_state)
break;
}

tmp_uint32 = plugin_state->automation_kb_layout;
if(!flipper_format_insert_or_update_uint32(
file, TOTP_CONFIG_KEY_AUTOMATION_KB_LAYOUT, &tmp_uint32, 1)) {
break;
}

update_result = true;
} while(false);

Expand Down Expand Up @@ -273,6 +283,12 @@ bool totp_config_file_update_user_settings(const PluginState* plugin_state) {
break;
}

tmp_uint32 = plugin_state->automation_kb_layout;
if(!flipper_format_insert_or_update_uint32(
file, TOTP_CONFIG_KEY_AUTOMATION_KB_LAYOUT, &tmp_uint32, 1)) {
break;
}

update_result = true;
} while(false);

Expand Down Expand Up @@ -458,6 +474,17 @@ bool totp_config_file_load(PluginState* const plugin_state) {
break;
}

if(!flipper_format_read_uint32(
fff_data_file, TOTP_CONFIG_KEY_AUTOMATION_KB_LAYOUT, &tmp_uint32, 1)) {
tmp_uint32 = AutomationKeyboardLayoutQWERTY;
}

plugin_state->automation_kb_layout = tmp_uint32;

if(!flipper_format_rewind(fff_data_file)) {
break;
}

if(!flipper_format_read_uint32(fff_data_file, TOTP_CONFIG_KEY_FONT, &tmp_uint32, 1)) {
tmp_uint32 = 0;
}
Expand Down
3 changes: 2 additions & 1 deletion services/config/constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

#define CONFIG_FILE_DIRECTORY_PATH EXT_PATH("authenticator")
#define CONFIG_FILE_HEADER "Flipper TOTP plugin config file"
#define CONFIG_FILE_ACTUAL_VERSION (7)
#define CONFIG_FILE_ACTUAL_VERSION (8)

#define TOTP_CONFIG_KEY_TIMEZONE "Timezone"
#define TOTP_CONFIG_KEY_TOKEN_NAME "TokenName"
Expand All @@ -18,6 +18,7 @@
#define TOTP_CONFIG_KEY_PINSET "PinIsSet"
#define TOTP_CONFIG_KEY_NOTIFICATION_METHOD "NotificationMethod"
#define TOTP_CONFIG_KEY_AUTOMATION_METHOD "AutomationMethod"
#define TOTP_CONFIG_KEY_AUTOMATION_KB_LAYOUT "AutomationKbLayout"
#define TOTP_CONFIG_KEY_FONT "Font"
#define TOTP_CONFIG_KEY_CRYPTO_VERSION "CryptoVersion"
#define TOTP_CONFIG_KEY_CRYPTO_KEY_SLOT "CryptoKeySlot"
16 changes: 16 additions & 0 deletions services/config/migrations/common_migration.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "common_migration.h"
#include "../constants.h"
#include "../../../types/token_info.h"
#include "../../../types/automation_kb_layout.h"
#include <flipper_format/flipper_format_i.h>

bool totp_config_migrate_to_latest(
Expand Down Expand Up @@ -90,6 +91,21 @@ bool totp_config_migrate_to_latest(

flipper_format_rewind(fff_backup_data_file);

if(flipper_format_read_string(
fff_backup_data_file, TOTP_CONFIG_KEY_AUTOMATION_KB_LAYOUT, temp_str)) {
flipper_format_write_string(
fff_data_file, TOTP_CONFIG_KEY_AUTOMATION_KB_LAYOUT, temp_str);
} else {
uint32_t default_automation_kb_layout = AutomationKeyboardLayoutQWERTY;
flipper_format_write_uint32(
fff_data_file,
TOTP_CONFIG_KEY_AUTOMATION_KB_LAYOUT,
&default_automation_kb_layout,
1);
}

flipper_format_rewind(fff_backup_data_file);

while(true) {
if(!flipper_format_read_string(
fff_backup_data_file, TOTP_CONFIG_KEY_TOKEN_NAME, temp_str)) {
Expand Down
8 changes: 8 additions & 0 deletions types/automation_kb_layout.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#pragma once

typedef uint8_t AutomationKeyboardLayout;

enum AutomationKeyboardLayouts {
AutomationKeyboardLayoutQWERTY = 0,
AutomationKeyboardLayoutAZERTY = 1
};
6 changes: 6 additions & 0 deletions types/plugin_state.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "../services/idle_timeout/idle_timeout.h"
#include "notification_method.h"
#include "automation_method.h"
#include "automation_kb_layout.h"
#ifdef TOTP_BADBT_TYPE_ENABLED
#include "../workers/bt_type_code/bt_type_code.h"
#endif
Expand Down Expand Up @@ -83,6 +84,11 @@ typedef struct {
*/
AutomationMethod automation_method;

/**
* @brief Automation keyboard layout to be used
*/
AutomationKeyboardLayout automation_kb_layout;

#ifdef TOTP_BADBT_TYPE_ENABLED
/**
* @brief Bad-Bluetooth worker context
Expand Down
Loading

0 comments on commit bc5daa2

Please sign in to comment.