add.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #include "add.h"
  2. #include <stdlib.h>
  3. #include <lib/toolbox/args.h>
  4. #include "../../../list/list.h"
  5. #include "../../../../types/token_info.h"
  6. #include "../../../config/config.h"
  7. #include "../../cli_common_helpers.h"
  8. #include "../../../../scenes/scene_director.h"
  9. static bool token_info_set_digits_from_str(TokenInfo* token_info, FuriString* str) {
  10. switch(furi_string_get_char(str, 0)) {
  11. case '6':
  12. token_info->digits = TOTP_6_DIGITS;
  13. return true;
  14. case '8':
  15. token_info->digits = TOTP_8_DIGITS;
  16. return true;
  17. }
  18. return false;
  19. }
  20. static bool token_info_set_algo_from_str(TokenInfo* token_info, FuriString* str) {
  21. if(furi_string_cmpi_str(str, TOTP_CONFIG_TOKEN_ALGO_SHA1_NAME) == 0) {
  22. token_info->algo = SHA1;
  23. return true;
  24. }
  25. if(furi_string_cmpi_str(str, TOTP_CONFIG_TOKEN_ALGO_SHA256_NAME) == 0) {
  26. token_info->algo = SHA256;
  27. return true;
  28. }
  29. if(furi_string_cmpi_str(str, TOTP_CONFIG_TOKEN_ALGO_SHA512_NAME) == 0) {
  30. token_info->algo = SHA512;
  31. return true;
  32. }
  33. return false;
  34. }
  35. void totp_cli_handle_add_command(PluginState* plugin_state, FuriString* args) {
  36. FuriString* temp_str = furi_string_alloc();
  37. const char* temp_cstr;
  38. TokenInfo* token_info = token_info_alloc();
  39. // Reading token name
  40. if (!args_read_probably_quoted_string_and_trim(args, temp_str)) {
  41. totp_cli_print_invalid_arguments();
  42. furi_string_free(temp_str);
  43. token_info_free(token_info);
  44. return;
  45. }
  46. temp_cstr = furi_string_get_cstr(temp_str);
  47. token_info->name = malloc(strlen(temp_cstr) + 1);
  48. strcpy(token_info->name, temp_cstr);
  49. // Reading token secret
  50. if (!args_read_probably_quoted_string_and_trim(args, temp_str)) {
  51. totp_cli_print_invalid_arguments();
  52. furi_string_free(temp_str);
  53. token_info_free(token_info);
  54. return;
  55. }
  56. temp_cstr = furi_string_get_cstr(temp_str);
  57. if (!token_info_set_secret(token_info, temp_cstr, strlen(temp_cstr), plugin_state->iv)) {
  58. printf("Token secret seems to be invalid and can not be parsed\r\n");
  59. furi_string_free(temp_str);
  60. token_info_free(token_info);
  61. return;
  62. }
  63. // Read optional arguments
  64. while (args_read_string_and_trim(args, temp_str)) {
  65. bool parsed = false;
  66. if (furi_string_cmpi_str(temp_str, "-a") == 0) {
  67. if (!args_read_string_and_trim(args, temp_str)) {
  68. printf("Missed value for argument \"-a\"\r\n");
  69. } else if (!token_info_set_algo_from_str(token_info, temp_str)) {
  70. printf("\"%s\" is incorrect value for argument \"-a\"\r\n", furi_string_get_cstr(temp_str));
  71. } else {
  72. parsed = true;
  73. }
  74. } else if (furi_string_cmpi_str(temp_str, "-d") == 0) {
  75. if (!args_read_string_and_trim(args, temp_str)) {
  76. printf("Missed value for argument \"-d\"\r\n");
  77. } else if (!token_info_set_digits_from_str(token_info, temp_str)) {
  78. printf("\"%s\" is incorrect value for argument \"-d\"\r\n", furi_string_get_cstr(temp_str));
  79. } else {
  80. parsed = true;
  81. }
  82. }
  83. if (!parsed) {
  84. totp_cli_print_invalid_arguments();
  85. furi_string_free(temp_str);
  86. token_info_free(token_info);
  87. return;
  88. }
  89. }
  90. bool load_generate_token_scene = false;
  91. if (plugin_state->current_scene == TotpSceneGenerateToken) {
  92. totp_scene_director_activate_scene(plugin_state, TotpSceneNone, NULL);
  93. load_generate_token_scene = true;
  94. }
  95. if(plugin_state->tokens_list == NULL) {
  96. plugin_state->tokens_list = list_init_head(token_info);
  97. } else {
  98. list_add(plugin_state->tokens_list, token_info);
  99. }
  100. plugin_state->tokens_count++;
  101. totp_config_file_save_new_token(token_info);
  102. if (load_generate_token_scene) {
  103. totp_scene_director_activate_scene(plugin_state, TotpSceneGenerateToken, NULL);
  104. }
  105. furi_string_free(temp_str);
  106. printf("Token \"%s\" has been successfully added\r\n", token_info->name);
  107. }