33 #ifndef _IDENTT_QUERY_PARAM_LOOKUP_SERVICE_HPP_ 34 #define _IDENTT_QUERY_PARAM_LOOKUP_SERVICE_HPP_ 38 #include <store/LookupService.hpp> 41 #include <rapidjson/writer.h> 42 #include <rapidjson/stringbuffer.h> 43 #include <rapidjson/pointer.h> 48 template <
class HttpServerT>
73 identt::utils::SharedTable::pointer stptr,
74 typename std::shared_ptr<HttpServerT> server,
75 ::identt::query::HelpQuery::pointer helpquery,
77 :
identt::query::
ServiceBase<HttpServerT>(IDENTT_SERVICE_SCOPE_HTTP | IDENTT_SERVICE_SCOPE_HTTPS)
79 if (!(this->myscope & scope))
return;
82 helpquery->add({scope,
"POST _matrix/identity/api/v1/lookup_params", {
83 "Look up the Matrix user ID for a 3pid.",
84 "query parameters: medium, address",
85 "medium Required. The literal string email.",
86 "address Required. The email address being looked up.",
87 "accesskey Required if lookup_requires_accesskey is set.",
88 "This variant sends back additional parameters from Params" 92 server->resource[
"/_matrix/identity/api/v1/lookup_params$"][
"POST"]
93 =[
this,stptr](
typename HttpServerT::RespPtr response,
typename HttpServerT::ReqPtr request) {
94 IDENTT_PARALLEL_ONE([
this,stptr,response,request] {
96 LOG(INFO) << request->path;
100 identt::query::LookupDataT lact;
103 int stat =
json2pb( request->content.string() , lact.mutable_query() , err);
106 form2pb( request->content.string() , lact.mutable_query() );
113 if (stptr->lookup_requires_key.Get()) {
114 bool is_server_query =
false;
115 auto it=request->header.find(
"Shared-Secret");
116 if (it!=request->header.end()) {
117 is_server_query = (it->second == stptr->shared_secret.Get());
119 if (!is_server_query ) {
130 ::identt::query::PubKeyT pubkey;
133 pubkey.set_owner(stptr->hostname.Get());
134 pubkey.set_algo(THREEPID_DEFAULT_ALGO);
135 pubkey.set_identifier(THREEPID_DEFAULT_ALGO_ID);
138 std::vector<::identt::query::SignatureT> signatures;
139 lservice.
AddSign(stptr, lact.mutable_result() , &pubkey, output,signatures);
140 if (!output.length()) output=
"{}";
142 this->
HttpOKAction(response,request,200,
"OK",
"application/json",output,
true);
145 int ecode = (e.ecode()>=IDENTT_SYDENT_ERROR_MIN && e.ecode()<=IDENTT_SYDENT_ERROR_MAX) ? e.ecode() : M_UNKNOWN;
146 std::string output =
err2json(SydentErrors.at(ecode),e.what());
147 this->
HttpOKAction(response,request,200,
"OK",
"application/json",output,
true);
150 std::string output =
err2json(SydentErrors.at(M_UNKNOWN),e.what());
151 this->
HttpOKAction(response,request,200,
"OK",
"application/json",output,
true);
152 }
catch (std::exception& e)
154 std::string output =
err2json(SydentErrors.at(M_UNKNOWN),e.what());
155 this->
HttpOKAction(response,request,200,
"OK",
"application/json",output,
true);
164 helpquery->add({scope,
"GET _matrix/identity/api/v1/lookup_params", {
165 "Look up the Matrix user ID for a 3pid.",
166 "This is the GET version of lookup_params",
167 "query parameters: medium, address",
168 "medium Required. The literal string email.",
169 "address Required. The email address being looked up.",
170 "accesskey Required if lookup_requires_accesskey is set.",
171 "This variant sends back additional parameters from Params" 175 server->resource[
"/_matrix/identity/api/v1/lookup_params\\\?(.*)$"][
"GET"]
176 =[
this,stptr](
typename HttpServerT::RespPtr response,
typename HttpServerT::ReqPtr request) {
177 IDENTT_PARALLEL_ONE([
this,stptr,response,request] {
179 LOG(INFO) << request->path;
181 std::string params = request->path_match[1];
182 identt::query::LookupDataT lact;
183 form2pb( params , lact.mutable_query());
190 if (stptr->lookup_requires_key.Get()) {
191 bool is_server_query =
false;
192 auto it=request->header.find(
"Shared-Secret");
193 if (it!=request->header.end()) {
194 is_server_query = (it->second == stptr->shared_secret.Get());
196 if (!is_server_query ) {
207 ::identt::query::PubKeyT pubkey;
210 pubkey.set_owner(stptr->hostname.Get());
211 pubkey.set_algo(THREEPID_DEFAULT_ALGO);
212 pubkey.set_identifier(THREEPID_DEFAULT_ALGO_ID);
215 std::vector<::identt::query::SignatureT> signatures;
216 lservice.
AddSign(stptr, lact.mutable_result() , &pubkey, output,signatures);
218 this->
HttpOKAction(response,request,200,
"OK",
"application/json",output,
true);
221 int ecode = (e.ecode()>=IDENTT_SYDENT_ERROR_MIN && e.ecode()<=IDENTT_SYDENT_ERROR_MAX) ? e.ecode() : M_UNKNOWN;
222 std::string output =
err2json(SydentErrors.at(ecode),e.what());
223 this->
HttpOKAction(response,request,200,
"OK",
"application/json",output,
true);
226 std::string output =
err2json(SydentErrors.at(M_UNKNOWN),e.what());
227 this->
HttpOKAction(response,request,200,
"OK",
"application/json",output,
true);
228 }
catch (std::exception& e)
230 std::string output =
err2json(SydentErrors.at(M_UNKNOWN),e.what());
231 this->
HttpOKAction(response,request,200,
"OK",
"application/json",output,
true);
240 helpquery->add({scope,
"POST _matrix/identity/api/v1/bulk_lookup_params", {
241 "Look up the Matrix user ID for a list of 3pid.",
242 "query parameters: list of {medium, address}",
243 "medium Required. The literal string email.",
244 "address Required. The email address being looked up.",
245 "accesskey Required if lookup_requires_accesskey is set.",
246 "This variant sends back additional parameters from Params" 250 server->resource[
"/_matrix/identity/api/v1/bulk_lookup_params$"][
"POST"]
251 =[
this,stptr](
typename HttpServerT::RespPtr response,
typename HttpServerT::ReqPtr request) {
252 IDENTT_PARALLEL_ONE([
this,stptr,response,request] {
254 LOG(INFO) << request->path;
258 identt::query::BulkLookupDataT blact;
261 int stat =
json2pb( request->content.string() , blact.mutable_query() , err);
266 DLOG(INFO) << blact.DebugString();
272 if (stptr->lookup_requires_key.Get()) {
273 bool is_server_query =
false;
274 auto it=request->header.find(
"Shared-Secret");
275 if (it!=request->header.end()) {
276 is_server_query = (it->second == stptr->shared_secret.Get());
278 if (!is_server_query ) {
286 std::string instring;
287 this->BulkLookupJson(blact.mutable_result(),instring);
291 ::identt::query::PubKeyT pubkey;
294 pubkey.set_owner(stptr->hostname.Get());
295 pubkey.set_algo(THREEPID_DEFAULT_ALGO);
296 pubkey.set_identifier(THREEPID_DEFAULT_ALGO_ID);
301 std::vector<::identt::query::SignatureT> signatures;
302 lservice.
AddSign(stptr, instring , &pubkey, output,signatures);
303 if (!output.length()) output=
"{}";
305 this->
HttpOKAction(response,request,200,
"OK",
"application/json",output,
true);
308 int ecode = (e.ecode()>=IDENTT_SYDENT_ERROR_MIN && e.ecode()<=IDENTT_SYDENT_ERROR_MAX) ? e.ecode() : M_UNKNOWN;
309 std::string output =
err2json(SydentErrors.at(ecode),e.what());
310 this->
HttpOKAction(response,request,200,
"OK",
"application/json",output,
true);
313 std::string output =
err2json(SydentErrors.at(M_UNKNOWN),e.what());
314 this->
HttpOKAction(response,request,200,
"OK",
"application/json",output,
true);
315 }
catch (std::exception& e)
317 std::string output =
err2json(SydentErrors.at(M_UNKNOWN),e.what());
318 this->
HttpOKAction(response,request,200,
"OK",
"application/json",output,
true);
341 void BulkLookupJson(identt::query::BulkLookupResultT* bulkres, std::string& output)
345 rapidjson::Pointer(
"/threepids").Create(d , allocator );
347 if (!threepids->IsArray()) threepids->SetArray();
349 for (
auto& res : bulkres->threepids() ) {
355 threepids->PushBack(entry,allocator);
357 rapidjson::StringBuffer buffer;
358 rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
361 output.append(buffer.GetString(), buffer.GetSize());
368 #endif // _IDENTT_QUERY_PARAM_LOOKUP_SERVICE_HPP_ void HttpOKAction(typename HttpServerT::RespPtr response, typename HttpServerT::ReqPtr request, int ec, const char *em, const char *content_type, std::string &payload, bool add_cors)
HttpOKAction : OK Action template.
Definition: ServiceBase.hpp:154
ParamLookupService(identt::utils::SharedTable::pointer stptr, typename std::shared_ptr< HttpServerT > server, ::identt::query::HelpQuery::pointer helpquery, unsigned int scope)
ParamLookupService : constructor.
Definition: ParamLookupService.hpp:72
Allocator AllocatorType
Allocator type from template parameter.
Definition: document.h:2078
array
Definition: rapidjson.h:608
Sydent Exceptions.
Definition: SydentQuery.hpp:91
void HttpErrorAction(typename HttpServerT::RespPtr response, typename HttpServerT::ReqPtr request, int ec, const char *em)
HttpErrorAction : Error Action Template no payload maybe template.
Definition: ServiceBase.hpp:85
Definition: BaseUtils.hpp:52
GenericValue< UTF8<> > Value
GenericValue with UTF8 encoding.
Definition: document.h:2060
Definition: ServiceBase.hpp:55
Definition: CryptoBase.hpp:49
GenericDocument< UTF8<> > Document
GenericDocument with UTF8 encoding.
Definition: document.h:2452
Definition: BaseUtils.hpp:89
void BulkLookupAction(::identt::utils::SharedTable::pointer stptr, ::identt::query::BulkLookupDataT *blact)
BulkLookupAction : Service Endpoint Lookup.
Definition: LookupService.cc:62
GenericStringRef< CharType > StringRef(const CharType *str)
Mark a character pointer as constant string.
Definition: document.h:354
void VerifyAccessKeyAction(::identt::utils::SharedTable::pointer stptr, std::string akey)
VerifyAccessKeyAction : verify the access key.
Definition: AccessKeyService.cc:44
Definition: LookupService.hpp:43
std::string err2json(const std::string errorcode, const std::string error)
err2json : Generate a Json for Error Message
Definition: ProtoJson.cc:511
void AddSign(::identt::utils::SharedTable::pointer stptr, const google::protobuf::Message *result, const ::identt::query::PubKeyT *pubkey, std::string &output, SignatureListT &signatures)
AddSign: Add Signature pb, also adds older signatures.
bool JsonRequest(typename HttpServerT::ReqPtr request)
JsonRequest : Check if request is json from Content-Type and Accept fields.
Definition: ServiceBase.hpp:178
Definition: ParamLookupService.hpp:49
void LookupAction(::identt::utils::SharedTable::pointer stptr, ::identt::query::LookupDataT *lact)
LookupAction : Service Endpoint Lookup.
Definition: LookupService.cc:47
int json2pb(const std::string &json, google::protobuf::Message *msg, std::string &err)
json2pb : Convert json to protobuf
Definition: ProtoJson.cc:446
Definition: AccessKeyService.hpp:42