33 #ifndef _IDENTT_QUERY_VALIDATE_SERVICE_HPP_ 34 #define _IDENTT_QUERY_VALIDATE_SERVICE_HPP_ 40 #ifndef IDENTT_SEND_SMS_REQUEST_TOKEN 41 #define IDENTT_SEND_SMS_REQUEST_TOKEN 47 template <
class HttpServerT>
72 identt::utils::SharedTable::pointer stptr,
73 typename std::shared_ptr<HttpServerT> server,
74 ::identt::query::HelpQuery::pointer helpquery,
76 :
identt::query::
ServiceBase<HttpServerT>(IDENTT_SERVICE_SCOPE_HTTP | IDENTT_SERVICE_SCOPE_HTTPS)
78 if (!(this->myscope & scope))
return;
82 helpquery->add({scope,
"POST _matrix/identity/api/v1/validate/{service:string}/requestToken", {
83 "service can be email or msisdn ",
84 "params : email , client_secret , send_attempt, next_link",
85 "params : phone_number, country , client_secret , send_attempt, next_link",
86 "This will create a new session on the identity service, identified by an sid.",
87 "It may also optionally specify next_link to redirect on successful validation.",
88 "The identity service will send an email/sms containing a token to verify.",
89 "We return the sid generated for this session to the caller.",
90 "If a send_attempt is specified, the server will only send an request if the ",
91 "send_attempt is a number greater than the most recent one which it has seen",
92 "Note that Home Servers offer APIs that proxy this API" 96 server->resource[
"/_matrix/identity/api/v1/validate/(email|msisdn)/requestToken$"][
"POST"]
97 =[
this,stptr](
typename HttpServerT::RespPtr response,
typename HttpServerT::ReqPtr request) {
98 IDENTT_PARALLEL_ONE([
this,stptr,response,request] {
100 LOG(INFO) << request->path;
102 identt::query::RequestTokenDataT rtoka;
103 rtoka.set_medium ( request->path_match[1] );
108 int stat =
json2pb( request->content.string() , rtoka.mutable_reqtok() , err);
111 form2pb( request->content.string() , rtoka.mutable_reqtok());
115 DLOG(INFO) << rtoka.DebugString();
117 if (stptr->is_master.Get())
123 hclient.
SendToMaster(stptr,::identt::hrpc::M_REQUESTTOKEN,&rtoka);
126 IDENTT_SEND_SMS_REQUEST_TOKEN
129 <<
"phone_number=\"" << rtoka.mutable_reqtok()->phone_number() <<
"\"" 130 <<
" token=\"" << rtoka.token()
131 <<
"\" sid=\"" << rtoka.mutable_ssid()->sid() <<
"\"";
135 pb2json(rtoka.mutable_ssid() , output);
136 this->
HttpOKAction(response,request,200,
"OK",
"application/json",output,
true);
139 int ecode = (e.ecode()>=IDENTT_SYDENT_ERROR_MIN && e.ecode()<=IDENTT_SYDENT_ERROR_MAX) ? e.ecode() : M_UNKNOWN;
140 std::string output =
err2json(SydentErrors.at(ecode),e.what());
141 this->
HttpOKAction(response,request,200,
"OK",
"application/json",output,
true);
144 std::string output =
err2json(SydentErrors.at(M_UNKNOWN),e.what());
145 this->
HttpOKAction(response,request,200,
"OK",
"application/json",output,
true);
146 }
catch (std::exception& e)
148 std::string output =
err2json(SydentErrors.at(M_UNKNOWN),e.what());
149 this->
HttpOKAction(response,request,200,
"OK",
"application/json",output,
true);
159 helpquery->add({scope,
"GET _matrix/identity/api/v1/validate/{service:string}/requestToken?{params}", {
160 "service can be email or msisdn ",
161 "params : email , client_secret , send_attempt, next_link",
162 "params : phone_number, country , client_secret , send_attempt, next_link",
163 "This is the GET version of requestToken" 164 "Note that Home Servers offer APIs that proxy this API" 168 server->resource[
"/_matrix/identity/api/v1/validate/(email|msisdn)/requestToken\\\?(.*)$"][
"GET"]
169 =[
this,stptr](
typename HttpServerT::RespPtr response,
typename HttpServerT::ReqPtr request) {
170 IDENTT_PARALLEL_ONE([
this,stptr,response,request] {
172 LOG(INFO) << request->path;
174 identt::query::RequestTokenDataT rtoka;
175 rtoka.set_medium ( request->path_match[1] );
177 std::string params = request->path_match[2];
178 form2pb( params , rtoka.mutable_reqtok());
182 DLOG(INFO) << rtoka.DebugString();
185 if (stptr->is_master.Get())
191 hclient.
SendToMaster(stptr,::identt::hrpc::M_REQUESTTOKEN,&rtoka);
194 IDENTT_SEND_SMS_REQUEST_TOKEN
196 LOG(INFO) <<
"token=\"" << rtoka.token() <<
"\" sid=\"" << rtoka.ssid().sid() <<
"\"";
200 pb2json(rtoka.mutable_ssid() , output);
201 this->
HttpOKAction(response,request,200,
"OK",
"application/json",output,
true);
204 int ecode = (e.ecode()>=IDENTT_SYDENT_ERROR_MIN && e.ecode()<=IDENTT_SYDENT_ERROR_MAX) ? e.ecode() : M_UNKNOWN;
205 std::string output =
err2json(SydentErrors.at(ecode),e.what());
206 this->
HttpOKAction(response,request,200,
"OK",
"application/json",output,
true);
209 std::string output =
err2json(SydentErrors.at(M_UNKNOWN),e.what());
210 this->
HttpOKAction(response,request,200,
"OK",
"application/json",output,
true);
211 }
catch (std::exception& e)
213 std::string output =
err2json(SydentErrors.at(M_UNKNOWN),e.what());
214 this->
HttpOKAction(response,request,200,
"OK",
"application/json",output,
true);
224 helpquery->add({scope,
"POST _matrix/identity/api/v1/validate/{service:string}/submitToken", {
225 "service can be email or msisdn ",
226 "A user may make a POST request with the following parameters",
227 "sid: the sid for the session, generated by the requestToken call.",
228 "client_secret: the client secret which was supplied to the requestToken call.",
229 "token: the token generated by the requestToken call, and emailed to the user.",
230 "If these three values are consistent with a set generated by a requestToken call,",
231 "ownership of the email address is considered to have been validated.",
232 "Note that Home Servers offer APIs that proxy this API" 236 server->resource[
"/_matrix/identity/api/v1/validate/(email|msisdn)/submitToken$"][
"POST"]
237 =[
this,stptr](
typename HttpServerT::RespPtr response,
typename HttpServerT::ReqPtr request) {
238 IDENTT_PARALLEL_ONE([
this,stptr,response,request] {
240 LOG(INFO) << request->path;
242 identt::query::SubmitTokenDataT stoka;
243 stoka.set_medium ( request->path_match[1] );
248 int stat =
json2pb( request->content.string() , stoka.mutable_subtok() , err);
251 form2pb( request->content.string() , stoka.mutable_subtok());
256 if (stptr->is_master.Get())
262 hclient.
SendToMaster(stptr,::identt::hrpc::M_SUBMITTOKEN,&stoka);
267 pb2json(stoka.mutable_result() , output);
268 this->
HttpOKAction(response,request,200,
"OK",
"application/json",output,
true);
271 int ecode = (e.ecode()>=IDENTT_SYDENT_ERROR_MIN && e.ecode()<=IDENTT_SYDENT_ERROR_MAX) ? e.ecode() : M_UNKNOWN;
272 std::string output =
err2json(SydentErrors.at(ecode),e.what());
273 this->
HttpOKAction(response,request,200,
"OK",
"application/json",output,
true);
276 std::string output =
err2json(SydentErrors.at(M_UNKNOWN),e.what());
277 this->
HttpOKAction(response,request,200,
"OK",
"application/json",output,
true);
278 }
catch (std::exception& e)
280 std::string output =
err2json(SydentErrors.at(M_UNKNOWN),e.what());
281 this->
HttpOKAction(response,request,200,
"OK",
"application/json",output,
true);
291 helpquery->add({scope,
"GET _matrix/identity/api/v1/validate/{service:string}/submitToken?{params}", {
292 "service can be email or msisdn ",
293 "params : sid , client_secret , token ",
294 "This is the GET version of submitToken" 295 "Note that Home Servers offer APIs that proxy this API" 299 server->resource[
"/_matrix/identity/api/v1/validate/(email|msisdn)/submitToken\\\?(.*)$"][
"GET"]
300 =[
this,stptr](
typename HttpServerT::RespPtr response,
typename HttpServerT::ReqPtr request) {
301 IDENTT_PARALLEL_ONE([
this,stptr,response,request] {
303 LOG(INFO) << request->path;
305 identt::query::SubmitTokenDataT stoka;
306 stoka.set_medium ( request->path_match[1] );
307 std::string params = request->path_match[2];
309 form2pb( params , stoka.mutable_subtok());
313 if (stptr->is_master.Get())
319 hclient.
SendToMaster(stptr,::identt::hrpc::M_SUBMITTOKEN,&stoka);
324 pb2json(stoka.mutable_result() , output);
325 this->
HttpOKAction(response,request,200,
"OK",
"application/json",output,
true);
328 int ecode = (e.ecode()>=IDENTT_SYDENT_ERROR_MIN && e.ecode()<=IDENTT_SYDENT_ERROR_MAX) ? e.ecode() : M_UNKNOWN;
329 std::string output =
err2json(SydentErrors.at(ecode),e.what());
330 this->
HttpOKAction(response,request,200,
"OK",
"application/json",output,
true);
333 std::string output =
err2json(SydentErrors.at(M_UNKNOWN),e.what());
334 this->
HttpOKAction(response,request,200,
"OK",
"application/json",output,
true);
335 }
catch (std::exception& e)
337 std::string output =
err2json(SydentErrors.at(M_UNKNOWN),e.what());
338 this->
HttpOKAction(response,request,200,
"OK",
"application/json",output,
true);
353 #endif // _IDENTT_QUERY_VALIDATE_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
Definition: ValidateService.hpp:42
Definition: ValidateService.hpp:48
Sydent Exceptions.
Definition: SydentQuery.hpp:91
void SubmitTokenAction(::identt::utils::SharedTable::pointer stptr, ::identt::query::SubmitTokenDataT *stoka)
SubmitTokenAction : Service Endpoint SubmitToken.
Definition: ValidateService.cc:154
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
void pb2json(const google::protobuf::Message *msg, std::string &str)
pb2json : Convert protobuf to json
Definition: ProtoJson.cc:415
Definition: ServiceBase.hpp:55
Definition: CryptoBase.hpp:49
Definition: BaseUtils.hpp:89
void RequestTokenAction(::identt::utils::SharedTable::pointer stptr, ::identt::query::RequestTokenDataT *rtoka)
RequestTokenAction : Service Endpoint RequestToken.
Definition: ValidateService.cc:47
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
ValidateService(identt::utils::SharedTable::pointer stptr, typename std::shared_ptr< HttpServerT > server, ::identt::query::HelpQuery::pointer helpquery, unsigned int scope)
ValidateService : constructor.
Definition: ValidateService.hpp:71
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.