comparison Plugin/AuthorizationWebService.cpp @ 70:786b202ef24e

use more recent httpclient
author Alain Mazy <am@osimis.io>
date Tue, 21 Feb 2023 09:23:47 +0100
parents af44dce56328
children 30fb3ce960d9
comparison
equal deleted inserted replaced
69:af44dce56328 70:786b202ef24e
81 else 81 else
82 { 82 {
83 body["identifier"] = Json::nullValue; 83 body["identifier"] = Json::nullValue;
84 } 84 }
85 85
86 MemoryBuffer answerBody; 86 Orthanc::WebServiceParameters authWebservice;
87 MemoryBuffer answerHeaders; 87 authWebservice.SetUrl(url_);
88 uint16_t httpStatus = 0; 88
89 89 if (!username_.empty())
90 uint32_t headersCount = 0; 90 {
91 const char* headersKeys[2]; 91 authWebservice.SetCredentials(username_, password_);
92 const char* headersValues[2]; 92 }
93 93
94 std::string bodyAsString;
95 Orthanc::Toolbox::WriteFastJson(bodyAsString, body);
96
97 Orthanc::HttpClient authClient(authWebservice, "");
98 authClient.AssignBody(bodyAsString);
99 authClient.SetMethod(Orthanc::HttpMethod_Post);
100 authClient.AddHeader("Content-Type", "application/json");
101 authClient.AddHeader("Expect", "");
102 authClient.SetTimeout(10);
103
94 if (token != NULL && 104 if (token != NULL &&
95 token->GetType() == TokenType_HttpHeader) 105 token->GetType() == TokenType_HttpHeader)
96 { 106 {
97 // If the token source is a HTTP header, forward it also as a 107 // If the token source is a HTTP header, forward it also as a
98 // HTTP header except if it is the Authorization header that might conflict with username_ and password_ 108 // HTTP header except if it is the Authorization header that might conflict with username_ and password_
99 std::string lowerTokenKey; 109 std::string lowerTokenKey;
100 Orthanc::Toolbox::ToLowerCase(lowerTokenKey, token->GetKey()); 110 Orthanc::Toolbox::ToLowerCase(lowerTokenKey, token->GetKey());
101 111
102 if (!(lowerTokenKey == "authorization" && !username_.empty())) 112 if (!(lowerTokenKey == "authorization" && !username_.empty()))
103 { 113 {
104 headersKeys[headersCount] = token->GetKey().c_str(); 114 authClient.AddHeader(token->GetKey(), tokenValue);
105 headersValues[headersCount] = tokenValue.c_str();
106 headersCount++;
107 } 115 }
108 } 116 }
109 117
110 // set the correct content type for the outgoing
111 headersKeys[headersCount] = "Content-Type";
112 headersValues[headersCount] = "application/json";
113 headersCount++;
114
115 // set the correct content type for the outgoing
116 headersKeys[headersCount] = "Expect";
117 headersValues[headersCount] = "";
118 headersCount++;
119
120 std::string flatBody = body.toStyledString();
121
122 if (OrthancPluginHttpClient(GetGlobalContext(), *answerBody, *answerHeaders,
123 &httpStatus, OrthancPluginHttpMethod_Post,
124 url_.c_str(), headersCount, headersKeys, headersValues,
125 flatBody.c_str(), flatBody.size(),
126 username_.empty() ? NULL : username_.c_str(),
127 password_.empty() ? NULL : password_.c_str(),
128 10 /* timeout */, NULL, NULL, NULL, 0)
129 != OrthancPluginErrorCode_Success)
130 {
131 throw Orthanc::OrthancException(Orthanc::ErrorCode_NetworkProtocol);
132 }
133
134 Json::Value answer; 118 Json::Value answer;
135 answerBody.ToJson(answer); 119 authClient.ApplyAndThrowException(answer);
136 120
137 static const char* GRANTED = "granted"; 121 static const char* GRANTED = "granted";
138 static const char* VALIDITY = "validity"; 122 static const char* VALIDITY = "validity";
139 123
140 if (answer.type() != Json::objectValue || 124 if (answer.type() != Json::objectValue ||
220 Orthanc::HttpClient authClient(authWebservice, ""); 204 Orthanc::HttpClient authClient(authWebservice, "");
221 authClient.AssignBody(bodyAsString); 205 authClient.AssignBody(bodyAsString);
222 authClient.SetMethod(Orthanc::HttpMethod_Post); 206 authClient.SetMethod(Orthanc::HttpMethod_Post);
223 authClient.AddHeader("Content-Type", "application/json"); 207 authClient.AddHeader("Content-Type", "application/json");
224 authClient.AddHeader("Expect", ""); 208 authClient.AddHeader("Expect", "");
209 authClient.SetTimeout(10);
225 210
226 authClient.ApplyAndThrowException(profile); 211 authClient.ApplyAndThrowException(profile);
227 return true; 212 return true;
228 } 213 }
229 catch (Orthanc::OrthancException& ex) 214 catch (Orthanc::OrthancException& ex)