LCOV - code coverage report
Current view: top level - lib - merchant_api_get_otp_devices.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 0.0 % 75 0
Test Date: 2025-10-31 14:20:14 Functions: 0.0 % 4 0

            Line data    Source code
       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            0 : parse_otp_devices (const json_t *ia,
      82              :                    struct TALER_MERCHANT_OtpDevicesGetResponse *tgr,
      83              :                    struct TALER_MERCHANT_OtpDevicesGetHandle *tgh)
      84              : {
      85            0 :   unsigned int otp_len = (unsigned int) json_array_size (ia);
      86              : 
      87            0 :   if ( (json_array_size (ia) != (size_t)  otp_len) ||
      88              :        (otp_len > MAX_OTP) )
      89              :   {
      90            0 :     GNUNET_break (0);
      91            0 :     return GNUNET_SYSERR;
      92              :   }
      93            0 :   {
      94            0 :     struct TALER_MERCHANT_OtpDeviceEntry otp[GNUNET_NZL (otp_len)];
      95              :     size_t index;
      96              :     json_t *value;
      97              : 
      98            0 :     json_array_foreach (ia, index, value) {
      99            0 :       struct TALER_MERCHANT_OtpDeviceEntry *ie = &otp[index];
     100              :       struct GNUNET_JSON_Specification spec[] = {
     101            0 :         GNUNET_JSON_spec_string ("otp_device_id",
     102              :                                  &ie->otp_device_id),
     103            0 :         GNUNET_JSON_spec_string ("device_description",
     104              :                                  &ie->device_description),
     105            0 :         GNUNET_JSON_spec_end ()
     106              :       };
     107              : 
     108            0 :       if (GNUNET_OK !=
     109            0 :           GNUNET_JSON_parse (value,
     110              :                              spec,
     111              :                              NULL, NULL))
     112              :       {
     113            0 :         GNUNET_break_op (0);
     114            0 :         return GNUNET_SYSERR;
     115              :       }
     116              :     }
     117            0 :     tgr->details.ok.otp_devices_length = otp_len;
     118            0 :     tgr->details.ok.otp_devices = otp;
     119            0 :     tgh->cb (tgh->cb_cls,
     120              :              tgr);
     121            0 :     tgh->cb = NULL; /* just to be sure */
     122              :   }
     123            0 :   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            0 : handle_get_otp_devices_finished (void *cls,
     137              :                                  long response_code,
     138              :                                  const void *response)
     139              : {
     140            0 :   struct TALER_MERCHANT_OtpDevicesGetHandle *tgh = cls;
     141            0 :   const json_t *json = response;
     142            0 :   struct TALER_MERCHANT_OtpDevicesGetResponse tgr = {
     143            0 :     .hr.http_status = (unsigned int) response_code,
     144              :     .hr.reply = json
     145              :   };
     146              : 
     147            0 :   tgh->job = NULL;
     148            0 :   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
     149              :               "Got /otp-devices response with status code %u\n",
     150              :               (unsigned int) response_code);
     151            0 :   switch (response_code)
     152              :   {
     153            0 :   case MHD_HTTP_OK:
     154              :     {
     155              :       const json_t *otp_devices;
     156              :       struct GNUNET_JSON_Specification spec[] = {
     157            0 :         GNUNET_JSON_spec_array_const ("otp_devices",
     158              :                                       &otp_devices),
     159            0 :         GNUNET_JSON_spec_end ()
     160              :       };
     161              : 
     162            0 :       if (GNUNET_OK !=
     163            0 :           GNUNET_JSON_parse (json,
     164              :                              spec,
     165              :                              NULL, NULL))
     166              :       {
     167            0 :         tgr.hr.http_status = 0;
     168            0 :         tgr.hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
     169            0 :         break;
     170              :       }
     171            0 :       if (GNUNET_OK ==
     172            0 :           parse_otp_devices (otp_devices,
     173              :                              &tgr,
     174              :                              tgh))
     175              :       {
     176            0 :         TALER_MERCHANT_otp_devices_get_cancel (tgh);
     177            0 :         return;
     178              :       }
     179            0 :       tgr.hr.http_status = 0;
     180            0 :       tgr.hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
     181            0 :       break;
     182              :     }
     183            0 :   case MHD_HTTP_UNAUTHORIZED:
     184            0 :     tgr.hr.ec = TALER_JSON_get_error_code (json);
     185            0 :     tgr.hr.hint = TALER_JSON_get_error_hint (json);
     186              :     /* Nothing really to verify, merchant says we need to authenticate. */
     187            0 :     break;
     188            0 :   default:
     189              :     /* unexpected response code */
     190            0 :     tgr.hr.ec = TALER_JSON_get_error_code (json);
     191            0 :     tgr.hr.hint = TALER_JSON_get_error_hint (json);
     192            0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     193              :                 "Unexpected response code %u/%d\n",
     194              :                 (unsigned int) response_code,
     195              :                 (int) tgr.hr.ec);
     196            0 :     break;
     197              :   }
     198            0 :   tgh->cb (tgh->cb_cls,
     199              :            &tgr);
     200            0 :   TALER_MERCHANT_otp_devices_get_cancel (tgh);
     201              : }
     202              : 
     203              : 
     204              : struct TALER_MERCHANT_OtpDevicesGetHandle *
     205            0 : 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            0 :   tgh = GNUNET_new (struct TALER_MERCHANT_OtpDevicesGetHandle);
     215            0 :   tgh->ctx = ctx;
     216            0 :   tgh->cb = cb;
     217            0 :   tgh->cb_cls = cb_cls;
     218            0 :   tgh->url = TALER_url_join (backend_url,
     219              :                              "private/otp-devices",
     220              :                              NULL);
     221            0 :   if (NULL == tgh->url)
     222              :   {
     223            0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     224              :                 "Could not construct request URL.\n");
     225            0 :     GNUNET_free (tgh);
     226            0 :     return NULL;
     227              :   }
     228            0 :   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
     229              :               "Requesting URL '%s'\n",
     230              :               tgh->url);
     231            0 :   eh = TALER_MERCHANT_curl_easy_get_ (tgh->url);
     232            0 :   tgh->job = GNUNET_CURL_job_add (ctx,
     233              :                                   eh,
     234              :                                   &handle_get_otp_devices_finished,
     235              :                                   tgh);
     236            0 :   return tgh;
     237              : }
     238              : 
     239              : 
     240              : void
     241            0 : TALER_MERCHANT_otp_devices_get_cancel (
     242              :   struct TALER_MERCHANT_OtpDevicesGetHandle *tgh)
     243              : {
     244            0 :   if (NULL != tgh->job)
     245            0 :     GNUNET_CURL_job_cancel (tgh->job);
     246            0 :   GNUNET_free (tgh->url);
     247            0 :   GNUNET_free (tgh);
     248            0 : }
        

Generated by: LCOV version 2.0-1