Temporarily backport app updates from apps repo

This commit is contained in:
Willy-JL
2023-11-12 11:06:02 +00:00
parent 79e7f491fe
commit e309fa8a88
1498 changed files with 1325977 additions and 20227 deletions

View File

@@ -0,0 +1,128 @@
#include <flipper_application/flipper_application.h>
#include <lib/toolbox/args.h>
#include "../../../cli_helpers.h"
#include "../../../cli_shared_methods.h"
#include "../../../cli_plugin_interface.h"
#include "../../../../types/token_info.h"
#include "../../../../services/config/config.h"
#include "../../../../services/convert/convert.h"
#include "../../../../ui/scene_director.h"
#include "../common.h"
struct TotpAddContext {
FuriString* args;
Cli* cli;
const CryptoSettings* crypto_settings;
};
enum TotpIteratorUpdateTokenResultsEx {
TotpIteratorUpdateTokenResultInvalidSecret = 1,
TotpIteratorUpdateTokenResultCancelled = 2,
TotpIteratorUpdateTokenResultInvalidArguments = 3
};
static TotpIteratorUpdateTokenResult
add_token_handler(TokenInfo* token_info, const void* context) {
const struct TotpAddContext* context_t = context;
// Reading token name
if(!args_read_probably_quoted_string_and_trim(context_t->args, token_info->name)) {
return TotpIteratorUpdateTokenResultInvalidArguments;
}
FuriString* temp_str = furi_string_alloc();
// Read optional arguments
bool mask_user_input = true;
PlainTokenSecretEncoding token_secret_encoding = PlainTokenSecretEncodingBase32;
while(args_read_string_and_trim(context_t->args, temp_str)) {
bool parsed = false;
if(!totp_cli_try_read_algo(token_info, temp_str, context_t->args, &parsed) &&
!totp_cli_try_read_digits(token_info, temp_str, context_t->args, &parsed) &&
!totp_cli_try_read_duration(token_info, temp_str, context_t->args, &parsed) &&
!totp_cli_try_read_unsecure_flag(temp_str, &parsed, &mask_user_input) &&
!totp_cli_try_read_automation_features(token_info, temp_str, context_t->args, &parsed) &&
!totp_cli_try_read_plain_token_secret_encoding(
temp_str, context_t->args, &parsed, &token_secret_encoding) &&
!totp_cli_try_read_token_type(token_info, temp_str, context_t->args, &parsed) &&
!totp_cli_try_read_token_counter(token_info, temp_str, context_t->args, &parsed)) {
totp_cli_printf_unknown_argument(temp_str);
}
if(!parsed) {
furi_string_free(temp_str);
return TotpIteratorUpdateTokenResultInvalidArguments;
}
}
// Reading token secret
furi_string_reset(temp_str);
TOTP_CLI_PRINTF("Enter token secret and confirm with [ENTER]:\r\n");
if(!totp_cli_read_line(context_t->cli, temp_str, mask_user_input)) {
TOTP_CLI_DELETE_LAST_LINE();
furi_string_secure_free(temp_str);
return TotpIteratorUpdateTokenResultCancelled;
}
TOTP_CLI_DELETE_LAST_LINE();
bool secret_set = token_info_set_secret(
token_info,
furi_string_get_cstr(temp_str),
furi_string_size(temp_str),
token_secret_encoding,
context_t->crypto_settings);
furi_string_secure_free(temp_str);
if(!secret_set) {
return TotpIteratorUpdateTokenResultInvalidSecret;
}
return TotpIteratorUpdateTokenResultSuccess;
}
static void handle(PluginState* plugin_state, FuriString* args, Cli* cli) {
if(!totp_cli_ensure_authenticated(plugin_state, cli)) {
return;
}
TokenInfoIteratorContext* iterator_context =
totp_config_get_token_iterator_context(plugin_state);
TOTP_CLI_LOCK_UI(plugin_state);
struct TotpAddContext add_context = {
.args = args, .cli = cli, .crypto_settings = &plugin_state->crypto_settings};
TotpIteratorUpdateTokenResult add_result =
totp_token_info_iterator_add_new_token(iterator_context, &add_token_handler, &add_context);
if(add_result == TotpIteratorUpdateTokenResultSuccess) {
TOTP_CLI_PRINTF_SUCCESS(
"Token \"%s\" has been successfully added\r\n",
furi_string_get_cstr(
totp_token_info_iterator_get_current_token(iterator_context)->name));
} else if(add_result == TotpIteratorUpdateTokenResultCancelled) {
TOTP_CLI_PRINTF_INFO("Cancelled by user\r\n");
} else if(add_result == TotpIteratorUpdateTokenResultInvalidArguments) {
TOTP_CLI_PRINT_INVALID_ARGUMENTS();
} else if(add_result == TotpIteratorUpdateTokenResultInvalidSecret) {
TOTP_CLI_PRINTF_ERROR("Token secret seems to be invalid and can not be parsed\r\n");
} else if(add_result == TotpIteratorUpdateTokenResultFileUpdateFailed) {
TOTP_CLI_PRINT_ERROR_UPDATING_CONFIG_FILE();
}
TOTP_CLI_UNLOCK_UI(plugin_state);
}
static const CliPlugin plugin = {.name = "TOTP CLI Plugin: Add", .handle = &handle};
static const FlipperAppPluginDescriptor plugin_descriptor = {
.appid = PLUGIN_APP_ID,
.ep_api_version = PLUGIN_API_VERSION,
.entry_point = &plugin,
};
const FlipperAppPluginDescriptor* totp_cli_add_plugin_ep() {
return &plugin_descriptor;
}

View File

@@ -0,0 +1,6 @@
#pragma once
#define TOTP_CLI_COMMAND_ADD "add"
#define TOTP_CLI_COMMAND_ADD_ALT "mk"
#define TOTP_CLI_COMMAND_ADD_ALT2 "new"
#define TOTP_CLI_PLUGIN_ADD_FILE_NAME "totp_cli_add_plugin"

View File

@@ -0,0 +1,182 @@
#include "common.h"
#include <lib/toolbox/args.h>
#include "stdint.h"
#include "../../cli_helpers.h"
#include "../../cli_shared_methods.h"
bool totp_cli_try_read_algo(TokenInfo* token_info, FuriString* arg, FuriString* args, bool* parsed) {
if(furi_string_cmpi_str(arg, TOTP_CLI_COMMAND_ARG_ALGO_PREFIX) == 0) {
if(!args_read_string_and_trim(args, arg)) {
totp_cli_printf_missed_argument_value(TOTP_CLI_COMMAND_ARG_ALGO_PREFIX);
} else if(!token_info_set_algo_from_str(token_info, arg)) {
TOTP_CLI_PRINTF_ERROR(
"\"%s\" is incorrect value for argument \"" TOTP_CLI_COMMAND_ARG_ALGO_PREFIX
"\"\r\n",
furi_string_get_cstr(arg));
} else {
*parsed = true;
}
return true;
}
return false;
}
bool totp_cli_try_read_digits(
TokenInfo* token_info,
const FuriString* arg,
FuriString* args,
bool* parsed) {
if(furi_string_cmpi_str(arg, TOTP_CLI_COMMAND_ARG_DIGITS_PREFIX) == 0) {
uint8_t digit_value;
if(!args_read_uint8_and_trim(args, &digit_value)) {
totp_cli_printf_missed_argument_value(TOTP_CLI_COMMAND_ARG_DIGITS_PREFIX);
} else if(!token_info_set_digits_from_int(token_info, digit_value)) {
TOTP_CLI_PRINTF_ERROR(
"\"%" PRIu8
"\" is incorrect value for argument \"" TOTP_CLI_COMMAND_ARG_DIGITS_PREFIX
"\"\r\n",
digit_value);
} else {
*parsed = true;
}
return true;
}
return false;
}
bool totp_cli_try_read_duration(
TokenInfo* token_info,
const FuriString* arg,
FuriString* args,
bool* parsed) {
if(furi_string_cmpi_str(arg, TOTP_CLI_COMMAND_ARG_DURATION_PREFIX) == 0) {
uint8_t duration_value;
if(!args_read_uint8_and_trim(args, &duration_value)) {
totp_cli_printf_missed_argument_value(TOTP_CLI_COMMAND_ARG_DURATION_PREFIX);
} else if(!token_info_set_duration_from_int(token_info, duration_value)) {
TOTP_CLI_PRINTF_ERROR(
"\"%" PRIu8
"\" is incorrect value for argument \"" TOTP_CLI_COMMAND_ARG_DURATION_PREFIX
"\"\r\n",
duration_value);
} else {
*parsed = true;
}
return true;
}
return false;
}
bool totp_cli_try_read_automation_features(
TokenInfo* token_info,
FuriString* arg,
FuriString* args,
bool* parsed) {
if(furi_string_cmpi_str(arg, TOTP_CLI_COMMAND_ARG_AUTOMATION_FEATURE_PREFIX) == 0) {
if(!args_read_string_and_trim(args, arg)) {
totp_cli_printf_missed_argument_value(TOTP_CLI_COMMAND_ARG_AUTOMATION_FEATURE_PREFIX);
} else if(!token_info_set_automation_feature_from_str(token_info, arg)) {
TOTP_CLI_PRINTF_ERROR(
"\"%s\" is incorrect value for argument \"" TOTP_CLI_COMMAND_ARG_AUTOMATION_FEATURE_PREFIX
"\"\r\n",
furi_string_get_cstr(arg));
} else {
*parsed = true;
}
return true;
}
return false;
}
bool totp_cli_try_read_unsecure_flag(const FuriString* arg, bool* parsed, bool* unsecure_flag) {
if(furi_string_cmpi_str(arg, TOTP_CLI_COMMAND_ARG_UNSECURE_PREFIX) == 0) {
*unsecure_flag = false;
*parsed = true;
return true;
}
return false;
}
bool totp_cli_try_read_plain_token_secret_encoding(
FuriString* arg,
FuriString* args,
bool* parsed,
PlainTokenSecretEncoding* secret_encoding) {
if(furi_string_cmpi_str(arg, TOTP_CLI_COMMAND_ARG_SECRET_ENCODING_PREFIX) == 0) {
if(!args_read_string_and_trim(args, arg)) {
totp_cli_printf_missed_argument_value(TOTP_CLI_COMMAND_ARG_SECRET_ENCODING_PREFIX);
} else {
if(furi_string_cmpi_str(arg, PLAIN_TOKEN_ENCODING_BASE32_NAME) == 0) {
*secret_encoding = PlainTokenSecretEncodingBase32;
*parsed = true;
} else if(furi_string_cmpi_str(arg, PLAIN_TOKEN_ENCODING_BASE64_NAME) == 0) {
*secret_encoding = PlainTokenSecretEncodingBase64;
*parsed = true;
} else {
TOTP_CLI_PRINTF_ERROR(
"\"%s\" is incorrect value for argument \"" TOTP_CLI_COMMAND_ARG_SECRET_ENCODING_PREFIX
"\"\r\n",
furi_string_get_cstr(arg));
}
}
return true;
}
return false;
}
bool totp_cli_try_read_token_type(
TokenInfo* token_info,
FuriString* arg,
FuriString* args,
bool* parsed) {
if(furi_string_cmpi_str(arg, TOTP_CLI_COMMAND_ARG_TYPE_PREFIX) == 0) {
if(!args_read_string_and_trim(args, arg)) {
totp_cli_printf_missed_argument_value(TOTP_CLI_COMMAND_ARG_TYPE_PREFIX);
} else if(!token_info_set_token_type_from_str(token_info, arg)) {
TOTP_CLI_PRINTF_ERROR(
"\"%s\" is incorrect value for argument \"" TOTP_CLI_COMMAND_ARG_TYPE_PREFIX
"\"\r\n",
furi_string_get_cstr(arg));
} else {
*parsed = true;
}
return true;
}
return false;
}
bool totp_cli_try_read_token_counter(
TokenInfo* token_info,
FuriString* arg,
FuriString* args,
bool* parsed) {
if(furi_string_cmpi_str(arg, TOTP_CLI_COMMAND_ARG_COUNTER_PREFIX) == 0) {
if(!args_read_string_and_trim(args, arg)) {
totp_cli_printf_missed_argument_value(TOTP_CLI_COMMAND_ARG_COUNTER_PREFIX);
} else if(!token_info_set_token_counter_from_str(token_info, arg)) {
TOTP_CLI_PRINTF_ERROR(
"\"%s\" is incorrect value for argument \"" TOTP_CLI_COMMAND_ARG_COUNTER_PREFIX
"\"\r\n",
furi_string_get_cstr(arg));
} else {
*parsed = true;
}
return true;
}
return false;
}

View File

@@ -0,0 +1,124 @@
#pragma once
#include <stdlib.h>
#include "../../../types/token_info.h"
#ifdef __cplusplus
extern "C" {
#endif
#define TOTP_CLI_COMMAND_ARG_NAME_PREFIX "-n"
#define TOTP_CLI_COMMAND_ARG_ALGO_PREFIX "-a"
#define TOTP_CLI_COMMAND_ARG_DIGITS_PREFIX "-d"
#define TOTP_CLI_COMMAND_ARG_UNSECURE_PREFIX "-u"
#define TOTP_CLI_COMMAND_ARG_DURATION_PREFIX "-l"
#define TOTP_CLI_COMMAND_ARG_AUTOMATION_FEATURE_PREFIX "-b"
#define TOTP_CLI_COMMAND_ARG_SECRET_ENCODING_PREFIX "-e"
#define TOTP_CLI_COMMAND_ARG_TYPE_PREFIX "-t"
#define TOTP_CLI_COMMAND_ARG_COUNTER_PREFIX "-i"
/**
* @brief Tries to read token hashing algo
* @param token_info token info to set parsed algo to if successfully read and parsed
* @param arg argument to parse
* @param args rest of arguments
* @param[out] parsed will be set to \c true if token hashing algo sucecssfully read and parsed; \c false otherwise
* @return \c true if \c arg represents token hashing algo argument; \c false otherwise
*/
bool totp_cli_try_read_algo(TokenInfo* token_info, FuriString* arg, FuriString* args, bool* parsed);
/**
* @brief Tries to read token digits count
* @param token_info token info to set parsed digits count to if successfully read and parsed
* @param arg argument to parse
* @param args rest of arguments
* @param[out] parsed will be set to \c true if token digits count sucecssfully read and parsed; \c false otherwise
* @return \c true if \c arg represents token digits count argument; \c false otherwise
*/
bool totp_cli_try_read_digits(
TokenInfo* token_info,
const FuriString* arg,
FuriString* args,
bool* parsed);
/**
* @brief Tries to read token duration
* @param token_info token info to set parsed duration to if successfully read and parsed
* @param arg argument to parse
* @param args rest of arguments
* @param[out] parsed will be set to \c true if token duration sucecssfully read and parsed; \c false otherwise
* @return \c true if \c arg represents token duration argument; \c false otherwise
*/
bool totp_cli_try_read_duration(
TokenInfo* token_info,
const FuriString* arg,
FuriString* args,
bool* parsed);
/**
* @brief Tries to read token automation features
* @param token_info token info to set parsed automation features to if successfully read and parsed
* @param arg argument to parse
* @param args rest of arguments
* @param[out] parsed will be set to \c true if token automation features sucecssfully read and parsed; \c false otherwise
* @return \c true if \c arg represents token automation features argument; \c false otherwise
*/
bool totp_cli_try_read_automation_features(
TokenInfo* token_info,
FuriString* arg,
FuriString* args,
bool* parsed);
/**
* @brief Tries to read unsecure flag
* @param arg argument to parse
* @param[out] parsed will be set to \c true if unsecure flag sucecssfully read and parsed; \c false otherwise
* @param[out] unsecure_flag will be set to parsed unsecure flag state if read and parsed successfully
* @return \c true if \c arg represents unsecure flag argument; \c false otherwise
*/
bool totp_cli_try_read_unsecure_flag(const FuriString* arg, bool* parsed, bool* unsecure_flag);
/**
* @brief Tries to read plain token secret encoding
* @param arg argument to parse
* @param args rest of arguments
* @param[out] parsed will be set to \c true if plain token secret encoding sucecssfully read and parsed; \c false otherwise
* @param[out] secret_encoding will be set to parsed plain token secret encoding if read and parsed successfully
* @return \c true if \c arg represents plain token secret encoding argument; \c false otherwise
*/
bool totp_cli_try_read_plain_token_secret_encoding(
FuriString* arg,
FuriString* args,
bool* parsed,
PlainTokenSecretEncoding* secret_encoding);
/**
* @brief Tries to read token type
* @param token_info token info to set parsed token type to if successfully read and parsed
* @param arg argument to parse
* @param args rest of arguments
* @param parsed will be set to \c true if token type sucecssfully read and parsed; \c false otherwise
* @return \c true if \c arg represents token type argument; \c false otherwise
*/
bool totp_cli_try_read_token_type(
TokenInfo* token_info,
FuriString* arg,
FuriString* args,
bool* parsed);
/**
* @brief Tries to read token counter
* @param token_info token info to set parsed token counter to if successfully read and parsed
* @param arg argument to parse
* @param args rest of arguments
* @param parsed will be set to \c true if token counter sucecssfully read and parsed; \c false otherwise
* @return \c true if \c arg represents token counter argument; \c false otherwise
*/
bool totp_cli_try_read_token_counter(
TokenInfo* token_info,
FuriString* arg,
FuriString* args,
bool* parsed);
#ifdef __cplusplus
}
#endif

View File

@@ -0,0 +1,4 @@
#pragma once
#define TOTP_CLI_COMMAND_UPDATE "update"
#define TOTP_CLI_PLUGIN_UPDATE_FILE_NAME "totp_cli_update_plugin"

View File

@@ -0,0 +1,167 @@
#include <flipper_application/flipper_application.h>
#include <lib/toolbox/args.h>
#include "../../../cli_helpers.h"
#include "../../../cli_shared_methods.h"
#include "../../../cli_plugin_interface.h"
#include "../../../../types/token_info.h"
#include "../../../../services/config/config.h"
#include "../../../../services/convert/convert.h"
#include "../../../../ui/scene_director.h"
#include "../common.h"
#define TOTP_CLI_COMMAND_UPDATE_ARG_SECRET_PREFIX "-s"
struct TotpUpdateContext {
FuriString* args;
Cli* cli;
const CryptoSettings* crypto_settings;
};
enum TotpIteratorUpdateTokenResultsEx {
TotpIteratorUpdateTokenResultInvalidSecret = 1,
TotpIteratorUpdateTokenResultCancelled = 2,
TotpIteratorUpdateTokenResultInvalidArguments = 3
};
static bool totp_cli_try_read_name(
TokenInfo* token_info,
const FuriString* arg,
FuriString* args,
bool* parsed) {
if(furi_string_cmpi_str(arg, TOTP_CLI_COMMAND_ARG_NAME_PREFIX) == 0) {
if(!args_read_probably_quoted_string_and_trim(args, token_info->name) ||
furi_string_empty(token_info->name)) {
totp_cli_printf_missed_argument_value(TOTP_CLI_COMMAND_ARG_NAME_PREFIX);
} else {
*parsed = true;
}
return true;
}
return false;
}
static bool totp_cli_try_read_change_secret_flag(const FuriString* arg, bool* parsed, bool* flag) {
if(furi_string_cmpi_str(arg, TOTP_CLI_COMMAND_UPDATE_ARG_SECRET_PREFIX) == 0) {
*flag = true;
*parsed = true;
return true;
}
return false;
}
static TotpIteratorUpdateTokenResult
update_token_handler(TokenInfo* token_info, const void* context) {
const struct TotpUpdateContext* context_t = context;
// Read optional arguments
FuriString* temp_str = furi_string_alloc();
bool mask_user_input = true;
bool update_token_secret = false;
PlainTokenSecretEncoding token_secret_encoding = PlainTokenSecretEncodingBase32;
while(args_read_string_and_trim(context_t->args, temp_str)) {
bool parsed = false;
if(!totp_cli_try_read_name(token_info, temp_str, context_t->args, &parsed) &&
!totp_cli_try_read_algo(token_info, temp_str, context_t->args, &parsed) &&
!totp_cli_try_read_digits(token_info, temp_str, context_t->args, &parsed) &&
!totp_cli_try_read_duration(token_info, temp_str, context_t->args, &parsed) &&
!totp_cli_try_read_unsecure_flag(temp_str, &parsed, &mask_user_input) &&
!totp_cli_try_read_change_secret_flag(temp_str, &parsed, &update_token_secret) &&
!totp_cli_try_read_automation_features(token_info, temp_str, context_t->args, &parsed) &&
!totp_cli_try_read_plain_token_secret_encoding(
temp_str, context_t->args, &parsed, &token_secret_encoding) &&
!totp_cli_try_read_token_type(token_info, temp_str, context_t->args, &parsed) &&
!totp_cli_try_read_token_counter(token_info, temp_str, context_t->args, &parsed)) {
totp_cli_printf_unknown_argument(temp_str);
}
if(!parsed) {
furi_string_free(temp_str);
return TotpIteratorUpdateTokenResultInvalidArguments;
}
}
if(update_token_secret) {
// Reading token secret
furi_string_reset(temp_str);
TOTP_CLI_PRINTF("Enter token secret and confirm with [ENTER]:\r\n");
bool token_secret_read = totp_cli_read_line(context_t->cli, temp_str, mask_user_input);
TOTP_CLI_DELETE_LAST_LINE();
if(!token_secret_read) {
furi_string_secure_free(temp_str);
return TotpIteratorUpdateTokenResultCancelled;
}
if(!token_info_set_secret(
token_info,
furi_string_get_cstr(temp_str),
furi_string_size(temp_str),
token_secret_encoding,
context_t->crypto_settings)) {
furi_string_secure_free(temp_str);
return TotpIteratorUpdateTokenResultInvalidSecret;
}
}
furi_string_secure_free(temp_str);
return TotpIteratorUpdateTokenResultSuccess;
}
static void handle(PluginState* plugin_state, FuriString* args, Cli* cli) {
if(!totp_cli_ensure_authenticated(plugin_state, cli)) {
return;
}
TokenInfoIteratorContext* iterator_context =
totp_config_get_token_iterator_context(plugin_state);
int token_number;
if(!args_read_int_and_trim(args, &token_number) || token_number <= 0 ||
(size_t)token_number > totp_token_info_iterator_get_total_count(iterator_context)) {
TOTP_CLI_PRINT_INVALID_ARGUMENTS();
return;
}
TOTP_CLI_LOCK_UI(plugin_state);
size_t previous_index = totp_token_info_iterator_get_current_token_index(iterator_context);
totp_token_info_iterator_go_to(iterator_context, token_number - 1);
struct TotpUpdateContext update_context = {
.args = args, .cli = cli, .crypto_settings = &plugin_state->crypto_settings};
TotpIteratorUpdateTokenResult update_result = totp_token_info_iterator_update_current_token(
iterator_context, &update_token_handler, &update_context);
if(update_result == TotpIteratorUpdateTokenResultSuccess) {
TOTP_CLI_PRINTF_SUCCESS(
"Token \"%s\" has been successfully updated\r\n",
furi_string_get_cstr(
totp_token_info_iterator_get_current_token(iterator_context)->name));
} else if(update_result == TotpIteratorUpdateTokenResultInvalidArguments) {
TOTP_CLI_PRINT_INVALID_ARGUMENTS();
} else if(update_result == TotpIteratorUpdateTokenResultCancelled) {
TOTP_CLI_PRINTF_INFO("Cancelled by user\r\n");
} else if(update_result == TotpIteratorUpdateTokenResultInvalidSecret) {
TOTP_CLI_PRINTF_ERROR("Token secret seems to be invalid and can not be parsed\r\n");
} else if(update_result == TotpIteratorUpdateTokenResultFileUpdateFailed) {
TOTP_CLI_PRINT_ERROR_UPDATING_CONFIG_FILE();
}
totp_token_info_iterator_go_to(iterator_context, previous_index);
TOTP_CLI_UNLOCK_UI(plugin_state);
}
static const CliPlugin plugin = {.name = "TOTP CLI Plugin: Update", .handle = &handle};
static const FlipperAppPluginDescriptor plugin_descriptor = {
.appid = PLUGIN_APP_ID,
.ep_api_version = PLUGIN_API_VERSION,
.entry_point = &plugin,
};
const FlipperAppPluginDescriptor* totp_cli_update_plugin_ep() {
return &plugin_descriptor;
}