-
Notifications
You must be signed in to change notification settings - Fork 48
/
add-common.c
56 lines (45 loc) · 1.34 KB
/
add-common.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <string.h>
#include "data.h"
#include "imports.h"
#include "parse-uri.h"
static gchar *check_params (GSList *otps);
gchar *
add_data_to_db (const gchar *otp_uri,
AppData *app_data)
{
GSList *otps = NULL;
set_otps_from_uris (otp_uri, &otps);
if (g_slist_length (otps) != 1) {
return g_strdup ("No valid otpauth uris found");
}
gchar *err_msg = check_params (otps);
if (err_msg != NULL) {
return err_msg;
}
err_msg = update_db_from_otps (otps, app_data);
if (err_msg != NULL) {
return err_msg;
}
free_otps_gslist (otps, g_slist_length (otps));
return NULL;
}
static gchar *
check_params (GSList *otps)
{
otp_t *otp = g_slist_nth_data (otps, 0);
if (otp->account_name == NULL) {
return g_strdup ("Label can not be empty, otp not imported");
}
if (otp->secret == NULL || otp->secret[0] == '\0') {
return g_strdup ("Secret can not be empty, otp not imported");
}
if (g_ascii_strcasecmp (otp->type, "TOTP") == 0) {
if (otp->period < 10 || otp->period > 120) {
gchar *msg = g_strconcat("[INFO]: invalid period for '", otp->account_name, "'. Defaulting back to 30 seconds.", NULL);
g_printerr ("%s\n", msg);
g_free (msg);
otp->period = 30;
}
}
return NULL;
}