@@ -87,6 +87,43 @@ static void furi_string_secure_free(FuriString* str) {
8787 furi_string_free (str );
8888}
8989
90+ static bool totp_cli_read_secret (Cli * cli , FuriString * out_str , bool mask_user_input ) {
91+ uint8_t c ;
92+ while (cli_read (cli , & c , 1 ) == 1 ) {
93+ if (c == CliSymbolAsciiEsc ) {
94+ // Some keys generating escape-sequences
95+ // We need to ignore them as we case about alpha-numerics only
96+ uint8_t c2 ;
97+ cli_read_timeout (cli , & c2 , 1 , 0 );
98+ cli_read_timeout (cli , & c2 , 1 , 0 );
99+ } else if (c == CliSymbolAsciiETX ) {
100+ TOTP_CLI_DELETE_CURRENT_LINE ();
101+ TOTP_CLI_PRINTF ("Cancelled by user\r\n" );
102+ return false;
103+ } else if ((c >= '0' && c <= '9' ) || (c >= 'a' && c <= 'z' ) || (c >= 'A' && c <= 'Z' )) {
104+ if (mask_user_input ) {
105+ putc ('*' , stdout );
106+ } else {
107+ putc (c , stdout );
108+ }
109+ fflush (stdout );
110+ furi_string_push_back (out_str , c );
111+ } else if (c == CliSymbolAsciiBackspace || c == CliSymbolAsciiDel ) {
112+ size_t out_str_size = furi_string_size (out_str );
113+ if (out_str_size > 0 ) {
114+ TOTP_CLI_DELETE_LAST_CHAR ();
115+ furi_string_left (out_str , out_str_size - 1 );
116+ }
117+ } else if (c == CliSymbolAsciiCR ) {
118+ cli_nl ();
119+ break ;
120+ }
121+ }
122+
123+ TOTP_CLI_DELETE_LAST_LINE ();
124+ return true;
125+ }
126+
90127void totp_cli_command_add_handle (PluginState * plugin_state , FuriString * args , Cli * cli ) {
91128 FuriString * temp_str = furi_string_alloc ();
92129 TokenInfo * token_info = token_info_alloc ();
@@ -148,44 +185,8 @@ void totp_cli_command_add_handle(PluginState* plugin_state, FuriString* args, Cl
148185 // Reading token secret
149186 furi_string_reset (temp_str );
150187 TOTP_CLI_PRINTF ("Enter token secret and confirm with [ENTER]\r\n" );
151-
152- uint8_t c ;
153- while (cli_read (cli , & c , 1 ) == 1 ) {
154- if (c == CliSymbolAsciiEsc ) {
155- // Some keys generating escape-sequences
156- // We need to ignore them as we case about alpha-numerics only
157- uint8_t c2 ;
158- cli_read_timeout (cli , & c2 , 1 , 0 );
159- cli_read_timeout (cli , & c2 , 1 , 0 );
160- } else if (c == CliSymbolAsciiETX ) {
161- TOTP_CLI_DELETE_CURRENT_LINE ();
162- TOTP_CLI_PRINTF ("Cancelled by user\r\n" );
163- furi_string_secure_free (temp_str );
164- token_info_free (token_info );
165- return ;
166- } else if ((c >= '0' && c <= '9' ) || (c >= 'a' && c <= 'z' ) || (c >= 'A' && c <= 'Z' )) {
167- if (mask_user_input ) {
168- putc ('*' , stdout );
169- } else {
170- putc (c , stdout );
171- }
172- fflush (stdout );
173- furi_string_push_back (temp_str , c );
174- } else if (c == CliSymbolAsciiBackspace || c == CliSymbolAsciiDel ) {
175- size_t temp_str_size = furi_string_size (temp_str );
176- if (temp_str_size > 0 ) {
177- TOTP_CLI_DELETE_LAST_CHAR ();
178- furi_string_left (temp_str , temp_str_size - 1 );
179- }
180- } else if (c == CliSymbolAsciiCR ) {
181- cli_nl ();
182- break ;
183- }
184- }
185-
186- TOTP_CLI_DELETE_LAST_LINE ();
187-
188- if (!totp_cli_ensure_authenticated (plugin_state , cli )) {
188+ if (!totp_cli_read_secret (cli , temp_str , mask_user_input ) ||
189+ !totp_cli_ensure_authenticated (plugin_state , cli )) {
189190 furi_string_secure_free (temp_str );
190191 token_info_free (token_info );
191192 return ;
@@ -210,11 +211,7 @@ void totp_cli_command_add_handle(PluginState* plugin_state, FuriString* args, Cl
210211 load_generate_token_scene = true;
211212 }
212213
213- if (plugin_state -> tokens_list == NULL ) {
214- plugin_state -> tokens_list = list_init_head (token_info );
215- } else {
216- list_add (plugin_state -> tokens_list , token_info );
217- }
214+ TOTP_LIST_INIT_OR_ADD (plugin_state -> tokens_list , token_info );
218215 plugin_state -> tokens_count ++ ;
219216 totp_config_file_save_new_token (token_info );
220217
0 commit comments