details.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #include <flipper_application/flipper_application.h>
  2. #include <lib/toolbox/args.h>
  3. #include "../../../services/config/config.h"
  4. #include "../../../services/config/constants.h"
  5. #include "../../../types/token_info.h"
  6. #include "../../cli_helpers.h"
  7. #include "../../cli_shared_methods.h"
  8. #include "../../cli_plugin_interface.h"
  9. #include "../../../ui/scene_director.h"
  10. #include "formatters/table/details_output_formatter_table.h"
  11. #include "formatters/tsv/details_output_formatter_tsv.h"
  12. typedef void (*TOTP_CLI_DETAILS_HEADER_FORMATTER)();
  13. typedef void (*TOTP_CLI_DETAILS_FOOTER_FORMATTER)();
  14. typedef void (*TOTP_CLI_DETAILS_AUTOMATION_FEATURE_ITEM_FORMATTER)(
  15. const char* key,
  16. const char* feature,
  17. bool* header_printed);
  18. typedef void (*TOTP_CLI_DETAILS_CSTR_FORMATTER)(const char* key, const char* value);
  19. typedef void (*TOTP_CLI_DETAILS_UINT8T_FORMATTER)(const char* key, uint8_t value);
  20. typedef void (*TOTP_CLI_DETAILS_SIZET_FORMATTER)(const char* key, size_t value);
  21. typedef struct {
  22. const TOTP_CLI_DETAILS_HEADER_FORMATTER header_formatter;
  23. const TOTP_CLI_DETAILS_FOOTER_FORMATTER footer_formatter;
  24. const TOTP_CLI_DETAILS_AUTOMATION_FEATURE_ITEM_FORMATTER automation_feature_item_formatter;
  25. const TOTP_CLI_DETAILS_CSTR_FORMATTER cstr_formatter;
  26. const TOTP_CLI_DETAILS_UINT8T_FORMATTER uint8t_formatter;
  27. const TOTP_CLI_DETAILS_SIZET_FORMATTER sizet_formatter;
  28. } TotpCliDetailsFormatter;
  29. static const TotpCliDetailsFormatter available_formatters[] = {
  30. {.header_formatter = &details_output_formatter_print_header_table,
  31. .footer_formatter = &details_output_formatter_print_footer_table,
  32. .automation_feature_item_formatter = &details_output_formatter_print_automation_feature_table,
  33. .cstr_formatter = &details_output_formatter_print_cstr_table,
  34. .uint8t_formatter = &details_output_formatter_print_uint8t_table,
  35. .sizet_formatter = &details_output_formatter_print_sizet_table},
  36. {.header_formatter = &details_output_formatter_print_header_tsv,
  37. .footer_formatter = &details_output_formatter_print_footer_tsv,
  38. .automation_feature_item_formatter = &details_output_formatter_print_automation_feature_tsv,
  39. .cstr_formatter = &details_output_formatter_print_cstr_tsv,
  40. .uint8t_formatter = &details_output_formatter_print_uint8t_tsv,
  41. .sizet_formatter = &details_output_formatter_print_sizet_tsv},
  42. };
  43. static void print_automation_features(
  44. const TokenInfo* token_info,
  45. const TotpCliDetailsFormatter* formatter) {
  46. bool header_printed = false;
  47. const char* AUTOMATION_FEATURES_PRINT_KEY = "Automation features";
  48. if(token_info->automation_features == TokenAutomationFeatureNone) {
  49. (*formatter->automation_feature_item_formatter)(
  50. AUTOMATION_FEATURES_PRINT_KEY, "None", &header_printed);
  51. return;
  52. }
  53. if(token_info->automation_features & TokenAutomationFeatureEnterAtTheEnd) {
  54. (*formatter->automation_feature_item_formatter)(
  55. AUTOMATION_FEATURES_PRINT_KEY, "Type <Enter> key at the end", &header_printed);
  56. }
  57. if(token_info->automation_features & TokenAutomationFeatureTabAtTheEnd) {
  58. (*formatter->automation_feature_item_formatter)(
  59. AUTOMATION_FEATURES_PRINT_KEY, "Type <Tab> key at the end", &header_printed);
  60. }
  61. if(token_info->automation_features & TokenAutomationFeatureTypeSlower) {
  62. (*formatter->automation_feature_item_formatter)(
  63. AUTOMATION_FEATURES_PRINT_KEY, "Type slower", &header_printed);
  64. }
  65. }
  66. static void handle(PluginState* plugin_state, FuriString* args, Cli* cli) {
  67. if(!totp_cli_ensure_authenticated(plugin_state, cli)) {
  68. return;
  69. }
  70. int token_number;
  71. TokenInfoIteratorContext* iterator_context =
  72. totp_config_get_token_iterator_context(plugin_state);
  73. if(!args_read_int_and_trim(args, &token_number) || token_number <= 0 ||
  74. (size_t)token_number > totp_token_info_iterator_get_total_count(iterator_context)) {
  75. TOTP_CLI_PRINT_INVALID_ARGUMENTS();
  76. return;
  77. }
  78. const TotpCliDetailsFormatter* formatter = &available_formatters[0];
  79. FuriString* arg = furi_string_alloc();
  80. if(args_read_string_and_trim(args, arg) && furi_string_cmpi_str(arg, "--tsv") == 0) {
  81. formatter = &available_formatters[1];
  82. }
  83. furi_string_free(arg);
  84. TOTP_CLI_LOCK_UI(plugin_state);
  85. size_t original_token_index =
  86. totp_token_info_iterator_get_current_token_index(iterator_context);
  87. if(totp_token_info_iterator_go_to(iterator_context, token_number - 1)) {
  88. const TokenInfo* token_info = totp_token_info_iterator_get_current_token(iterator_context);
  89. (*formatter->header_formatter)();
  90. (*formatter->sizet_formatter)("Index", token_number);
  91. (*formatter->cstr_formatter)("Name", furi_string_get_cstr(token_info->name));
  92. (*formatter->cstr_formatter)("Hashing algorithm", token_info_get_algo_as_cstr(token_info));
  93. (*formatter->uint8t_formatter)("Number of digits", token_info->digits);
  94. (*formatter->uint8t_formatter)("Token lifetime", token_info->duration);
  95. print_automation_features(token_info, formatter);
  96. (*formatter->footer_formatter)();
  97. } else {
  98. TOTP_CLI_PRINT_ERROR_LOADING_TOKEN_INFO();
  99. }
  100. totp_token_info_iterator_go_to(iterator_context, original_token_index);
  101. TOTP_CLI_UNLOCK_UI(plugin_state);
  102. }
  103. static const CliPlugin plugin = {.name = "TOTP CLI Plugin: Details", .handle = &handle};
  104. static const FlipperAppPluginDescriptor plugin_descriptor = {
  105. .appid = PLUGIN_APP_ID,
  106. .ep_api_version = PLUGIN_API_VERSION,
  107. .entry_point = &plugin,
  108. };
  109. const FlipperAppPluginDescriptor* totp_cli_details_plugin_ep() {
  110. return &plugin_descriptor;
  111. }