git.fiddlerwoaroof.com
Browse code

Finish OTP in dual control tool

Ed Langley authored on 27/05/2017 21:47:18
Showing 4 changed files
... ...
@@ -42,10 +42,10 @@ installer init_installer()
42 42
     stdlib stdlib (stdlib::get());
43 43
     sys_time timer (sys_time::get());
44 44
     int code_digits = 6;
45
-    std::shared_ptr<totp_generator> totp_generator =
45
+    auto the_generator =
46 46
         std::make_shared<totp_generator> (timer, "\x00", code_digits);
47
-    generator generator = std::bind (&TOTPGenerator::generate_token,
48
-                                     totp_generator);
47
+    generator generator = std::bind (&totp_generator::generate_token,
48
+                                     the_generator);
49 49
     installer installer (installer::create (tokens, unistd, directory,
50 50
                                             generator));
51 51
 
... ...
@@ -50,19 +50,7 @@ time_t time_step (const time_t time, const int step)
50 50
 class impl : public token_generator_ifc
51 51
 {
52 52
 private:
53
-    const sys_time &sys_time;
54
-    unsigned int code_digits;
55
-    const std::shared_ptr<CryptoPP::SecByteBlock> key;
56
-
57
-    unsigned long truncate (const std::string &mac) const;
58
-
59
-    unsigned long hotp (const CryptoPP::SecByteBlock &key,
60
-                        const CryptoPP::Integer &counter) const;
61
-
62
-    // TODO: move elsewhere
63
-    CryptoPP::SecByteBlock generate_key (unsigned int size) const;
64
-
65
-    unsigned long totp_generator::truncate (const std::string &mac) const
53
+    unsigned long truncate (const std::string &mac) const
66 54
     {
67 55
         uint8_t offset = static_cast<uint8_t > (mac[19]) & static_cast<uint8_t>
68 56
                          (0x0f);
... ...
@@ -70,8 +58,8 @@ private:
70 58
         return bytesToInt (offsetBytes) & 0x7fffffff;
71 59
     }
72 60
 
73
-    unsigned long totp_generator::hotp (const CryptoPP::SecByteBlock &key,
74
-                                        const CryptoPP::Integer &counter) const
61
+    unsigned long hotp (const CryptoPP::SecByteBlock &key,
62
+                        const CryptoPP::Integer &counter) const
75 63
     {
76 64
         std::string mac;
77 65
 
... ...
@@ -93,7 +81,7 @@ private:
93 81
         return result;
94 82
     }
95 83
 
96
-    CryptoPP::SecByteBlock totp_generator::generate_key (unsigned int size)
84
+    CryptoPP::SecByteBlock generate_key (unsigned int size)
97 85
     const
98 86
     {
99 87
         CryptoPP::AutoSeededRandomPool prng;
... ...
@@ -103,7 +91,18 @@ private:
103 91
         return key;
104 92
     }
105 93
 
94
+    const sys_time &sys_time;
95
+    unsigned int code_digits;
96
+    const std::shared_ptr<CryptoPP::SecByteBlock> key;
106 97
 public:
98
+    impl (const class sys_time &sys_time,
99
+          const std::string &key_c,
100
+          const int code_digits) :
101
+        sys_time (sys_time), code_digits (code_digits),
102
+        key (std::make_shared<CryptoPP::SecByteBlock> (CryptoPP::SecByteBlock (
103
+                    reinterpret_cast<const unsigned char *> (key_c.c_str()), key_c.size())))
104
+    {}
105
+
107 106
     std::string generate_token () const override
108 107
     {
109 108
         time_t foo = 111;
... ...
@@ -121,6 +120,14 @@ public:
121 120
 };
122 121
 
123 122
 // Generator goes here....
124
-std::string totp_generator::generate_token () const
125
-;
123
+
124
+totp_generator::totp_generator (
125
+    const class sys_time
126
+    &sys_time,
127
+    const std::string &key_c,
128
+    const int code_digits) :
129
+    delegate_ (std::make_shared<impl> (sys_time,
130
+                                       key_c,
131
+                                       code_digits))
132
+{}
126 133
 
... ...
@@ -35,21 +35,28 @@ time_t time_step (const time_t time, const int step);
35 35
 class token_generator_ifc
36 36
 {
37 37
 public:
38
-    virtual std::string generate_token () const;
38
+    virtual std::string generate_token () const
39
+    {
40
+        return "<dummy string>";
41
+    }
39 42
 };
40 43
 
41
-class totp_generator : public token_generator
44
+class totp_generator
42 45
 {
43 46
 public:
47
+    using delegate = std::shared_ptr<token_generator_ifc>;
48
+private:
49
+    delegate delegate_;
50
+
51
+public:
52
+    std::string generate_token () const
53
+    {
54
+        return delegate_->generate_token();
55
+    };
56
+
44 57
     totp_generator (const class sys_time &sys_time,
45 58
                     const std::string &key_c,
46
-                    const int code_digits) :
47
-        sys_time (sys_time), code_digits (code_digits),
48
-        key (std::make_shared<CryptoPP::SecByteBlock> (CryptoPP::SecByteBlock (
49
-                    reinterpret_cast<const unsigned char *> (key_c.c_str()), key_c.size())))
50
-    {};
51
-
52
-    std::string generate_token () const;
59
+                    const int code_digits);
53 60
 };
54 61
 
55 62
 #endif
... ...
@@ -196,3 +196,4 @@ int main (int argc, char *argv[])
196 196
 {
197 197
     return !run_tests();
198 198
 }
199
+