aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/src
diff options
context:
space:
mode:
authorDavid M. Syzdek <david@syzdek.net>2022-12-31 18:59:19 -0900
committerDavid M. Syzdek <david@syzdek.net>2022-12-31 18:59:19 -0900
commitdd240105b08d37b4b48567c078d313713c35fc73 (patch)
treef762644ca08d20bc2d63866632a2c18e959af060 /src
parent74883f567f1b6cb5d7f38ed573ae55982ab494cb (diff)
downloadotputils-dd240105b08d37b4b48567c078d313713c35fc73.tar.gz
otputils-dd240105b08d37b4b48567c078d313713c35fc73.tar.bz2
otputils-dd240105b08d37b4b48567c078d313713c35fc73.tar.xz
otputils-dd240105b08d37b4b48567c078d313713c35fc73.zip
otputil: refactoring totp widget
Diffstat (limited to 'src')
-rw-r--r--src/widget-totp.c83
1 files changed, 63 insertions, 20 deletions
diff --git a/src/widget-totp.c b/src/widget-totp.c
index 34a77b0..b3f552f 100644
--- a/src/widget-totp.c
+++ b/src/widget-totp.c
@@ -55,6 +55,23 @@
55#include "otputil.h" 55#include "otputil.h"
56 56
57 57
58//////////////////
59// //
60// Prototypes //
61// //
62//////////////////
63#pragma mark - Prototypes
64
65static int
66otputil_widget_totp_code(
67 otputil_config_t * cnf );
68
69
70static int
71otputil_widget_totp_verify(
72 otputil_config_t * cnf );
73
74
58///////////////// 75/////////////////
59// // 76// //
60// Functions // 77// Functions //
@@ -74,10 +91,6 @@ otputil_widget_totp(
74{ 91{
75 int rc; 92 int rc;
76 uint64_t meth; 93 uint64_t meth;
77 uint64_t totp_tx;
78 char totp_tx_str[64];
79 static char buff[OTPUTIL_MAX_CODE_SIZE];
80 const char * code;
81 94
82 assert(cnf != NULL); 95 assert(cnf != NULL);
83 96
@@ -100,12 +113,24 @@ otputil_widget_totp(
100 }; 113 };
101 otputil_pass = (cnf->argc > optind) ? cnf->argv[optind] : otputil_pass; 114 otputil_pass = (cnf->argc > optind) ? cnf->argv[optind] : otputil_pass;
102 115
103 // retrieve OTP secret information 116 if ((otputil_pass))
104 if ((rc = otputil_get_param(NULL, OTPUTIL_OPT_TOTP_X, &totp_tx)) != 0) 117 return(otputil_widget_totp_verify(cnf));
105 { 118
106 fprintf(stderr, "%s: otputil_get_param(TX): %s\n", cnf->prog_name, otputil_err2string(rc)); 119 return(otputil_widget_totp_code(cnf));
107 return(1); 120}
108 }; 121
122
123int
124otputil_widget_totp_code(
125 otputil_config_t * cnf )
126{
127 int rc;
128 uint64_t totp_tx;
129 char totp_tx_str[64];
130 const char * code;
131 static char buff[OTPUTIL_MAX_CODE_SIZE];
132
133 assert(cnf != NULL);
109 134
110 if ((code = otputil_str(NULL, buff, sizeof(buff))) == NULL) 135 if ((code = otputil_str(NULL, buff, sizeof(buff))) == NULL)
111 { 136 {
@@ -113,17 +138,10 @@ otputil_widget_totp(
113 return(1); 138 return(1);
114 }; 139 };
115 140
116 if ((otputil_pass)) 141 if ((rc = otputil_get_param(NULL, OTPUTIL_OPT_TOTP_X, &totp_tx)) != 0)
117 { 142 {
118 if ((strcasecmp(otputil_pass, code))) 143 fprintf(stderr, "%s: otputil_get_param(TX): %s\n", cnf->prog_name, otputil_err2string(rc));
119 { 144 return(1);
120 printf("%s\n", "invalid code");
121 return(2);
122 } else
123 {
124 printf("%s\n", "valid code");
125 return(0);
126 };
127 }; 145 };
128 146
129 if (!(cnf->quiet)) 147 if (!(cnf->quiet))
@@ -139,4 +157,29 @@ otputil_widget_totp(
139} 157}
140 158
141 159
160int
161otputil_widget_totp_verify(
162 otputil_config_t * cnf )
163{
164 const char * code;
165
166 assert(cnf != NULL);
167
168 if ((code = otputil_str(NULL, NULL, 0)) == NULL)
169 {
170 fprintf(stderr, "%s: internal error\n", cnf->prog_name);
171 return(1);
172 };
173
174 if ((strcasecmp(otputil_pass, code)))
175 {
176 printf("%s\n", "invalid code");
177 return(2);
178 };
179
180 printf("%s\n", "valid code");
181
182 return(0);
183}
184
142/* end of source file */ 185/* end of source file */