list.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #include "list.h"
  2. #include <cli/cli.h>
  3. #include <stdlib.h>
  4. #include "../../../list/list.h"
  5. #include "../../../../types/token_info.h"
  6. #include "../../../config/constants.h"
  7. #include "../../cli_common_helpers.h"
  8. static char* get_algo_as_cstr(TokenHashAlgo algo) {
  9. switch(algo) {
  10. case SHA1:
  11. return TOTP_CONFIG_TOKEN_ALGO_SHA1_NAME;
  12. case SHA256:
  13. return TOTP_CONFIG_TOKEN_ALGO_SHA256_NAME;
  14. case SHA512:
  15. return TOTP_CONFIG_TOKEN_ALGO_SHA512_NAME;
  16. }
  17. return "UNKNOWN";
  18. }
  19. static uint8_t get_digits_as_int(TokenDigitsCount digits) {
  20. switch(digits) {
  21. case TOTP_6_DIGITS:
  22. return 6;
  23. case TOTP_8_DIGITS:
  24. return 8;
  25. }
  26. return 6;
  27. }
  28. void totp_cli_command_list_print_help() {
  29. TOTP_CLI_PRINTF("\t" TOTP_CLI_COMMAND_LIST " - list all tokens\r\n\r\n");
  30. }
  31. void totp_cli_command_list_handle(PluginState* plugin_state) {
  32. if (plugin_state->tokens_list == NULL) {
  33. TOTP_CLI_PRINTF("There are no tokens");
  34. return;
  35. }
  36. ListNode* node = plugin_state->tokens_list;
  37. TOTP_CLI_PRINTF("+-----+-----------------------------+--------+--------+\r\n");
  38. TOTP_CLI_PRINTF("| %-*s | %-*s | %-*s | %-s |\r\n", 3, "#", 27, "Name", 6, "Algo", "Digits");
  39. TOTP_CLI_PRINTF("+-----+-----------------------------+--------+--------+\r\n");
  40. uint16_t index = 1;
  41. while(node != NULL) {
  42. TokenInfo* token_info = (TokenInfo* )node->data;
  43. token_info_get_digits_count(token_info);
  44. TOTP_CLI_PRINTF("| %-3" PRIu16 " | %-27.27s | %-6s | %-6" PRIu8 " |\r\n", index, token_info->name, get_algo_as_cstr(token_info->algo), get_digits_as_int(token_info->digits));
  45. node = node->next;
  46. index++;
  47. }
  48. TOTP_CLI_PRINTF("+-----+-----------------------------+--------+--------+\r\n");
  49. }