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