git.fiddlerwoaroof.com
Browse code

refactor conversation

Greg Wiley authored on 19/04/2017 22:05:36
Showing 7 changed files
... ...
@@ -4,8 +4,8 @@
4 4
 #include <string>
5 5
 
6 6
 #include "conversation.h"
7
-#include "pam.h"
8 7
 
8
+/*
9 9
 pam_token_conversation::pam_token_conversation (pam_handle_t *pamh,
10 10
         const pam_p pam)
11 11
 {
... ...
@@ -63,4 +63,5 @@ std::string pam_token_conversation::user_name()
63 63
 {
64 64
     return user_;
65 65
 }
66
+*/
66 67
 
... ...
@@ -1,55 +1,44 @@
1 1
 #ifndef _CONVERSATION_H
2 2
 #define _CONVERSATION_H
3 3
 
4
-#include <security/pam_modules.h>
5 4
 #include <string>
6 5
 #include <memory>
7 6
 
8
-#include "pam.h"
7
+#include "request.h"
9 8
 
10
-class conversation_result
11
-{
12
-private:
13
-    std::string token_;
14
-    std::string user_name_;
15
-public:
16
-    conversation_result (std::string user_name, std::string token)
17
-        : token_ (token),
18
-          user_name_ (user_name) {}
19
-    std::string token()
20
-    {
21
-        return token_;
22
-    }
23
-    std::string user_name()
24
-    {
25
-        return user_name_;
26
-    }
9
+struct conversation_result {
10
+    std::string user_name;
11
+    std::string token;
27 12
 };
28 13
 
29
-class conversations_ifc
14
+class conversation_ifc
30 15
 {
31 16
 public:
32
-    virtual ~conversations_ifc() {}
33
-    virtual conversation_result initiate_conversation()
17
+    virtual conversation_result initiate (const pam_request &request)
34 18
     {
35
-        return conversation_result ("","");
19
+        return { "", "" };
36 20
     }
37 21
 };
38 22
 
39
-class conversations : public conversations_ifc
23
+class conversation: public conversation_ifc
40 24
 {
41 25
 private:
42
-    std::shared_ptr<conversations_ifc> delegate_;
26
+    std::shared_ptr<conversation_ifc> delegate_;
43 27
 public:
44
-    conversations() : conversations (std::shared_ptr<conversations_ifc>
45
-                                         (new conversations_ifc)) {}
46
-    conversations (const std::shared_ptr<conversations_ifc> &delegate) :
28
+    conversation (const std::shared_ptr<conversation_ifc> &delegate) :
47 29
         delegate_ (delegate) {}
48
-    conversation_result initiate_conversation()
30
+    conversation() : conversation (std::shared_ptr<conversation_ifc>
31
+                                       (new conversation_ifc)) {}
32
+    conversation_result initiate (const pam_request &request)
49 33
     {
50
-        return delegate_->initiate_conversation();
34
+        return delegate_->initiate (request);
51 35
     }
52 36
 };
53 37
 
38
+inline conversation wrap (conversation_ifc *delegate)
39
+{
40
+    return conversation (std::shared_ptr<conversation_ifc> (delegate));
41
+};
42
+
54 43
 #endif
55 44
 
... ...
@@ -20,7 +20,7 @@ int dual_control_ifc::setcred ( const pam_request &request)
20 20
 class impl : public dual_control_ifc
21 21
 {
22 22
 private:
23
-    conversations conversations_;
23
+    conversation conversation_;
24 24
     validator validator_;
25 25
     logger logger_;
26 26
 public:
... ...
@@ -30,7 +30,7 @@ public:
30 30
 };
31 31
 
32 32
 impl::impl (const dual_control_configuration &configuration) :
33
-    conversations_ (configuration.conversations),
33
+    conversation_ (configuration.conversation),
34 34
     validator_ (configuration.validator),
35 35
     logger_ (configuration.logger) {}
36 36
 
... ...
@@ -41,15 +41,12 @@ int impl::setcred (const pam_request &request)
41 41
 
42 42
 int impl::authenticate (const pam_request &request)
43 43
 {
44
+    conversation_result input (conversation_.initiate (request));
44 45
 
45
-    conversation_result conversation = conversations_.initiate_conversation();
46
-    std::string user_name = conversation.user_name();
47
-    std::string token = conversation.token();
46
+    int auth_result = validator_.validate (input.user_name,
47
+                                           input.token) ? PAM_SUCCESS : PAM_AUTH_ERR;
48 48
 
49
-    int auth_result = validator_.validate (user_name,
50
-                                           token) ? PAM_SUCCESS : PAM_AUTH_ERR;
51
-
52
-    logger_.log (auth_result, user_name, token);
49
+    logger_.log (auth_result, input.user_name, input.token);
53 50
     return auth_result;
54 51
 }
55 52
 
... ...
@@ -16,7 +16,7 @@
16 16
 
17 17
 struct dual_control_configuration {
18 18
     validator validator;
19
-    conversations conversations;
19
+    conversation conversation;
20 20
     logger logger;
21 21
 };
22 22
 
... ...
@@ -20,10 +20,10 @@ void use_validator (dual_control_configuration &config,
20 20
     config.validator = validator (share (value));
21 21
 }
22 22
 
23
-void use_conversations (dual_control_configuration &config,
24
-                        conversations_ifc *value)
23
+void use_conversation (dual_control_configuration &config,
24
+                       conversation_ifc *value)
25 25
 {
26
-    config.conversations = conversations (share (value));
26
+    config.conversation = conversation (share (value));
27 27
 }
28 28
 
29 29
 void use_logger (dual_control_configuration &config, logger_ifc *value)
... ...
@@ -59,17 +59,17 @@ public:
59 59
     }
60 60
 };
61 61
 
62
-class fake_conversations : public conversations_ifc
62
+class fake_conversation : public conversation_ifc
63 63
 {
64 64
 private:
65 65
     std::string user_name_;
66 66
     std::string token_;
67 67
 public:
68
-    fake_conversations (const std::string &user_name,
69
-                        const std::string &token) : user_name_ (user_name), token_ (token) {}
70
-    conversation_result initiate_conversation()
68
+    fake_conversation (const std::string &user_name,
69
+                       const std::string &token) : user_name_ (user_name), token_ (token) {}
70
+    conversation_result initiate (const pam_request &request)
71 71
     {
72
-        return conversation_result (user_name_, token_);
72
+        return {user_name_, token_};
73 73
     }
74 74
 };
75 75
 
... ...
@@ -114,7 +114,7 @@ int authenticate_validates_with_received_token()
114 114
     std::string user ("user");
115 115
     std::string token ("token");
116 116
     use_validator (configuration, new fake_validator (user, token));
117
-    use_conversations (configuration, new fake_conversations (user, token));
117
+    use_conversation (configuration, new fake_conversation (user, token));
118 118
     dual_control dc (create_dual_control (configuration));
119 119
     pam_handle_t *handle (0);
120 120
     std::vector<const std::string> arguments;
... ...
@@ -133,8 +133,8 @@ int authenticate_fails_with_wrong_user()
133 133
     dual_control_configuration configuration;
134 134
     std::string token ("token");
135 135
     use_validator (configuration, new fake_validator ("user", token));
136
-    use_conversations (configuration, new fake_conversations ("wrong user",
137
-                       token));
136
+    use_conversation (configuration, new fake_conversation ("wrong user",
137
+                      token));
138 138
     dual_control dc (create_dual_control (configuration));
139 139
 
140 140
     // when
... ...
@@ -151,8 +151,8 @@ int authenticate_fails_with_wrong_token()
151 151
     dual_control_configuration configuration;
152 152
     std::string user ("user");
153 153
     use_validator (configuration, new fake_validator (user, "token"));
154
-    use_conversations (configuration, new fake_conversations (user,
155
-                       "wrong token"));
154
+    use_conversation (configuration, new fake_conversation (user,
155
+                      "wrong token"));
156 156
     dual_control dc (create_dual_control (configuration));
157 157
 
158 158
     // when
... ...
@@ -170,7 +170,7 @@ int logs_authentication()
170 170
     std::string user ("user");
171 171
     std::string token ("token");
172 172
     use_validator (configuration, new fake_validator (user, token));
173
-    use_conversations (configuration, new fake_conversations (user, token));
173
+    use_conversation (configuration, new fake_conversation (user, token));
174 174
     mock_logger *test_logger;
175 175
     use_logger (configuration, test_logger = new mock_logger);
176 176
     dual_control dc (create_dual_control (configuration));
... ...
@@ -196,7 +196,7 @@ int logs_authentication_failure()
196 196
     std::string token ("token");
197 197
     use_validator (configuration, new fake_validator (user,
198 198
                    "not the received token"));
199
-    use_conversations (configuration, new fake_conversations (user, token));
199
+    use_conversation (configuration, new fake_conversation (user, token));
200 200
     mock_logger *test_logger;
201 201
     use_logger (configuration, test_logger = new mock_logger);
202 202
     dual_control dc (create_dual_control (configuration));
... ...
@@ -1,11 +1,17 @@
1
-#include <string>
2 1
 #include <vector>
3
-#include <memory>
2
+#include <tuple>
4 3
 #include <security/pam_modules.h>
5 4
 #include <security/pam_appl.h>
6 5
 
7 6
 #include "pam.h"
8 7
 
8
+std::tuple<int,std::vector<pam_response>> pam_ifc::conv (pam_handle *handle,
9
+                                       const std::vector<pam_message> &prompts)
10
+{
11
+    return std::make_tuple (PAM_SERVICE_ERR, std::vector<pam_response>());
12
+}
13
+
14
+/*
9 15
 class pam_conversation_impl : public pam_conversation
10 16
 {
11 17
 private:
... ...
@@ -45,4 +51,5 @@ pam_p get_system_pam()
45 51
 {
46 52
     return (pam_p)new pam_impl;
47 53
 }
54
+*/
48 55
 
... ...
@@ -3,25 +3,33 @@
3 3
 #include <string>
4 4
 #include <vector>
5 5
 #include <memory>
6
+#include <tuple>
6 7
 #include <security/pam_modules.h>
7 8
 
8
-class pam_conversation
9
+class pam_ifc
9 10
 {
10 11
 public:
11
-    virtual int conv (const std::vector<const struct pam_message *> &prompts,
12
-                      std::vector<struct pam_response *> &answers) = 0;
12
+    virtual std::tuple<int,std::vector<pam_response>> conv (pam_handle *handle,
13
+            const std::vector<pam_message> &prompts);
14
+
13 15
 };
14
-typedef std::shared_ptr<pam_conversation> pam_conversation_p;
15 16
 
16
-class pam
17
+class pam : public pam_ifc
17 18
 {
19
+    typedef std::shared_ptr<pam_ifc> delegate;
20
+private:
21
+    delegate delegate_;
18 22
 public:
19
-    virtual int get_conversation (pam_handle_t *pamh,
20
-                                  std::shared_ptr<pam_conversation> &conversation) = 0;
23
+    pam (const delegate &delegate) : delegate_ (delegate) {}
24
+    pam() : pam (delegate (new pam_ifc)) {}
25
+    std::tuple<int,std::vector<pam_response>> conv (pam_handle *handle,
26
+                                           const std::vector<pam_message> &prompts)
27
+    {
28
+        return delegate_-> conv (handle, prompts);
29
+    }
21 30
 };
22
-typedef std::shared_ptr<pam> pam_p;
23 31
 
24
-pam_p get_system_pam();
32
+pam system_pam();
25 33
 
26 34
 #endif
27 35