Mercurial > hg > orthanc-authorization
comparison Plugin/AuthorizationWebService.cpp @ 69:af44dce56328
new 'auth/user-profile' Rest API route
author | Alain Mazy <am@osimis.io> |
---|---|
date | Mon, 20 Feb 2023 11:56:14 +0100 |
parents | 1a13c4fbc9a1 |
children | 786b202ef24e |
comparison
equal
deleted
inserted
replaced
68:1a13c4fbc9a1 | 69:af44dce56328 |
---|---|
20 | 20 |
21 #include "../Resources/Orthanc/Plugins/OrthancPluginCppWrapper.h" | 21 #include "../Resources/Orthanc/Plugins/OrthancPluginCppWrapper.h" |
22 | 22 |
23 #include <Logging.h> | 23 #include <Logging.h> |
24 #include <Toolbox.h> | 24 #include <Toolbox.h> |
25 #include <HttpClient.h> | |
25 | 26 |
26 namespace OrthancPlugins | 27 namespace OrthancPlugins |
27 { | 28 { |
28 bool AuthorizationWebService::IsGrantedInternal(unsigned int& validity, | 29 bool AuthorizationWebService::IsGrantedInternal(unsigned int& validity, |
29 OrthancPluginHttpMethod method, | 30 OrthancPluginHttpMethod method, |
168 { | 169 { |
169 username_ = username; | 170 username_ = username; |
170 password_ = password; | 171 password_ = password; |
171 } | 172 } |
172 | 173 |
174 void AuthorizationWebService::SetUserProfileUrl(const std::string& url) | |
175 { | |
176 userProfileUrl_ = url; | |
177 } | |
178 | |
173 void AuthorizationWebService::SetIdentifier(const std::string& webServiceIdentifier) | 179 void AuthorizationWebService::SetIdentifier(const std::string& webServiceIdentifier) |
174 { | 180 { |
175 identifier_ = webServiceIdentifier; | 181 identifier_ = webServiceIdentifier; |
176 } | 182 } |
177 | 183 |
184 bool AuthorizationWebService::GetUserProfile(Json::Value& profile /* out */, | |
185 const Token& token, | |
186 const std::string& tokenValue) | |
187 { | |
188 if (userProfileUrl_.empty()) | |
189 { | |
190 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadRequest, "Can not get user profile if the 'WebServiceUserProfileUrl' is not configured"); | |
191 } | |
192 | |
193 Orthanc::WebServiceParameters authWebservice; | |
194 authWebservice.SetUrl(userProfileUrl_); | |
195 | |
196 if (!username_.empty()) | |
197 { | |
198 authWebservice.SetCredentials(username_, password_); | |
199 } | |
200 | |
201 Json::Value body; | |
202 | |
203 body["token-key"] = token.GetKey(); | |
204 body["token-value"] = tokenValue; | |
205 | |
206 if (!identifier_.empty()) | |
207 { | |
208 body["identifier"] = identifier_; | |
209 } | |
210 else | |
211 { | |
212 body["identifier"] = Json::nullValue; | |
213 } | |
214 | |
215 std::string bodyAsString; | |
216 Orthanc::Toolbox::WriteFastJson(bodyAsString, body); | |
217 | |
218 try | |
219 { | |
220 Orthanc::HttpClient authClient(authWebservice, ""); | |
221 authClient.AssignBody(bodyAsString); | |
222 authClient.SetMethod(Orthanc::HttpMethod_Post); | |
223 authClient.AddHeader("Content-Type", "application/json"); | |
224 authClient.AddHeader("Expect", ""); | |
225 | |
226 authClient.ApplyAndThrowException(profile); | |
227 return true; | |
228 } | |
229 catch (Orthanc::OrthancException& ex) | |
230 { | |
231 return false; | |
232 } | |
233 } | |
234 | |
178 } | 235 } |