update.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. #include <flipper_application/flipper_application.h>
  2. #include <lib/toolbox/args.h>
  3. #include "../../../cli_helpers.h"
  4. #include "../../../cli_shared_methods.h"
  5. #include "../../../cli_plugin_interface.h"
  6. #include "../../../../types/token_info.h"
  7. #include "../../../../services/config/config.h"
  8. #include "../../../../services/convert/convert.h"
  9. #include "../../../../ui/scene_director.h"
  10. #include "../common.h"
  11. #define TOTP_CLI_COMMAND_UPDATE_ARG_SECRET_PREFIX "-s"
  12. struct TotpUpdateContext {
  13. FuriString* args;
  14. PipeSide* pipe;
  15. const CryptoSettings* crypto_settings;
  16. };
  17. enum TotpIteratorUpdateTokenResultsEx {
  18. TotpIteratorUpdateTokenResultInvalidSecret = 1,
  19. TotpIteratorUpdateTokenResultCancelled = 2,
  20. TotpIteratorUpdateTokenResultInvalidArguments = 3
  21. };
  22. static bool totp_cli_try_read_name(
  23. TokenInfo* token_info,
  24. const FuriString* arg,
  25. FuriString* args,
  26. bool* parsed) {
  27. if(furi_string_cmpi_str(arg, TOTP_CLI_COMMAND_ARG_NAME_PREFIX) == 0) {
  28. if(!args_read_probably_quoted_string_and_trim(args, token_info->name) ||
  29. furi_string_empty(token_info->name)) {
  30. totp_cli_printf_missed_argument_value(TOTP_CLI_COMMAND_ARG_NAME_PREFIX);
  31. } else {
  32. *parsed = true;
  33. }
  34. return true;
  35. }
  36. return false;
  37. }
  38. static bool totp_cli_try_read_change_secret_flag(const FuriString* arg, bool* parsed, bool* flag) {
  39. if(furi_string_cmpi_str(arg, TOTP_CLI_COMMAND_UPDATE_ARG_SECRET_PREFIX) == 0) {
  40. *flag = true;
  41. *parsed = true;
  42. return true;
  43. }
  44. return false;
  45. }
  46. static TotpIteratorUpdateTokenResult
  47. update_token_handler(TokenInfo* token_info, const void* context) {
  48. const struct TotpUpdateContext* context_t = context;
  49. // Read optional arguments
  50. FuriString* temp_str = furi_string_alloc();
  51. bool mask_user_input = true;
  52. bool update_token_secret = false;
  53. PlainTokenSecretEncoding token_secret_encoding = PlainTokenSecretEncodingBase32;
  54. while(args_read_string_and_trim(context_t->args, temp_str)) {
  55. bool parsed = false;
  56. if(!totp_cli_try_read_name(token_info, temp_str, context_t->args, &parsed) &&
  57. !totp_cli_try_read_algo(token_info, temp_str, context_t->args, &parsed) &&
  58. !totp_cli_try_read_digits(token_info, temp_str, context_t->args, &parsed) &&
  59. !totp_cli_try_read_duration(token_info, temp_str, context_t->args, &parsed) &&
  60. !totp_cli_try_read_unsecure_flag(temp_str, &parsed, &mask_user_input) &&
  61. !totp_cli_try_read_change_secret_flag(temp_str, &parsed, &update_token_secret) &&
  62. !totp_cli_try_read_automation_features(token_info, temp_str, context_t->args, &parsed) &&
  63. !totp_cli_try_read_plain_token_secret_encoding(
  64. temp_str, context_t->args, &parsed, &token_secret_encoding) &&
  65. !totp_cli_try_read_token_type(token_info, temp_str, context_t->args, &parsed) &&
  66. !totp_cli_try_read_token_counter(token_info, temp_str, context_t->args, &parsed)) {
  67. totp_cli_printf_unknown_argument(temp_str);
  68. }
  69. if(!parsed) {
  70. furi_string_free(temp_str);
  71. return TotpIteratorUpdateTokenResultInvalidArguments;
  72. }
  73. }
  74. if(update_token_secret) {
  75. // Reading token secret
  76. furi_string_reset(temp_str);
  77. TOTP_CLI_PRINTF("Enter token secret and confirm with [ENTER]:\r\n");
  78. bool token_secret_read = totp_cli_read_line(context_t->pipe, temp_str, mask_user_input);
  79. TOTP_CLI_DELETE_LAST_LINE();
  80. if(!token_secret_read) {
  81. furi_string_secure_free(temp_str);
  82. return TotpIteratorUpdateTokenResultCancelled;
  83. }
  84. if(!token_info_set_secret(
  85. token_info,
  86. furi_string_get_cstr(temp_str),
  87. furi_string_size(temp_str),
  88. token_secret_encoding,
  89. context_t->crypto_settings)) {
  90. furi_string_secure_free(temp_str);
  91. return TotpIteratorUpdateTokenResultInvalidSecret;
  92. }
  93. }
  94. furi_string_secure_free(temp_str);
  95. return TotpIteratorUpdateTokenResultSuccess;
  96. }
  97. static void handle(PluginState* plugin_state, FuriString* args, PipeSide* pipe) {
  98. if(!totp_cli_ensure_authenticated(plugin_state, pipe)) {
  99. return;
  100. }
  101. TokenInfoIteratorContext* iterator_context =
  102. totp_config_get_token_iterator_context(plugin_state);
  103. int token_number;
  104. if(!args_read_int_and_trim(args, &token_number) || token_number <= 0 ||
  105. (size_t)token_number > totp_token_info_iterator_get_total_count(iterator_context)) {
  106. TOTP_CLI_PRINT_INVALID_ARGUMENTS();
  107. return;
  108. }
  109. TOTP_CLI_LOCK_UI(plugin_state);
  110. size_t previous_index = totp_token_info_iterator_get_current_token_index(iterator_context);
  111. totp_token_info_iterator_go_to(iterator_context, token_number - 1);
  112. struct TotpUpdateContext update_context = {
  113. .args = args, .pipe = pipe, .crypto_settings = &plugin_state->crypto_settings};
  114. TotpIteratorUpdateTokenResult update_result = totp_token_info_iterator_update_current_token(
  115. iterator_context, &update_token_handler, &update_context);
  116. if(update_result == TotpIteratorUpdateTokenResultSuccess) {
  117. TOTP_CLI_PRINTF_SUCCESS(
  118. "Token \"%s\" has been successfully updated\r\n",
  119. furi_string_get_cstr(
  120. totp_token_info_iterator_get_current_token(iterator_context)->name));
  121. } else if(update_result == TotpIteratorUpdateTokenResultInvalidArguments) {
  122. TOTP_CLI_PRINT_INVALID_ARGUMENTS();
  123. } else if(update_result == TotpIteratorUpdateTokenResultCancelled) {
  124. TOTP_CLI_PRINTF_INFO("Cancelled by user\r\n");
  125. } else if(update_result == TotpIteratorUpdateTokenResultInvalidSecret) {
  126. TOTP_CLI_PRINTF_ERROR("Token secret seems to be invalid and can not be parsed\r\n");
  127. } else if(update_result == TotpIteratorUpdateTokenResultFileUpdateFailed) {
  128. TOTP_CLI_PRINT_ERROR_UPDATING_CONFIG_FILE();
  129. }
  130. totp_token_info_iterator_go_to(iterator_context, previous_index);
  131. TOTP_CLI_UNLOCK_UI(plugin_state);
  132. }
  133. static const CliPlugin plugin = {.name = "TOTP CLI Plugin: Update", .handle = &handle};
  134. static const FlipperAppPluginDescriptor plugin_descriptor = {
  135. .appid = PLUGIN_APP_ID,
  136. .ep_api_version = PLUGIN_API_VERSION,
  137. .entry_point = &plugin,
  138. };
  139. const FlipperAppPluginDescriptor* totp_cli_update_plugin_ep() {
  140. return &plugin_descriptor;
  141. }