merchant

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

merchant_api_get_otp_devices.c (6918B)


      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 it under the
      6   terms of the GNU Lesser General Public License as published by the Free Software
      7   Foundation; either version 2.1, or (at your option) any later version.
      8 
      9   TALER is distributed in the hope that it will be useful, but WITHOUT ANY
     10   WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
     11   A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more details.
     12 
     13   You should have received a copy of the GNU Lesser General Public License along with
     14   TALER; see the file COPYING.LGPL.  If not, see
     15   <http://www.gnu.org/licenses/>
     16 */
     17 /**
     18  * @file merchant_api_get_otp_devices.c
     19  * @brief Implementation of the GET /otp-devices request of the merchant's HTTP API
     20  * @author Christian Grothoff
     21  */
     22 #include "platform.h"
     23 #include <curl/curl.h>
     24 #include <jansson.h>
     25 #include <microhttpd.h> /* just for HTTP status codes */
     26 #include <gnunet/gnunet_util_lib.h>
     27 #include <gnunet/gnunet_curl_lib.h>
     28 #include "taler_merchant_service.h"
     29 #include "merchant_api_curl_defaults.h"
     30 #include <taler/taler_json_lib.h>
     31 #include <taler/taler_signatures.h>
     32 
     33 /**
     34  * Maximum number of OTP devices we return.
     35  */
     36 #define MAX_OTP 1024
     37 
     38 
     39 /**
     40  * Handle for a GET /otp-devices operation.
     41  */
     42 struct TALER_MERCHANT_OtpDevicesGetHandle
     43 {
     44   /**
     45    * The url for this request.
     46    */
     47   char *url;
     48 
     49   /**
     50    * Handle for the request.
     51    */
     52   struct GNUNET_CURL_Job *job;
     53 
     54   /**
     55    * Function to call with the result.
     56    */
     57   TALER_MERCHANT_OtpDevicesGetCallback cb;
     58 
     59   /**
     60    * Closure for @a cb.
     61    */
     62   void *cb_cls;
     63 
     64   /**
     65    * Reference to the execution context.
     66    */
     67   struct GNUNET_CURL_Context *ctx;
     68 
     69 };
     70 
     71 
     72 /**
     73  * Parse OTP device information from @a ia.
     74  *
     75  * @param ia JSON array (or NULL!) with otp_device data
     76  * @param[in] tgr partially filled response
     77  * @param tgh operation handle
     78  * @return #GNUNET_OK on success
     79  */
     80 static enum GNUNET_GenericReturnValue
     81 parse_otp_devices (const json_t *ia,
     82                    struct TALER_MERCHANT_OtpDevicesGetResponse *tgr,
     83                    struct TALER_MERCHANT_OtpDevicesGetHandle *tgh)
     84 {
     85   unsigned int otp_len = (unsigned int) json_array_size (ia);
     86 
     87   if ( (json_array_size (ia) != (size_t)  otp_len) ||
     88        (otp_len > MAX_OTP) )
     89   {
     90     GNUNET_break (0);
     91     return GNUNET_SYSERR;
     92   }
     93   {
     94     struct TALER_MERCHANT_OtpDeviceEntry otp[GNUNET_NZL (otp_len)];
     95     size_t index;
     96     json_t *value;
     97 
     98     json_array_foreach (ia, index, value) {
     99       struct TALER_MERCHANT_OtpDeviceEntry *ie = &otp[index];
    100       struct GNUNET_JSON_Specification spec[] = {
    101         GNUNET_JSON_spec_string ("otp_device_id",
    102                                  &ie->otp_device_id),
    103         GNUNET_JSON_spec_string ("device_description",
    104                                  &ie->device_description),
    105         GNUNET_JSON_spec_end ()
    106       };
    107 
    108       if (GNUNET_OK !=
    109           GNUNET_JSON_parse (value,
    110                              spec,
    111                              NULL, NULL))
    112       {
    113         GNUNET_break_op (0);
    114         return GNUNET_SYSERR;
    115       }
    116     }
    117     tgr->details.ok.otp_devices_length = otp_len;
    118     tgr->details.ok.otp_devices = otp;
    119     tgh->cb (tgh->cb_cls,
    120              tgr);
    121     tgh->cb = NULL; /* just to be sure */
    122   }
    123   return GNUNET_OK;
    124 }
    125 
    126 
    127 /**
    128  * Function called when we're done processing the
    129  * HTTP /otp-devices request.
    130  *
    131  * @param cls the `struct TALER_MERCHANT_OtpDevicesGetHandle`
    132  * @param response_code HTTP response code, 0 on error
    133  * @param response response body, NULL if not in JSON
    134  */
    135 static void
    136 handle_get_otp_devices_finished (void *cls,
    137                                  long response_code,
    138                                  const void *response)
    139 {
    140   struct TALER_MERCHANT_OtpDevicesGetHandle *tgh = cls;
    141   const json_t *json = response;
    142   struct TALER_MERCHANT_OtpDevicesGetResponse tgr = {
    143     .hr.http_status = (unsigned int) response_code,
    144     .hr.reply = json
    145   };
    146 
    147   tgh->job = NULL;
    148   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
    149               "Got /otp-devices response with status code %u\n",
    150               (unsigned int) response_code);
    151   switch (response_code)
    152   {
    153   case MHD_HTTP_OK:
    154     {
    155       const json_t *otp_devices;
    156       struct GNUNET_JSON_Specification spec[] = {
    157         GNUNET_JSON_spec_array_const ("otp_devices",
    158                                       &otp_devices),
    159         GNUNET_JSON_spec_end ()
    160       };
    161 
    162       if (GNUNET_OK !=
    163           GNUNET_JSON_parse (json,
    164                              spec,
    165                              NULL, NULL))
    166       {
    167         tgr.hr.http_status = 0;
    168         tgr.hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
    169         break;
    170       }
    171       if (GNUNET_OK ==
    172           parse_otp_devices (otp_devices,
    173                              &tgr,
    174                              tgh))
    175       {
    176         TALER_MERCHANT_otp_devices_get_cancel (tgh);
    177         return;
    178       }
    179       tgr.hr.http_status = 0;
    180       tgr.hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
    181       break;
    182     }
    183   case MHD_HTTP_UNAUTHORIZED:
    184     tgr.hr.ec = TALER_JSON_get_error_code (json);
    185     tgr.hr.hint = TALER_JSON_get_error_hint (json);
    186     /* Nothing really to verify, merchant says we need to authenticate. */
    187     break;
    188   default:
    189     /* unexpected response code */
    190     tgr.hr.ec = TALER_JSON_get_error_code (json);
    191     tgr.hr.hint = TALER_JSON_get_error_hint (json);
    192     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    193                 "Unexpected response code %u/%d\n",
    194                 (unsigned int) response_code,
    195                 (int) tgr.hr.ec);
    196     break;
    197   }
    198   tgh->cb (tgh->cb_cls,
    199            &tgr);
    200   TALER_MERCHANT_otp_devices_get_cancel (tgh);
    201 }
    202 
    203 
    204 struct TALER_MERCHANT_OtpDevicesGetHandle *
    205 TALER_MERCHANT_otp_devices_get (
    206   struct GNUNET_CURL_Context *ctx,
    207   const char *backend_url,
    208   TALER_MERCHANT_OtpDevicesGetCallback cb,
    209   void *cb_cls)
    210 {
    211   struct TALER_MERCHANT_OtpDevicesGetHandle *tgh;
    212   CURL *eh;
    213 
    214   tgh = GNUNET_new (struct TALER_MERCHANT_OtpDevicesGetHandle);
    215   tgh->ctx = ctx;
    216   tgh->cb = cb;
    217   tgh->cb_cls = cb_cls;
    218   tgh->url = TALER_url_join (backend_url,
    219                              "private/otp-devices",
    220                              NULL);
    221   if (NULL == tgh->url)
    222   {
    223     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    224                 "Could not construct request URL.\n");
    225     GNUNET_free (tgh);
    226     return NULL;
    227   }
    228   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
    229               "Requesting URL '%s'\n",
    230               tgh->url);
    231   eh = TALER_MERCHANT_curl_easy_get_ (tgh->url);
    232   tgh->job = GNUNET_CURL_job_add (ctx,
    233                                   eh,
    234                                   &handle_get_otp_devices_finished,
    235                                   tgh);
    236   return tgh;
    237 }
    238 
    239 
    240 void
    241 TALER_MERCHANT_otp_devices_get_cancel (
    242   struct TALER_MERCHANT_OtpDevicesGetHandle *tgh)
    243 {
    244   if (NULL != tgh->job)
    245     GNUNET_CURL_job_cancel (tgh->job);
    246   GNUNET_free (tgh->url);
    247   GNUNET_free (tgh);
    248 }