list.c 1.8 KB

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