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_device.c
      19              :  * @brief Implementation of the GET /otp-devices/$ID 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              : /**
      35              :  * Handle for a GET /otp-devices/$ID operation.
      36              :  */
      37              : struct TALER_MERCHANT_OtpDeviceGetHandle
      38              : {
      39              :   /**
      40              :    * The url for this request.
      41              :    */
      42              :   char *url;
      43              : 
      44              :   /**
      45              :    * Handle for the request.
      46              :    */
      47              :   struct GNUNET_CURL_Job *job;
      48              : 
      49              :   /**
      50              :    * Function to call with the result.
      51              :    */
      52              :   TALER_MERCHANT_OtpDeviceGetCallback cb;
      53              : 
      54              :   /**
      55              :    * Closure for @a cb.
      56              :    */
      57              :   void *cb_cls;
      58              : 
      59              :   /**
      60              :    * Reference to the execution context.
      61              :    */
      62              :   struct GNUNET_CURL_Context *ctx;
      63              : 
      64              : };
      65              : 
      66              : 
      67              : /**
      68              :  * Function called when we're done processing the
      69              :  * HTTP GET /otp-devices/$ID request.
      70              :  *
      71              :  * @param cls the `struct TALER_MERCHANT_OtpDeviceGetHandle`
      72              :  * @param response_code HTTP response code, 0 on error
      73              :  * @param response response body, NULL if not in JSON
      74              :  */
      75              : static void
      76            0 : handle_get_otp_device_finished (void *cls,
      77              :                                 long response_code,
      78              :                                 const void *response)
      79              : {
      80            0 :   struct TALER_MERCHANT_OtpDeviceGetHandle *tgh = cls;
      81            0 :   const json_t *json = response;
      82            0 :   struct TALER_MERCHANT_OtpDeviceGetResponse tgr = {
      83            0 :     .hr.http_status = (unsigned int) response_code,
      84              :     .hr.reply = json
      85              :   };
      86              : 
      87            0 :   tgh->job = NULL;
      88            0 :   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
      89              :               "Got /otp-devices/$ID response with status code %u\n",
      90              :               (unsigned int) response_code);
      91            0 :   switch (response_code)
      92              :   {
      93            0 :   case MHD_HTTP_OK:
      94              :     {
      95              :       uint32_t alg32;
      96              :       struct GNUNET_JSON_Specification spec[] = {
      97            0 :         GNUNET_JSON_spec_string ("otp_device_description",
      98              :                                  &tgr.details.ok.otp_device_description),
      99            0 :         GNUNET_JSON_spec_uint32 ("otp_algorithm",
     100              :                                  &alg32),
     101            0 :         GNUNET_JSON_spec_mark_optional (
     102              :           GNUNET_JSON_spec_uint64 ("otp_ctr",
     103              :                                    &tgr.details.ok.otp_ctr),
     104              :           NULL),
     105            0 :         GNUNET_JSON_spec_mark_optional (
     106              :           GNUNET_JSON_spec_uint64 ("otp_timestamp",
     107              :                                    &tgr.details.ok.otp_timestamp_s),
     108              :           NULL),
     109            0 :         GNUNET_JSON_spec_mark_optional (
     110              :           GNUNET_JSON_spec_string ("otp_code",
     111              :                                    &tgr.details.ok.otp_code),
     112              :           NULL),
     113            0 :         GNUNET_JSON_spec_end ()
     114              :       };
     115              : 
     116            0 :       if (GNUNET_OK ==
     117            0 :           GNUNET_JSON_parse (json,
     118              :                              spec,
     119              :                              NULL, NULL))
     120              :       {
     121            0 :         tgr.details.ok.otp_alg =
     122              :           (enum TALER_MerchantConfirmationAlgorithm) alg32;
     123            0 :         tgh->cb (tgh->cb_cls,
     124              :                  &tgr);
     125            0 :         TALER_MERCHANT_otp_device_get_cancel (tgh);
     126            0 :         return;
     127              :       }
     128            0 :       tgr.hr.http_status = 0;
     129            0 :       tgr.hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
     130            0 :       break;
     131              :     }
     132            0 :   case MHD_HTTP_UNAUTHORIZED:
     133            0 :     tgr.hr.ec = TALER_JSON_get_error_code (json);
     134            0 :     tgr.hr.hint = TALER_JSON_get_error_hint (json);
     135              :     /* Nothing really to verify, merchant says we need to authenticate. */
     136            0 :     break;
     137            0 :   case MHD_HTTP_NOT_FOUND:
     138            0 :     tgr.hr.ec = TALER_JSON_get_error_code (json);
     139            0 :     tgr.hr.hint = TALER_JSON_get_error_hint (json);
     140            0 :     break;
     141            0 :   default:
     142              :     /* unexpected response code */
     143            0 :     tgr.hr.ec = TALER_JSON_get_error_code (json);
     144            0 :     tgr.hr.hint = TALER_JSON_get_error_hint (json);
     145            0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     146              :                 "Unexpected response code %u/%d\n",
     147              :                 (unsigned int) response_code,
     148              :                 (int) tgr.hr.ec);
     149            0 :     break;
     150              :   }
     151            0 :   tgh->cb (tgh->cb_cls,
     152              :            &tgr);
     153            0 :   TALER_MERCHANT_otp_device_get_cancel (tgh);
     154              : }
     155              : 
     156              : 
     157              : struct TALER_MERCHANT_OtpDeviceGetHandle *
     158            0 : TALER_MERCHANT_otp_device_get (
     159              :   struct GNUNET_CURL_Context *ctx,
     160              :   const char *backend_url,
     161              :   const char *otp_device_id,
     162              :   TALER_MERCHANT_OtpDeviceGetCallback cb,
     163              :   void *cb_cls)
     164              : {
     165              :   struct TALER_MERCHANT_OtpDeviceGetHandle *tgh;
     166              :   CURL *eh;
     167              : 
     168            0 :   tgh = GNUNET_new (struct TALER_MERCHANT_OtpDeviceGetHandle);
     169            0 :   tgh->ctx = ctx;
     170            0 :   tgh->cb = cb;
     171            0 :   tgh->cb_cls = cb_cls;
     172              :   {
     173              :     char *path;
     174              : 
     175            0 :     GNUNET_asprintf (&path,
     176              :                      "private/otp-devices/%s",
     177              :                      otp_device_id);
     178            0 :     tgh->url = TALER_url_join (backend_url,
     179              :                                path,
     180              :                                NULL);
     181            0 :     GNUNET_free (path);
     182              :   }
     183            0 :   if (NULL == tgh->url)
     184              :   {
     185            0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     186              :                 "Could not construct request URL.\n");
     187            0 :     GNUNET_free (tgh);
     188            0 :     return NULL;
     189              :   }
     190            0 :   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
     191              :               "Requesting URL '%s'\n",
     192              :               tgh->url);
     193            0 :   eh = TALER_MERCHANT_curl_easy_get_ (tgh->url);
     194            0 :   tgh->job = GNUNET_CURL_job_add (ctx,
     195              :                                   eh,
     196              :                                   &handle_get_otp_device_finished,
     197              :                                   tgh);
     198            0 :   return tgh;
     199              : }
     200              : 
     201              : 
     202              : void
     203            0 : TALER_MERCHANT_otp_device_get_cancel (
     204              :   struct TALER_MERCHANT_OtpDeviceGetHandle *tgh)
     205              : {
     206            0 :   if (NULL != tgh->job)
     207            0 :     GNUNET_CURL_job_cancel (tgh->job);
     208            0 :   GNUNET_free (tgh->url);
     209            0 :   GNUNET_free (tgh);
     210            0 : }
        
               |