merchant

Merchant backend to process payments, run by merchants
Log | Files | Refs | Submodules | README | LICENSE

merchant_api_post_otp_devices.c (7054B)


      1 /*
      2   This file is part of TALER
      3   Copyright (C) 2022 Taler Systems SA
      4 
      5   TALER is free software; you can redistribute it and/or modify
      6   it under the terms of the GNU Lesser General Public License as
      7   published by the Free Software Foundation; either version 2.1,
      8   or (at your option) any later version.
      9 
     10   TALER is distributed in the hope that it will be useful,
     11   but WITHOUT ANY WARRANTY; without even the implied warranty of
     12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     13   GNU Lesser General Public License for more details.
     14 
     15   You should have received a copy of the GNU Lesser General
     16   Public License along with TALER; see the file COPYING.LGPL.
     17   If not, see <http://www.gnu.org/licenses/>
     18 */
     19 /**
     20  * @file merchant_api_post_otp_devices.c
     21  * @brief Implementation of the POST /otp-devices request
     22  *        of the merchant's HTTP API
     23  * @author Christian Grothoff
     24  */
     25 #include "platform.h"
     26 #include <curl/curl.h>
     27 #include <jansson.h>
     28 #include <microhttpd.h> /* just for HTTP status codes */
     29 #include <gnunet/gnunet_util_lib.h>
     30 #include "taler_merchant_service.h"
     31 #include "merchant_api_curl_defaults.h"
     32 #include "merchant_api_common.h"
     33 #include <taler/taler_json_lib.h>
     34 #include <taler/taler_curl_lib.h>
     35 
     36 
     37 /**
     38  * Handle for a POST /otp-devices/$ID operation.
     39  */
     40 struct TALER_MERCHANT_OtpDevicesPostHandle
     41 {
     42 
     43   /**
     44    * The url for this request.
     45    */
     46   char *url;
     47 
     48   /**
     49    * Handle for the request.
     50    */
     51   struct GNUNET_CURL_Job *job;
     52 
     53   /**
     54    * Function to call with the result.
     55    */
     56   TALER_MERCHANT_OtpDevicesPostCallback cb;
     57 
     58   /**
     59    * Closure for @a cb.
     60    */
     61   void *cb_cls;
     62 
     63   /**
     64    * Reference to the execution context.
     65    */
     66   struct GNUNET_CURL_Context *ctx;
     67 
     68   /**
     69    * Minor context that holds body and headers.
     70    */
     71   struct TALER_CURL_PostContext post_ctx;
     72 };
     73 
     74 
     75 /**
     76  * Function called when we're done processing the
     77  * HTTP POST /otp-devices request.
     78  *
     79  * @param cls the `struct TALER_MERCHANT_OtpDevicesPostHandle`
     80  * @param response_code HTTP response code, 0 on error
     81  * @param response response body, NULL if not in JSON
     82  */
     83 static void
     84 handle_post_otp_devices_finished (void *cls,
     85                                   long response_code,
     86                                   const void *response)
     87 {
     88   struct TALER_MERCHANT_OtpDevicesPostHandle *tph = cls;
     89   const json_t *json = response;
     90   struct TALER_MERCHANT_HttpResponse hr = {
     91     .http_status = (unsigned int) response_code,
     92     .reply = json
     93   };
     94 
     95   tph->job = NULL;
     96   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
     97               "POST /otp-devices completed with response code %u\n",
     98               (unsigned int) response_code);
     99   switch (response_code)
    100   {
    101   case 0:
    102     hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
    103     break;
    104   case MHD_HTTP_NO_CONTENT:
    105     break;
    106   case MHD_HTTP_BAD_REQUEST:
    107     hr.ec = TALER_JSON_get_error_code (json);
    108     hr.hint = TALER_JSON_get_error_hint (json);
    109     /* This should never happen, either us
    110      * or the merchant is buggy (or API version conflict);
    111      * just pass JSON reply to the application */
    112     break;
    113   case MHD_HTTP_UNAUTHORIZED:
    114     hr.ec = TALER_JSON_get_error_code (json);
    115     hr.hint = TALER_JSON_get_error_hint (json);
    116     /* Nothing really to verify, merchant says we need to authenticate. */
    117     break;
    118   case MHD_HTTP_FORBIDDEN:
    119     hr.ec = TALER_JSON_get_error_code (json);
    120     hr.hint = TALER_JSON_get_error_hint (json);
    121     /* Nothing really to verify, merchant says we tried to abort the payment
    122      * after it was successful. We should pass the JSON reply to the
    123      * application */
    124     break;
    125   case MHD_HTTP_NOT_FOUND:
    126     hr.ec = TALER_JSON_get_error_code (json);
    127     hr.hint = TALER_JSON_get_error_hint (json);
    128     /* Nothing really to verify, this should never
    129        happen, we should pass the JSON reply to the
    130        application */
    131     break;
    132   case MHD_HTTP_CONFLICT:
    133     hr.ec = TALER_JSON_get_error_code (json);
    134     hr.hint = TALER_JSON_get_error_hint (json);
    135     break;
    136   case MHD_HTTP_INTERNAL_SERVER_ERROR:
    137     hr.ec = TALER_JSON_get_error_code (json);
    138     hr.hint = TALER_JSON_get_error_hint (json);
    139     /* Server had an internal issue; we should retry,
    140        but this API leaves this to the application */
    141     break;
    142   default:
    143     TALER_MERCHANT_parse_error_details_ (json,
    144                                          response_code,
    145                                          &hr);
    146     /* unexpected response code */
    147     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    148                 "Unexpected response code %u/%d\n",
    149                 (unsigned int) response_code,
    150                 (int) hr.ec);
    151     GNUNET_break_op (0);
    152     break;
    153   }
    154   tph->cb (tph->cb_cls,
    155            &hr);
    156   TALER_MERCHANT_otp_devices_post_cancel (tph);
    157 }
    158 
    159 
    160 struct TALER_MERCHANT_OtpDevicesPostHandle *
    161 TALER_MERCHANT_otp_devices_post (
    162   struct GNUNET_CURL_Context *ctx,
    163   const char *backend_url,
    164   const char *otp_device_id,
    165   const char *otp_device_description,
    166   const char *otp_key,
    167   enum TALER_MerchantConfirmationAlgorithm otp_algorithm,
    168   uint64_t otp_ctr,
    169   TALER_MERCHANT_OtpDevicesPostCallback cb,
    170   void *cb_cls)
    171 {
    172   struct TALER_MERCHANT_OtpDevicesPostHandle *tph;
    173   json_t *req_obj;
    174 
    175   req_obj = GNUNET_JSON_PACK (
    176     GNUNET_JSON_pack_string ("otp_device_id",
    177                              otp_device_id),
    178     GNUNET_JSON_pack_string ("otp_device_description",
    179                              otp_device_description),
    180     GNUNET_JSON_pack_uint64 ("otp_algorithm",
    181                              (uint32_t) otp_algorithm),
    182     GNUNET_JSON_pack_allow_null (
    183       GNUNET_JSON_pack_string ("otp_key",
    184                                otp_key)),
    185     GNUNET_JSON_pack_uint64 ("otp_ctr",
    186                              otp_ctr));
    187   tph = GNUNET_new (struct TALER_MERCHANT_OtpDevicesPostHandle);
    188   tph->ctx = ctx;
    189   tph->cb = cb;
    190   tph->cb_cls = cb_cls;
    191   tph->url = TALER_url_join (backend_url,
    192                              "private/otp-devices",
    193                              NULL);
    194   if (NULL == tph->url)
    195   {
    196     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    197                 "Could not construct request URL.\n");
    198     json_decref (req_obj);
    199     GNUNET_free (tph);
    200     return NULL;
    201   }
    202   {
    203     CURL *eh;
    204 
    205     eh = TALER_MERCHANT_curl_easy_get_ (tph->url);
    206     GNUNET_assert (GNUNET_OK ==
    207                    TALER_curl_easy_post (&tph->post_ctx,
    208                                          eh,
    209                                          req_obj));
    210     json_decref (req_obj);
    211     tph->job = GNUNET_CURL_job_add2 (ctx,
    212                                      eh,
    213                                      tph->post_ctx.headers,
    214                                      &handle_post_otp_devices_finished,
    215                                      tph);
    216     GNUNET_assert (NULL != tph->job);
    217   }
    218   return tph;
    219 }
    220 
    221 
    222 void
    223 TALER_MERCHANT_otp_devices_post_cancel (
    224   struct TALER_MERCHANT_OtpDevicesPostHandle *tph)
    225 {
    226   if (NULL != tph->job)
    227   {
    228     GNUNET_CURL_job_cancel (tph->job);
    229     tph->job = NULL;
    230   }
    231   TALER_curl_easy_post_finished (&tph->post_ctx);
    232   GNUNET_free (tph->url);
    233   GNUNET_free (tph);
    234 }
    235 
    236 
    237 /* end of merchant_api_post_otp_devices.c */