summaryrefslogtreecommitdiff
path: root/src/backend/taler-merchant-httpd_private-post-reserves-ID-authorize-tip.c
blob: 1c28173bbdac57c535e005cd627f39c9d4b52f8e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/*
  This file is part of TALER
  (C) 2014-2020 Taler Systems SA

  TALER is free software; you can redistribute it and/or modify it under the
  terms of the GNU Affero General Public License as published by the Free Software
  Foundation; either version 3, or (at your option) any later version.

  TALER is distributed in the hope that it will be useful, but WITHOUT ANY
  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  A PARTICULAR PURPOSE.  See the GNU General Public License for more details.

  You should have received a copy of the GNU General Public License along with
  TALER; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
*/
/**
 * @file backend/taler-merchant-httpd_private-post-reserves-ID-authorize-tip.c
 * @brief implement API for authorizing tips to be paid to visitors
 * @author Christian Grothoff
 */
#include "platform.h"
#include <jansson.h>
#include <taler/taler_util.h>
#include <taler/taler_json_lib.h>
#include "taler-merchant-httpd.h"
#include "taler-merchant-httpd_mhd.h"
#include "taler-merchant-httpd_private-post-reserves-ID-authorize-tip.h"


/**
 * Handle a "tip-authorize" request.
 *
 * @param rh context of the handler
 * @param connection the MHD connection to handle
 * @param[in,out] hc context with further information about the request
 * @param reserve_pub reserve to use, or NULL for "any"
 * @return MHD result code
 */
static MHD_RESULT
authorize_tip (const struct TMH_RequestHandler *rh,
               struct MHD_Connection *connection,
               struct TMH_HandlerContext *hc,
               const struct TALER_ReservePublicKeyP *reserve_pub)
{
  enum TALER_ErrorCode ec;
  struct GNUNET_TIME_Absolute expiration;
  struct GNUNET_HashCode tip_id;
  const char *justification;
  const char *next_url;
  struct TALER_Amount amount;
  {
    struct GNUNET_JSON_Specification spec[] = {
      TALER_JSON_spec_amount ("amount", &amount),
      GNUNET_JSON_spec_string ("justification", &justification),
      GNUNET_JSON_spec_string ("next_url", &next_url),
      GNUNET_JSON_spec_end ()
    };
    enum GNUNET_GenericReturnValue res;

    res = TALER_MHD_parse_json_data (connection,
                                     hc->request_body,
                                     spec);
    if (GNUNET_YES != res)
    {
      GNUNET_break_op (0);
      return (GNUNET_NO == res)
             ? MHD_YES
             : MHD_NO;
    }
  }
  TMH_db->preflight (TMH_db->cls);
  ec = TMH_db->authorize_tip (TMH_db->cls,
                              hc->instance->settings.id,
                              reserve_pub,
                              &amount,
                              justification,
                              next_url,
                              &tip_id,
                              &expiration);
  /* handle errors */
  if (TALER_EC_NONE != ec)
  {
    unsigned int rc;
    const char *msg;

    switch (ec)
    {
    case TALER_EC_TIP_AUTHORIZE_INSUFFICIENT_FUNDS:
      rc = MHD_HTTP_PRECONDITION_FAILED;
      msg = "Failed to approve tip: merchant has insufficient tipping funds";
      break;
    case TALER_EC_TIP_AUTHORIZE_RESERVE_EXPIRED:
      msg = "Failed to approve tip: merchant's tipping reserve expired";
      rc = MHD_HTTP_GONE;
      break;
    case TALER_EC_TIP_AUTHORIZE_RESERVE_UNKNOWN:
      msg = "Failed to approve tip: merchant's tipping reserve does not exist";
      rc = MHD_HTTP_SERVICE_UNAVAILABLE;
      break;
    case TALER_EC_TIP_AUTHORIZE_DB_RESERVE_NOT_FOUND:
      msg = "Failed to approve tip: merchant's tipping reserve does not exist";
      rc = MHD_HTTP_NOT_FOUND;
      break;
    default:
      rc = MHD_HTTP_INTERNAL_SERVER_ERROR;
      msg = "Failed to approve tip: internal server error";
      break;
    }

    return TALER_MHD_reply_with_error (connection,
                                       rc,
                                       ec,
                                       msg);
  }

  /* generate success response */
  {
    char *taler_tip_uri;
    const char *host;
    const char *forwarded_host;
    const char *uri_path;
    struct GNUNET_CRYPTO_HashAsciiEncoded hash_enc;
    MHD_RESULT res;

    host = MHD_lookup_connection_value (connection,
                                        MHD_HEADER_KIND,
                                        "Host");
    forwarded_host = MHD_lookup_connection_value (connection,
                                                  MHD_HEADER_KIND,
                                                  "X-Forwarded-Host");

    uri_path = MHD_lookup_connection_value (connection,
                                            MHD_HEADER_KIND,
                                            "X-Forwarded-Prefix");
    if (NULL != forwarded_host)
      host = forwarded_host;
    if (NULL == host)
    {
      /* Should never happen, at last the host header should be defined */
      GNUNET_break (0);
      return TALER_MHD_reply_with_error (connection,
                                         MHD_HTTP_INTERNAL_SERVER_ERROR,
                                         TALER_EC_INTERNAL_INVARIANT_FAILURE,
                                         "unable to identify backend host");
    }

    GNUNET_CRYPTO_hash_to_enc (&tip_id,
                               &hash_enc);
    GNUNET_assert (0 < GNUNET_asprintf (&taler_tip_uri,
                                        "taler://tip/%s/%s%sinstances/%s/%s",
                                        host,
                                        (NULL == uri_path) ? "" : uri_path,
                                        (NULL == uri_path) ? "" : "/",
                                        hc->instance->settings.id,
                                        hash_enc.encoding));
    GNUNET_TIME_round_abs (&expiration);
    res = TALER_MHD_reply_json_pack (connection,
                                     MHD_HTTP_OK,
                                     "{s:s, s:s, s:o}",
                                     "tip_id",
                                     hash_enc.encoding,
                                     "tip_redirect_url",
                                     taler_tip_uri,
                                     "tip_expiration",
                                     GNUNET_JSON_from_time_abs (expiration));
    GNUNET_free (taler_tip_uri);
    return res;
  }
}


/**
 * Handle a "/reserves/$ID/tip-authorize" request.
 *
 * @param rh context of the handler
 * @param connection the MHD connection to handle
 * @param[in,out] hc context with further information about the request
 * @return MHD result code
 */
MHD_RESULT
TMH_private_post_reserves_ID_authorize_tip (const struct TMH_RequestHandler *rh,
                                            struct MHD_Connection *connection,
                                            struct TMH_HandlerContext *hc)
{
  struct TALER_ReservePublicKeyP reserve_pub;

  if (GNUNET_OK !=
      GNUNET_STRINGS_string_to_data (hc->infix,
                                     strlen (hc->infix),
                                     &reserve_pub,
                                     sizeof (reserve_pub)))
  {
    GNUNET_break_op (0);
    return TALER_MHD_reply_with_error (connection,
                                       MHD_HTTP_BAD_REQUEST,
                                       TALER_EC_RESERVES_INVALID_RESERVE_PUB,
                                       "reserve public key malformed");
  }
  return authorize_tip (rh,
                        connection,
                        hc,
                        &reserve_pub);
}


/**
 * Handle a POST "/tips" request.
 * Here the client does not specify the reserve public key, so we
 * are free to pick "any" available reserve.
 *
 * @param rh context of the handler
 * @param connection the MHD connection to handle
 * @param[in,out] hc context with further information about the request
 * @return MHD result code
 */
MHD_RESULT
TMH_private_post_tips (const struct TMH_RequestHandler *rh,
                       struct MHD_Connection *connection,
                       struct TMH_HandlerContext *hc)
{
  return authorize_tip (rh,
                        connection,
                        hc,
                        NULL);
}


/* end of taler-merchant-httpd_private-post-reserves-ID-authorize-tip.c */