identt
InviteService.hpp
Go to the documentation of this file.
1 
33 #ifndef _IDENTT_QUERY_INVITE_SERVICE_HPP_
34 #define _IDENTT_QUERY_INVITE_SERVICE_HPP_
35 
36 #include <query/QueryBase.hpp>
37 #include <hrpc/HrpcClient.hpp>
38 #include <store/InviteService.hpp>
40 
41 namespace identt {
42 namespace query {
43 
44 template <class HttpServerT>
45 class InviteService : public identt::query::ServiceBase<HttpServerT> {
46 public:
47 
68  identt::utils::SharedTable::pointer stptr,
69  typename std::shared_ptr<HttpServerT> server,
70  ::identt::query::HelpQuery::pointer helpquery,
71  unsigned int scope)
72  : identt::query::ServiceBase<HttpServerT>(IDENTT_SERVICE_SCOPE_HTTP | IDENTT_SERVICE_SCOPE_HTTPS)
73  {
74  if (!(this->myscope & scope)) return; // scope mismatch
75 
76  // Endpoint : POST _matrix/identity/api/v1/store-invite
77  helpquery->add({scope,"POST _matrix/identity/api/v1/store-invite", {
78  "An identity service can store pending invitations to a user’s 3pid, which will",
79  "be retrieved and can be either notified on or look up when the 3pid is associated with a Matrix user ID.",
80  "following URL encoded POST parameters:",
81  " medium (string, required): The literal string email.",
82  " address (string, required): The email address or msisdn of the invited user.",
83  " room_id (string, required): The Matrix room ID to which the user is invited.",
84  " sender (string, required): The matrix user ID of the inviting user.",
85  " accesskey (string, required): if invite_requires_accesskey is set."
86  "An arbitrary number of other parameters may also be specified."
87  }
88  });
89 
90  server->resource["/_matrix/identity/api/v1/store-invite$"]["POST"]
91  =[this,stptr](typename HttpServerT::RespPtr response, typename HttpServerT::ReqPtr request) {
92  IDENTT_PARALLEL_ONE([this,stptr,response,request] {
93  try {
94  LOG(INFO) << request->path;
95  std::string err;
96  bool use_json = this->JsonRequest(request);
97 
98  identt::query::StoreInviteDataT inv;
99  if (use_json)
100  {
101  int stat = json2pb( request->content.string() , inv.mutable_invqry() , err);
102  if (stat<0) throw SydentException("Bad Json Format",M_BAD_JSON);
103  } else {
104  throw SydentException("Content-Type json only allowed",M_BAD_JSON);
105  }
106  if (!stptr->is_ready.Get()) throw identt::BadDataException("System Not Ready");
107 
108  // action
109 
110  // if accesskey is shared_secret it is server request, else check
111  if (stptr->invite_requires_key.Get()) {
112  bool is_server_query = false;
113  auto it=request->header.find("Shared-Secret");
114  if (it!=request->header.end()) {
115  is_server_query = (it->second == stptr->shared_secret.Get());
116  }
117  if (!is_server_query ) {
119  aservice.VerifyAccessKeyAction(stptr, inv.mutable_invqry()->accesskey());
120  }
121  }
122 
123  if (stptr->is_master.Get())
124  {
125  identt::store::InviteService storeinvite;
126  storeinvite.StoreInviteAction(stptr,&inv);
127  } else {
128  identt::hrpc::HrpcClient hclient;
129  hclient.SendToMaster(stptr,::identt::hrpc::M_STOREINVITE,&inv);
130  }
131 
132  // aftermath
133  std::string output;
134  pb2json(inv.mutable_invres() , output);
135  this->HttpOKAction(response,request,200,"OK","application/json",output,true);
136  } catch (SydentException& e)
137  {
138  int ecode = (e.ecode()>=IDENTT_SYDENT_ERROR_MIN && e.ecode()<=IDENTT_SYDENT_ERROR_MAX) ? e.ecode() : M_UNKNOWN;
139  std::string output = err2json(SydentErrors.at(ecode),e.what());
140  this->HttpOKAction(response,request,200,"OK","application/json",output,true);
141  } catch (identt::JdException& e)
142  {
143  std::string output = err2json(SydentErrors.at(M_UNKNOWN),e.what());
144  this->HttpOKAction(response,request,200,"OK","application/json",output,true);
145  } catch (std::exception& e)
146  {
147  std::string output = err2json(SydentErrors.at(M_UNKNOWN),e.what());
148  this->HttpOKAction(response,request,200,"OK","application/json",output,true);
149  } catch (...)
150  {
151  this->HttpErrorAction(response,request,500,"INTERNAL SERVER ERROR");
152  }
153  });
154  };
155  }
156 
157 private:
158 
159 };
160 } // namespace query
161 } // namespace identt
162 
163 #endif // _IDENTT_QUERY_INVITE_SERVICE_HPP_
164 
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
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
void StoreInviteAction(::identt::utils::SharedTable::pointer stptr, identt::query::StoreInviteDataT *inv)
StoreInviteAction : invite query internals.
Definition: InviteService.cc:52
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
Definition: InviteService.hpp:45
void VerifyAccessKeyAction(::identt::utils::SharedTable::pointer stptr, std::string akey)
VerifyAccessKeyAction : verify the access key.
Definition: AccessKeyService.cc:44
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
int json2pb(const std::string &json, google::protobuf::Message *msg, std::string &err)
json2pb : Convert json to protobuf
Definition: ProtoJson.cc:446
InviteService(identt::utils::SharedTable::pointer stptr, typename std::shared_ptr< HttpServerT > server, ::identt::query::HelpQuery::pointer helpquery, unsigned int scope)
InviteService : constructor.
Definition: InviteService.hpp:67
Definition: InviteService.hpp:42
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.
Definition: AccessKeyService.hpp:42