list.c 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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_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: break;
  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. default: break;
  26. }
  27. return 6;
  28. }
  29. void totp_cli_command_list_docopt_commands() {
  30. TOTP_CLI_PRINTF(" " TOTP_CLI_COMMAND_LIST ", " TOTP_CLI_COMMAND_LIST_ALT
  31. " List all available tokens\r\n");
  32. }
  33. void totp_cli_command_list_docopt_usage() {
  34. TOTP_CLI_PRINTF(" " TOTP_CLI_COMMAND_NAME " " DOCOPT_REQUIRED(
  35. TOTP_CLI_COMMAND_LIST " | " TOTP_CLI_COMMAND_LIST_ALT) "\r\n");
  36. }
  37. void totp_cli_command_list_handle(PluginState* plugin_state, Cli* cli) {
  38. if(!totp_cli_ensure_authenticated(plugin_state, cli)) {
  39. return;
  40. }
  41. if(plugin_state->tokens_list == NULL) {
  42. TOTP_CLI_PRINTF("There are no tokens");
  43. return;
  44. }
  45. ListNode* node = plugin_state->tokens_list;
  46. TOTP_CLI_PRINTF("+-----+-----------------------------+--------+--------+\r\n");
  47. TOTP_CLI_PRINTF("| %-*s | %-*s | %-*s | %-s |\r\n", 3, "#", 27, "Name", 6, "Algo", "Digits");
  48. TOTP_CLI_PRINTF("+-----+-----------------------------+--------+--------+\r\n");
  49. uint16_t index = 1;
  50. while(node != NULL) {
  51. TokenInfo* token_info = (TokenInfo*)node->data;
  52. token_info_get_digits_count(token_info);
  53. TOTP_CLI_PRINTF(
  54. "| %-3" PRIu16 " | %-27.27s | %-6s | %-6" PRIu8 " |\r\n",
  55. index,
  56. token_info->name,
  57. get_algo_as_cstr(token_info->algo),
  58. get_digits_as_int(token_info->digits));
  59. node = node->next;
  60. index++;
  61. }
  62. TOTP_CLI_PRINTF("+-----+-----------------------------+--------+--------+\r\n");
  63. }