OpenVPN 3 Core Library
Loading...
Searching...
No Matches
clicreds.hpp
Go to the documentation of this file.
1// OpenVPN -- An application to securely tunnel IP networks
2// over a single port, with support for SSL/TLS-based
3// session authentication and key exchange,
4// packet encryption, packet authentication, and
5// packet compression.
6//
7// Copyright (C) 2012- OpenVPN Inc.
8//
9// SPDX-License-Identifier: MPL-2.0 OR AGPL-3.0-only WITH openvpn3-openssl-exception
10//
11
12// This class encapsulates the state of authentication credentials
13// maintained by an OpenVPN client. It understands dynamic
14// challenge/response cookies, and Session Token IDs (where the
15// password in the object is wiped and replaced by a token used
16// for further authentications).
17
18#ifndef OPENVPN_CLIENT_CLICREDS_H
19#define OPENVPN_CLIENT_CLICREDS_H
20
21#include <string>
22
24#include <openvpn/common/rc.hpp>
26#include <openvpn/auth/cr.hpp>
27
28namespace openvpn {
29
30class ClientCreds : public RC<thread_unsafe_refcount>
31{
32 public:
34
35 void set_username(const std::string &username_arg)
36 {
37 username = username_arg;
38 }
39
40 void set_password(const std::string &password_arg)
41 {
42 password = password_arg;
43 if (!password.empty())
44 {
45 password_needed_ = true;
46 }
47 }
48
49 void set_http_proxy_username(const std::string &username)
50 {
52 }
53
54 void set_http_proxy_password(const std::string &password)
55 {
57 }
58
59 void set_response(const std::string &response_arg)
60 {
61 response = response_arg;
62 if (!response.empty())
63 {
65 }
66 }
67
68 void set_dynamic_challenge_cookie(const std::string &cookie, const std::string &username)
69 {
70 if (!cookie.empty())
72 }
73
74 void set_session_id(const std::string &user, const std::string &sess_id)
75 {
77 {
79 // for dynamic challenge we use dynamic password only once
81 }
82 else if (!user.empty())
83 {
85 }
86
87 // response is used only once
88 response.clear();
89
90 session_id = sess_id;
91 }
92
93 std::string get_username() const
94 {
97 if (!session_id_username.empty())
99 return username;
100 }
101
102 std::string get_password() const
103 {
106 if (response.empty())
107 {
108 if (!session_id.empty())
109 return session_id;
110 return password;
111 }
113 }
114
115 std::string get_http_proxy_username() const
116 {
117 return http_proxy_user;
118 }
119
120 std::string get_http_proxy_password() const
121 {
122 return http_proxy_pass;
123 }
124
125 bool username_defined() const
126 {
127 return !username.empty();
128 }
129
130 bool password_defined() const
131 {
132 return !password.empty();
133 }
134
136 {
137 return !http_proxy_user.empty();
138 }
139
141 {
142 return !http_proxy_pass.empty();
143 }
144
146 {
147 return !session_id.empty();
148 }
149
151 {
152 OPENVPN_LOG("Clearing session-id");
153 session_id.clear();
154 session_id_username.clear();
155 }
156
158 {
159 OPENVPN_LOG("Clearing credentials");
160 username.clear();
161 password.clear();
162 }
163
165 {
166 if (session_id_username.empty())
167 {
169 }
170 }
171
173 {
175 }
176
178 {
180 }
181
182 bool password_needed() const
183 {
184 return password_needed_;
185 }
186
187 std::string auth_info() const
188 {
189 std::string ret;
191 {
192 ret = "DynamicChallenge";
193 }
194 else if (response.empty())
195 {
196 if (!username.empty())
197 {
198 ret += "Username";
199 }
200 else if (!session_id_username.empty())
201 {
202 ret += "UsernameSessionId";
203 }
204 else
205 {
206 ret += "UsernameEmpty";
207 }
208 ret += '/';
209 if (!session_id.empty())
210 {
211 ret += "SessionID";
212 }
213 else if (!password.empty())
214 {
215 ret += "Password";
216 }
217 else
218 {
219 ret += "PasswordEmpty";
220 }
221 }
222 else
223 {
224 ret = "StaticChallenge";
225 }
226 return ret;
227 }
228
229 private:
230 // Standard credentials
231 std::string username;
232 std::string password;
233
234 // HTTP proxy credentials
235 std::string http_proxy_user;
236 std::string http_proxy_pass;
237
238 std::string session_id;
240
241 // Response to a challenge
242 std::string response;
243
244 // Need user interaction to authenticate - such as static/dynamic challenge or SAML
246
247 // Non-empty password provided
248 bool password_needed_ = false;
249
250 // Info describing a dynamic challenge
252};
253
254} // namespace openvpn
255
256#endif
std::string construct_dynamic_password(const std::string &response) const
Definition cr.hpp:128
const std::string & get_username() const
Definition cr.hpp:206
static std::string construct_static_password(const std::string &password, const std::string &response)
Definition cr.hpp:133
void set_http_proxy_username(const std::string &username)
Definition clicreds.hpp:49
void set_dynamic_challenge_cookie(const std::string &cookie, const std::string &username)
Definition clicreds.hpp:68
std::string session_id_username
Definition clicreds.hpp:239
bool password_defined() const
Definition clicreds.hpp:130
std::string get_password() const
Definition clicreds.hpp:102
std::string session_id
Definition clicreds.hpp:238
bool username_defined() const
Definition clicreds.hpp:125
bool password_needed() const
Definition clicreds.hpp:182
std::string http_proxy_pass
Definition clicreds.hpp:236
void set_username(const std::string &username_arg)
Definition clicreds.hpp:35
std::string auth_info() const
Definition clicreds.hpp:187
void set_need_user_interaction()
Definition clicreds.hpp:172
std::string username
Definition clicreds.hpp:231
bool need_user_interaction() const
Definition clicreds.hpp:177
std::string get_http_proxy_username() const
Definition clicreds.hpp:115
std::string response
Definition clicreds.hpp:242
void set_session_id(const std::string &user, const std::string &sess_id)
Definition clicreds.hpp:74
bool http_proxy_password_defined() const
Definition clicreds.hpp:140
std::string get_http_proxy_password() const
Definition clicreds.hpp:120
std::string password
Definition clicreds.hpp:232
void set_http_proxy_password(const std::string &password)
Definition clicreds.hpp:54
std::string get_username() const
Definition clicreds.hpp:93
std::string http_proxy_user
Definition clicreds.hpp:235
void set_response(const std::string &response_arg)
Definition clicreds.hpp:59
bool session_id_defined() const
Definition clicreds.hpp:145
ChallengeResponse::Ptr dynamic_challenge
Definition clicreds.hpp:251
void set_password(const std::string &password_arg)
Definition clicreds.hpp:40
bool http_proxy_username_defined() const
Definition clicreds.hpp:135
void save_username_for_session_id()
Definition clicreds.hpp:164
void reset() noexcept
Points this RCPtr<T> to nullptr safely.
Definition rc.hpp:290
Reference count base class for objects tracked by RCPtr. Disallows copying and assignment.
Definition rc.hpp:908
#define OPENVPN_LOG(args)
std::string ret