Skip to content

Commit

Permalink
Code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
akopachov committed Jul 11, 2023
1 parent 0a424a5 commit 6315ccc
Show file tree
Hide file tree
Showing 8 changed files with 145 additions and 112 deletions.
41 changes: 20 additions & 21 deletions cli/commands/add/add.c
Original file line number Diff line number Diff line change
Expand Up @@ -102,22 +102,22 @@ void totp_cli_command_add_docopt_options() {
TOTP_CLI_PRINTF(" " DOCOPT_OPTION(
TOTP_CLI_COMMAND_ARG_ALGO_PREFIX,
DOCOPT_ARGUMENT(
TOTP_CLI_COMMAND_ARG_ALGO)) " Token hashing algorithm. Must be one of: " TOTP_TOKEN_ALGO_SHA1_NAME
", " TOTP_TOKEN_ALGO_SHA256_NAME
", " TOTP_TOKEN_ALGO_SHA512_NAME
", " TOTP_TOKEN_ALGO_STEAM_NAME
" " DOCOPT_DEFAULT(TOTP_TOKEN_ALGO_SHA1_NAME) "\r\n");
TOTP_CLI_COMMAND_ARG_ALGO)) " Token hashing algorithm. Must be one of: " TOKEN_HASH_ALGO_SHA1_NAME
", " TOKEN_HASH_ALGO_SHA256_NAME
", " TOKEN_HASH_ALGO_SHA512_NAME
", " TOKEN_HASH_ALGO_STEAM_NAME
" " DOCOPT_DEFAULT(TOKEN_HASH_ALGO_SHA1_NAME) "\r\n");
TOTP_CLI_PRINTF(
" " DOCOPT_OPTION(
TOTP_CLI_COMMAND_ARG_DIGITS_PREFIX,
DOCOPT_ARGUMENT(
TOTP_CLI_COMMAND_ARG_DIGITS)) " Number of digits to generate, one of: %" PRIu8
", %" PRIu8 ", %" PRIu8
" " DOCOPT_DEFAULT("%" PRIu8) "\r\n",
TotpFiveDigitsCount,
TotpSixDigitsCount,
TotpEightDigitsCount,
TotpSixDigitsCount);
TokenDigitsCountFive,
TokenDigitsCountSix,
TokenDigitsCountEight,
TokenDigitsCountSix);

TOTP_CLI_PRINTF(" " DOCOPT_OPTION(
TOTP_CLI_COMMAND_ARG_SECRET_ENCODING_PREFIX,
Expand All @@ -134,26 +134,25 @@ void totp_cli_command_add_docopt_options() {
TOTP_CLI_COMMAND_ARG_DURATION)) " Token lifetime duration in seconds, between: %" PRIu8
" and %" PRIu8
" " DOCOPT_DEFAULT("%" PRIu8) "\r\n",
TOTP_TOKEN_DURATION_MIN,
TOTP_TOKEN_DURATION_MAX,
TOTP_TOKEN_DURATION_DEFAULT);
TokenDurationMin,
TokenDurationMax,
TokenDurationDefault);
TOTP_CLI_PRINTF(" " DOCOPT_SWITCH(
TOTP_CLI_COMMAND_ARG_UNSECURE_PREFIX) " Show console user input as-is without masking\r\n");
TOTP_CLI_PRINTF(" " DOCOPT_OPTION(
TOTP_CLI_COMMAND_ARG_AUTOMATION_FEATURE_PREFIX,
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
TOTP_CLI_COMMAND_ARG_AUTOMATION_FEATURE)) " Token automation features to be enabled. Must be one of: " TOKEN_AUTOMATION_FEATURE_NONE_NAME
", " TOKEN_AUTOMATION_FEATURE_ENTER_AT_THE_END_NAME
", " 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
TOKEN_AUTOMATION_FEATURE_NONE_NAME) "\r\n");
TOTP_CLI_PRINTF(" # " TOKEN_AUTOMATION_FEATURE_NONE_NAME " - No features\r\n");
TOTP_CLI_PRINTF(" # " 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
TOTP_CLI_PRINTF(" # " TOKEN_AUTOMATION_FEATURE_TAB_AT_THE_END_NAME
" - Type <Tab> key at the end of token input automation\r\n");
TOTP_CLI_PRINTF(" # " TOTP_TOKEN_AUTOMATION_FEATURE_TYPE_SLOWER_NAME
TOTP_CLI_PRINTF(" # " TOKEN_AUTOMATION_FEATURE_TYPE_SLOWER_NAME
" - Type slower\r\n");
}

Expand Down
20 changes: 10 additions & 10 deletions services/config/migrations/common_migration.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,13 @@ bool totp_config_migrate_to_latest(
fff_backup_data_file, TOTP_CONFIG_KEY_TOKEN_ALGO, temp_str);

if(current_version < 5) {
uint32_t algo_as_uint32t = SHA1;
if(furi_string_cmpi_str(temp_str, TOTP_TOKEN_ALGO_SHA256_NAME) == 0) {
algo_as_uint32t = SHA256;
} else if(furi_string_cmpi_str(temp_str, TOTP_TOKEN_ALGO_SHA512_NAME) == 0) {
algo_as_uint32t = SHA512;
} else if(furi_string_cmpi_str(temp_str, TOTP_TOKEN_ALGO_STEAM_NAME) == 0) {
algo_as_uint32t = STEAM;
uint32_t algo_as_uint32t = TokenHashAlgoDefault;
if(furi_string_cmpi_str(temp_str, TOKEN_HASH_ALGO_SHA256_NAME) == 0) {
algo_as_uint32t = TokenHashAlgoSha256;
} else if(furi_string_cmpi_str(temp_str, TOKEN_HASH_ALGO_SHA512_NAME) == 0) {
algo_as_uint32t = TokenHashAlgoSha512;
} else if(furi_string_cmpi_str(temp_str, TOKEN_HASH_ALGO_STEAM_NAME) == 0) {
algo_as_uint32t = TokenHashAlgoSteam;
}

flipper_format_write_uint32(
Expand All @@ -95,10 +95,10 @@ bool totp_config_migrate_to_latest(
fff_backup_data_file, TOTP_CONFIG_KEY_TOKEN_DIGITS, temp_str);
flipper_format_write_string(fff_data_file, TOTP_CONFIG_KEY_TOKEN_DIGITS, temp_str);
} else {
const uint32_t default_algo = SHA1;
const uint32_t default_algo = TokenHashAlgoDefault;
flipper_format_write_uint32(
fff_data_file, TOTP_CONFIG_KEY_TOKEN_ALGO, &default_algo, 1);
const uint32_t default_digits = TotpSixDigitsCount;
const uint32_t default_digits = TokenDigitsCountSix;
flipper_format_write_uint32(
fff_data_file, TOTP_CONFIG_KEY_TOKEN_DIGITS, &default_digits, 1);
}
Expand All @@ -109,7 +109,7 @@ bool totp_config_migrate_to_latest(
flipper_format_write_string(
fff_data_file, TOTP_CONFIG_KEY_TOKEN_DURATION, temp_str);
} else {
const uint32_t default_duration = TOTP_TOKEN_DURATION_DEFAULT;
const uint32_t default_duration = TokenDurationDefault;
flipper_format_write_uint32(
fff_data_file, TOTP_CONFIG_KEY_TOKEN_DURATION, &default_duration, 1);
}
Expand Down
6 changes: 3 additions & 3 deletions services/config/token_info_iterator.c
Original file line number Diff line number Diff line change
Expand Up @@ -496,19 +496,19 @@ bool totp_token_info_iterator_go_to(TokenInfoIteratorContext* context, size_t to
if(!flipper_format_read_uint32(
context->config_file, TOTP_CONFIG_KEY_TOKEN_ALGO, &temp_data32, 1) ||
!token_info_set_algo_from_int(tokenInfo, temp_data32)) {
tokenInfo->algo = SHA1;
tokenInfo->algo = TokenHashAlgoDefault;
}

if(!flipper_format_read_uint32(
context->config_file, TOTP_CONFIG_KEY_TOKEN_DIGITS, &temp_data32, 1) ||
!token_info_set_digits_from_int(tokenInfo, temp_data32)) {
tokenInfo->digits = TotpSixDigitsCount;
tokenInfo->digits = TokenDigitsCountSix;
}

if(!flipper_format_read_uint32(
context->config_file, TOTP_CONFIG_KEY_TOKEN_DURATION, &temp_data32, 1) ||
!token_info_set_duration_from_int(tokenInfo, temp_data32)) {
tokenInfo->duration = TOTP_TOKEN_DURATION_DEFAULT;
tokenInfo->duration = TokenDurationDefault;
}

if(flipper_format_read_uint32(
Expand Down
72 changes: 36 additions & 36 deletions types/token_info.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,13 @@ bool token_info_set_secret(
bool token_info_set_digits_from_int(TokenInfo* token_info, uint8_t digits) {
switch(digits) {
case 5:
token_info->digits = TotpFiveDigitsCount;
token_info->digits = TokenDigitsCountFive;
return true;
case 6:
token_info->digits = TotpSixDigitsCount;
token_info->digits = TokenDigitsCountSix;
return true;
case 8:
token_info->digits = TotpEightDigitsCount;
token_info->digits = TokenDigitsCountEight;
return true;
default:
break;
Expand All @@ -87,7 +87,7 @@ bool token_info_set_digits_from_int(TokenInfo* token_info, uint8_t digits) {
bool token_info_set_duration_from_int(TokenInfo* token_info, uint8_t duration) {
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wtype-limits"
if(duration >= TOTP_TOKEN_DURATION_MIN && duration <= TOTP_TOKEN_DURATION_MAX) { //-V560
if(duration >= TokenDurationMin && duration <= TokenDurationMax) { //-V560
token_info->duration = duration;
return true;
}
Expand All @@ -97,23 +97,23 @@ bool token_info_set_duration_from_int(TokenInfo* token_info, uint8_t duration) {
}

bool token_info_set_algo_from_str(TokenInfo* token_info, const FuriString* str) {
if(furi_string_cmpi_str(str, TOTP_TOKEN_ALGO_SHA1_NAME) == 0) {
token_info->algo = SHA1;
if(furi_string_cmpi_str(str, TOKEN_HASH_ALGO_SHA1_NAME) == 0) {
token_info->algo = TokenHashAlgoSha1;
return true;
}

if(furi_string_cmpi_str(str, TOTP_TOKEN_ALGO_SHA256_NAME) == 0) {
token_info->algo = SHA256;
if(furi_string_cmpi_str(str, TOKEN_HASH_ALGO_SHA256_NAME) == 0) {
token_info->algo = TokenHashAlgoSha256;
return true;
}

if(furi_string_cmpi_str(str, TOTP_TOKEN_ALGO_SHA512_NAME) == 0) {
token_info->algo = SHA512;
if(furi_string_cmpi_str(str, TOKEN_HASH_ALGO_SHA512_NAME) == 0) {
token_info->algo = TokenHashAlgoSha512;
return true;
}

if(furi_string_cmpi_str(str, TOTP_TOKEN_ALGO_STEAM_NAME) == 0) {
token_info->algo = STEAM;
if(furi_string_cmpi_str(str, TOKEN_HASH_ALGO_STEAM_NAME) == 0) {
token_info->algo = TokenHashAlgoSteam;
return true;
}

Expand All @@ -122,17 +122,17 @@ bool token_info_set_algo_from_str(TokenInfo* token_info, const FuriString* str)

bool token_info_set_algo_from_int(TokenInfo* token_info, uint8_t algo_code) {
switch(algo_code) {
case SHA1:
token_info->algo = SHA1;
case TokenHashAlgoSha1:
token_info->algo = TokenHashAlgoSha1;
break;
case SHA256:
token_info->algo = SHA256;
case TokenHashAlgoSha256:
token_info->algo = TokenHashAlgoSha256;
break;
case SHA512:
token_info->algo = SHA512;
case TokenHashAlgoSha512:
token_info->algo = TokenHashAlgoSha512;
break;
case STEAM:
token_info->algo = STEAM;
case TokenHashAlgoSteam:
token_info->algo = TokenHashAlgoSteam;
break;
default:
return false;
Expand All @@ -141,16 +141,16 @@ bool token_info_set_algo_from_int(TokenInfo* token_info, uint8_t algo_code) {
return true;
}

char* token_info_get_algo_as_cstr(const TokenInfo* token_info) {
const char* token_info_get_algo_as_cstr(const TokenInfo* token_info) {
switch(token_info->algo) {
case SHA1:
return TOTP_TOKEN_ALGO_SHA1_NAME;
case SHA256:
return TOTP_TOKEN_ALGO_SHA256_NAME;
case SHA512:
return TOTP_TOKEN_ALGO_SHA512_NAME;
case STEAM:
return TOTP_TOKEN_ALGO_STEAM_NAME;
case TokenHashAlgoSha1:
return TOKEN_HASH_ALGO_SHA1_NAME;
case TokenHashAlgoSha256:
return TOKEN_HASH_ALGO_SHA256_NAME;
case TokenHashAlgoSha512:
return TOKEN_HASH_ALGO_SHA512_NAME;
case TokenHashAlgoSteam:
return TOKEN_HASH_ALGO_STEAM_NAME;
default:
break;
}
Expand All @@ -159,22 +159,22 @@ char* token_info_get_algo_as_cstr(const TokenInfo* token_info) {
}

bool token_info_set_automation_feature_from_str(TokenInfo* token_info, const FuriString* str) {
if(furi_string_cmpi_str(str, TOTP_TOKEN_AUTOMATION_FEATURE_ENTER_AT_THE_END_NAME) == 0) {
if(furi_string_cmpi_str(str, TOKEN_AUTOMATION_FEATURE_ENTER_AT_THE_END_NAME) == 0) {
token_info->automation_features |= TokenAutomationFeatureEnterAtTheEnd;
return true;
}

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

if(furi_string_cmpi_str(str, TOTP_TOKEN_AUTOMATION_FEATURE_TYPE_SLOWER_NAME) == 0) {
if(furi_string_cmpi_str(str, TOKEN_AUTOMATION_FEATURE_TYPE_SLOWER_NAME) == 0) {
token_info->automation_features |= TokenAutomationFeatureTypeSlower;
return true;
}

if(furi_string_cmpi_str(str, TOTP_TOKEN_AUTOMATION_FEATURE_NONE_NAME) == 0) {
if(furi_string_cmpi_str(str, TOKEN_AUTOMATION_FEATURE_NONE_NAME) == 0) {
token_info->automation_features = TokenAutomationFeatureNone;
return true;
}
Expand All @@ -198,9 +198,9 @@ TokenInfo* token_info_clone(const TokenInfo* src) {

void token_info_set_defaults(TokenInfo* token_info) {
furi_check(token_info != NULL);
token_info->algo = SHA1;
token_info->digits = TotpSixDigitsCount;
token_info->duration = TOTP_TOKEN_DURATION_DEFAULT;
token_info->algo = TokenHashAlgoDefault;
token_info->digits = TokenDigitsCountDefault;
token_info->duration = TokenDurationDefault;
token_info->automation_features = TokenAutomationFeatureNone;
furi_string_reset(token_info->name);
}
Loading

0 comments on commit 6315ccc

Please sign in to comment.