Skip to content

Commit

Permalink
Refactoring & bugfixes (#142)
Browse files Browse the repository at this point in the history
* Another round of refactoring

* * FlipC.org assets added

* Few bugfixes

* * Updated firmware references

* CLang format ran
  • Loading branch information
akopachov authored Apr 26, 2023
1 parent ebcc079 commit d6383b3
Show file tree
Hide file tree
Showing 33 changed files with 522 additions and 196 deletions.
Binary file added .flipcorg/gallery/1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added .flipcorg/gallery/2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added .flipcorg/gallery/3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added .flipcorg/gallery/4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
36 changes: 28 additions & 8 deletions docs/conf-file_description.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Flipper Authenticator config file description

By default Flipper Authenticator stores all its settings in [`/ext/authenticator/totp.conf`](https://github.com/akopachov/flipper-zero_authenticator/blob/master/totp/services/config/config.c#:~:text=%23define%20CONFIG_FILE_DIRECTORY_PATH,totp.conf%22) file.
By default Flipper Authenticator stores all its settings in `/ext/authenticator/totp.conf` file.

File format is standard for Flipper Zero device. Each line has one setting identified by key, where key and value are separated by `:` symbol.

Expand Down Expand Up @@ -160,11 +160,11 @@ File format is standard for Flipper Zero device. Each line has one setting ident
<summary><h3 style="display: inline">TokenAlgo</h3></summary>
<p>

**Type:** enum (available options are: `sha1`, `sha256`, `sha512`)
**Type:** enum (available options are: `0` (`sha1`), `1` (`sha256`), `2` (`sha512`), `3` (`steam`))

**Default value:** `sha1`
**Default value:** `0` (`sha1`)

**Description:** Token hashing algorithm to be used to generate TOTP code. If you don't know which one to use - use `sha1`.
**Description:** Token hashing algorithm to be used to generate TOTP code. If you don't know which one to use - use `0` (`sha1`).

</p>
</details>
Expand Down Expand Up @@ -195,29 +195,49 @@ File format is standard for Flipper Zero device. Each line has one setting ident
</p>
</details>

<details>
<summary><h3 style="display: inline">TokenAutomationFeatures</h3></summary>
<p>

**Type:** unsigned int

**Default value:** `0` (no features enabled)

**Description:** Token automation features. Meaningful bits:

* Bit 1 - if set, token input automation will type \<ENTER\> key at the end of automation
* Bit 2 - if set, token input automation will type \<TAB\> key at the end of automation
* Bit 3 - if set, token input automation will type slower

</p>
</details>

## Example config file

```text
Filetype: Flipper TOTP plugin config file
Version: 3
Version: 5
BaseIV: AD F2 DE F3 31 92 C8 77 4B EB BF FE 7D E1 27 51
Crypto: FE CC 38 99 28 A9 28 6B BC E1 E3 92 B9 02 8A DF
Timezone: 2.000000
PinIsSet: true
NotificationMethod: 3
TokenName: Test plain
TokenSecret: 95 6B CE 3E 2F 01 AF 29 B2 9A DE CA E7 EF F5 B1
TokenAlgo: sha1
TokenAlgo: 0
TokenDigits: 6
TokenDuration: 30
TokenAutomationFeatures: 0
TokenName: Verifyr sha256
TokenSecret: SSECIUHGRYRCRBCNKKXPUQBLBGEQZ3PKNA7TA7TQV6IL5WDFU62TNNT3NHKVWRCQWF4QTSE4IGLG4S7RGY3LDMVDZVMAGB2ARPG7XYQ
TokenAlgo: sha256
TokenAlgo: 1
TokenDigits: 6
TokenDuration: 60
TokenAutomationFeatures: 0
TokenName: Verifyr sha512 8
TokenSecret: 3KKGABEJ4CKS5AHBZBDHFFNKUZHN6D7TKUGI3T7SHEUBAMIAPBUBWQNCMEEGEJX2LF23PYAFUCSRNVQ2ENOQWLHISCOJQCU2SCND4CI
TokenAlgo: sha512
TokenAlgo: 2
TokenDigits: 8
TokenDuration: 30
TokenAutomationFeatures: 0
```
28 changes: 16 additions & 12 deletions totp/cli/commands/add/add.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ enum TotpIteratorUpdateTokenResultsEx {
TotpIteratorUpdateTokenResultInvalidArguments = 3
};

static TotpIteratorUpdateTokenResult add_token_handler(TokenInfo* token_info, const void* context) {
static TotpIteratorUpdateTokenResult
add_token_handler(TokenInfo* token_info, const void* context) {
const struct TotpAddContext* context_t = context;

// Reading token name
Expand All @@ -32,7 +33,7 @@ static TotpIteratorUpdateTokenResult add_token_handler(TokenInfo* token_info, co

// Read optional arguments
bool mask_user_input = true;
PlainTokenSecretEncoding token_secret_encoding = PLAIN_TOKEN_ENCODING_BASE32;
PlainTokenSecretEncoding token_secret_encoding = PlainTokenSecretEncodingBase32;
while(args_read_string_and_trim(context_t->args, temp_str)) {
bool parsed = false;
if(!totp_cli_try_read_algo(token_info, temp_str, context_t->args, &parsed) &&
Expand Down Expand Up @@ -71,7 +72,7 @@ static TotpIteratorUpdateTokenResult add_token_handler(TokenInfo* token_info, co

furi_string_secure_free(temp_str);

if (!secret_set) {
if(!secret_set) {
return TotpIteratorUpdateTokenResultInvalidSecret;
}

Expand Down Expand Up @@ -148,24 +149,27 @@ void totp_cli_command_add_handle(PluginState* plugin_state, FuriString* args, Cl
return;
}

TokenInfoIteratorContext* iterator_context = totp_config_get_token_iterator_context(plugin_state);
TokenInfoIteratorContext* iterator_context =
totp_config_get_token_iterator_context(plugin_state);

TOTP_CLI_LOCK_UI(plugin_state);

struct TotpAddContext add_context = { .args = args, .cli = cli, .iv = &plugin_state->iv[0] };
TotpIteratorUpdateTokenResult add_result = totp_token_info_iterator_add_new_token(iterator_context, &add_token_handler, &add_context);
struct TotpAddContext add_context = {.args = args, .cli = cli, .iv = &plugin_state->iv[0]};
TotpIteratorUpdateTokenResult add_result =
totp_token_info_iterator_add_new_token(iterator_context, &add_token_handler, &add_context);

if(add_result == TotpIteratorUpdateTokenResultSuccess) {
TOTP_CLI_PRINTF_SUCCESS(
"Token \"%s\" has been successfully added\r\n",
furi_string_get_cstr(totp_token_info_iterator_get_current_token(iterator_context)->name));
} else if (add_result == TotpIteratorUpdateTokenResultCancelled) {
"Token \"%s\" has been successfully added\r\n",
furi_string_get_cstr(
totp_token_info_iterator_get_current_token(iterator_context)->name));
} else if(add_result == TotpIteratorUpdateTokenResultCancelled) {
TOTP_CLI_PRINTF_INFO("Cancelled by user\r\n");
} else if (add_result == TotpIteratorUpdateTokenResultInvalidArguments) {
} else if(add_result == TotpIteratorUpdateTokenResultInvalidArguments) {
totp_cli_print_invalid_arguments();
} else if (add_result == TotpIteratorUpdateTokenResultInvalidSecret) {
} else if(add_result == TotpIteratorUpdateTokenResultInvalidSecret) {
TOTP_CLI_PRINTF_ERROR("Token secret seems to be invalid and can not be parsed\r\n");
} else if (add_result == TotpIteratorUpdateTokenResultFileUpdateFailed) {
} else if(add_result == TotpIteratorUpdateTokenResultFileUpdateFailed) {
totp_cli_print_error_updating_config_file();
}

Expand Down
9 changes: 5 additions & 4 deletions totp/cli/commands/delete/delete.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ void totp_cli_command_delete_handle(PluginState* plugin_state, FuriString* args,
return;
}

TokenInfoIteratorContext* iterator_context = totp_config_get_token_iterator_context(plugin_state);
TokenInfoIteratorContext* iterator_context =
totp_config_get_token_iterator_context(plugin_state);

int token_number;
if(!args_read_int_and_trim(args, &token_number) || token_number <= 0 ||
(size_t)token_number >
totp_token_info_iterator_get_total_count(iterator_context)) {
(size_t)token_number > totp_token_info_iterator_get_total_count(iterator_context)) {
totp_cli_print_invalid_arguments();
return;
}
Expand All @@ -62,7 +62,8 @@ void totp_cli_command_delete_handle(PluginState* plugin_state, FuriString* args,

TOTP_CLI_LOCK_UI(plugin_state);

size_t original_token_index = totp_token_info_iterator_get_current_token_index(iterator_context);
size_t original_token_index =
totp_token_info_iterator_get_current_token_index(iterator_context);
totp_token_info_iterator_go_to(iterator_context, token_number - 1);
const TokenInfo* token_info = totp_token_info_iterator_get_current_token(iterator_context);
const char* token_info_name = furi_string_get_cstr(token_info->name);
Expand Down
14 changes: 8 additions & 6 deletions totp/cli/commands/details/details.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,21 @@
} while(false)

static void print_automation_features(const TokenInfo* token_info) {
if(token_info->automation_features == TOKEN_AUTOMATION_FEATURE_NONE) {
if(token_info->automation_features == TokenAutomationFeatureNone) {
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) {
if(token_info->automation_features & TokenAutomationFeatureEnterAtTheEnd) {
TOTP_CLI_PRINTF_AUTOMATION_FEATURE("Type <Enter> key at the end", header_printed);
}

if(token_info->automation_features & TOKEN_AUTOMATION_FEATURE_TAB_AT_THE_END) {
if(token_info->automation_features & TokenAutomationFeatureTabAtTheEnd) {
TOTP_CLI_PRINTF_AUTOMATION_FEATURE("Type <Tab> key at the end", header_printed);
}

if(token_info->automation_features & TOKEN_AUTOMATION_FEATURE_TYPE_SLOWER) {
if(token_info->automation_features & TokenAutomationFeatureTypeSlower) {
TOTP_CLI_PRINTF_AUTOMATION_FEATURE("Type slower", header_printed);
}
}
Expand All @@ -54,7 +54,8 @@ void totp_cli_command_details_handle(PluginState* plugin_state, FuriString* args
}

int token_number;
TokenInfoIteratorContext* iterator_context = totp_config_get_token_iterator_context(plugin_state);
TokenInfoIteratorContext* iterator_context =
totp_config_get_token_iterator_context(plugin_state);
if(!args_read_int_and_trim(args, &token_number) || token_number <= 0 ||
(size_t)token_number > totp_token_info_iterator_get_total_count(iterator_context)) {
totp_cli_print_invalid_arguments();
Expand All @@ -63,7 +64,8 @@ void totp_cli_command_details_handle(PluginState* plugin_state, FuriString* args

TOTP_CLI_LOCK_UI(plugin_state);

size_t original_token_index = totp_token_info_iterator_get_current_token_index(iterator_context);
size_t original_token_index =
totp_token_info_iterator_get_current_token_index(iterator_context);
if(totp_token_info_iterator_go_to(iterator_context, token_number - 1)) {
const TokenInfo* token_info = totp_token_info_iterator_get_current_token(iterator_context);

Expand Down
3 changes: 2 additions & 1 deletion totp/cli/commands/list/list.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ void totp_cli_command_list_handle(PluginState* plugin_state, Cli* cli) {
return;
}

TokenInfoIteratorContext* iterator_context = totp_config_get_token_iterator_context(plugin_state);
TokenInfoIteratorContext* iterator_context =
totp_config_get_token_iterator_context(plugin_state);
size_t total_count = totp_token_info_iterator_get_total_count(iterator_context);
if(total_count <= 0) {
TOTP_CLI_PRINTF("There are no tokens");
Expand Down
9 changes: 6 additions & 3 deletions totp/cli/commands/move/move.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ void totp_cli_command_move_handle(PluginState* plugin_state, FuriString* args, C
}

int token_number;
TokenInfoIteratorContext* iterator_context = totp_config_get_token_iterator_context(plugin_state);
TokenInfoIteratorContext* iterator_context =
totp_config_get_token_iterator_context(plugin_state);
size_t total_count = totp_token_info_iterator_get_total_count(iterator_context);
if(!args_read_int_and_trim(args, &token_number) || token_number < 1 ||
(size_t)token_number > total_count) {
Expand All @@ -59,7 +60,8 @@ void totp_cli_command_move_handle(PluginState* plugin_state, FuriString* args, C
size_t token_index = token_number - 1;
size_t new_token_index = new_token_number - 1;

size_t original_token_index = totp_token_info_iterator_get_current_token_index(iterator_context);
size_t original_token_index =
totp_token_info_iterator_get_current_token_index(iterator_context);

totp_cli_print_processing();

Expand All @@ -68,7 +70,8 @@ void totp_cli_command_move_handle(PluginState* plugin_state, FuriString* args, C
totp_cli_delete_last_line();
TOTP_CLI_PRINTF_SUCCESS(
"Token \"%s\" has been successfully updated\r\n",
furi_string_get_cstr(totp_token_info_iterator_get_current_token(iterator_context)->name));
furi_string_get_cstr(
totp_token_info_iterator_get_current_token(iterator_context)->name));
} else {
totp_cli_delete_last_line();
totp_cli_print_error_updating_config_file();
Expand Down
50 changes: 29 additions & 21 deletions totp/cli/commands/update/update.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,11 @@ enum TotpIteratorUpdateTokenResultsEx {
TotpIteratorUpdateTokenResultInvalidArguments = 3
};

static bool
totp_cli_try_read_name(TokenInfo* token_info, const FuriString* arg, FuriString* args, bool* parsed) {
static bool totp_cli_try_read_name(
TokenInfo* token_info,
const FuriString* arg,
FuriString* args,
bool* parsed) {
if(furi_string_cmpi_str(arg, TOTP_CLI_COMMAND_ARG_NAME_PREFIX) == 0) {
if(!args_read_probably_quoted_string_and_trim(args, token_info->name) ||
furi_string_empty(token_info->name)) {
Expand All @@ -48,14 +51,15 @@ static bool totp_cli_try_read_change_secret_flag(const FuriString* arg, bool* pa
return false;
}

static TotpIteratorUpdateTokenResult update_token_handler(TokenInfo* token_info, const void* context) {
static TotpIteratorUpdateTokenResult
update_token_handler(TokenInfo* token_info, const void* context) {
const struct TotpUpdateContext* context_t = context;

// Read optional arguments
FuriString* temp_str = furi_string_alloc();
bool mask_user_input = true;
bool update_token_secret = false;
PlainTokenSecretEncoding token_secret_encoding = PLAIN_TOKEN_ENCODING_BASE32;
PlainTokenSecretEncoding token_secret_encoding = PlainTokenSecretEncodingBase32;
while(args_read_string_and_trim(context_t->args, temp_str)) {
bool parsed = false;
if(!totp_cli_try_read_name(token_info, temp_str, context_t->args, &parsed) &&
Expand All @@ -82,17 +86,17 @@ static TotpIteratorUpdateTokenResult update_token_handler(TokenInfo* token_info,
TOTP_CLI_PRINTF("Enter token secret and confirm with [ENTER]\r\n");
bool token_secret_read = totp_cli_read_line(context_t->cli, temp_str, mask_user_input);
totp_cli_delete_last_line();
if (!token_secret_read) {
if(!token_secret_read) {
furi_string_secure_free(temp_str);
return TotpIteratorUpdateTokenResultCancelled;
}

if (!token_info_set_secret(
token_info,
furi_string_get_cstr(temp_str),
furi_string_size(temp_str),
token_secret_encoding,
context_t->iv)) {
if(!token_info_set_secret(
token_info,
furi_string_get_cstr(temp_str),
furi_string_size(temp_str),
token_secret_encoding,
context_t->iv)) {
furi_string_secure_free(temp_str);
return TotpIteratorUpdateTokenResultInvalidSecret;
}
Expand Down Expand Up @@ -131,7 +135,8 @@ void totp_cli_command_update_handle(PluginState* plugin_state, FuriString* args,
return;
}

TokenInfoIteratorContext* iterator_context = totp_config_get_token_iterator_context(plugin_state);
TokenInfoIteratorContext* iterator_context =
totp_config_get_token_iterator_context(plugin_state);

int token_number;
if(!args_read_int_and_trim(args, &token_number) || token_number <= 0 ||
Expand All @@ -145,23 +150,26 @@ void totp_cli_command_update_handle(PluginState* plugin_state, FuriString* args,
size_t previous_index = totp_token_info_iterator_get_current_token_index(iterator_context);
totp_token_info_iterator_go_to(iterator_context, token_number - 1);

struct TotpUpdateContext update_context = { .args = args, .cli = cli, .iv = &plugin_state->iv[0] };
TotpIteratorUpdateTokenResult update_result = totp_token_info_iterator_update_current_token(iterator_context, &update_token_handler, &update_context);

struct TotpUpdateContext update_context = {
.args = args, .cli = cli, .iv = &plugin_state->iv[0]};
TotpIteratorUpdateTokenResult update_result = totp_token_info_iterator_update_current_token(
iterator_context, &update_token_handler, &update_context);

if(update_result == TotpIteratorUpdateTokenResultSuccess) {
TOTP_CLI_PRINTF_SUCCESS(
"Token \"%s\" has been successfully updated\r\n",
furi_string_get_cstr(totp_token_info_iterator_get_current_token(iterator_context)->name));
} else if (update_result == TotpIteratorUpdateTokenResultInvalidArguments) {
furi_string_get_cstr(
totp_token_info_iterator_get_current_token(iterator_context)->name));
} else if(update_result == TotpIteratorUpdateTokenResultInvalidArguments) {
totp_cli_print_invalid_arguments();
} else if (update_result == TotpIteratorUpdateTokenResultCancelled) {
} else if(update_result == TotpIteratorUpdateTokenResultCancelled) {
TOTP_CLI_PRINTF_INFO("Cancelled by user\r\n");
} else if (update_result == TotpIteratorUpdateTokenResultInvalidSecret) {
} else if(update_result == TotpIteratorUpdateTokenResultInvalidSecret) {
TOTP_CLI_PRINTF_ERROR("Token secret seems to be invalid and can not be parsed\r\n");
} else if (update_result == TotpIteratorUpdateTokenResultFileUpdateFailed) {
} else if(update_result == TotpIteratorUpdateTokenResultFileUpdateFailed) {
totp_cli_print_error_updating_config_file();
}

totp_token_info_iterator_go_to(iterator_context, previous_index);
TOTP_CLI_UNLOCK_UI(plugin_state);
}
4 changes: 2 additions & 2 deletions totp/cli/common_command_arguments.c
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,10 @@ bool totp_cli_try_read_plain_token_secret_encoding(
totp_cli_printf_missed_argument_value(TOTP_CLI_COMMAND_ARG_SECRET_ENCODING_PREFIX);
} else {
if(furi_string_cmpi_str(arg, PLAIN_TOKEN_ENCODING_BASE32_NAME) == 0) {
*secret_encoding = PLAIN_TOKEN_ENCODING_BASE32;
*secret_encoding = PlainTokenSecretEncodingBase32;
*parsed = true;
} else if(furi_string_cmpi_str(arg, PLAIN_TOKEN_ENCODING_BASE64_NAME) == 0) {
*secret_encoding = PLAIN_TOKEN_ENCODING_BASE64;
*secret_encoding = PlainTokenSecretEncodingBase64;
*parsed = true;
} else {
TOTP_CLI_PRINTF_ERROR(
Expand Down
1 change: 1 addition & 0 deletions totp/lib/base64/base64.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/*
* Base64 encoding/decoding (RFC1341)
* Copyright (c) 2005, Jouni Malinen <j@w1.fi>
* Modified and optimized for Flipepr Zero device purposes by Alex Kopachov (@akopachov)
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
Expand Down
Loading

0 comments on commit d6383b3

Please sign in to comment.