Mercurial > hg > orthanc-authorization
comparison Plugin/AuthorizationWebService.cpp @ 72:e381ba725669
new PUT auth/tokens/{token-type} API route + updated interface with WebService
author | Alain Mazy <am@osimis.io> |
---|---|
date | Fri, 24 Feb 2023 18:13:36 +0100 |
parents | 30fb3ce960d9 |
children | 512247750f0a |
comparison
equal
deleted
inserted
replaced
71:30fb3ce960d9 | 72:e381ba725669 |
---|---|
80 body["token-value"] = tokenValue; | 80 body["token-value"] = tokenValue; |
81 } | 81 } |
82 | 82 |
83 if (!identifier_.empty()) | 83 if (!identifier_.empty()) |
84 { | 84 { |
85 body["identifier"] = identifier_; | 85 body["server-id"] = identifier_; |
86 } | 86 } |
87 else | 87 else |
88 { | 88 { |
89 body["identifier"] = Json::nullValue; | 89 body["server-id"] = Json::nullValue; |
90 } | 90 } |
91 | 91 |
92 Orthanc::WebServiceParameters authWebservice; | 92 Orthanc::WebServiceParameters authWebservice; |
93 authWebservice.SetUrl(url_); | |
94 | 93 |
95 if (!username_.empty()) | 94 if (!username_.empty()) |
96 { | 95 { |
97 authWebservice.SetCredentials(username_, password_); | 96 authWebservice.SetCredentials(username_, password_); |
98 } | 97 } |
99 | 98 |
100 std::string bodyAsString; | 99 std::string bodyAsString; |
101 Orthanc::Toolbox::WriteFastJson(bodyAsString, body); | 100 Orthanc::Toolbox::WriteFastJson(bodyAsString, body); |
102 | 101 |
103 Orthanc::HttpClient authClient(authWebservice, ""); | 102 Orthanc::HttpClient authClient(authWebservice, ""); |
103 authClient.SetUrl(tokenValidationUrl_); | |
104 authClient.AssignBody(bodyAsString); | 104 authClient.AssignBody(bodyAsString); |
105 authClient.SetMethod(Orthanc::HttpMethod_Post); | 105 authClient.SetMethod(Orthanc::HttpMethod_Post); |
106 authClient.AddHeader("Content-Type", "application/json"); | 106 authClient.AddHeader("Content-Type", "application/json"); |
107 authClient.AddHeader("Expect", ""); | 107 authClient.AddHeader("Expect", ""); |
108 authClient.SetTimeout(10); | 108 authClient.SetTimeout(10); |
156 { | 156 { |
157 username_ = username; | 157 username_ = username; |
158 password_ = password; | 158 password_ = password; |
159 } | 159 } |
160 | 160 |
161 void AuthorizationWebService::SetUserProfileUrl(const std::string& url) | |
162 { | |
163 userProfileUrl_ = url; | |
164 } | |
165 | |
166 void AuthorizationWebService::SetIdentifier(const std::string& webServiceIdentifier) | 161 void AuthorizationWebService::SetIdentifier(const std::string& webServiceIdentifier) |
167 { | 162 { |
168 identifier_ = webServiceIdentifier; | 163 identifier_ = webServiceIdentifier; |
169 } | 164 } |
165 | |
166 bool AuthorizationWebService::CreateToken(IAuthorizationService::CreatedToken& response, | |
167 const std::string& tokenType, | |
168 const std::string& id, | |
169 const std::vector<IAuthorizationService::OrthancResource>& resources, | |
170 const std::string& expirationDateString) | |
171 { | |
172 if (tokenCreationBaseUrl_.empty()) | |
173 { | |
174 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadRequest, "Can not create tokens if the 'WebServiceTokenCreationBaseUrl' is not configured"); | |
175 } | |
176 std::string url = Orthanc::Toolbox::JoinUri(tokenCreationBaseUrl_, tokenType); | |
177 | |
178 Orthanc::WebServiceParameters authWebservice; | |
179 | |
180 if (!username_.empty()) | |
181 { | |
182 authWebservice.SetCredentials(username_, password_); | |
183 } | |
184 | |
185 Json::Value body; | |
186 | |
187 if (!id.empty()) | |
188 { | |
189 body["id"] = id; | |
190 } | |
191 | |
192 body["resources"] = Json::arrayValue; | |
193 for (size_t i = 0; i < resources.size(); ++i) | |
194 { | |
195 Json::Value resource; | |
196 if (!resources[i].dicomUid.empty()) | |
197 { | |
198 resource["dicom-uid"] = resources[i].dicomUid; | |
199 } | |
200 if (!resources[i].orthancId.empty()) | |
201 { | |
202 resource["orthanc-id"] = resources[i].orthancId; | |
203 } | |
204 if (!resources[i].url.empty()) | |
205 { | |
206 resource["url"] = resources[i].url; | |
207 } | |
208 if (!resources[i].level.empty()) | |
209 { | |
210 resource["level"] = resources[i].level; | |
211 } | |
212 | |
213 body["resources"].append(resource); | |
214 } | |
215 | |
216 body["type"] = tokenType; | |
217 if (!expirationDateString.empty()) | |
218 { | |
219 body["expiration-date"] = expirationDateString; | |
220 } | |
221 | |
222 std::string bodyAsString; | |
223 Orthanc::Toolbox::WriteFastJson(bodyAsString, body); | |
224 | |
225 Json::Value tokenResponse; | |
226 try | |
227 { | |
228 Orthanc::HttpClient authClient(authWebservice, ""); | |
229 authClient.SetUrl(url); | |
230 authClient.AssignBody(bodyAsString); | |
231 authClient.SetMethod(Orthanc::HttpMethod_Put); | |
232 authClient.AddHeader("Content-Type", "application/json"); | |
233 authClient.AddHeader("Expect", ""); | |
234 authClient.SetTimeout(10); | |
235 | |
236 authClient.ApplyAndThrowException(tokenResponse); | |
237 | |
238 response.token = tokenResponse["token"].asString(); | |
239 response.url = tokenResponse["url"].asString(); | |
240 | |
241 return true; | |
242 } | |
243 catch (Orthanc::OrthancException& ex) | |
244 { | |
245 return false; | |
246 } | |
247 | |
248 } | |
249 | |
170 | 250 |
171 bool AuthorizationWebService::GetUserProfileInternal(unsigned int& validity, | 251 bool AuthorizationWebService::GetUserProfileInternal(unsigned int& validity, |
172 Json::Value& profile /* out */, | 252 Json::Value& profile /* out */, |
173 const Token* token, | 253 const Token* token, |
174 const std::string& tokenValue) | 254 const std::string& tokenValue) |
177 { | 257 { |
178 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadRequest, "Can not get user profile if the 'WebServiceUserProfileUrl' is not configured"); | 258 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadRequest, "Can not get user profile if the 'WebServiceUserProfileUrl' is not configured"); |
179 } | 259 } |
180 | 260 |
181 Orthanc::WebServiceParameters authWebservice; | 261 Orthanc::WebServiceParameters authWebservice; |
182 authWebservice.SetUrl(userProfileUrl_); | |
183 | 262 |
184 if (!username_.empty()) | 263 if (!username_.empty()) |
185 { | 264 { |
186 authWebservice.SetCredentials(username_, password_); | 265 authWebservice.SetCredentials(username_, password_); |
187 } | 266 } |
207 Orthanc::Toolbox::WriteFastJson(bodyAsString, body); | 286 Orthanc::Toolbox::WriteFastJson(bodyAsString, body); |
208 | 287 |
209 try | 288 try |
210 { | 289 { |
211 Orthanc::HttpClient authClient(authWebservice, ""); | 290 Orthanc::HttpClient authClient(authWebservice, ""); |
291 authClient.SetUrl(userProfileUrl_); | |
212 authClient.AssignBody(bodyAsString); | 292 authClient.AssignBody(bodyAsString); |
213 authClient.SetMethod(Orthanc::HttpMethod_Post); | 293 authClient.SetMethod(Orthanc::HttpMethod_Post); |
214 authClient.AddHeader("Content-Type", "application/json"); | 294 authClient.AddHeader("Content-Type", "application/json"); |
215 authClient.AddHeader("Expect", ""); | 295 authClient.AddHeader("Expect", ""); |
216 authClient.SetTimeout(10); | 296 authClient.SetTimeout(10); |