| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | #include "error_resource.hh" |
|---|
| 21 | #include "reauthn_resource.hh" |
|---|
| 22 | #include "redir_resource.hh" |
|---|
| 23 | #include "request.hh" |
|---|
| 24 | #include "templated_resource.hh" |
|---|
| 25 | #include "token.hh" |
|---|
| 26 | |
|---|
| 27 | #include "../config.hh" |
|---|
| 28 | #include "../db/db.hh" |
|---|
| 29 | #include "../db/user.hh" |
|---|
| 30 | #include "../html/util.hh" |
|---|
| 31 | #include "../msg/lexutils.hh" |
|---|
| 32 | #include "../tlate/tlate.hh" |
|---|
| 33 | #include "../smtp_client/smtp_client.hh" |
|---|
| 34 | |
|---|
| 35 | #define REQPATH "/confirm" |
|---|
| 36 | #define REQPREFIX REQPATH "/" |
|---|
| 37 | |
|---|
| 38 | namespace http |
|---|
| 39 | { |
|---|
| 40 | class confirm : public templated_resource |
|---|
| 41 | { |
|---|
| 42 | public: |
|---|
| 43 | confirm(server::conn_cb cb) |
|---|
| 44 | : templated_resource(cb, "confirm.html") |
|---|
| 45 | {} |
|---|
| 46 | void set_attributes(boost::shared_ptr<request>, |
|---|
| 47 | tlate::data_model::ptr); |
|---|
| 48 | }; |
|---|
| 49 | |
|---|
| 50 | void confirm::set_attributes(boost::shared_ptr<request> req, |
|---|
| 51 | tlate::data_model::ptr am) |
|---|
| 52 | { |
|---|
| 53 | std::string (*const q)(std::string,bool) = html::quote; |
|---|
| 54 | std::string (*const p)(std::string) = uri::percent_encode; |
|---|
| 55 | std::string (*const dep)(std::string) = uri::percent_decode; |
|---|
| 56 | |
|---|
| 57 | if (req->get_path().length() <= sizeof REQPREFIX - 1) |
|---|
| 58 | { |
|---|
| 59 | boost::shared_ptr<resource> er |
|---|
| 60 | (new error_resource(cb, "404 Not found")); |
|---|
| 61 | throw resource_exception(er); |
|---|
| 62 | } |
|---|
| 63 | |
|---|
| 64 | if (!req->is_authenticated()) |
|---|
| 65 | { |
|---|
| 66 | boost::shared_ptr<resource> er |
|---|
| 67 | (new reauthn_resource(cb)); |
|---|
| 68 | throw resource_exception(er); |
|---|
| 69 | } |
|---|
| 70 | |
|---|
| 71 | boost::shared_ptr<db::user> u = req->get_user(); |
|---|
| 72 | |
|---|
| 73 | std::string tokstr = |
|---|
| 74 | dep(req->get_path().substr(sizeof REQPREFIX - 1)); |
|---|
| 75 | |
|---|
| 76 | if (req->get_method() == "POST") |
|---|
| 77 | { |
|---|
| 78 | boost::shared_ptr<resource> er; |
|---|
| 79 | if (u->verify_delivery_email(tokstr, cb)) |
|---|
| 80 | er.reset(new redir_resource(cb, |
|---|
| 81 | "/confirmed.html", |
|---|
| 82 | "303 See other")); |
|---|
| 83 | else |
|---|
| 84 | er.reset(new error_resource |
|---|
| 85 | (cb, "410 Invalid or expired link")); |
|---|
| 86 | throw resource_exception(er); |
|---|
| 87 | } |
|---|
| 88 | am->insert("userid", q(u->get_userid(), false)); |
|---|
| 89 | am->insert("username", q(u->get_display_name(), false)); |
|---|
| 90 | am->insert("diaddr", q(u->get_delivery_email(), false)); |
|---|
| 91 | am->insert("action", q(REQPREFIX + p(tokstr), false)); |
|---|
| 92 | am->insert("method", "post"); |
|---|
| 93 | am->insert("enctype", "application/x-www-form-urlencoded"); |
|---|
| 94 | am->insert("accept_charlist", q("utf8",false)); |
|---|
| 95 | } |
|---|
| 96 | }; |
|---|
| 97 | |
|---|
| 98 | namespace |
|---|
| 99 | { |
|---|
| 100 | class factory : public server::http_resource_factory |
|---|
| 101 | { |
|---|
| 102 | public: |
|---|
| 103 | factory() { |
|---|
| 104 | server::register_http_resource(REQPATH, this); |
|---|
| 105 | } |
|---|
| 106 | boost::shared_ptr<http::resource> operator() |
|---|
| 107 | (server::conn_cb cb, std::string) { |
|---|
| 108 | boost::shared_ptr<http::resource> rv |
|---|
| 109 | (new http::confirm(cb)); |
|---|
| 110 | return rv; |
|---|
| 111 | } |
|---|
| 112 | }; |
|---|
| 113 | factory f; |
|---|
| 114 | } |
|---|