@@ -42,13 +42,7 @@ class ClientCreds : public RC<thread_unsafe_refcount>
42
42
public:
43
43
typedef RCPtr<ClientCreds> Ptr;
44
44
45
- ClientCreds ()
46
- : allow_cache_password(false ),
47
- password_save_defined (false ),
48
- replace_password_with_session_id(false ),
49
- did_replace_password_with_session_id(false )
50
- {
51
- }
45
+ ClientCreds () = default ;
52
46
53
47
void set_username (const std::string &username_arg)
54
48
{
@@ -58,7 +52,10 @@ class ClientCreds : public RC<thread_unsafe_refcount>
58
52
void set_password (const std::string &password_arg)
59
53
{
60
54
password = password_arg;
61
- did_replace_password_with_session_id = false ;
55
+ if (!password.empty ())
56
+ {
57
+ password_needed_ = true ;
58
+ }
62
59
}
63
60
64
61
void set_http_proxy_username (const std::string &username)
@@ -74,6 +71,10 @@ class ClientCreds : public RC<thread_unsafe_refcount>
74
71
void set_response (const std::string &response_arg)
75
72
{
76
73
response = response_arg;
74
+ if (!response.empty ())
75
+ {
76
+ need_user_interaction_ = true ;
77
+ }
77
78
}
78
79
79
80
void set_dynamic_challenge_cookie (const std::string &cookie, const std::string &username)
@@ -82,51 +83,31 @@ class ClientCreds : public RC<thread_unsafe_refcount>
82
83
dynamic_challenge.reset (new ChallengeResponse (cookie, username));
83
84
}
84
85
85
- void set_replace_password_with_session_id (const bool value)
86
- {
87
- replace_password_with_session_id = value;
88
- }
89
-
90
- void enable_password_cache (const bool value)
91
- {
92
- allow_cache_password = value;
93
- }
94
-
95
- bool get_replace_password_with_session_id () const
96
- {
97
- return replace_password_with_session_id;
98
- }
99
-
100
86
void set_session_id (const std::string &user, const std::string &sess_id)
101
87
{
102
- // force Session ID use if dynamic challenge is enabled
103
- if (dynamic_challenge && !replace_password_with_session_id)
104
- replace_password_with_session_id = true ;
105
-
106
- if (replace_password_with_session_id)
88
+ if (dynamic_challenge)
107
89
{
108
- if (allow_cache_password && !password_save_defined)
109
- {
110
- password_save = password;
111
- password_save_defined = true ;
112
- }
113
- password = sess_id;
114
- response = " " ;
115
- if (dynamic_challenge)
116
- {
117
- username = dynamic_challenge->get_username ();
118
- dynamic_challenge.reset ();
119
- }
120
- else if (!user.empty ())
121
- username = user;
122
- did_replace_password_with_session_id = true ;
90
+ session_id_username = dynamic_challenge->get_username ();
91
+ // for dynamic challenge we use dynamic password only once
92
+ dynamic_challenge.reset ();
93
+ }
94
+ else if (!user.empty ())
95
+ {
96
+ session_id_username = user;
123
97
}
98
+
99
+ // response is used only once
100
+ response.clear ();
101
+
102
+ session_id = sess_id;
124
103
}
125
104
126
105
std::string get_username () const
127
106
{
128
107
if (dynamic_challenge)
129
108
return dynamic_challenge->get_username ();
109
+ else if (!session_id_username.empty ())
110
+ return session_id_username;
130
111
else
131
112
return username;
132
113
}
@@ -136,7 +117,12 @@ class ClientCreds : public RC<thread_unsafe_refcount>
136
117
if (dynamic_challenge)
137
118
return dynamic_challenge->construct_dynamic_password (response);
138
119
else if (response.empty ())
139
- return password;
120
+ {
121
+ if (!session_id.empty ())
122
+ return session_id;
123
+ else
124
+ return password;
125
+ }
140
126
else
141
127
return ChallengeResponse::construct_static_password (password, response);
142
128
}
@@ -173,34 +159,44 @@ class ClientCreds : public RC<thread_unsafe_refcount>
173
159
174
160
bool session_id_defined () const
175
161
{
176
- return did_replace_password_with_session_id ;
162
+ return !session_id. empty () ;
177
163
}
178
164
179
- // If we have a saved password that is not a session ID,
180
- // restore it and wipe any existing session ID.
181
- bool reset_to_cached_password ()
165
+ void purge_session_id ()
182
166
{
183
- if (password_save_defined)
184
- {
185
- password = password_save;
186
- password_save.clear ();
187
- password_save_defined = false ;
188
- did_replace_password_with_session_id = false ;
189
- return true ;
190
- }
191
- else
192
- return false ;
167
+ session_id.clear ();
168
+ session_id_username.clear ();
193
169
}
194
170
195
- void purge_session_id ()
171
+ void purge_user_pass ()
172
+ {
173
+ username.clear ();
174
+ password.clear ();
175
+ }
176
+
177
+ void save_username_for_session_id ()
196
178
{
197
- if (! reset_to_cached_password ())
179
+ if (session_id_username. empty ())
198
180
{
199
- password.clear ();
200
- did_replace_password_with_session_id = false ;
181
+ session_id_username = username;
201
182
}
202
183
}
203
184
185
+ void set_need_user_interaction ()
186
+ {
187
+ need_user_interaction_ = true ;
188
+ }
189
+
190
+ bool need_user_interaction () const
191
+ {
192
+ return need_user_interaction_;
193
+ }
194
+
195
+ bool password_needed () const
196
+ {
197
+ return password_needed_;
198
+ }
199
+
204
200
std::string auth_info () const
205
201
{
206
202
std::string ret;
@@ -210,20 +206,27 @@ class ClientCreds : public RC<thread_unsafe_refcount>
210
206
}
211
207
else if (response.empty ())
212
208
{
213
- if (!username.empty ())
209
+ if (!session_id_username.empty () || !username.empty ())
210
+ {
214
211
ret += " Username" ;
212
+ }
215
213
else
214
+ {
216
215
ret += " UsernameEmpty" ;
216
+ }
217
217
ret += ' /' ;
218
- if (!password.empty ())
218
+ if (!session_id.empty ())
219
+ {
220
+ ret += " SessionID" ;
221
+ }
222
+ else if (!password.empty ())
219
223
{
220
- if (did_replace_password_with_session_id)
221
- ret += " SessionID" ;
222
- else
223
- ret += " Password" ;
224
+ ret += " Password" ;
224
225
}
225
226
else
227
+ {
226
228
ret += " PasswordEmpty" ;
229
+ }
227
230
}
228
231
else
229
232
{
@@ -241,23 +244,20 @@ class ClientCreds : public RC<thread_unsafe_refcount>
241
244
std::string http_proxy_user;
242
245
std::string http_proxy_pass;
243
246
244
- // Password caching
245
- bool allow_cache_password;
246
- bool password_save_defined;
247
- std::string password_save;
247
+ std::string session_id;
248
+ std::string session_id_username;
248
249
249
- // Response to challenge
250
+ // Response to a challenge
250
251
std::string response;
251
252
252
- // Info describing a dynamic challenge
253
- ChallengeResponse::Ptr dynamic_challenge ;
253
+ // Need user interaction to authenticate - such as static/ dynamic challenge or SAML
254
+ bool need_user_interaction_ = false ;
254
255
255
- // If true, on successful connect, we will replace the password
256
- // with the session ID we receive from the server.
257
- bool replace_password_with_session_id;
256
+ // Non-empty password provided
257
+ bool password_needed_ = false ;
258
258
259
- // true if password has been replaced with Session ID
260
- bool did_replace_password_with_session_id ;
259
+ // Info describing a dynamic challenge
260
+ ChallengeResponse::Ptr dynamic_challenge ;
261
261
};
262
262
263
263
} // namespace openvpn
0 commit comments