Browse code
Rename install_token, save and print key not token
Ed Langley authored on 13/06/2017 00:32:50
Showing 6 changed files
Showing 6 changed files
... | ... |
@@ -53,7 +53,6 @@ int main (int argc, char *argv[]) |
53 | 53 |
{ |
54 | 54 |
class system system (init_system()); |
55 | 55 |
installer tool (init_installer()); |
56 |
- std::string generated_token = tool.install_token(); |
|
57 |
- std::cout << generated_token << std::endl; |
|
58 |
- return 0; |
|
56 |
+ std::string generated_key = tool.install_key(); |
|
57 |
+ std::cout << generated_key << std::endl; |
|
59 | 58 |
} |
... | ... |
@@ -34,7 +34,7 @@ public: |
34 | 34 |
const totp_generator generator) : |
35 | 35 |
tokens_ (tokens), unistd_ (unistd), directory_ (directory), |
36 | 36 |
generator_ (generator) {} |
37 |
- std::string install_token() const override |
|
37 |
+ std::string install_key() const override |
|
38 | 38 |
{ |
39 | 39 |
const char *c_user_name = unistd_.getlogin(); |
40 | 40 |
|
... | ... |
@@ -51,9 +51,10 @@ public: |
51 | 51 |
} |
52 | 52 |
|
53 | 53 |
user user (found_user[0]); |
54 |
- std::string token (tokens_.token(user)); |
|
55 |
- tokens_.save (user, token); |
|
56 |
- return token; |
|
54 |
+ |
|
55 |
+ std::string key = tokens_.ensure_key(user); |
|
56 |
+ |
|
57 |
+ return key; |
|
57 | 58 |
} |
58 | 59 |
}; |
59 | 60 |
|
... | ... |
@@ -24,7 +24,7 @@ |
24 | 24 |
class installer_ifc |
25 | 25 |
{ |
26 | 26 |
public: |
27 |
- virtual std::string install_token() const |
|
27 |
+ virtual std::string install_key() const |
|
28 | 28 |
{ |
29 | 29 |
return "123456"; |
30 | 30 |
} |
... | ... |
@@ -39,9 +39,9 @@ private: |
39 | 39 |
public: |
40 | 40 |
installer (const delegate &delegate = std::make_shared<installer_ifc>()) : |
41 | 41 |
delegate_ (delegate) {} |
42 |
- std::string install_token() const |
|
42 |
+ std::string install_key() const |
|
43 | 43 |
{ |
44 |
- return delegate_->install_token(); |
|
44 |
+ return delegate_->install_key(); |
|
45 | 45 |
} |
46 | 46 |
static installer create (const tokens &tokens, const unistd &unistd, |
47 | 47 |
const directory &directory, |
... | ... |
@@ -34,6 +34,11 @@ public: |
34 | 34 |
return key_; |
35 | 35 |
} |
36 | 36 |
|
37 |
+ std::string ensure_key(const user &user) const override { |
|
38 |
+ save(user, key_); |
|
39 |
+ return key_; |
|
40 |
+ } |
|
41 |
+ |
|
37 | 42 |
void save (const user &user, const std::string &token) const override |
38 | 43 |
{ |
39 | 44 |
captured_token = token; |
... | ... |
@@ -115,7 +120,7 @@ int installs_token() |
115 | 120 |
generator); |
116 | 121 |
|
117 | 122 |
//when |
118 |
- std::string result = installer.install_token(); |
|
123 |
+ std::string result = installer.install_key(); |
|
119 | 124 |
|
120 | 125 |
//then |
121 | 126 |
check (test_tokens->captured_token == key, "installed wrong token"); |
... | ... |
@@ -138,7 +143,7 @@ int unistd_does_not_find_user_name_nullptr_case() |
138 | 143 |
generator); |
139 | 144 |
|
140 | 145 |
//when |
141 |
- auto returned = installer.install_token(); |
|
146 |
+ auto returned = installer.install_key(); |
|
142 | 147 |
|
143 | 148 |
//then |
144 | 149 |
check (test_tokens->captured_token == "", |
... | ... |
@@ -162,7 +167,7 @@ int unistd_does_not_find_user_name_empty_string_case() |
162 | 167 |
generator); |
163 | 168 |
|
164 | 169 |
//when |
165 |
- auto returned = installer.install_token(); |
|
170 |
+ auto returned = installer.install_key(); |
|
166 | 171 |
|
167 | 172 |
//then |
168 | 173 |
check (test_tokens->captured_token == "", |
... | ... |
@@ -185,7 +190,7 @@ int directory_finds_no_user_info() |
185 | 190 |
generator); |
186 | 191 |
|
187 | 192 |
//when |
188 |
- auto returned = installer.install_token(); |
|
193 |
+ auto returned = installer.install_key(); |
|
189 | 194 |
|
190 | 195 |
//then |
191 | 196 |
check (test_tokens->captured_token == "", "installed wrong token"); |
... | ... |
@@ -34,7 +34,41 @@ public: |
34 | 34 |
std::string token (const user &user) const override |
35 | 35 |
{ |
36 | 36 |
// Get key |
37 |
- const std::string file_path (user.home_directory() + "/.dual_control"); |
|
37 |
+ std::string line = read_key(user); |
|
38 |
+ if (line == "") { |
|
39 |
+ return ""; |
|
40 |
+ } |
|
41 |
+ |
|
42 |
+ base32 codec; |
|
43 |
+ std::vector<uint8_t> key = codec.decode(line); |
|
44 |
+ |
|
45 |
+ // TODO: generate the token |
|
46 |
+ return generator_.generate_token (std::string (key.begin(), key.end())); |
|
47 |
+ } |
|
48 |
+ |
|
49 |
+private: |
|
50 |
+ std::string get_key_path(const user &user) const { |
|
51 |
+ return user.home_directory() + "/.dual_control"; |
|
52 |
+ } |
|
53 |
+ |
|
54 |
+ bool key_exists (const user &user) const { |
|
55 |
+ // check if file exists |
|
56 |
+ std::string file_path = get_key_path(user); |
|
57 |
+ fstreams::pstream stream (fstreams_.open_fstream(file_path)); |
|
58 |
+ return stream->good(); |
|
59 |
+ } |
|
60 |
+ |
|
61 |
+ std::string generate_key() const { |
|
62 |
+ base32 codec; |
|
63 |
+ // get randomness |
|
64 |
+ std::vector<unsigned char> random_bytes (16); |
|
65 |
+ // base32encode it |
|
66 |
+ std::string key = codec.encode(random_bytes); |
|
67 |
+ return "QWERQWERQWERQWER"; |
|
68 |
+ } |
|
69 |
+ |
|
70 |
+ std::string read_key (const user &user) const { |
|
71 |
+ std::string file_path = get_key_path(user); |
|
38 | 72 |
fstreams::pstream stream (fstreams_.open_fstream (file_path)); |
39 | 73 |
|
40 | 74 |
// TODO: ignore newlines |
... | ... |
@@ -45,16 +79,23 @@ public: |
45 | 79 |
return ""; |
46 | 80 |
} |
47 | 81 |
|
48 |
- base32 codec; |
|
49 | 82 |
std::string line(line_v.begin(), line_v.end()); |
50 |
- std::vector<uint8_t> key = codec.decode(line); |
|
51 |
- |
|
52 |
- // TODO: generate the token |
|
53 |
- return generator_.generate_token (std::string (key.begin(), key.end())); |
|
83 |
+ return line; |
|
84 |
+ } |
|
85 |
+public: |
|
86 |
+ std::string ensure_key (const user &user) const override { |
|
87 |
+ if (!key_exists(user)) { |
|
88 |
+ std::string key = generate_key(); |
|
89 |
+ save(user, key); |
|
90 |
+ return key; |
|
91 |
+ } else { |
|
92 |
+ return read_key(user); |
|
93 |
+ } |
|
54 | 94 |
} |
95 |
+ |
|
55 | 96 |
void save (const user &user, const std::string &token) const override |
56 | 97 |
{ |
57 |
- std::string file_path (user.home_directory() + "/.dual_control"); |
|
98 |
+ std::string file_path = get_key_path(user); |
|
58 | 99 |
fstreams::postream stream (fstreams_.open_ofstream (file_path, |
59 | 100 |
std::ios_base::trunc)); |
60 | 101 |
*stream << token << std::endl; |
... | ... |
@@ -29,6 +29,7 @@ public: |
29 | 29 |
{ |
30 | 30 |
return ""; |
31 | 31 |
} |
32 |
+ virtual std::string ensure_key (const user &user) const { return ""; }; |
|
32 | 33 |
virtual void save (const user &user, const std::string &token) const {} |
33 | 34 |
}; |
34 | 35 |
|
... | ... |
@@ -43,6 +44,9 @@ public: |
43 | 44 |
delegate_ (delegate) {} |
44 | 45 |
tokens() : tokens ( |
45 | 46 |
delegate (new tokens_ifc)) {} |
47 |
+ std::string ensure_key (const user &user) const { |
|
48 |
+ return delegate_->ensure_key(user); |
|
49 |
+ } |
|
46 | 50 |
std::string token (const user &user) const |
47 | 51 |
{ |
48 | 52 |
return delegate_->token (user); |