details.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #include "details.h"
  2. #include <stdlib.h>
  3. #include <lib/toolbox/args.h>
  4. #include "../../../types/token_info.h"
  5. #include "../../../services/config/constants.h"
  6. #include "../../../services/config/config.h"
  7. #include "../../../ui/scene_director.h"
  8. #include "../../cli_helpers.h"
  9. #include "../../common_command_arguments.h"
  10. #define TOTP_CLI_PRINTF_AUTOMATION_FEATURE(description, header_printed) \
  11. do { \
  12. TOTP_CLI_PRINTF( \
  13. "| %-20s | %-28.28s |\r\n", \
  14. header_printed ? "" : "Automation features", \
  15. description); \
  16. header_printed = true; \
  17. } while(false)
  18. static void print_automation_features(const TokenInfo* token_info) {
  19. if(token_info->automation_features == TOKEN_AUTOMATION_FEATURE_NONE) {
  20. TOTP_CLI_PRINTF("| %-20s | %-28.28s |\r\n", "Automation features", "None");
  21. return;
  22. }
  23. bool header_printed = false;
  24. if(token_info->automation_features & TOKEN_AUTOMATION_FEATURE_ENTER_AT_THE_END) {
  25. TOTP_CLI_PRINTF_AUTOMATION_FEATURE("Type <Enter> key at the end", header_printed);
  26. }
  27. if(token_info->automation_features & TOKEN_AUTOMATION_FEATURE_TAB_AT_THE_END) {
  28. TOTP_CLI_PRINTF_AUTOMATION_FEATURE("Type <Tab> key at the end", header_printed);
  29. }
  30. if(token_info->automation_features & TOKEN_AUTOMATION_FEATURE_TYPE_SLOWER) {
  31. TOTP_CLI_PRINTF_AUTOMATION_FEATURE("Type slower", header_printed);
  32. }
  33. }
  34. void totp_cli_command_details_docopt_commands() {
  35. TOTP_CLI_PRINTF(" " TOTP_CLI_COMMAND_DETAILS ", " TOTP_CLI_COMMAND_DETAILS_ALT
  36. " Displays token details\r\n");
  37. }
  38. void totp_cli_command_details_docopt_usage() {
  39. TOTP_CLI_PRINTF(" " TOTP_CLI_COMMAND_NAME " " DOCOPT_REQUIRED(
  40. TOTP_CLI_COMMAND_DETAILS
  41. " | " TOTP_CLI_COMMAND_DETAILS_ALT) " " DOCOPT_ARGUMENT(TOTP_CLI_COMMAND_ARG_INDEX) "\r\n");
  42. }
  43. void totp_cli_command_details_handle(PluginState* plugin_state, FuriString* args, Cli* cli) {
  44. if(!totp_cli_ensure_authenticated(plugin_state, cli)) {
  45. return;
  46. }
  47. int token_number;
  48. TokenInfoIteratorContext* iterator_context = totp_config_get_token_iterator_context(plugin_state);
  49. if(!args_read_int_and_trim(args, &token_number) || token_number <= 0 ||
  50. (size_t)token_number > totp_token_info_iterator_get_total_count(iterator_context)) {
  51. totp_cli_print_invalid_arguments();
  52. return;
  53. }
  54. TOTP_CLI_LOCK_UI(plugin_state);
  55. size_t original_token_index = totp_token_info_iterator_get_current_token_index(iterator_context);
  56. if(totp_token_info_iterator_go_to(iterator_context, token_number - 1)) {
  57. const TokenInfo* token_info = totp_token_info_iterator_get_current_token(iterator_context);
  58. TOTP_CLI_PRINTF("+----------------------+------------------------------+\r\n");
  59. TOTP_CLI_PRINTF("| %-20s | %-28s |\r\n", "Property", "Value");
  60. TOTP_CLI_PRINTF("+----------------------+------------------------------+\r\n");
  61. TOTP_CLI_PRINTF("| %-20s | %-28d |\r\n", "Index", token_number);
  62. TOTP_CLI_PRINTF(
  63. "| %-20s | %-28.28s |\r\n", "Name", furi_string_get_cstr(token_info->name));
  64. TOTP_CLI_PRINTF(
  65. "| %-20s | %-28s |\r\n", "Hashing algorithm", token_info_get_algo_as_cstr(token_info));
  66. TOTP_CLI_PRINTF("| %-20s | %-28" PRIu8 " |\r\n", "Number of digits", token_info->digits);
  67. TOTP_CLI_PRINTF(
  68. "| %-20s | %" PRIu8 " sec.%-21s |\r\n", "Token lifetime", token_info->duration, " ");
  69. print_automation_features(token_info);
  70. TOTP_CLI_PRINTF("+----------------------+------------------------------+\r\n");
  71. } else {
  72. totp_cli_print_error_loading_token_info();
  73. }
  74. totp_token_info_iterator_go_to(iterator_context, original_token_index);
  75. TOTP_CLI_UNLOCK_UI(plugin_state);
  76. }