Mercurial > hg > orthanc
comparison OrthancServer/OrthancPeerParameters.cpp @ 2019:9c9332e486ca
HTTPS client certificates can be associated with Orthanc peers to enhance security over Internet
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Tue, 14 Jun 2016 17:53:23 +0200 |
parents | b1291df2f780 |
children |
comparison
equal
deleted
inserted
replaced
2018:300599489cab | 2019:9c9332e486ca |
---|---|
31 | 31 |
32 | 32 |
33 #include "PrecompiledHeadersServer.h" | 33 #include "PrecompiledHeadersServer.h" |
34 #include "OrthancPeerParameters.h" | 34 #include "OrthancPeerParameters.h" |
35 | 35 |
36 #include "../Core/Logging.h" | |
37 #include "../Core/Toolbox.h" | |
36 #include "../Core/OrthancException.h" | 38 #include "../Core/OrthancException.h" |
37 | 39 |
38 namespace Orthanc | 40 namespace Orthanc |
39 { | 41 { |
40 OrthancPeerParameters::OrthancPeerParameters() : | 42 OrthancPeerParameters::OrthancPeerParameters() : |
43 advancedFormat_(false), | |
41 url_("http://localhost:8042/") | 44 url_("http://localhost:8042/") |
42 { | 45 { |
43 } | 46 } |
44 | 47 |
45 | 48 |
49 void OrthancPeerParameters::SetClientCertificate(const std::string& certificateFile, | |
50 const std::string& certificateKeyFile, | |
51 const std::string& certificateKeyPassword) | |
52 { | |
53 if (certificateFile.empty()) | |
54 { | |
55 throw OrthancException(ErrorCode_ParameterOutOfRange); | |
56 } | |
57 | |
58 if (!Toolbox::IsRegularFile(certificateFile)) | |
59 { | |
60 LOG(ERROR) << "Cannot open certificate file: " << certificateFile; | |
61 throw OrthancException(ErrorCode_InexistentFile); | |
62 } | |
63 | |
64 if (!certificateKeyFile.empty() && | |
65 !Toolbox::IsRegularFile(certificateKeyFile)) | |
66 { | |
67 LOG(ERROR) << "Cannot open key file: " << certificateKeyFile; | |
68 throw OrthancException(ErrorCode_InexistentFile); | |
69 } | |
70 | |
71 advancedFormat_ = true; | |
72 certificateFile_ = certificateFile; | |
73 certificateKeyFile_ = certificateKeyFile; | |
74 certificateKeyPassword_ = certificateKeyPassword; | |
75 } | |
76 | |
77 | |
78 static void AddTrailingSlash(std::string& url) | |
79 { | |
80 if (url.size() != 0 && | |
81 url[url.size() - 1] != '/') | |
82 { | |
83 url += '/'; | |
84 } | |
85 } | |
86 | |
87 | |
88 void OrthancPeerParameters::FromJsonArray(const Json::Value& peer) | |
89 { | |
90 assert(peer.isArray()); | |
91 | |
92 advancedFormat_ = false; | |
93 | |
94 if (peer.size() != 1 && | |
95 peer.size() != 3) | |
96 { | |
97 throw OrthancException(ErrorCode_BadFileFormat); | |
98 } | |
99 | |
100 std::string url = peer.get(0u, "").asString(); | |
101 if (url.empty()) | |
102 { | |
103 throw OrthancException(ErrorCode_BadFileFormat); | |
104 } | |
105 | |
106 AddTrailingSlash(url); | |
107 SetUrl(url); | |
108 | |
109 if (peer.size() == 1) | |
110 { | |
111 SetUsername(""); | |
112 SetPassword(""); | |
113 } | |
114 else if (peer.size() == 3) | |
115 { | |
116 SetUsername(peer.get(1u, "").asString()); | |
117 SetPassword(peer.get(2u, "").asString()); | |
118 } | |
119 else | |
120 { | |
121 throw OrthancException(ErrorCode_BadFileFormat); | |
122 } | |
123 } | |
124 | |
125 | |
126 static std::string GetStringMember(const Json::Value& peer, | |
127 const std::string& key, | |
128 const std::string& defaultValue) | |
129 { | |
130 if (!peer.isMember(key)) | |
131 { | |
132 return defaultValue; | |
133 } | |
134 else if (peer[key].type() != Json::stringValue) | |
135 { | |
136 throw OrthancException(ErrorCode_BadFileFormat); | |
137 } | |
138 else | |
139 { | |
140 return peer[key].asString(); | |
141 } | |
142 } | |
143 | |
144 | |
145 void OrthancPeerParameters::FromJsonObject(const Json::Value& peer) | |
146 { | |
147 assert(peer.isObject()); | |
148 advancedFormat_ = true; | |
149 | |
150 std::string url = GetStringMember(peer, "Url", ""); | |
151 if (url.empty()) | |
152 { | |
153 throw OrthancException(ErrorCode_BadFileFormat); | |
154 } | |
155 | |
156 AddTrailingSlash(url); | |
157 SetUrl(url); | |
158 | |
159 SetUsername(GetStringMember(peer, "Username", "")); | |
160 SetPassword(GetStringMember(peer, "Password", "")); | |
161 | |
162 if (peer.isMember("CertificateFile")) | |
163 { | |
164 SetClientCertificate(GetStringMember(peer, "CertificateFile", ""), | |
165 GetStringMember(peer, "CertificateKeyFile", ""), | |
166 GetStringMember(peer, "CertificateKeyPassword", "")); | |
167 } | |
168 } | |
169 | |
170 | |
46 void OrthancPeerParameters::FromJson(const Json::Value& peer) | 171 void OrthancPeerParameters::FromJson(const Json::Value& peer) |
47 { | 172 { |
48 if (!peer.isArray() || | |
49 (peer.size() != 1 && peer.size() != 3)) | |
50 { | |
51 throw OrthancException(ErrorCode_BadFileFormat); | |
52 } | |
53 | |
54 std::string url; | |
55 | |
56 try | 173 try |
57 { | 174 { |
58 url = peer.get(0u, "").asString(); | 175 if (peer.isArray()) |
59 | 176 { |
60 if (peer.size() == 1) | 177 FromJsonArray(peer); |
61 { | 178 } |
62 SetUsername(""); | 179 else if (peer.isObject()) |
63 SetPassword(""); | 180 { |
64 } | 181 FromJsonObject(peer); |
65 else if (peer.size() == 3) | |
66 { | |
67 SetUsername(peer.get(1u, "").asString()); | |
68 SetPassword(peer.get(2u, "").asString()); | |
69 } | 182 } |
70 else | 183 else |
71 { | 184 { |
72 throw OrthancException(ErrorCode_BadFileFormat); | 185 throw OrthancException(ErrorCode_BadFileFormat); |
73 } | 186 } |
74 } | 187 } |
75 catch (...) | 188 catch (...) |
76 { | 189 { |
77 throw OrthancException(ErrorCode_BadFileFormat); | 190 throw OrthancException(ErrorCode_BadFileFormat); |
78 } | 191 } |
79 | |
80 if (url.size() != 0 && url[url.size() - 1] != '/') | |
81 { | |
82 url += '/'; | |
83 } | |
84 | |
85 SetUrl(url); | |
86 } | 192 } |
87 | 193 |
88 | 194 |
89 void OrthancPeerParameters::ToJson(Json::Value& value) const | 195 void OrthancPeerParameters::ToJson(Json::Value& value) const |
90 { | 196 { |
91 value = Json::arrayValue; | 197 if (advancedFormat_) |
92 value.append(GetUrl()); | 198 { |
93 value.append(GetUsername()); | 199 value = Json::objectValue; |
94 value.append(GetPassword()); | 200 value["Url"] = url_; |
201 | |
202 if (!username_.empty() || | |
203 !password_.empty()) | |
204 { | |
205 value["Username"] = username_; | |
206 value["Password"] = password_; | |
207 } | |
208 | |
209 if (!certificateFile_.empty()) | |
210 { | |
211 value["CertificateFile"] = certificateFile_; | |
212 } | |
213 | |
214 if (!certificateKeyFile_.empty()) | |
215 { | |
216 value["CertificateKeyFile"] = certificateKeyFile_; | |
217 } | |
218 | |
219 if (!certificateKeyPassword_.empty()) | |
220 { | |
221 value["CertificateKeyPassword"] = certificateKeyPassword_; | |
222 } | |
223 } | |
224 else | |
225 { | |
226 value = Json::arrayValue; | |
227 value.append(url_); | |
228 | |
229 if (!username_.empty() || | |
230 !password_.empty()) | |
231 { | |
232 value.append(username_); | |
233 value.append(password_); | |
234 } | |
235 } | |
236 } | |
237 | |
238 | |
239 void OrthancPeerParameters::ConfigureClient(HttpClient& client) const | |
240 { | |
241 if (username_.size() != 0 && | |
242 password_.size() != 0) | |
243 { | |
244 client.SetCredentials(username_.c_str(), | |
245 password_.c_str()); | |
246 } | |
247 | |
248 if (!GetCertificateFile().empty()) | |
249 { | |
250 client.SetClientCertificate(certificateFile_, certificateKeyFile_, certificateKeyPassword_); | |
251 } | |
95 } | 252 } |
96 } | 253 } |