git.fiddlerwoaroof.com
Browse code

Add generator to token, refactor reference->value

Ed Langley authored on 08/06/2017 23:10:26
Showing 10 changed files
... ...
@@ -17,19 +17,20 @@
17 17
 #include <memory>
18 18
 #include <vector>
19 19
 
20
-#include "request.h"
20
+#include "conversation.h"
21 21
 #include "dual_control.h"
22
-#include "validator.h"
22
+#include "generator.h"
23 23
 #include "logger.h"
24
-#include "conversation.h"
25
-#include "user.h"
26
-#include "token.h"
27
-#include "sys_pwd.h"
28
-#include "sys_unistd.h"
24
+#include "request.h"
25
+#include "session.h"
29 26
 #include "sys_fstream.h"
30 27
 #include "sys_pam.h"
28
+#include "sys_pwd.h"
31 29
 #include "sys_syslog.h"
32
-#include "session.h"
30
+#include "sys_unistd.h"
31
+#include "token.h"
32
+#include "user.h"
33
+#include "validator.h"
33 34
 
34 35
 namespace
35 36
 {
... ...
@@ -40,7 +41,10 @@ dual_control initialize()
40 41
     unistd unistd (unistd::create());
41 42
     directory directory (directory::create (unistd, pwd));
42 43
     fstreams fstreams (fstreams::create());
43
-    tokens tokens (tokens::create (fstreams));
44
+    sys_time time (sys_time::get());
45
+    int code_digits = 6;
46
+    totp_generator generator = totp_generator (time, "\x00", code_digits);
47
+    tokens tokens (tokens::create (fstreams, generator));
44 48
     validator validator (validator::create (directory, tokens));
45 49
     pam pam (pam::create());
46 50
     conversation conversation (conversation::create (pam));
... ...
@@ -67,4 +71,3 @@ PAM_EXTERN int pam_sm_setcred (pam_handle_t *pamh, int flags, int argc,
67 71
 {
68 72
     return dc.setcred (pam_request ( pamh, flags, argc, argv));
69 73
 }
70
-
... ...
@@ -35,17 +35,15 @@ class system init_system()
35 35
 installer init_installer()
36 36
 {
37 37
     fstreams fstreams (fstreams::create());
38
-    tokens tokens (tokens::create (fstreams));
39 38
     pwd pwd (pwd::create());
40 39
     unistd unistd (unistd::create());
41 40
     directory directory (directory::create (unistd, pwd));
42 41
     stdlib stdlib (stdlib::get());
43 42
     sys_time time (sys_time::get());
44 43
     int code_digits = 6;
45
-    auto the_generator = std::make_shared<totp_generator> (time, "\x00",
46
-                         code_digits);
47
-    installer installer (installer::create (tokens, unistd, directory,
48
-                                            the_generator));
44
+    totp_generator generator = totp_generator (time, "\x00", code_digits);
45
+    tokens tokens (tokens::create (fstreams, generator));
46
+    installer installer (installer::create (tokens, unistd, directory, generator));
49 47
 
50 48
     return installer;
51 49
 }
... ...
@@ -59,4 +57,3 @@ int main (int argc, char *argv[])
59 57
     std::cout << generated_token << std::endl;
60 58
     return 0;
61 59
 }
62
-
... ...
@@ -27,11 +27,11 @@ private:
27 27
     tokens tokens_;
28 28
     unistd unistd_;
29 29
     directory directory_;
30
-    std::shared_ptr<totp_generator> generator_;
30
+    totp_generator generator_;
31 31
 public:
32 32
     impl (const tokens &tokens, const unistd &unistd,
33 33
           const directory &directory,
34
-          const std::shared_ptr<totp_generator> generator) :
34
+          const totp_generator generator) :
35 35
         tokens_ (tokens), unistd_ (unistd), directory_ (directory),
36 36
         generator_ (generator) {}
37 37
     std::string install_token() const override
... ...
@@ -51,7 +51,7 @@ public:
51 51
         }
52 52
 
53 53
         user user (found_user[0]);
54
-        std::string token (generator_->generate_token());
54
+        std::string token (generator_.generate_token());
55 55
         tokens_.save (user, token);
56 56
         return token;
57 57
     }
... ...
@@ -60,9 +60,7 @@ public:
60 60
 }
61 61
 
62 62
 installer installer::create (const tokens &tokens, const unistd &unistd,
63
-                             const directory &directory, const std::shared_ptr<totp_generator> generator)
63
+                             const directory &directory, const totp_generator &generator)
64 64
 {
65
-    return installer (std::make_shared<impl> (tokens, unistd, directory,
66
-                      generator));
65
+    return installer (std::make_shared<impl> (tokens, unistd, directory, generator));
67 66
 }
68
-
... ...
@@ -45,8 +45,7 @@ public:
45 45
     }
46 46
     static installer create (const tokens &tokens, const unistd &unistd,
47 47
                              const directory &directory,
48
-                             const std::shared_ptr<totp_generator> generator);
48
+                             const totp_generator &generator);
49 49
 };
50 50
 
51 51
 #endif
52
-
... ...
@@ -52,5 +52,6 @@ public:
52 52
     static unistd create();
53 53
 };
54 54
 
55
-#endif
55
+template class std::shared_ptr<unistd_ifc>;
56 56
 
57
+#endif
... ...
@@ -26,21 +26,27 @@ class tokens_impl : public tokens_ifc
26 26
 {
27 27
 private:
28 28
     fstreams fstreams_;
29
+    totp_generator generator_;
29 30
 public:
30
-    tokens_impl (const fstreams &fstreams) :
31
-        fstreams_ (fstreams) {}
31
+    tokens_impl (const fstreams &fstreams, const totp_generator generator) :
32
+        fstreams_ (fstreams), generator_(generator) {}
32 33
     std::string token (const user &user) const override
33 34
     {
34
-        const std::string file_path (user.home_directory() + "/.dual_control");
35
-        std::vector<char> line (7);
35
+        // return generator_.generate_token();
36 36
 
37
+        // Get key
38
+        const std::string file_path (user.home_directory() + "/.dual_control");
37 39
         fstreams::pstream stream (fstreams_.open_fstream (file_path));
38 40
 
39 41
         if (!stream->good()) {
40 42
             return "";
41 43
         }
42 44
 
45
+        // TODO: decode key
46
+        std::vector<char> line (32);
43 47
         stream->getline (line.data(), line.size());
48
+
49
+        // TODO: generate the token
44 50
         return std::string (line.data());
45 51
     }
46 52
     void save (const user &user, const std::string &token) const override
... ...
@@ -52,9 +58,9 @@ public:
52 58
     }
53 59
 };
54 60
 }
55
-tokens tokens::create (const fstreams &fstreams)
61
+
62
+tokens tokens::create (const fstreams &fstreams, const totp_generator &generator)
56 63
 {
57 64
     return tokens (tokens::delegate
58
-                   (new tokens_impl (fstreams)));
65
+                   (new tokens_impl (fstreams, generator)));
59 66
 }
60
-
... ...
@@ -18,6 +18,7 @@
18 18
 
19 19
 #include "user.h"
20 20
 #include "sys_fstream.h"
21
+#include "generator.h"
21 22
 
22 23
 class tokens_ifc
23 24
 {
... ...
@@ -50,8 +51,7 @@ public:
50 51
     {
51 52
         return delegate_->save (user, token);
52 53
     }
53
-    static tokens create (const fstreams &fstreams);
54
+    static tokens create (const fstreams &fstreams, const totp_generator &generator);
54 55
 };
55 56
 
56 57
 #endif
57
-
... ...
@@ -21,6 +21,7 @@
21 21
 #include "test_util.h"
22 22
 #include "user.h"
23 23
 #include "sys_fstream.h"
24
+#include "generator.h"
24 25
 
25 26
 class fake_user : public user_ifc
26 27
 {
... ...
@@ -82,6 +83,16 @@ public:
82 83
     }
83 84
 };
84 85
 
86
+class fake_totp_generator : public token_generator_ifc
87
+{
88
+private:
89
+    std::string expected_token = "<unspecified>";
90
+public:
91
+    std::string generate_token () const override {
92
+        return expected_token;
93
+    }
94
+};
95
+
85 96
 int reads_from_the_right_file ()
86 97
 {
87 98
     //given
... ...
@@ -91,11 +102,11 @@ int reads_from_the_right_file ()
91 102
     std::string token ("123456");
92 103
     fstreams test_streams (fstreams::delegate (new fake_fstreams (token_file,
93 104
                            token)));
105
+    totp_generator generator (totp_generator::delegate (new fake_totp_generator ()));
94 106
 
95 107
     //file_reader test_file_reader (file_reader::delegate (new fake_file_reader));
96 108
     user test_user (user::delegate (new fake_user (home_directory)));
97
-    tokens supplier (tokens::create (
98
-                         test_streams));
109
+    tokens supplier (tokens::create (test_streams, generator));
99 110
 
100 111
     //when
101 112
     std::string actual = supplier.token (test_user);
... ...
@@ -113,9 +124,10 @@ int returns_empty_string_if_file_open_fail()
113 124
     std::string token_file = home_directory + "/.not_dual_control";
114 125
     fstreams test_streams (fstreams::delegate (new fake_fstreams (token_file,
115 126
                            "654321")));
127
+    totp_generator generator (totp_generator::delegate (new fake_totp_generator ()));
128
+
116 129
     user test_user (user::delegate (new fake_user (home_directory)));
117
-    tokens supplier (tokens::create (
118
-                         test_streams));
130
+    tokens supplier (tokens::create (test_streams, generator));
119 131
 
120 132
     //when
121 133
     std::string actual = supplier.token (test_user);
... ...
@@ -132,8 +144,9 @@ int writes_the_token ()
132 144
     user test_user (user::delegate (new fake_user (home_directory)));
133 145
     mock_write_fstreams *mockfs (new mock_write_fstreams);
134 146
     fstreams test_streams{fstreams::delegate (mockfs)};
147
+    totp_generator generator (totp_generator::delegate (new fake_totp_generator ()));
135 148
     std::string token ("token");
136
-    tokens tokens (tokens::create (test_streams));
149
+    tokens tokens (tokens::create (test_streams, generator));
137 150
 
138 151
     //when
139 152
     tokens.save (test_user, token);
... ...
@@ -162,4 +175,3 @@ int main (int argc, char *argv[])
162 175
 {
163 176
     return !run_tests();
164 177
 }
165
-
... ...
@@ -61,4 +61,3 @@ validator validator::create (const directory &directory,
61 61
             tokens));
62 62
     return validator (delegate);
63 63
 }
64
-
... ...
@@ -17,6 +17,7 @@
17 17
 
18 18
 #include "user.h"
19 19
 #include "token.h"
20
+#include "generator.h"
20 21
 
21 22
 class validator_ifc
22 23
 {
... ...
@@ -53,4 +54,3 @@ public:
53 54
 };
54 55
 
55 56
 #endif
56
-