list.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #include "list.h"
  2. #include <stdlib.h>
  3. #include "../../../lib/list/list.h"
  4. #include "../../../types/token_info.h"
  5. #include "../../../services/config/constants.h"
  6. #include "../../cli_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. default:
  16. break;
  17. }
  18. return "UNKNOWN";
  19. }
  20. void totp_cli_command_list_docopt_commands() {
  21. TOTP_CLI_PRINTF(" " TOTP_CLI_COMMAND_LIST ", " TOTP_CLI_COMMAND_LIST_ALT
  22. " List all available tokens\r\n");
  23. }
  24. void totp_cli_command_list_docopt_usage() {
  25. TOTP_CLI_PRINTF(" " TOTP_CLI_COMMAND_NAME " " DOCOPT_REQUIRED(
  26. TOTP_CLI_COMMAND_LIST " | " TOTP_CLI_COMMAND_LIST_ALT) "\r\n");
  27. }
  28. void totp_cli_command_list_handle(PluginState* plugin_state, Cli* cli) {
  29. if(!totp_cli_ensure_authenticated(plugin_state, cli)) {
  30. return;
  31. }
  32. if(plugin_state->tokens_list == NULL) {
  33. TOTP_CLI_PRINTF("There are no tokens");
  34. return;
  35. }
  36. TOTP_CLI_PRINTF("+-----+---------------------------+--------+----+-----+\r\n");
  37. TOTP_CLI_PRINTF(
  38. "| %-*s | %-*s | %-*s | %-s | %-s |\r\n", 3, "#", 25, "Name", 6, "Algo", "Ln", "Dur");
  39. TOTP_CLI_PRINTF("+-----+---------------------------+--------+----+-----+\r\n");
  40. uint16_t index = 1;
  41. TOTP_LIST_FOREACH(plugin_state->tokens_list, node, {
  42. TokenInfo* token_info = (TokenInfo*)node->data;
  43. TOTP_CLI_PRINTF(
  44. "| %-3" PRIu16 " | %-25.25s | %-6s | %-2" PRIu8 " | %-3" PRIu8 " |\r\n",
  45. index,
  46. token_info->name,
  47. get_algo_as_cstr(token_info->algo),
  48. token_info->digits,
  49. token_info->duration);
  50. index++;
  51. });
  52. TOTP_CLI_PRINTF("+-----+---------------------------+--------+----+-----+\r\n");
  53. }