33 #ifndef _IDENTT_QUERY_LOOKUP_SERVICE_HPP_ 34 #define _IDENTT_QUERY_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", {
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." 91 server->resource[
"/_matrix/identity/api/v1/lookup$"][
"POST"]
92 =[
this,stptr](
typename HttpServerT::RespPtr response,
typename HttpServerT::ReqPtr request) {
93 IDENTT_PARALLEL_ONE([
this,stptr,response,request] {
95 LOG(INFO) << request->path;
99 identt::query::LookupDataT lact;
102 int stat =
json2pb( request->content.string() , lact.mutable_query() , err);
105 form2pb( request->content.string() , lact.mutable_query() );
112 if (stptr->lookup_requires_key.Get()) {
113 bool is_server_query =
false;
114 auto it=request->header.find(
"Shared-Secret");
115 if (it!=request->header.end()) {
116 is_server_query = (it->second == stptr->shared_secret.Get());
118 if (!is_server_query ) {
129 ::identt::query::PubKeyT pubkey;
132 pubkey.set_owner(stptr->hostname.Get());
133 pubkey.set_algo(THREEPID_DEFAULT_ALGO);
134 pubkey.set_identifier(THREEPID_DEFAULT_ALGO_ID);
137 std::vector<::identt::query::SignatureT> signatures;
138 lservice.
AddSign(stptr, lact.mutable_result() , &pubkey, output,signatures);
139 if (!output.length()) output=
"{}";
141 this->
HttpOKAction(response,request,200,
"OK",
"application/json",output,
true);
144 int ecode = (e.ecode()>=IDENTT_SYDENT_ERROR_MIN && e.ecode()<=IDENTT_SYDENT_ERROR_MAX) ? e.ecode() : M_UNKNOWN;
145 std::string output =
err2json(SydentErrors.at(ecode),e.what());
146 this->
HttpOKAction(response,request,200,
"OK",
"application/json",output,
true);
149 std::string output =
err2json(SydentErrors.at(M_UNKNOWN),e.what());
150 this->
HttpOKAction(response,request,200,
"OK",
"application/json",output,
true);
151 }
catch (std::exception& e)
153 std::string output =
err2json(SydentErrors.at(M_UNKNOWN),e.what());
154 this->
HttpOKAction(response,request,200,
"OK",
"application/json",output,
true);
163 helpquery->add({scope,
"GET _matrix/identity/api/v1/lookup", {
164 "Look up the Matrix user ID for a 3pid.",
165 "This is the GET version of lookup",
166 "query parameters: medium, address",
167 "medium Required. The literal string email.",
168 "address Required. The email address being looked up.",
169 "accesskey Required if lookup_requires_accesskey is set." 173 server->resource[
"/_matrix/identity/api/v1/lookup\\\?(.*)$"][
"GET"]
174 =[
this,stptr](
typename HttpServerT::RespPtr response,
typename HttpServerT::ReqPtr request) {
175 IDENTT_PARALLEL_ONE([
this,stptr,response,request] {
177 LOG(INFO) << request->path;
179 std::string params = request->path_match[1];
180 identt::query::LookupDataT lact;
181 form2pb( params , lact.mutable_query());
188 if (stptr->lookup_requires_key.Get()) {
189 bool is_server_query =
false;
190 auto it=request->header.find(
"Shared-Secret");
191 if (it!=request->header.end()) {
192 is_server_query = (it->second == stptr->shared_secret.Get());
194 if (!is_server_query ) {
205 ::identt::query::PubKeyT pubkey;
208 pubkey.set_owner(stptr->hostname.Get());
209 pubkey.set_algo(THREEPID_DEFAULT_ALGO);
210 pubkey.set_identifier(THREEPID_DEFAULT_ALGO_ID);
213 std::vector<::identt::query::SignatureT> signatures;
214 lservice.
AddSign(stptr, lact.mutable_result() , &pubkey, output,signatures);
216 this->
HttpOKAction(response,request,200,
"OK",
"application/json",output,
true);
219 int ecode = (e.ecode()>=IDENTT_SYDENT_ERROR_MIN && e.ecode()<=IDENTT_SYDENT_ERROR_MAX) ? e.ecode() : M_UNKNOWN;
220 std::string output =
err2json(SydentErrors.at(ecode),e.what());
221 this->
HttpOKAction(response,request,200,
"OK",
"application/json",output,
true);
224 std::string output =
err2json(SydentErrors.at(M_UNKNOWN),e.what());
225 this->
HttpOKAction(response,request,200,
"OK",
"application/json",output,
true);
226 }
catch (std::exception& e)
228 std::string output =
err2json(SydentErrors.at(M_UNKNOWN),e.what());
229 this->
HttpOKAction(response,request,200,
"OK",
"application/json",output,
true);
238 helpquery->add({scope,
"POST _matrix/identity/api/v1/bulk_lookup", {
239 "Look up the Matrix user ID for a list of 3pid.",
240 "query parameters: list of {medium, address}",
241 "medium Required. The literal string email.",
242 "address Required. The email address being looked up.",
243 "accesskey Required if lookup_requires_accesskey is set." 247 server->resource[
"/_matrix/identity/api/v1/bulk_lookup$"][
"POST"]
248 =[
this,stptr](
typename HttpServerT::RespPtr response,
typename HttpServerT::ReqPtr request) {
249 IDENTT_PARALLEL_ONE([
this,stptr,response,request] {
251 LOG(INFO) << request->path;
255 identt::query::BulkLookupDataT blact;
258 int stat =
json2pb( request->content.string() , blact.mutable_query() , err);
263 DLOG(INFO) << blact.DebugString();
269 if (stptr->lookup_requires_key.Get()) {
270 bool is_server_query =
false;
271 auto it=request->header.find(
"Shared-Secret");
272 if (it!=request->header.end()) {
273 is_server_query = (it->second == stptr->shared_secret.Get());
275 if (!is_server_query ) {
283 std::string instring;
284 this->BulkLookupJson(blact.mutable_result(),instring);
288 ::identt::query::PubKeyT pubkey;
291 pubkey.set_owner(stptr->hostname.Get());
292 pubkey.set_algo(THREEPID_DEFAULT_ALGO);
293 pubkey.set_identifier(THREEPID_DEFAULT_ALGO_ID);
298 std::vector<::identt::query::SignatureT> signatures;
299 lservice.
AddSign(stptr, instring , &pubkey, output,signatures);
300 if (!output.length()) output=
"{}";
302 this->
HttpOKAction(response,request,200,
"OK",
"application/json",output,
true);
305 int ecode = (e.ecode()>=IDENTT_SYDENT_ERROR_MIN && e.ecode()<=IDENTT_SYDENT_ERROR_MAX) ? e.ecode() : M_UNKNOWN;
306 std::string output =
err2json(SydentErrors.at(ecode),e.what());
307 this->
HttpOKAction(response,request,200,
"OK",
"application/json",output,
true);
310 std::string output =
err2json(SydentErrors.at(M_UNKNOWN),e.what());
311 this->
HttpOKAction(response,request,200,
"OK",
"application/json",output,
true);
312 }
catch (std::exception& e)
314 std::string output =
err2json(SydentErrors.at(M_UNKNOWN),e.what());
315 this->
HttpOKAction(response,request,200,
"OK",
"application/json",output,
true);
338 void BulkLookupJson(identt::query::BulkLookupResultT* bulkres, std::string& output)
342 rapidjson::Pointer(
"/threepids").Create(d , allocator );
344 if (!threepids->IsArray()) threepids->SetArray();
346 for (
auto& res : bulkres->threepids() ) {
352 threepids->PushBack(entry,allocator);
354 rapidjson::StringBuffer buffer;
355 rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
358 output.append(buffer.GetString(), buffer.GetSize());
365 #endif // _IDENTT_QUERY_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
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
LookupService(identt::utils::SharedTable::pointer stptr, typename std::shared_ptr< HttpServerT > server, ::identt::query::HelpQuery::pointer helpquery, unsigned int scope)
LookupService : constructor.
Definition: LookupService.hpp:72
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: LookupService.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