identt
HrpcMasterEndpointService.hpp
Go to the documentation of this file.
1 
33 #ifndef _IDENTT_HRPC_MASTER_ENDPOINT_SERVICE_HPP_
34 #define _IDENTT_HRPC_MASTER_ENDPOINT_SERVICE_HPP_
35 
36 
37 #include "query/QueryBase.hpp"
39 #include "store/InviteService.hpp"
40 #include "store/LookupService.hpp"
41 #include "store/MailSmsService.hpp"
42 #include "store/PubKeyService.hpp"
46 #include "store/StoreTrans.hpp"
47 #include "hrpc/RemoteKeeper.hpp"
48 
49 namespace identt {
50 namespace hrpc {
51 
52 template <class HttpServerT>
54  public identt::query::ServiceBase<HttpServerT>,
55  protected identt::hrpc::ProtoServiceBase<HttpServerT> {
56 public:
57 
78  identt::utils::SharedTable::pointer stptr,
79  typename std::shared_ptr<HttpServerT> server,
80  ::identt::hrpc::RemoteKeeper::pointer rkeeper,
81  unsigned int scope)
82  :
83  identt::query::ServiceBase<HttpServerT>(IDENTT_SERVICE_SCOPE_HTTP | IDENTT_SERVICE_SCOPE_HTTPS)
84  {
85  if (!(this->myscope & scope)) return; // scope mismatch
86 
87  // Endpoint : POST _identt/identity/master/v1/endpoint
88 
89  server->resource["/_identt/identity/master/v1/endpoint$"]["POST"]
90  =[this,stptr,rkeeper](typename HttpServerT::RespPtr response, typename HttpServerT::ReqPtr request) {
91  IDENTT_PARALLEL_ONE([this,stptr,response,request] {
92  std::string output;
93  int ecode=M_UNKNOWN;
94  try
95  {
96  if (!this->ProtoRequest(request))
97  throw identt::BadDataException("Only Protobuf Supported");
98  if (!this->SharedSecretOK(request,stptr->shared_secret.Get()))
99  throw identt::BadDataException("Bad Shared Secret");
100 
101  auto sname = this->ServiceName(request);
102 
103  if (!stptr->is_ready.Get()) throw identt::BadDataException("System Not Ready");
104  if (!stptr->is_master.Get()) throw identt::BadDataException("System Not Master");
105 
106  // start here
107  switch(sname) {
108  default: {
109  DLOG(INFO) << request->path;
110  throw identt::BadDataException("Service Name unknown or unimplemented");
111  break;
112  }
113  case ::identt::hrpc::M_REQUESTTOKEN : {
114  identt::query::RequestTokenDataT data;
115  if (!data.ParseFromString( b64_decode(request->content.string()) ))
116  throw identt::BadDataException("Bad Protobuf Format");
117  // action
118  identt::store::ValidateService service; //RequestTokenAction
119  service.RequestTokenAction(stptr, &data);
120  // aftermath
121  data.SerializeToString(&output);
122  DLOG(INFO) << request->path;
123  break;
124  }
125  case ::identt::hrpc::M_SUBMITTOKEN : {
126  identt::query::SubmitTokenDataT data;
127  if (!data.ParseFromString( b64_decode(request->content.string()) ))
128  throw identt::BadDataException("Bad Protobuf Format");
129  // action
130  identt::store::ValidateService service; //SubmitTokenAction
131  service.SubmitTokenAction(stptr, &data);
132  // aftermath
133  data.SerializeToString(&output);
134  DLOG(INFO) << request->path;
135  break;
136  }
137  case ::identt::hrpc::M_GETVALIDATED3PID : {
138  identt::query::GetValidated3pidDataT data;
139  if (!data.ParseFromString( b64_decode(request->content.string()) ))
140  throw identt::BadDataException("Bad Protobuf Format");
141  // action
142  identt::store::ThreePidService service; //GetValidated3pidAction
143  service.GetValidated3pidAction(stptr, &data);
144  // aftermath
145  data.SerializeToString(&output);
146  DLOG(INFO) << request->path;
147  break;
148  }
149  case ::identt::hrpc::M_BIND3PID : {
150  identt::query::Bind3pidDataT data;
151  if (!data.ParseFromString( b64_decode(request->content.string()) ))
152  throw identt::BadDataException("Bad Protobuf Format");
153  // action
154  identt::store::ThreePidService service; //Bind3pidAction
155  service.Bind3pidAction(stptr, &data);
156  // aftermath
157  data.SerializeToString(&output);
158  DLOG(INFO) << request->path;
159  break;
160  }
161  case ::identt::hrpc::M_LOOKUP : {
162  identt::query::LookupDataT data;
163  if (!data.ParseFromString( b64_decode(request->content.string()) ))
164  throw identt::BadDataException("Bad Protobuf Format");
165  // action
166  identt::store::LookupService service; //LookupAction
167  service.LookupAction(stptr, &data);
168  // aftermath
169  data.SerializeToString(&output);
170  DLOG(INFO) << request->path;
171  break;
172  }
173  case ::identt::hrpc::M_BULKLOOKUP : {
174  identt::query::BulkLookupDataT data;
175  if (!data.ParseFromString( b64_decode(request->content.string()) ))
176  throw identt::BadDataException("Bad Protobuf Format");
177  // action
178  identt::store::LookupService service; //BulkLookupAction
179  service.BulkLookupAction(stptr, &data);
180  // aftermath
181  data.SerializeToString(&output);
182  DLOG(INFO) << request->path;
183  break;
184  }
185  case ::identt::hrpc::M_GETPUBKEY : {
186  identt::query::PubKeyT data;
187  if (!data.ParseFromString( b64_decode(request->content.string()) ))
188  throw identt::BadDataException("Bad Protobuf Format");
189  // action
190  identt::store::PubKeyService service; //GetPubKeyAction
191  service.GetPubKeyAction(stptr, &data);
192  // aftermath
193  data.SerializeToString(&output);
194  DLOG(INFO) << request->path;
195  break;
196  }
197  case ::identt::hrpc::M_GETPUBKEYVALID : {
198  identt::query::PubKeyT data;
199  if (!data.ParseFromString( b64_decode(request->content.string()) ))
200  throw identt::BadDataException("Bad Protobuf Format");
201  // action
202  identt::store::PubKeyService service; //GetPubKeyValidAction
203  service.GetPubKeyValidAction(stptr, &data);
204  // aftermath
205  data.SerializeToString(&output);
206  DLOG(INFO) << request->path;
207  break;
208  }
209  case ::identt::hrpc::M_GETEPHEMERALVALID : {
210  identt::query::PubKeyT data;
211  if (!data.ParseFromString( b64_decode(request->content.string()) ))
212  throw identt::BadDataException("Bad Protobuf Format");
213  // action
214  identt::store::PubKeyService service; //GetEphemeralValidAction
215  service.GetEphemeralValidAction(stptr, &data);
216  // aftermath
217  data.SerializeToString(&output);
218  DLOG(INFO) << request->path;
219  break;
220  }
221  case ::identt::hrpc::M_STOREINVITE : {
222  identt::query::StoreInviteDataT data;
223  if (!data.ParseFromString( b64_decode(request->content.string()) ))
224  throw identt::BadDataException("Bad Protobuf Format");
225  // action
226  identt::store::InviteService service; //StoreInviteAction
227  service.StoreInviteAction(stptr,&data);
228  // aftermath
229  data.SerializeToString(&output);
230  DLOG(INFO) << request->path;
231  break;
232  }
233  case ::identt::hrpc::M_PENDING : {
234  identt::mail::MailQueryT data;
235  if (!data.ParseFromString( b64_decode(request->content.string()) ))
236  throw identt::BadDataException("Bad Protobuf Format");
237  // action
238  identt::store::MailSmsService service; //PendingAction
239  service.PendingAction(stptr, &data);
240  // aftermath
241  data.SerializeToString(&output);
242  DLOG(INFO) << request->path;
243  break;
244  }
245  case ::identt::hrpc::M_GETACCESSKEY : {
246  identt::query::GetAccessDataT data;
247  if (!data.ParseFromString( b64_decode(request->content.string()) ))
248  throw identt::BadDataException("Bad Protobuf Format");
249  // action
250  identt::store::BlobDataService service; // GetAccessKeyAction
251  service.GetAccessKeyAction(stptr, &data);
252  // aftermath
253  data.SerializeToString(&output);
254  DLOG(INFO) << request->path;
255  break;
256  }
257  case ::identt::hrpc::M_SETBLOBDATA : {
258  identt::query::SetBlobDataT data;
259  if (!data.ParseFromString( b64_decode(request->content.string()) ))
260  throw identt::BadDataException("Bad Protobuf Format");
261  // action
262  identt::store::BlobDataService service; // GetBlobDataAction
263  service.SetBlobDataAction(stptr, &data);
264  // aftermath
265  data.SerializeToString(&output);
266  DLOG(INFO) << request->path;
267  break;
268  }
269  case ::identt::hrpc::M_GETBLOBDATA : {
270  identt::query::GetBlobDataT data;
271  if (!data.ParseFromString( b64_decode(request->content.string()) ))
272  throw identt::BadDataException("Bad Protobuf Format");
273  // action
274  identt::store::BlobDataService service; // GetBlobDataAction
275  service.GetBlobDataAction(stptr, &data);
276  // aftermath
277  data.SerializeToString(&output);
278  DLOG(INFO) << request->path;
279  break;
280  }
281  }
282  // end here
283  ecode=0; // something reached here
284 
285  } catch (::identt::query::SydentException& e)
286  {
287  int ecode = (e.ecode()>=IDENTT_SYDENT_ERROR_MIN && e.ecode()<=IDENTT_SYDENT_ERROR_MAX) ? e.ecode() : M_UNKNOWN;
288  output=e.what();
289  } catch (identt::JdException& e)
290  {
291  ecode=M_UNKNOWN; // default
292  output=e.what();
293  } catch (std::exception& e)
294  {
295  ecode=M_UNKNOWN; // default
296  output=e.what();
297  } catch (...)
298  {
299  ecode=M_UNKNOWN; // default
300  output="Unknown Error";
301  }
302 
303  if (ecode==0)
304  {
305  this->ServiceOKAction(response,request,b64_encode(output));
306  }
307  else
308  {
309  this->ServiceErrAction(response,request,ecode,output);
310  }
311  });
312  };
313  }
314 };
315 } // namespace query
316 } // namespace identt
317 
318 #endif // _IDENTT_HRPC_MASTER_ENDPOINT_SERVICE_HPP_
319 
void GetAccessKeyAction(::identt::utils::SharedTable::pointer stptr, ::identt::query::GetAccessDataT *gva)
GetAccessKeyAction : get the access key.
Definition: BlobDataService.cc:50
void SetBlobDataAction(::identt::utils::SharedTable::pointer stptr, ::identt::query::SetBlobDataT *bdata)
SetBlobDataAction : set the access key.
Definition: BlobDataService.cc:108
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
void GetPubKeyValidAction(::identt::utils::SharedTable::pointer stptr, ::identt::query::PubKeyT *pubkey)
GetPubKeyValidAction : Service Endpoint GetPubKeyValid.
Definition: PubKeyService.cc:58
Definition: ValidateService.hpp:42
void GetBlobDataAction(::identt::utils::SharedTable::pointer stptr, ::identt::query::GetBlobDataT *bdata)
GetBlobDataAction : get the access key.
Definition: BlobDataService.cc:156
ServiceBase(const unsigned int myscope_)
Constructor : to be used by inherited classes.
Definition: ServiceBase.hpp:65
void ServiceOKAction(typename HttpServerT::RespPtr response, typename HttpServerT::ReqPtr request, std::string &payload)
ServiceOKAction : OK Action template for service.
Definition: ProtoServiceBase.hpp:114
Definition: BlobDataService.hpp:42
int ServiceName(typename HttpServerT::ReqPtr request)
ServiceName : Check protobuf request service name.
Definition: ProtoServiceBase.hpp:93
Sydent Exceptions.
Definition: SydentQuery.hpp:91
void GetPubKeyAction(::identt::utils::SharedTable::pointer stptr, ::identt::query::PubKeyT *pubkey)
GetPubKeyAction : Service Endpoint GetPubKey.
Definition: PubKeyService.cc:43
void PendingAction(::identt::utils::SharedTable::pointer stptr, identt::mail::MailQueryT *mailq)
PendingAction : Get Pending as required.
Definition: MailSmsService.cc:46
Definition: HrpcMasterEndpointService.hpp:53
void SubmitTokenAction(::identt::utils::SharedTable::pointer stptr, ::identt::query::SubmitTokenDataT *stoka)
SubmitTokenAction : Service Endpoint SubmitToken.
Definition: ValidateService.cc:154
void StoreInviteAction(::identt::utils::SharedTable::pointer stptr, identt::query::StoreInviteDataT *inv)
StoreInviteAction : invite query internals.
Definition: InviteService.cc:52
Definition: BaseUtils.hpp:52
Definition: PubKeyService.hpp:42
Definition: MailSmsService.hpp:42
Definition: ThreePidService.hpp:42
void GetEphemeralValidAction(::identt::utils::SharedTable::pointer stptr, ::identt::query::PubKeyT *pubkey)
GetEphemeralValidAction : Service Endpoint GetEphemeralValid.
Definition: PubKeyService.cc:77
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
bool SharedSecretOK(typename HttpServerT::ReqPtr request, std::string sharedkey)
SharedSecretOK : Check if shared key header is present and ok.
Definition: ProtoServiceBase.hpp:77
void BulkLookupAction(::identt::utils::SharedTable::pointer stptr, ::identt::query::BulkLookupDataT *blact)
BulkLookupAction : Service Endpoint Lookup.
Definition: LookupService.cc:62
void ServiceErrAction(typename HttpServerT::RespPtr response, typename HttpServerT::ReqPtr request, int ecode, std::string &emsg)
ServiceErrAction : Error Action template for service.
Definition: ProtoServiceBase.hpp:173
Definition: ProtoServiceBase.hpp:43
Definition: LookupService.hpp:43
bool ProtoRequest(typename HttpServerT::ReqPtr request)
ProtoRequest : Check if request is protobuf from Content-Type and Accept fields.
Definition: ProtoServiceBase.hpp:55
HrpcMasterEndpointService(identt::utils::SharedTable::pointer stptr, typename std::shared_ptr< HttpServerT > server, ::identt::hrpc::RemoteKeeper::pointer rkeeper, unsigned int scope)
HrpcMasterEndpointService : constructor.
Definition: HrpcMasterEndpointService.hpp:77
void LookupAction(::identt::utils::SharedTable::pointer stptr, ::identt::query::LookupDataT *lact)
LookupAction : Service Endpoint Lookup.
Definition: LookupService.cc:47
Definition: InviteService.hpp:42