authenticator update

This commit is contained in:
jbohack
2023-01-18 08:50:21 -05:00
parent d231c49486
commit 7b1b405f80
12 changed files with 237 additions and 57 deletions

View File

@@ -21,6 +21,7 @@ typedef enum {
TokenSecretTextBox,
TokenAlgoSelect,
TokenLengthSelect,
TokenDurationSelect,
ConfirmButton,
} Control;
@@ -39,6 +40,8 @@ typedef struct {
int16_t screen_y_offset;
TokenHashAlgo algo;
uint8_t digits_count_index;
uint8_t duration;
FuriString* duration_text;
} SceneState;
void totp_scene_add_new_token_init(const PluginState* plugin_state) {
@@ -63,6 +66,10 @@ static void on_token_secret_user_comitted(InputTextSceneCallbackResult* result)
free(result);
}
static void update_duration_text(SceneState* scene_state) {
furi_string_printf(scene_state->duration_text, "%d sec.", scene_state->duration);
}
void totp_scene_add_new_token_activate(
PluginState* plugin_state,
const TokenAddEditSceneContext* context) {
@@ -89,6 +96,9 @@ void totp_scene_add_new_token_activate(
scene_state->screen_y_offset = 0;
scene_state->input_state = NULL;
scene_state->duration = TOTP_TOKEN_DURATION_DEFAULT;
scene_state->duration_text = furi_string_alloc();
update_duration_text(scene_state);
if(context == NULL) {
TOTP_NULLABLE_NULL(scene_state->current_token_index);
@@ -124,14 +134,23 @@ void totp_scene_add_new_token_render(Canvas* const canvas, PluginState* plugin_s
ui_control_select_render(
canvas,
0,
63 - scene_state->screen_y_offset,
61 - scene_state->screen_y_offset,
SCREEN_WIDTH,
TOKEN_DIGITS_TEXT_LIST[scene_state->digits_count_index],
scene_state->selected_control == TokenLengthSelect);
ui_control_select_render(
canvas,
0,
78 - scene_state->screen_y_offset,
SCREEN_WIDTH,
furi_string_get_cstr(scene_state->duration_text),
scene_state->selected_control == TokenDurationSelect);
ui_control_button_render(
canvas,
SCREEN_WIDTH_CENTER - 24,
85 - scene_state->screen_y_offset,
101 - scene_state->screen_y_offset,
48,
13,
"Confirm",
@@ -146,8 +165,12 @@ void totp_scene_add_new_token_render(Canvas* const canvas, PluginState* plugin_s
}
void update_screen_y_offset(SceneState* scene_state) {
if(scene_state->selected_control > TokenAlgoSelect) {
scene_state->screen_y_offset = 35;
if(scene_state->selected_control > TokenLengthSelect) {
scene_state->screen_y_offset = 51;
} else if(scene_state->selected_control > TokenAlgoSelect) {
scene_state->screen_y_offset = 34;
} else if(scene_state->selected_control > TokenSecretTextBox) {
scene_state->screen_y_offset = 17;
} else {
scene_state->screen_y_offset = 0;
}
@@ -197,6 +220,9 @@ bool totp_scene_add_new_token_handle_event(PluginEvent* const event, PluginState
} else if(scene_state->selected_control == TokenLengthSelect) {
totp_roll_value_uint8_t(
&scene_state->digits_count_index, 1, 0, 1, RollOverflowBehaviorRoll);
} else if(scene_state->selected_control == TokenDurationSelect) {
totp_roll_value_uint8_t(&scene_state->duration, 15, 15, 255, RollOverflowBehaviorStop);
update_duration_text(scene_state);
}
break;
case InputKeyLeft:
@@ -206,6 +232,10 @@ bool totp_scene_add_new_token_handle_event(PluginEvent* const event, PluginState
} else if(scene_state->selected_control == TokenLengthSelect) {
totp_roll_value_uint8_t(
&scene_state->digits_count_index, -1, 0, 1, RollOverflowBehaviorRoll);
} else if(scene_state->selected_control == TokenDurationSelect) {
totp_roll_value_uint8_t(
&scene_state->duration, -15, 15, 255, RollOverflowBehaviorStop);
update_duration_text(scene_state);
}
break;
case InputKeyOk:
@@ -230,6 +260,8 @@ bool totp_scene_add_new_token_handle_event(PluginEvent* const event, PluginState
break;
case TokenLengthSelect:
break;
case TokenDurationSelect:
break;
case ConfirmButton: {
TokenInfo* tokenInfo = token_info_alloc();
bool token_secret_set = token_info_set_secret(
@@ -245,6 +277,7 @@ bool totp_scene_add_new_token_handle_event(PluginEvent* const event, PluginState
tokenInfo->name, scene_state->token_name, scene_state->token_name_length + 1);
tokenInfo->algo = scene_state->algo;
tokenInfo->digits = TOKEN_DIGITS_VALUE_LIST[scene_state->digits_count_index];
tokenInfo->duration = scene_state->duration;
TOTP_LIST_INIT_OR_ADD(plugin_state->tokens_list, tokenInfo, furi_check);
plugin_state->tokens_count++;
@@ -310,6 +343,8 @@ void totp_scene_add_new_token_deactivate(PluginState* plugin_state) {
free(scene_state->token_secret_input_context->header_text);
free(scene_state->token_secret_input_context);
furi_string_free(scene_state->duration_text);
if(scene_state->input_state != NULL) {
totp_input_text_free(scene_state->input_state);
}

View File

@@ -16,13 +16,11 @@
#include "../token_menu/totp_scene_token_menu.h"
#include "../../../workers/type_code/type_code.h"
#define TOKEN_LIFETIME 30
typedef struct {
uint16_t current_token_index;
char last_code[TOTP_TOKEN_DIGITS_MAX_COUNT + 1];
char* last_code_name;
bool need_token_update;
TokenInfo* current_token;
uint32_t last_token_gen_time;
TotpTypeCodeWorkerContext* type_code_worker_context;
NotificationMessage const** notification_sequence_new_token;
@@ -151,7 +149,7 @@ static void update_totp_params(PluginState* const plugin_state) {
list_element_at(plugin_state->tokens_list, scene_state->current_token_index)->data;
scene_state->need_token_update = true;
scene_state->last_code_name = tokenInfo->name;
scene_state->current_token = tokenInfo;
}
}
@@ -229,7 +227,7 @@ void totp_scene_generate_token_render(Canvas* const canvas, PluginState* plugin_
furi_hal_rtc_get_datetime(&curr_dt);
uint32_t curr_ts = furi_hal_rtc_datetime_to_timestamp(&curr_dt);
bool is_new_token_time = curr_ts % TOKEN_LIFETIME == 0;
bool is_new_token_time = curr_ts % scene_state->current_token->duration == 0;
if(is_new_token_time && scene_state->last_token_gen_time != curr_ts) {
scene_state->need_token_update = true;
}
@@ -238,10 +236,7 @@ void totp_scene_generate_token_render(Canvas* const canvas, PluginState* plugin_
scene_state->need_token_update = false;
scene_state->last_token_gen_time = curr_ts;
const TokenInfo* tokenInfo =
(TokenInfo*)(list_element_at(
plugin_state->tokens_list, scene_state->current_token_index)
->data);
const TokenInfo* tokenInfo = scene_state->current_token;
if(tokenInfo->token != NULL && tokenInfo->token_length > 0) {
furi_mutex_acquire(
@@ -258,7 +253,7 @@ void totp_scene_generate_token_render(Canvas* const canvas, PluginState* plugin_
key_length,
curr_ts,
plugin_state->timezone_offset,
TOKEN_LIFETIME),
tokenInfo->duration),
scene_state->last_code,
tokenInfo->digits);
memset_s(key, key_length, 0, key_length);
@@ -279,7 +274,7 @@ void totp_scene_generate_token_render(Canvas* const canvas, PluginState* plugin_
}
canvas_set_font(canvas, FontPrimary);
uint16_t token_name_width = canvas_string_width(canvas, scene_state->last_code_name);
uint16_t token_name_width = canvas_string_width(canvas, scene_state->current_token->name);
if(SCREEN_WIDTH - token_name_width > 18) {
canvas_draw_str_aligned(
canvas,
@@ -287,7 +282,7 @@ void totp_scene_generate_token_render(Canvas* const canvas, PluginState* plugin_
SCREEN_HEIGHT_CENTER - 20,
AlignCenter,
AlignCenter,
scene_state->last_code_name);
scene_state->current_token->name);
} else {
canvas_draw_str_aligned(
canvas,
@@ -295,7 +290,7 @@ void totp_scene_generate_token_render(Canvas* const canvas, PluginState* plugin_
SCREEN_HEIGHT_CENTER - 20,
AlignLeft,
AlignCenter,
scene_state->last_code_name);
scene_state->current_token->name);
canvas_set_color(canvas, ColorWhite);
canvas_draw_box(canvas, 0, SCREEN_HEIGHT_CENTER - 24, 9, 9);
canvas_draw_box(canvas, SCREEN_WIDTH - 10, SCREEN_HEIGHT_CENTER - 24, 9, 9);
@@ -313,6 +308,7 @@ void totp_scene_generate_token_render(Canvas* const canvas, PluginState* plugin_
const uint8_t BAR_MARGIN = 3;
const uint8_t BAR_HEIGHT = 4;
const uint8_t TOKEN_LIFETIME = scene_state->current_token->duration;
float percentDone = (float)(TOKEN_LIFETIME - curr_ts % TOKEN_LIFETIME) / (float)TOKEN_LIFETIME;
uint8_t barWidth = (uint8_t)((float)(SCREEN_WIDTH - (BAR_MARGIN << 1)) * percentDone);
uint8_t barX = ((SCREEN_WIDTH - (BAR_MARGIN << 1) - barWidth) >> 1) + BAR_MARGIN;