git.fiddlerwoaroof.com
Browse code

CJPM-5223: Replace strings vectors, TOTP generator

Ed Langley authored on 14/06/2017 19:04:50
Showing 7 changed files
... ...
@@ -18,8 +18,6 @@
18 18
 #include "base32.h"
19 19
 namespace
20 20
 {
21
-using octet_vector = std::vector<uint8_t>;
22
-
23 21
 class hmac_failed_exception : public std::exception
24 22
 {};
25 23
 
... ...
@@ -119,16 +117,7 @@ public:
119 117
         clock (clock), code_digits (code_digits)
120 118
     {}
121 119
 
122
-    std::string generate_token (const std::string &key_string) const override
123
-    {
124
-        // std::cout << "KEY SIZE: " << key_string.size() << std::endl;
125
-        // std::vector<uint8_t> key = codec.decode(key_string);
126
-        // std::cout << "LENGTH: " << key.size() << " '" << key_string << "'" << std::endl;
127
-        std::vector<uint8_t> key (key_string.begin(), key_string.end());
128
-        return generate_token (key);
129
-    }
130
-
131
-    std::string generate_token (const std::vector<uint8_t> key) const
120
+    std::string generate_token (const octet_vector &key) const override
132 121
     {
133 122
         // Assuming time is > 0, integer division produces the result we want.
134 123
         const time_t &time_chunk = clock.time (nullptr) / 30;
... ...
@@ -25,11 +25,12 @@
25 25
 
26 26
 int ipow (int base, int exp);
27 27
 time_t time_step (const time_t time, const int step);
28
+using octet_vector = std::vector<uint8_t>;
28 29
 
29 30
 class token_generator_ifc
30 31
 {
31 32
 public:
32
-    virtual std::string generate_token (const std::string &key) const = 0;
33
+    virtual std::string generate_token (const octet_vector &key) const = 0;
33 34
 };
34 35
 
35 36
 class totp_generator
... ...
@@ -41,7 +42,7 @@ private:
41 42
     delegate delegate_;
42 43
 
43 44
 public:
44
-    std::string generate_token (const std::string &key) const
45
+    std::string generate_token (const octet_vector &key) const
45 46
     {
46 47
         return delegate_->generate_token (key);
47 48
     }
... ...
@@ -55,4 +56,3 @@ public:
55 56
 };
56 57
 
57 58
 #endif
58
-
... ...
@@ -81,7 +81,7 @@ int given_digits()
81 81
     auto generator = totp_generator (stdtime, 6);
82 82
 
83 83
     // when
84
-    std::string key { 0 };
84
+    octet_vector key { 0 };
85 85
     auto actual = generator.generate_token (key);
86 86
 
87 87
     // then
... ...
@@ -102,7 +102,7 @@ int modulated_source_modulates_tokens()
102 102
     auto generator = totp_generator (stdtime, 6);
103 103
 
104 104
     // when
105
-    std::string key ("\x00", 1);
105
+    octet_vector key { 0 };
106 106
     auto actual1 = generator.generate_token (key);
107 107
     auto actual2 = generator.generate_token (key);
108 108
 
... ...
@@ -119,7 +119,7 @@ int int_max()
119 119
 
120 120
     sys_time stdtime (test_stdtime);
121 121
     auto generator = totp_generator (stdtime, 6);
122
-    std::string key ("\x00", 1);
122
+    octet_vector key { 0 };
123 123
 
124 124
     // when
125 125
     auto actual = generator.generate_token (key);
... ...
@@ -137,7 +137,7 @@ int int_min()
137 137
     // given
138 138
     std::initializer_list<time_t> samples { INT_MIN };
139 139
     auto test_stdtime = std::make_shared<fake_time> (samples);
140
-    std::string key ("\x00", 1);
140
+    octet_vector key { 0 };
141 141
 
142 142
     sys_time stdtime (test_stdtime);
143 143
     auto generator = totp_generator (stdtime, 6);
... ...
@@ -164,7 +164,7 @@ int int_precomputed()
164 164
 
165 165
     sys_time stdtime (test_stdtime);
166 166
     // Fake the Key
167
-    std::string key = "\xff\x91\xebO\x04\xa4\xda$\xd2$a\x95Vs\xaf`";
167
+    octet_vector key {255, 145, 235, 79, 4, 164, 218, 36, 210, 36, 97, 149, 86, 115, 175, 96};
168 168
     auto generator = totp_generator (stdtime, 6);
169 169
     std::string expected = "258675";
170 170
 
... ...
@@ -191,4 +191,3 @@ int main (int argc, char *argv[])
191 191
 {
192 192
     return !run_tests();
193 193
 }
194
-
... ...
@@ -49,8 +49,7 @@ public:
49 49
         std::string key_string = tokens_.ensure_key (user);
50 50
         // TODO: use vectors in generator input instead of strings to avoid this nonsense
51 51
         std::vector<uint8_t> key = base32().decode (key_string);
52
-        std::string decoded_key (key.begin(), key.end());
53
-        std::string token = generator_.generate_token (decoded_key);
52
+        std::string token = generator_.generate_token (key);
54 53
 
55 54
         return {key_string, token};
56 55
     }
... ...
@@ -64,4 +63,3 @@ installer installer::create (const tokens &tokens, const unistd &unistd,
64 63
     return installer (std::make_shared<impl> (tokens, unistd, directory,
65 64
                       generator));
66 65
 }
67
-
... ...
@@ -79,7 +79,7 @@ public:
79 79
         expected_token (expected_token)
80 80
     {}
81 81
 
82
-    std::string generate_token (const std::string &key) const override
82
+    std::string generate_token (const octet_vector &key) const override
83 83
     {
84 84
         return expected_token;
85 85
     }
... ...
@@ -46,7 +46,7 @@ public:
46 46
         base32 codec;
47 47
         std::vector<uint8_t> key = codec.decode (line);
48 48
 
49
-        return generator_.generate_token (std::string (key.begin(), key.end()));
49
+        return generator_.generate_token (key);
50 50
     }
51 51
 
52 52
 private:
... ...
@@ -118,4 +118,3 @@ tokens tokens::create (const fstreams &fstreams,
118 118
     return tokens (tokens::delegate
119 119
                    (new tokens_impl (fstreams, generator, rand)));
120 120
 }
121
-
... ...
@@ -128,7 +128,7 @@ public:
128 128
     fake_totp_generator (std::string expected_token = "<unspecified>") :
129 129
         expected_token (expected_token)
130 130
     {}
131
-    std::string generate_token (const std::string &key) const override
131
+    std::string generate_token (const octet_vector &key) const override
132 132
     {
133 133
         return expected_token;
134 134
     }
... ...
@@ -333,4 +333,3 @@ int main (int argc, char *argv[])
333 333
 {
334 334
     return !run_tests();
335 335
 }
336
-