Mercurial > hg > orthanc-authorization
comparison Plugin/AuthorizationWebService.cpp @ 74:aa73b10c2db9
new API route to decode tokens
author | Alain Mazy <am@osimis.io> |
---|---|
date | Fri, 03 Mar 2023 18:03:22 +0100 |
parents | 512247750f0a |
children | e2c3c497eb8d |
comparison
equal
deleted
inserted
replaced
73:512247750f0a | 74:aa73b10c2db9 |
---|---|
161 void AuthorizationWebService::SetIdentifier(const std::string& webServiceIdentifier) | 161 void AuthorizationWebService::SetIdentifier(const std::string& webServiceIdentifier) |
162 { | 162 { |
163 identifier_ = webServiceIdentifier; | 163 identifier_ = webServiceIdentifier; |
164 } | 164 } |
165 | 165 |
166 | |
167 bool AuthorizationWebService::DecodeToken(DecodedToken& response, | |
168 const std::string& tokenKey, | |
169 const std::string& tokenValue) | |
170 { | |
171 if (tokenDecoderUrl_.empty()) | |
172 { | |
173 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadRequest, "Can not create tokens if the 'WebServiceTokenValidationUrl' is not configured"); | |
174 } | |
175 Orthanc::WebServiceParameters authWebservice; | |
176 | |
177 if (!username_.empty()) | |
178 { | |
179 authWebservice.SetCredentials(username_, password_); | |
180 } | |
181 | |
182 Json::Value body; | |
183 | |
184 body["token-key"] = tokenKey; | |
185 body["token-value"] = tokenValue; | |
186 | |
187 std::string bodyAsString; | |
188 Orthanc::Toolbox::WriteFastJson(bodyAsString, body); | |
189 | |
190 Json::Value tokenResponse; | |
191 try | |
192 { | |
193 Orthanc::HttpClient authClient(authWebservice, ""); | |
194 authClient.SetUrl(tokenDecoderUrl_); | |
195 authClient.AssignBody(bodyAsString); | |
196 authClient.SetMethod(Orthanc::HttpMethod_Post); | |
197 authClient.AddHeader("Content-Type", "application/json"); | |
198 authClient.AddHeader("Expect", ""); | |
199 authClient.SetTimeout(10); | |
200 | |
201 authClient.ApplyAndThrowException(tokenResponse); | |
202 | |
203 if (tokenResponse.isMember("redirect-url")) | |
204 { | |
205 response.redirectUrl = tokenResponse["redirect-url"].asString(); | |
206 } | |
207 | |
208 if (tokenResponse.isMember("error-code")) | |
209 { | |
210 response.errorCode = tokenResponse["error-code"].asString(); | |
211 } | |
212 | |
213 if (tokenResponse.isMember("token-type")) | |
214 { | |
215 response.tokenType = tokenResponse["token-type"].asString(); | |
216 } | |
217 | |
218 return true; | |
219 } | |
220 catch (Orthanc::OrthancException& ex) | |
221 { | |
222 return false; | |
223 } | |
224 | |
225 } | |
226 | |
166 bool AuthorizationWebService::CreateToken(IAuthorizationService::CreatedToken& response, | 227 bool AuthorizationWebService::CreateToken(IAuthorizationService::CreatedToken& response, |
167 const std::string& tokenType, | 228 const std::string& tokenType, |
168 const std::string& id, | 229 const std::string& id, |
169 const std::vector<IAuthorizationService::OrthancResource>& resources, | 230 const std::vector<IAuthorizationService::OrthancResource>& resources, |
170 const std::string& expirationDateString, | 231 const std::string& expirationDateString, |