Browse code
Refactor install_key, return key and sample token
Ed Langley authored on 13/06/2017 18:18:08
Showing 4 changed files
Showing 4 changed files
... | ... |
@@ -32,6 +32,7 @@ class system init_system() |
32 | 32 |
class system system (stdlib, time); |
33 | 33 |
return system; |
34 | 34 |
} |
35 |
+ |
|
35 | 36 |
installer init_installer() |
36 | 37 |
{ |
37 | 38 |
fstreams fstreams (fstreams::create()); |
... | ... |
@@ -53,6 +54,6 @@ int main (int argc, char *argv[]) |
53 | 54 |
{ |
54 | 55 |
class system system (init_system()); |
55 | 56 |
installer tool (init_installer()); |
56 |
- std::string generated_key = tool.install_key(); |
|
57 |
- std::cout << generated_key << std::endl; |
|
57 |
+ auto generated_key_and_sample_token = tool.install_key(); |
|
58 |
+ std::cout << generated_key_and_sample_token.first << " " << generated_key_and_sample_token.second << std::endl; |
|
58 | 59 |
} |
... | ... |
@@ -11,12 +11,14 @@ |
11 | 11 |
|
12 | 12 |
#include <string> |
13 | 13 |
#include <vector> |
14 |
+#include <utility> |
|
14 | 15 |
|
15 | 16 |
#include "installer.h" |
16 | 17 |
#include "token.h" |
17 | 18 |
#include "sys_unistd.h" |
18 | 19 |
#include "user.h" |
19 | 20 |
#include "generator.h" |
21 |
+#include "base32.h" |
|
20 | 22 |
|
21 | 23 |
namespace |
22 | 24 |
{ |
... | ... |
@@ -34,19 +36,23 @@ public: |
34 | 36 |
const totp_generator generator) : |
35 | 37 |
tokens_ (tokens), unistd_ (unistd), directory_ (directory), |
36 | 38 |
generator_ (generator) {} |
37 |
- std::string install_key() const override |
|
39 |
+ std::pair<std::string, std::string> install_key() const override |
|
38 | 40 |
{ |
39 | 41 |
auto found_user = directory_.get_current_user (); |
40 | 42 |
|
41 | 43 |
if (found_user.empty()) { |
42 |
- return ""; |
|
44 |
+ return {"", ""}; |
|
43 | 45 |
} |
44 | 46 |
|
45 | 47 |
user user (found_user[0]); |
46 | 48 |
|
47 |
- std::string key = tokens_.ensure_key(user); |
|
49 |
+ std::string key_string = tokens_.ensure_key(user); |
|
50 |
+ std::vector<uint8_t> key = base32().decode(key_string); |
|
51 |
+ std::string decoded_key (key.begin(), key.end()); |
|
52 |
+ std::string token = generator_.generate_token(decoded_key); |
|
48 | 53 |
|
49 |
- return key; |
|
54 |
+ // TODO: fix generator input |
|
55 |
+ return {key_string, token}; |
|
50 | 56 |
} |
51 | 57 |
}; |
52 | 58 |
|
... | ... |
@@ -15,6 +15,7 @@ |
15 | 15 |
#include <string> |
16 | 16 |
#include <memory> |
17 | 17 |
#include <functional> |
18 |
+#include <utility> |
|
18 | 19 |
|
19 | 20 |
#include "token.h" |
20 | 21 |
#include "sys_unistd.h" |
... | ... |
@@ -24,9 +25,9 @@ |
24 | 25 |
class installer_ifc |
25 | 26 |
{ |
26 | 27 |
public: |
27 |
- virtual std::string install_key() const |
|
28 |
+ virtual std::pair<std::string, std::string> install_key() const |
|
28 | 29 |
{ |
29 |
- return "123456"; |
|
30 |
+ return {"AAAA", "123456"}; |
|
30 | 31 |
} |
31 | 32 |
}; |
32 | 33 |
|
... | ... |
@@ -39,7 +40,7 @@ private: |
39 | 40 |
public: |
40 | 41 |
installer (const delegate &delegate = std::make_shared<installer_ifc>()) : |
41 | 42 |
delegate_ (delegate) {} |
42 |
- std::string install_key() const |
|
43 |
+ std::pair<std::string, std::string> install_key() const |
|
43 | 44 |
{ |
44 | 45 |
return delegate_->install_key(); |
45 | 46 |
} |
... | ... |
@@ -13,6 +13,7 @@ |
13 | 13 |
#include <memory> |
14 | 14 |
#include <string> |
15 | 15 |
#include <vector> |
16 |
+#include <tuple> |
|
16 | 17 |
|
17 | 18 |
#include "installer.h" |
18 | 19 |
#include "user.h" |
... | ... |
@@ -117,23 +118,25 @@ int installs_token() |
117 | 118 |
//given |
118 | 119 |
std::string user_name ("user"); |
119 | 120 |
std::string key ("thekey"); |
121 |
+ std::string token ("thetoken"); |
|
120 | 122 |
auto test_tokens = std::make_shared<mock_tokens>(key); |
121 | 123 |
tokens tokens{test_tokens}; |
122 | 124 |
unistd unistd (std::make_shared<fake_unistd> (user_name)); |
123 | 125 |
directory directory (std::make_shared<fake_directory> (user_name)); |
124 |
- std::shared_ptr<fake_totp_generator> fake_generator = |
|
125 |
- std::make_shared<fake_totp_generator> (); |
|
126 |
- totp_generator generator (fake_generator); |
|
126 |
+ std::shared_ptr<fake_totp_generator> fake_generator = std::make_shared<fake_totp_generator> (token); |
|
127 |
+ totp_generator generator (fake_generator ); |
|
127 | 128 |
|
128 | 129 |
installer installer = installer::create (tokens, unistd, directory, |
129 | 130 |
generator); |
130 | 131 |
|
131 | 132 |
//when |
132 |
- std::string result = installer.install_key(); |
|
133 |
+ std::string actual_key, actual_token; |
|
134 |
+ std::tie(actual_key, actual_token) = installer.install_key(); |
|
133 | 135 |
|
134 | 136 |
//then |
135 |
- check (test_tokens->captured_token == key, "installed wrong token"); |
|
136 |
- check (result == key, "installer returned wrong token"); |
|
137 |
+ check (test_tokens->captured_token == key, "installed wrong key"); |
|
138 |
+ check (actual_key == key, "installer returned wrong key"); |
|
139 |
+ check (actual_token == token, "installer returned wrong token"); |
|
137 | 140 |
succeed(); |
138 | 141 |
} |
139 | 142 |
|
... | ... |
@@ -176,12 +179,17 @@ int unistd_does_not_find_user_name_empty_string_case() |
176 | 179 |
generator); |
177 | 180 |
|
178 | 181 |
//when |
179 |
- auto returned = installer.install_key(); |
|
182 |
+ std::string actual_key, actual_token; |
|
183 |
+ std::tie(actual_key, actual_token) = installer.install_key(); |
|
180 | 184 |
|
181 | 185 |
//then |
186 |
+ // TODO: rethink this... |
|
182 | 187 |
check (test_tokens->captured_token == "", |
183 | 188 |
"should not have installed a token"); |
184 |
- check (returned == "", "did not return empty token"); |
|
189 |
+ check (actual_key == "", |
|
190 |
+ "should not have installed a token"); |
|
191 |
+ check (actual_token == "", |
|
192 |
+ "should not have installed a token"); |
|
185 | 193 |
succeed(); |
186 | 194 |
} |
187 | 195 |
|
... | ... |
@@ -199,11 +207,13 @@ int directory_finds_no_user_info() |
199 | 207 |
generator); |
200 | 208 |
|
201 | 209 |
//when |
202 |
- auto returned = installer.install_key(); |
|
210 |
+ std::string actual_key, actual_token; |
|
211 |
+ std::tie(actual_key, actual_token) = installer.install_key(); |
|
203 | 212 |
|
204 | 213 |
//then |
205 |
- check (test_tokens->captured_token == "", "installed wrong token"); |
|
206 |
- check (returned == "", "did not return empty token"); |
|
214 |
+ check (test_tokens->captured_token == "", "installed wrong key"); |
|
215 |
+ check (actual_key == "", "did not return empty token"); |
|
216 |
+ check (actual_token == "", "did not return an empty token"); |
|
207 | 217 |
succeed(); |
208 | 218 |
} |
209 | 219 |
|