Skip to content

Commit

Permalink
move base pack here
Browse files Browse the repository at this point in the history
  • Loading branch information
xMasterX committed Aug 12, 2023
0 parents commit 020928c
Show file tree
Hide file tree
Showing 146 changed files with 20,390 additions and 0 deletions.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2022 Alexander Kopachov

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
59 changes: 59 additions & 0 deletions application.fam
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
App(
appid="totp",
name="Authenticator",
apptype=FlipperAppType.EXTERNAL,
entry_point="totp_app",
cdefines=["APP_TOTP"],
requires=[
"gui",
"cli",
"dialogs",
"storage",
"input",
"notification",
"bt"
],
stack_size=2 * 1024,
order=20,
fap_version="4.01",
fap_author="Alexander Kopachov (@akopachov)",
fap_description="Software-based TOTP authenticator for Flipper Zero device",
fap_weburl="https://github.com/akopachov/flipper-zero_authenticator",
fap_category="Tools",
fap_icon_assets="images",
fap_icon="totp_10px.png",
fap_private_libs=[
Lib(
name="base32",
),
Lib(
name="base64",
),
Lib(
name="timezone_utils",
),
Lib(
name="polyfills",
),
Lib(
name="roll_value",
),
Lib(
name="fonts",
),
Lib(
name="wolfssl",
sources=[
"wolfcrypt/src/pwdbased.c",
"wolfcrypt/src/hmac.c",
"wolfcrypt/src/hash.c",
"wolfcrypt/src/sha.c",
"wolfcrypt/src/sha256.c",
"wolfcrypt/src/sha512.c"
],
cflags=["-Wno-error"],
cdefines=["HAVE_CONFIG_H"],
cincludes=["config/wolfssl"]
),
],
)
100 changes: 100 additions & 0 deletions cli/cli.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
// Original idea: https://github.com/br0ziliy

#include "cli.h"
#include <lib/toolbox/args.h>
#include "cli_helpers.h"
#include "commands/list/list.h"
#include "commands/add/add.h"
#include "commands/update/update.h"
#include "commands/delete/delete.h"
#include "commands/timezone/timezone.h"
#include "commands/help/help.h"
#include "commands/move/move.h"
#include "commands/pin/pin.h"
#include "commands/notification/notification.h"
#include "commands/reset/reset.h"
#include "commands/automation/automation.h"
#include "commands/details/details.h"

struct TotpCliContext {
PluginState* plugin_state;
};

static void totp_cli_print_unknown_command(const FuriString* unknown_command) {
TOTP_CLI_PRINTF_ERROR(
"Command \"%s\" is unknown. Use \"" TOTP_CLI_COMMAND_HELP
"\" command to get list of available commands.",
furi_string_get_cstr(unknown_command));
}

static void totp_cli_handler(Cli* cli, FuriString* args, void* context) {
TotpCliContext* cli_context = context;
PluginState* plugin_state = cli_context->plugin_state;

FuriString* cmd = furi_string_alloc();

args_read_string_and_trim(args, cmd);

if(furi_string_cmp_str(cmd, TOTP_CLI_COMMAND_HELP) == 0 ||
furi_string_cmp_str(cmd, TOTP_CLI_COMMAND_HELP_ALT) == 0 ||
furi_string_cmp_str(cmd, TOTP_CLI_COMMAND_HELP_ALT2) == 0 || furi_string_empty(cmd)) {
totp_cli_command_help_handle();
} else if(
furi_string_cmp_str(cmd, TOTP_CLI_COMMAND_ADD) == 0 ||
furi_string_cmp_str(cmd, TOTP_CLI_COMMAND_ADD_ALT) == 0 ||
furi_string_cmp_str(cmd, TOTP_CLI_COMMAND_ADD_ALT2) == 0) {
totp_cli_command_add_handle(plugin_state, args, cli);
} else if(
furi_string_cmp_str(cmd, TOTP_CLI_COMMAND_LIST) == 0 ||
furi_string_cmp_str(cmd, TOTP_CLI_COMMAND_LIST_ALT) == 0) {
totp_cli_command_list_handle(plugin_state, cli);
} else if(
furi_string_cmp_str(cmd, TOTP_CLI_COMMAND_DELETE) == 0 ||
furi_string_cmp_str(cmd, TOTP_CLI_COMMAND_DELETE_ALT) == 0) {
totp_cli_command_delete_handle(plugin_state, args, cli);
} else if(
furi_string_cmp_str(cmd, TOTP_CLI_COMMAND_TIMEZONE) == 0 ||
furi_string_cmp_str(cmd, TOTP_CLI_COMMAND_TIMEZONE_ALT) == 0) {
totp_cli_command_timezone_handle(plugin_state, args, cli);
} else if(
furi_string_cmp_str(cmd, TOTP_CLI_COMMAND_MOVE) == 0 ||
furi_string_cmp_str(cmd, TOTP_CLI_COMMAND_MOVE_ALT) == 0) {
totp_cli_command_move_handle(plugin_state, args, cli);
} else if(furi_string_cmp_str(cmd, TOTP_CLI_COMMAND_PIN) == 0) {
totp_cli_command_pin_handle(plugin_state, args, cli);
} else if(furi_string_cmp_str(cmd, TOTP_CLI_COMMAND_NOTIFICATION) == 0) {
totp_cli_command_notification_handle(plugin_state, args, cli);
} else if(furi_string_cmp_str(cmd, TOTP_CLI_COMMAND_AUTOMATION) == 0) {
totp_cli_command_automation_handle(plugin_state, args, cli);
} else if(furi_string_cmp_str(cmd, TOTP_CLI_COMMAND_RESET) == 0) {
totp_cli_command_reset_handle(plugin_state, cli);
} else if(furi_string_cmp_str(cmd, TOTP_CLI_COMMAND_UPDATE) == 0) {
totp_cli_command_update_handle(plugin_state, args, cli);
} else if(
furi_string_cmp_str(cmd, TOTP_CLI_COMMAND_DETAILS) == 0 ||
furi_string_cmp_str(cmd, TOTP_CLI_COMMAND_DETAILS_ALT) == 0) {
totp_cli_command_details_handle(plugin_state, args, cli);
} else {
totp_cli_print_unknown_command(cmd);
}

furi_string_free(cmd);
}

TotpCliContext* totp_cli_register_command_handler(PluginState* plugin_state) {
Cli* cli = furi_record_open(RECORD_CLI);
TotpCliContext* context = malloc(sizeof(TotpCliContext));
furi_check(context != NULL);
context->plugin_state = plugin_state;
cli_add_command(
cli, TOTP_CLI_COMMAND_NAME, CliCommandFlagParallelSafe, totp_cli_handler, context);
furi_record_close(RECORD_CLI);
return context;
}

void totp_cli_unregister_command_handler(TotpCliContext* context) {
Cli* cli = furi_record_open(RECORD_CLI);
cli_delete_command(cli, TOTP_CLI_COMMAND_NAME);
furi_record_close(RECORD_CLI);
free(context);
}
19 changes: 19 additions & 0 deletions cli/cli.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#pragma once

#include <cli/cli.h>
#include "../types/plugin_state.h"

typedef struct TotpCliContext TotpCliContext;

/**
* @brief Registers TOTP CLI handler
* @param plugin_state application state
* @return TOTP CLI context
*/
TotpCliContext* totp_cli_register_command_handler(PluginState* plugin_state);

/**
* @brief Unregisters TOTP CLI handler
* @param context application state
*/
void totp_cli_unregister_command_handler(TotpCliContext* context);
123 changes: 123 additions & 0 deletions cli/cli_helpers.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
#include "cli_helpers.h"
#include <cli/cli.h>
#include <lib/toolbox/args.h>
#include "../types/plugin_event.h"

const char* TOTP_CLI_COLOR_ERROR = "91m";
const char* TOTP_CLI_COLOR_WARNING = "93m";
const char* TOTP_CLI_COLOR_SUCCESS = "92m";
const char* TOTP_CLI_COLOR_INFO = "96m";

bool totp_cli_ensure_authenticated(const PluginState* plugin_state, Cli* cli) {
if(plugin_state->current_scene == TotpSceneAuthentication) {
TOTP_CLI_PRINTF("Pleases enter PIN on your flipper device\r\n");

while((plugin_state->current_scene == TotpSceneAuthentication ||
plugin_state->current_scene == TotpSceneNone) &&
!cli_cmd_interrupt_received(cli)) {
furi_delay_ms(100);
}

totp_cli_delete_last_line();

if(plugin_state->current_scene == TotpSceneAuthentication || //-V560
plugin_state->current_scene == TotpSceneNone) { //-V560
TOTP_CLI_PRINTF_INFO("Cancelled by user\r\n");
return false;
}
}

return true;
}

void totp_cli_force_close_app(FuriMessageQueue* event_queue) {
PluginEvent event = {.type = EventForceCloseApp};
furi_message_queue_put(event_queue, &event, FuriWaitForever);
}

bool totp_cli_read_line(Cli* cli, FuriString* out_str, bool mask_user_input) {
uint8_t c;
while(cli_read(cli, &c, 1) == 1) {
if(c == CliSymbolAsciiEsc) {
// Some keys generating escape-sequences
// We need to ignore them as we care about alpha-numerics only
uint8_t c2;
cli_read_timeout(cli, &c2, 1, 0);
cli_read_timeout(cli, &c2, 1, 0);
} else if(c == CliSymbolAsciiETX) {
cli_nl();
return false;
} else if(
(c >= '0' && c <= '9') || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
c == '/' || c == '=' || c == '+') {
if(mask_user_input) {
putc('*', stdout);
} else {
putc(c, stdout);
}
fflush(stdout);
furi_string_push_back(out_str, c);
} else if(c == CliSymbolAsciiBackspace || c == CliSymbolAsciiDel) {
size_t out_str_size = furi_string_size(out_str);
if(out_str_size > 0) {
totp_cli_delete_last_char();
furi_string_left(out_str, out_str_size - 1);
}
} else if(c == CliSymbolAsciiCR) {
cli_nl();
break;
}
}

return true;
}

bool args_read_uint8_and_trim(FuriString* args, uint8_t* value) {
int int_value;
if(!args_read_int_and_trim(args, &int_value) || int_value < 0 || int_value > UINT8_MAX) {
return false;
}

*value = (uint8_t)int_value;
return true;
}

void furi_string_secure_free(FuriString* str) {
for(long i = furi_string_size(str) - 1; i >= 0; i--) {
furi_string_set_char(str, i, '\0');
}

furi_string_free(str);
}

void totp_cli_print_invalid_arguments() {
TOTP_CLI_PRINTF_ERROR(
"Invalid command arguments. use \"help\" command to get list of available commands");
}

void totp_cli_print_error_updating_config_file() {
TOTP_CLI_PRINTF_ERROR("An error has occurred during updating config file\r\n");
}

void totp_cli_print_error_loading_token_info() {
TOTP_CLI_PRINTF_ERROR("An error has occurred during loading token information\r\n");
}

void totp_cli_print_processing() {
TOTP_CLI_PRINTF("Processing, please wait...\r\n");
}

void totp_cli_delete_last_char() {
TOTP_CLI_PRINTF("\b \b");
fflush(stdout);
}

void totp_cli_delete_current_line() {
TOTP_CLI_PRINTF("\33[2K\r");
fflush(stdout);
}

void totp_cli_delete_last_line() {
TOTP_CLI_PRINTF("\033[A\33[2K\r");
fflush(stdout);
}
Loading

0 comments on commit 020928c

Please sign in to comment.