details.c 4.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 == TokenAutomationFeatureNone) {
  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 & TokenAutomationFeatureEnterAtTheEnd) {
  25. TOTP_CLI_PRINTF_AUTOMATION_FEATURE("Type <Enter> key at the end", header_printed);
  26. }
  27. if(token_info->automation_features & TokenAutomationFeatureTabAtTheEnd) {
  28. TOTP_CLI_PRINTF_AUTOMATION_FEATURE("Type <Tab> key at the end", header_printed);
  29. }
  30. if(token_info->automation_features & TokenAutomationFeatureTypeSlower) {
  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 =
  49. totp_config_get_token_iterator_context(plugin_state);
  50. if(!args_read_int_and_trim(args, &token_number) || token_number <= 0 ||
  51. (size_t)token_number > totp_token_info_iterator_get_total_count(iterator_context)) {
  52. totp_cli_print_invalid_arguments();
  53. return;
  54. }
  55. TOTP_CLI_LOCK_UI(plugin_state);
  56. size_t original_token_index =
  57. totp_token_info_iterator_get_current_token_index(iterator_context);
  58. if(totp_token_info_iterator_go_to(iterator_context, token_number - 1)) {
  59. const TokenInfo* token_info = totp_token_info_iterator_get_current_token(iterator_context);
  60. TOTP_CLI_PRINTF("+----------------------+------------------------------+\r\n");
  61. TOTP_CLI_PRINTF("| %-20s | %-28s |\r\n", "Property", "Value");
  62. TOTP_CLI_PRINTF("+----------------------+------------------------------+\r\n");
  63. TOTP_CLI_PRINTF("| %-20s | %-28d |\r\n", "Index", token_number);
  64. TOTP_CLI_PRINTF(
  65. "| %-20s | %-28.28s |\r\n", "Name", furi_string_get_cstr(token_info->name));
  66. TOTP_CLI_PRINTF(
  67. "| %-20s | %-28s |\r\n", "Hashing algorithm", token_info_get_algo_as_cstr(token_info));
  68. TOTP_CLI_PRINTF("| %-20s | %-28" PRIu8 " |\r\n", "Number of digits", token_info->digits);
  69. TOTP_CLI_PRINTF(
  70. "| %-20s | %" PRIu8 " sec.%-21s |\r\n", "Token lifetime", token_info->duration, " ");
  71. print_automation_features(token_info);
  72. TOTP_CLI_PRINTF("+----------------------+------------------------------+\r\n");
  73. } else {
  74. totp_cli_print_error_loading_token_info();
  75. }
  76. totp_token_info_iterator_go_to(iterator_context, original_token_index);
  77. TOTP_CLI_UNLOCK_UI(plugin_state);
  78. }