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 ClientCreds() = default;
36
37 void set_username(const std::string &username_arg)
38 {
39 username = username_arg;
40 }
41
42 void set_password(const std::string &password_arg)
43 {
44 password = password_arg;
45 if (!password.empty())
46 {
47 password_needed_ = true;
48 }
49 }
50
51 void set_http_proxy_username(const std::string &username)
52 {
54 }
55
56 void set_http_proxy_password(const std::string &password)
57 {
59 }
60
61 void set_response(const std::string &response_arg)
62 {
63 response = response_arg;
64 if (!response.empty())
65 {
67 }
68 }
69
70 void set_dynamic_challenge_cookie(const std::string &cookie, const std::string &username)
71 {
72 if (!cookie.empty())
74 }
75
76 void set_session_id(const std::string &user, const std::string &sess_id)
77 {
79 {
81 // for dynamic challenge we use dynamic password only once
83 }
84 else if (!user.empty())
85 {
87 }
88
89 // response is used only once
90 response.clear();
91
92 session_id = sess_id;
93 }
94
95 std::string get_username() const
96 {
99 else if (!session_id_username.empty())
100 return session_id_username;
101 else
102 return username;
103 }
104
105 std::string get_password() const
106 {
109 else if (response.empty())
110 {
111 if (!session_id.empty())
112 return session_id;
113 else
114 return password;
115 }
116 else
118 }
119
120 std::string get_http_proxy_username() const
121 {
122 return http_proxy_user;
123 }
124
125 std::string get_http_proxy_password() const
126 {
127 return http_proxy_pass;
128 }
129
130 bool username_defined() const
131 {
132 return !username.empty();
133 }
134
135 bool password_defined() const
136 {
137 return !password.empty();
138 }
139
141 {
142 return !http_proxy_user.empty();
143 }
144
146 {
147 return !http_proxy_pass.empty();
148 }
149
151 {
152 return !session_id.empty();
153 }
154
156 {
157 OPENVPN_LOG("Clearing session-id");
158 session_id.clear();
159 session_id_username.clear();
160 }
161
163 {
164 OPENVPN_LOG("Clearing credentials");
165 username.clear();
166 password.clear();
167 }
168
170 {
171 if (session_id_username.empty())
172 {
174 }
175 }
176
178 {
180 }
181
183 {
185 }
186
187 bool password_needed() const
188 {
189 return password_needed_;
190 }
191
192 std::string auth_info() const
193 {
194 std::string ret;
196 {
197 ret = "DynamicChallenge";
198 }
199 else if (response.empty())
200 {
201 if (!username.empty())
202 {
203 ret += "Username";
204 }
205 else if (!session_id_username.empty())
206 {
207 ret += "UsernameSessionId";
208 }
209 else
210 {
211 ret += "UsernameEmpty";
212 }
213 ret += '/';
214 if (!session_id.empty())
215 {
216 ret += "SessionID";
217 }
218 else if (!password.empty())
219 {
220 ret += "Password";
221 }
222 else
223 {
224 ret += "PasswordEmpty";
225 }
226 }
227 else
228 {
229 ret = "StaticChallenge";
230 }
231 return ret;
232 }
233
234 private:
235 // Standard credentials
236 std::string username;
237 std::string password;
238
239 // HTTP proxy credentials
240 std::string http_proxy_user;
241 std::string http_proxy_pass;
242
243 std::string session_id;
245
246 // Response to a challenge
247 std::string response;
248
249 // Need user interaction to authenticate - such as static/dynamic challenge or SAML
251
252 // Non-empty password provided
253 bool password_needed_ = false;
254
255 // Info describing a dynamic challenge
257};
258
259} // namespace openvpn
260
261#endif
std::string construct_dynamic_password(const std::string &response) const
Definition cr.hpp:129
const std::string & get_username() const
Definition cr.hpp:211
static std::string construct_static_password(const std::string &password, const std::string &response)
Definition cr.hpp:136
void set_http_proxy_username(const std::string &username)
Definition clicreds.hpp:51
void set_dynamic_challenge_cookie(const std::string &cookie, const std::string &username)
Definition clicreds.hpp:70
std::string session_id_username
Definition clicreds.hpp:244
bool password_defined() const
Definition clicreds.hpp:135
std::string get_password() const
Definition clicreds.hpp:105
std::string session_id
Definition clicreds.hpp:243
bool username_defined() const
Definition clicreds.hpp:130
bool password_needed() const
Definition clicreds.hpp:187
std::string http_proxy_pass
Definition clicreds.hpp:241
void set_username(const std::string &username_arg)
Definition clicreds.hpp:37
std::string auth_info() const
Definition clicreds.hpp:192
RCPtr< ClientCreds > Ptr
Definition clicreds.hpp:33
void set_need_user_interaction()
Definition clicreds.hpp:177
std::string username
Definition clicreds.hpp:236
bool need_user_interaction() const
Definition clicreds.hpp:182
std::string get_http_proxy_username() const
Definition clicreds.hpp:120
std::string response
Definition clicreds.hpp:247
void set_session_id(const std::string &user, const std::string &sess_id)
Definition clicreds.hpp:76
bool http_proxy_password_defined() const
Definition clicreds.hpp:145
std::string get_http_proxy_password() const
Definition clicreds.hpp:125
std::string password
Definition clicreds.hpp:237
void set_http_proxy_password(const std::string &password)
Definition clicreds.hpp:56
std::string get_username() const
Definition clicreds.hpp:95
std::string http_proxy_user
Definition clicreds.hpp:240
void set_response(const std::string &response_arg)
Definition clicreds.hpp:61
bool session_id_defined() const
Definition clicreds.hpp:150
ChallengeResponse::Ptr dynamic_challenge
Definition clicreds.hpp:256
void set_password(const std::string &password_arg)
Definition clicreds.hpp:42
bool http_proxy_username_defined() const
Definition clicreds.hpp:140
void save_username_for_session_id()
Definition clicreds.hpp:169
The smart pointer class.
Definition rc.hpp:119
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:912
#define OPENVPN_LOG(args)
std::string ret