33 #ifndef _IDENTT_QUERY_THREEPID_SERVICE_HPP_ 34 #define _IDENTT_QUERY_THREEPID_SERVICE_HPP_ 43 template <
class HttpServerT>
67 identt::utils::SharedTable::pointer stptr,
68 typename std::shared_ptr<HttpServerT> server,
69 ::identt::query::HelpQuery::pointer helpquery,
71 :
identt::query::
ServiceBase<HttpServerT>(IDENTT_SERVICE_SCOPE_HTTP | IDENTT_SERVICE_SCOPE_HTTPS)
73 if (!(this->myscope & scope))
return;
76 helpquery->add({scope,
"POST _matrix/identity/api/v1/3pid/getValidated3pid", {
77 "A client can check whether ownership of a 3pid was validated by making a",
78 " request passing the sid and client_secret as query parameters from the requestToken call",
79 "params : sid , client_secret" 82 server->resource[
"/_matrix/identity/api/v1/3pid/getValidated3pid$"][
"POST"]
83 =[
this,stptr](
typename HttpServerT::RespPtr response,
typename HttpServerT::ReqPtr request) {
84 IDENTT_PARALLEL_ONE([
this,stptr,response,request] {
86 LOG(INFO) << request->path;
90 identt::query::GetValidated3pidDataT gva;
93 int stat =
json2pb( request->content.string() , gva.mutable_subtok() , err);
96 form2pb( request->content.string() , gva.mutable_subtok());
99 identt::query::ValidatedAtT valresult;
102 if (gva.mutable_subtok()->sid().length()==0)
103 throw ::identt::query::SydentException(
"sid value required", M_MISSING_PARAMS);
104 if (gva.mutable_subtok()->client_secret().length()==0)
105 throw ::identt::query::SydentException(
"client_secret value required", M_MISSING_PARAMS);
107 if (stptr->is_master.Get())
113 hclient.
SendToMaster(stptr,::identt::hrpc::M_GETVALIDATED3PID,&gva);
118 pb2json(gva.mutable_valresult() , output);
119 this->
HttpOKAction(response,request,200,
"OK",
"application/json",output,
true);
122 int ecode = (e.ecode()>=IDENTT_SYDENT_ERROR_MIN && e.ecode()<=IDENTT_SYDENT_ERROR_MAX) ? e.ecode() : M_UNKNOWN;
123 std::string output =
err2json(SydentErrors.at(ecode),e.what());
124 this->
HttpOKAction(response,request,200,
"OK",
"application/json",output,
true);
127 std::string output =
err2json(SydentErrors.at(M_UNKNOWN),e.what());
128 this->
HttpOKAction(response,request,200,
"OK",
"application/json",output,
true);
129 }
catch (std::exception& e)
131 std::string output =
err2json(SydentErrors.at(M_UNKNOWN),e.what());
132 this->
HttpOKAction(response,request,200,
"OK",
"application/json",output,
true);
141 helpquery->add({scope,
"GET _matrix/identity/api/v1/3pid/getValidated3pid", {
142 "This is the GET version of getValidated3pid",
143 "A client can check whether ownership of a 3pid was validated by making a",
144 " request passing the sid and client_secret as query parameters from the requestToken call",
145 "params : sid , client_secret" 149 server->resource[
"/_matrix/identity/api/v1/3pid/getValidated3pid\\\?(.*)$"][
"GET"]
150 =[
this,stptr](
typename HttpServerT::RespPtr response,
typename HttpServerT::ReqPtr request) {
151 IDENTT_PARALLEL_ONE([
this,stptr,response,request] {
153 LOG(INFO) << request->path;
155 std::string params = request->path_match[1];
157 identt::query::GetValidated3pidDataT gva;
158 form2pb( params , gva.mutable_subtok());
162 if (stptr->is_master.Get())
168 hclient.
SendToMaster(stptr,::identt::hrpc::M_GETVALIDATED3PID,&gva);
173 pb2json(gva.mutable_valresult() , output);
174 this->
HttpOKAction(response,request,200,
"OK",
"application/json",output,
true);
177 int ecode = (e.ecode()>=IDENTT_SYDENT_ERROR_MIN && e.ecode()<=IDENTT_SYDENT_ERROR_MAX) ? e.ecode() : M_UNKNOWN;
178 std::string output =
err2json(SydentErrors.at(ecode),e.what());
179 this->
HttpOKAction(response,request,200,
"OK",
"application/json",output,
true);
182 std::string output =
err2json(SydentErrors.at(M_UNKNOWN),e.what());
183 this->
HttpOKAction(response,request,200,
"OK",
"application/json",output,
true);
184 }
catch (std::exception& e)
186 std::string output =
err2json(SydentErrors.at(M_UNKNOWN),e.what());
187 this->
HttpOKAction(response,request,200,
"OK",
"application/json",output,
true);
196 helpquery->add({scope,
"POST _matrix/identity/api/v1/3pid/bind", {
197 "An association between a session and a Matrix user ID can be published by making a request to this.",
198 "params: sid , client_secret , mxid",
199 "where mxid takes the form @name:domain.com",
200 "If the session is still valid, this will publish an association between the 3pids validated on that",
201 "session and the passed Matrix user ID. Future calls to /lookup for any of the session’s 3pids",
202 "will return this association. If the 3pid has not yet been validated, the HTTP request will be",
203 "rejected, and the association will not be established. If the sid and client_secret were not recognised",
204 "or were not correct, an error will be returned." 208 server->resource[
"/_matrix/identity/api/v1/3pid/bind$"][
"POST"]
209 =[
this,stptr](
typename HttpServerT::RespPtr response,
typename HttpServerT::ReqPtr request) {
210 IDENTT_PARALLEL_ONE([
this,stptr,response,request] {
212 LOG(INFO) << request->path;
216 identt::query::Bind3pidDataT bpa;
219 int stat =
json2pb( request->content.string() , bpa.mutable_subtok() , err);
222 form2pb( request->content.string() , bpa.mutable_subtok());
227 if (bpa.mutable_subtok()->sid().length()==0)
228 throw ::identt::query::SydentException(
"sid value required", M_MISSING_PARAMS);
229 if (bpa.mutable_subtok()->client_secret().length()==0)
230 throw ::identt::query::SydentException(
"client_secret value required", M_MISSING_PARAMS);
231 if (bpa.mutable_subtok()->mxid().length()==0)
232 throw ::identt::query::SydentException(
"mxid value required", M_MISSING_PARAMS);
234 if (stptr->is_master.Get())
240 hclient.
SendToMaster(stptr,::identt::hrpc::M_BIND3PID,&bpa);
245 std::string output = bpa.output();
248 std::vector<std::string> splitres;
249 boost::algorithm::split(splitres, bpa.mutable_subtok()->mxid() , boost::algorithm::is_any_of(
":") );
250 if (splitres.size()>=2)
253 std::string up =splitres[1];
254 for (
auto i=2;i<splitres.size();++i) up +=
":" + splitres[i];
256 std::string url =
"https://" + up +
"/_matrix/federation/v1/3pid/onbind";
259 bool status = mclient.
PostJson(stptr,url,output,ret,
true);
260 LOG(INFO) <<
"Posted to " << url <<
" for " << splitres[0] <<
" status " << status;
263 this->
HttpOKAction(response,request,200,
"OK",
"application/json",output,
true);
267 int ecode = (e.ecode()>=IDENTT_SYDENT_ERROR_MIN && e.ecode()<=IDENTT_SYDENT_ERROR_MAX) ? e.ecode() : M_UNKNOWN;
268 std::string output =
err2json(SydentErrors.at(ecode),e.what());
269 this->
HttpOKAction(response,request,200,
"OK",
"application/json",output,
true);
272 std::string output =
err2json(SydentErrors.at(M_UNKNOWN),e.what());
273 this->
HttpOKAction(response,request,200,
"OK",
"application/json",output,
true);
274 }
catch (std::exception& e)
276 std::string output =
err2json(SydentErrors.at(M_UNKNOWN),e.what());
277 this->
HttpOKAction(response,request,200,
"OK",
"application/json",output,
true);
285 helpquery->add({scope,
"GET _matrix/identity/api/v1/3pid/bind", {
286 "This is the GET version of 3pid/bind",
287 "An association between a session and a Matrix user ID can be published by making a request to this.",
288 "params: sid , client_secret , mxid",
289 "where mxid takes the form @name:domain.com" 293 server->resource[
"/_matrix/identity/api/v1/3pid/bind\\\?(.*)$"][
"GET"]
294 =[
this,stptr](
typename HttpServerT::RespPtr response,
typename HttpServerT::ReqPtr request) {
295 IDENTT_PARALLEL_ONE([
this,stptr,response,request] {
297 LOG(INFO) << request->path;
299 std::string params = request->path_match[1];
301 identt::query::Bind3pidDataT bpa;
302 form2pb( params , bpa.mutable_subtok());
306 if (bpa.mutable_subtok()->sid().length()==0)
307 throw ::identt::query::SydentException(
"sid value required", M_MISSING_PARAMS);
308 if (bpa.mutable_subtok()->client_secret().length()==0)
309 throw ::identt::query::SydentException(
"client_secret value required", M_MISSING_PARAMS);
310 if (bpa.mutable_subtok()->mxid().length()==0)
311 throw ::identt::query::SydentException(
"mxid value required", M_MISSING_PARAMS);
313 if (stptr->is_master.Get())
319 hclient.
SendToMaster(stptr,::identt::hrpc::M_BIND3PID,&bpa);
324 std::string output = bpa.output();
327 std::vector<std::string> splitres;
328 boost::algorithm::split(splitres, bpa.mutable_subtok()->mxid() , boost::algorithm::is_any_of(
":") );
329 if (splitres.size()==2)
332 std::string up =splitres[1];
333 for (
auto i=2;i<splitres.size();++i) up +=
":" + splitres[i];
335 std::string url =
"https://" + up +
"/_matrix/federation/v1/3pid/onbind";
338 bool status = mclient.
PostJson(stptr,url,output,ret,
true);
339 LOG(INFO) <<
"Posted to " << url <<
" for " << splitres[0] <<
" status " << status;
342 this->
HttpOKAction(response,request,200,
"OK",
"application/json",output,
true);
345 int ecode = (e.ecode()>=IDENTT_SYDENT_ERROR_MIN && e.ecode()<=IDENTT_SYDENT_ERROR_MAX) ? e.ecode() : M_UNKNOWN;
346 std::string output =
err2json(SydentErrors.at(ecode),e.what());
347 this->
HttpOKAction(response,request,200,
"OK",
"application/json",output,
true);
350 std::string output =
err2json(SydentErrors.at(M_UNKNOWN),e.what());
351 this->
HttpOKAction(response,request,200,
"OK",
"application/json",output,
true);
352 }
catch (std::exception& e)
354 std::string output =
err2json(SydentErrors.at(M_UNKNOWN),e.what());
355 this->
HttpOKAction(response,request,200,
"OK",
"application/json",output,
true);
370 #endif // _IDENTT_QUERY_THREEPID_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
void Bind3pidAction(::identt::utils::SharedTable::pointer stptr, ::identt::query::Bind3pidDataT *bpa)
Bind3pidAction: Bind a 3pid Request.
Definition: ThreePidService.cc:93
void GetValidated3pidAction(::identt::utils::SharedTable::pointer stptr, ::identt::query::GetValidated3pidDataT *gva)
GetValidated3pidAction: Validate a 3pid Request.
Definition: ThreePidService.cc:58
Sydent Exceptions.
Definition: SydentQuery.hpp:91
Definition: ThreePidService.hpp:44
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: HttpClient.hpp:44
Definition: BaseUtils.hpp:52
void pb2json(const google::protobuf::Message *msg, std::string &str)
pb2json : Convert protobuf to json
Definition: ProtoJson.cc:415
Definition: ThreePidService.hpp:42
Definition: ServiceBase.hpp:55
Definition: CryptoBase.hpp:49
Definition: BaseUtils.hpp:89
std::string err2json(const std::string errorcode, const std::string error)
err2json : Generate a Json for Error Message
Definition: ProtoJson.cc:511
bool JsonRequest(typename HttpServerT::ReqPtr request)
JsonRequest : Check if request is json from Content-Type and Accept fields.
Definition: ServiceBase.hpp:178
Definition: HrpcClient.hpp:46
ThreePidService(identt::utils::SharedTable::pointer stptr, typename std::shared_ptr< HttpServerT > server, ::identt::query::HelpQuery::pointer helpquery, unsigned int scope)
ThreePidService : constructor.
Definition: ThreePidService.hpp:66
bool PostJson(::identt::utils::SharedTable::pointer stptr, std::string url, std::string &payload, std::string &returns, bool nothrow=false)
PostJson : send json to remote and get output.
int json2pb(const std::string &json, google::protobuf::Message *msg, std::string &err)
json2pb : Convert json to protobuf
Definition: ProtoJson.cc:446
bool SendToMaster(::identt::utils::SharedTable::pointer stptr, ::identt::hrpc::MasterCmdTypeE service_id, google::protobuf::Message *msg, bool nothrow=false)
SendToMaster : send to master and get output.