2022-10-28 15:34:35 +00:00
|
|
|
#include "add.h"
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <lib/toolbox/args.h>
|
2023-04-05 19:45:27 +00:00
|
|
|
#include <linked_list.h>
|
2022-11-23 22:19:19 +00:00
|
|
|
#include "../../../types/token_info.h"
|
|
|
|
#include "../../../services/config/config.h"
|
|
|
|
#include "../../../services/convert/convert.h"
|
2022-10-28 15:34:35 +00:00
|
|
|
#include "../../cli_helpers.h"
|
2022-11-23 22:19:19 +00:00
|
|
|
#include "../../../ui/scene_director.h"
|
2023-04-01 14:45:52 +00:00
|
|
|
#include "../../common_command_arguments.h"
|
2023-01-18 16:52:33 +00:00
|
|
|
|
2022-10-28 15:34:35 +00:00
|
|
|
void totp_cli_command_add_docopt_commands() {
|
|
|
|
TOTP_CLI_PRINTF(" " TOTP_CLI_COMMAND_ADD ", " TOTP_CLI_COMMAND_ADD_ALT
|
|
|
|
", " TOTP_CLI_COMMAND_ADD_ALT2 " Add new token\r\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
void totp_cli_command_add_docopt_usage() {
|
|
|
|
TOTP_CLI_PRINTF(
|
|
|
|
" " TOTP_CLI_COMMAND_NAME
|
2023-04-05 19:45:27 +00:00
|
|
|
" " DOCOPT_REQUIRED(TOTP_CLI_COMMAND_ADD " | " TOTP_CLI_COMMAND_ADD_ALT " | " TOTP_CLI_COMMAND_ADD_ALT2) " " DOCOPT_ARGUMENT(TOTP_CLI_COMMAND_ARG_NAME) " " DOCOPT_OPTIONAL(DOCOPT_OPTION(TOTP_CLI_COMMAND_ARG_ALGO_PREFIX, DOCOPT_ARGUMENT(TOTP_CLI_COMMAND_ARG_ALGO))) " " DOCOPT_OPTIONAL(DOCOPT_OPTION(TOTP_CLI_COMMAND_ARG_SECRET_ENCODING_PREFIX, DOCOPT_ARGUMENT(TOTP_CLI_COMMAND_ARG_SECRET_ENCODING))) " " DOCOPT_OPTIONAL(
|
2022-10-28 15:34:35 +00:00
|
|
|
DOCOPT_OPTION(
|
2023-04-05 19:45:27 +00:00
|
|
|
TOTP_CLI_COMMAND_ARG_DIGITS_PREFIX,
|
2022-10-28 15:34:35 +00:00
|
|
|
DOCOPT_ARGUMENT(
|
2023-04-05 19:45:27 +00:00
|
|
|
TOTP_CLI_COMMAND_ARG_DIGITS))) " " DOCOPT_OPTIONAL(DOCOPT_OPTION(TOTP_CLI_COMMAND_ARG_DURATION_PREFIX, DOCOPT_ARGUMENT(TOTP_CLI_COMMAND_ARG_DURATION))) " " DOCOPT_OPTIONAL(DOCOPT_SWITCH(TOTP_CLI_COMMAND_ARG_UNSECURE_PREFIX)) " " DOCOPT_MULTIPLE(DOCOPT_OPTIONAL(DOCOPT_OPTION(TOTP_CLI_COMMAND_ARG_AUTOMATION_FEATURE_PREFIX, DOCOPT_ARGUMENT(TOTP_CLI_COMMAND_ARG_AUTOMATION_FEATURE)))) "\r\n");
|
2022-10-28 15:34:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void totp_cli_command_add_docopt_arguments() {
|
2023-04-01 14:45:52 +00:00
|
|
|
TOTP_CLI_PRINTF(" " TOTP_CLI_COMMAND_ARG_NAME " Token name\r\n");
|
2022-10-28 15:34:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void totp_cli_command_add_docopt_options() {
|
|
|
|
TOTP_CLI_PRINTF(" " DOCOPT_OPTION(
|
2023-04-01 14:45:52 +00:00
|
|
|
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
|
2023-04-05 19:45:27 +00:00
|
|
|
", " TOTP_TOKEN_ALGO_STEAM_NAME
|
2023-04-01 14:45:52 +00:00
|
|
|
" " DOCOPT_DEFAULT(TOTP_TOKEN_ALGO_SHA1_NAME) "\r\n");
|
2022-10-28 15:34:35 +00:00
|
|
|
TOTP_CLI_PRINTF(" " DOCOPT_OPTION(
|
2023-04-01 14:45:52 +00:00
|
|
|
TOTP_CLI_COMMAND_ARG_DIGITS_PREFIX,
|
2022-10-28 15:34:35 +00:00
|
|
|
DOCOPT_ARGUMENT(
|
2023-04-05 19:45:27 +00:00
|
|
|
TOTP_CLI_COMMAND_ARG_DIGITS)) " Number of digits to generate, one of: 5, 6, 8 " DOCOPT_DEFAULT("6") "\r\n");
|
|
|
|
|
|
|
|
TOTP_CLI_PRINTF(" " DOCOPT_OPTION(
|
|
|
|
TOTP_CLI_COMMAND_ARG_SECRET_ENCODING_PREFIX,
|
|
|
|
DOCOPT_ARGUMENT(
|
|
|
|
TOTP_CLI_COMMAND_ARG_SECRET_ENCODING)) " Token secret encoding, one of " PLAIN_TOKEN_ENCODING_BASE32_NAME
|
|
|
|
", " PLAIN_TOKEN_ENCODING_BASE64_NAME
|
|
|
|
" " DOCOPT_DEFAULT(
|
|
|
|
PLAIN_TOKEN_ENCODING_BASE32_NAME) "\r\n");
|
|
|
|
|
2023-01-18 16:52:33 +00:00
|
|
|
TOTP_CLI_PRINTF(" " DOCOPT_OPTION(
|
2023-04-01 14:45:52 +00:00
|
|
|
TOTP_CLI_COMMAND_ARG_DURATION_PREFIX,
|
2023-01-18 16:52:33 +00:00
|
|
|
DOCOPT_ARGUMENT(
|
2023-04-01 14:45:52 +00:00
|
|
|
TOTP_CLI_COMMAND_ARG_DURATION)) " Token lifetime duration in seconds, between: 15 and 255 " DOCOPT_DEFAULT("30") "\r\n");
|
2022-10-28 15:34:35 +00:00
|
|
|
TOTP_CLI_PRINTF(" " DOCOPT_SWITCH(
|
2023-04-01 14:45:52 +00:00
|
|
|
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
|
|
|
|
" " 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");
|
|
|
|
TOTP_CLI_PRINTF(" # " TOTP_TOKEN_AUTOMATION_FEATURE_TYPE_SLOWER_NAME
|
|
|
|
" - Type slower\r\n");
|
2022-11-10 05:32:21 +00:00
|
|
|
}
|
|
|
|
|
2022-10-28 15:34:35 +00:00
|
|
|
void totp_cli_command_add_handle(PluginState* plugin_state, FuriString* args, Cli* cli) {
|
|
|
|
FuriString* temp_str = furi_string_alloc();
|
|
|
|
TokenInfo* token_info = token_info_alloc();
|
|
|
|
|
|
|
|
// Reading token name
|
|
|
|
if(!args_read_probably_quoted_string_and_trim(args, temp_str)) {
|
|
|
|
TOTP_CLI_PRINT_INVALID_ARGUMENTS();
|
|
|
|
furi_string_free(temp_str);
|
|
|
|
token_info_free(token_info);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-11-10 05:32:21 +00:00
|
|
|
size_t temp_cstr_len = furi_string_size(temp_str);
|
|
|
|
token_info->name = malloc(temp_cstr_len + 1);
|
|
|
|
furi_check(token_info->name != NULL);
|
|
|
|
strlcpy(token_info->name, furi_string_get_cstr(temp_str), temp_cstr_len + 1);
|
2022-10-28 15:34:35 +00:00
|
|
|
|
|
|
|
// Read optional arguments
|
|
|
|
bool mask_user_input = true;
|
2023-04-05 19:45:27 +00:00
|
|
|
PlainTokenSecretEncoding token_secret_encoding = PLAIN_TOKEN_ENCODING_BASE32;
|
2022-10-28 15:34:35 +00:00
|
|
|
while(args_read_string_and_trim(args, temp_str)) {
|
|
|
|
bool parsed = false;
|
2023-04-01 14:45:52 +00:00
|
|
|
if(!totp_cli_try_read_algo(token_info, temp_str, args, &parsed) &&
|
|
|
|
!totp_cli_try_read_digits(token_info, temp_str, args, &parsed) &&
|
|
|
|
!totp_cli_try_read_duration(token_info, temp_str, args, &parsed) &&
|
|
|
|
!totp_cli_try_read_unsecure_flag(temp_str, &parsed, &mask_user_input) &&
|
2023-04-05 19:45:27 +00:00
|
|
|
!totp_cli_try_read_automation_features(token_info, temp_str, args, &parsed) &&
|
|
|
|
!totp_cli_try_read_plain_token_secret_encoding(
|
|
|
|
temp_str, args, &parsed, &token_secret_encoding)) {
|
2023-04-01 14:45:52 +00:00
|
|
|
totp_cli_printf_unknown_argument(temp_str);
|
2022-10-28 15:34:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(!parsed) {
|
|
|
|
TOTP_CLI_PRINT_INVALID_ARGUMENTS();
|
|
|
|
furi_string_free(temp_str);
|
|
|
|
token_info_free(token_info);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reading token secret
|
|
|
|
furi_string_reset(temp_str);
|
|
|
|
TOTP_CLI_PRINTF("Enter token secret and confirm with [ENTER]\r\n");
|
2023-01-06 23:07:26 +00:00
|
|
|
if(!totp_cli_read_line(cli, temp_str, mask_user_input) ||
|
2022-11-10 05:32:21 +00:00
|
|
|
!totp_cli_ensure_authenticated(plugin_state, cli)) {
|
2023-01-06 23:07:26 +00:00
|
|
|
TOTP_CLI_DELETE_LAST_LINE();
|
2023-03-09 15:40:09 +00:00
|
|
|
TOTP_CLI_PRINTF_INFO("Cancelled by user\r\n");
|
2022-11-10 05:32:21 +00:00
|
|
|
furi_string_secure_free(temp_str);
|
2022-10-28 15:34:35 +00:00
|
|
|
token_info_free(token_info);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-01-06 23:07:26 +00:00
|
|
|
TOTP_CLI_DELETE_LAST_LINE();
|
|
|
|
|
2022-10-28 15:34:35 +00:00
|
|
|
bool load_generate_token_scene = false;
|
|
|
|
if(plugin_state->current_scene == TotpSceneGenerateToken) {
|
|
|
|
totp_scene_director_activate_scene(plugin_state, TotpSceneNone, NULL);
|
|
|
|
load_generate_token_scene = true;
|
|
|
|
}
|
|
|
|
|
2023-04-05 19:45:27 +00:00
|
|
|
bool secret_set = token_info_set_secret(
|
|
|
|
token_info,
|
|
|
|
furi_string_get_cstr(temp_str),
|
|
|
|
furi_string_size(temp_str),
|
|
|
|
token_secret_encoding,
|
|
|
|
plugin_state->iv);
|
|
|
|
|
|
|
|
furi_string_secure_free(temp_str);
|
|
|
|
|
|
|
|
if(secret_set) {
|
|
|
|
TOTP_LIST_INIT_OR_ADD(plugin_state->tokens_list, token_info, furi_check);
|
|
|
|
plugin_state->tokens_count++;
|
|
|
|
|
|
|
|
if(totp_config_file_save_new_token(token_info) == TotpConfigFileUpdateSuccess) {
|
|
|
|
TOTP_CLI_PRINTF_SUCCESS(
|
|
|
|
"Token \"%s\" has been successfully added\r\n", token_info->name);
|
|
|
|
} else {
|
|
|
|
TOTP_CLI_PRINT_ERROR_UPDATING_CONFIG_FILE();
|
|
|
|
}
|
2022-12-26 20:31:38 +00:00
|
|
|
} else {
|
2023-04-05 19:45:27 +00:00
|
|
|
token_info_free(token_info);
|
|
|
|
TOTP_CLI_PRINTF_ERROR("Token secret seems to be invalid and can not be parsed\r\n");
|
2022-12-26 20:31:38 +00:00
|
|
|
}
|
2022-10-28 15:34:35 +00:00
|
|
|
|
|
|
|
if(load_generate_token_scene) {
|
|
|
|
totp_scene_director_activate_scene(plugin_state, TotpSceneGenerateToken, NULL);
|
|
|
|
}
|
|
|
|
}
|