add.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. struct TotpAddContext {
  12. FuriString* args;
  13. PipeSide* pipe;
  14. const CryptoSettings* crypto_settings;
  15. };
  16. enum TotpIteratorUpdateTokenResultsEx {
  17. TotpIteratorUpdateTokenResultInvalidSecret = 1,
  18. TotpIteratorUpdateTokenResultCancelled = 2,
  19. TotpIteratorUpdateTokenResultInvalidArguments = 3
  20. };
  21. static TotpIteratorUpdateTokenResult
  22. add_token_handler(TokenInfo* token_info, const void* context) {
  23. const struct TotpAddContext* context_t = context;
  24. // Reading token name
  25. if(!args_read_probably_quoted_string_and_trim(context_t->args, token_info->name)) {
  26. return TotpIteratorUpdateTokenResultInvalidArguments;
  27. }
  28. FuriString* temp_str = furi_string_alloc();
  29. // Read optional arguments
  30. bool mask_user_input = true;
  31. PlainTokenSecretEncoding token_secret_encoding = PlainTokenSecretEncodingBase32;
  32. while(args_read_string_and_trim(context_t->args, temp_str)) {
  33. bool parsed = false;
  34. if(!totp_cli_try_read_algo(token_info, temp_str, context_t->args, &parsed) &&
  35. !totp_cli_try_read_digits(token_info, temp_str, context_t->args, &parsed) &&
  36. !totp_cli_try_read_duration(token_info, temp_str, context_t->args, &parsed) &&
  37. !totp_cli_try_read_unsecure_flag(temp_str, &parsed, &mask_user_input) &&
  38. !totp_cli_try_read_automation_features(token_info, temp_str, context_t->args, &parsed) &&
  39. !totp_cli_try_read_plain_token_secret_encoding(
  40. temp_str, context_t->args, &parsed, &token_secret_encoding) &&
  41. !totp_cli_try_read_token_type(token_info, temp_str, context_t->args, &parsed) &&
  42. !totp_cli_try_read_token_counter(token_info, temp_str, context_t->args, &parsed)) {
  43. totp_cli_printf_unknown_argument(temp_str);
  44. }
  45. if(!parsed) {
  46. furi_string_free(temp_str);
  47. return TotpIteratorUpdateTokenResultInvalidArguments;
  48. }
  49. }
  50. // Reading token secret
  51. furi_string_reset(temp_str);
  52. TOTP_CLI_PRINTF("Enter token secret and confirm with [ENTER]:\r\n");
  53. if(!totp_cli_read_line(context_t->pipe, temp_str, mask_user_input)) {
  54. TOTP_CLI_DELETE_LAST_LINE();
  55. furi_string_secure_free(temp_str);
  56. return TotpIteratorUpdateTokenResultCancelled;
  57. }
  58. TOTP_CLI_DELETE_LAST_LINE();
  59. bool secret_set = token_info_set_secret(
  60. token_info,
  61. furi_string_get_cstr(temp_str),
  62. furi_string_size(temp_str),
  63. token_secret_encoding,
  64. context_t->crypto_settings);
  65. furi_string_secure_free(temp_str);
  66. if(!secret_set) {
  67. return TotpIteratorUpdateTokenResultInvalidSecret;
  68. }
  69. return TotpIteratorUpdateTokenResultSuccess;
  70. }
  71. static void handle(PluginState* plugin_state, FuriString* args, PipeSide* pipe) {
  72. if(!totp_cli_ensure_authenticated(plugin_state, pipe)) {
  73. return;
  74. }
  75. TokenInfoIteratorContext* iterator_context =
  76. totp_config_get_token_iterator_context(plugin_state);
  77. TOTP_CLI_LOCK_UI(plugin_state);
  78. struct TotpAddContext add_context = {
  79. .args = args, .pipe = pipe, .crypto_settings = &plugin_state->crypto_settings};
  80. TotpIteratorUpdateTokenResult add_result =
  81. totp_token_info_iterator_add_new_token(iterator_context, &add_token_handler, &add_context);
  82. if(add_result == TotpIteratorUpdateTokenResultSuccess) {
  83. TOTP_CLI_PRINTF_SUCCESS(
  84. "Token \"%s\" has been successfully added\r\n",
  85. furi_string_get_cstr(
  86. totp_token_info_iterator_get_current_token(iterator_context)->name));
  87. } else if(add_result == TotpIteratorUpdateTokenResultCancelled) {
  88. TOTP_CLI_PRINTF_INFO("Cancelled by user\r\n");
  89. } else if(add_result == TotpIteratorUpdateTokenResultInvalidArguments) {
  90. TOTP_CLI_PRINT_INVALID_ARGUMENTS();
  91. } else if(add_result == TotpIteratorUpdateTokenResultInvalidSecret) {
  92. TOTP_CLI_PRINTF_ERROR("Token secret seems to be invalid and can not be parsed\r\n");
  93. } else if(add_result == TotpIteratorUpdateTokenResultFileUpdateFailed) {
  94. TOTP_CLI_PRINT_ERROR_UPDATING_CONFIG_FILE();
  95. }
  96. TOTP_CLI_UNLOCK_UI(plugin_state);
  97. }
  98. static const CliPlugin plugin = {.name = "TOTP CLI Plugin: Add", .handle = &handle};
  99. static const FlipperAppPluginDescriptor plugin_descriptor = {
  100. .appid = PLUGIN_APP_ID,
  101. .ep_api_version = PLUGIN_API_VERSION,
  102. .entry_point = &plugin,
  103. };
  104. const FlipperAppPluginDescriptor* totp_cli_add_plugin_ep() {
  105. return &plugin_descriptor;
  106. }