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_account.c
      19              :  * @brief Implementation of the GET /accounts/$ID request of the merchant's HTTP API
      20              :  * @author Priscilla HUANG
      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 /accounts/$ID operation.
      36              :  */
      37              : struct TALER_MERCHANT_AccountGetHandle
      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_AccountGetCallback 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 /accounts/$ID request.
      70              :  *
      71              :  * @param cls the `struct TALER_MERCHANT_AccountGetHandle`
      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_account_finished (void *cls,
      77              :                              long response_code,
      78              :                              const void *response)
      79              : {
      80            0 :   struct TALER_MERCHANT_AccountGetHandle *tgh = cls;
      81            0 :   const json_t *json = response;
      82            0 :   struct TALER_MERCHANT_AccountGetResponse 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 /accounts/$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              :       struct GNUNET_JSON_Specification spec[] = {
      96            0 :         GNUNET_JSON_spec_fixed_auto ("salt",
      97              :                                      &tgr.details.ok.ad.salt),
      98            0 :         GNUNET_JSON_spec_mark_optional (
      99              :           TALER_JSON_spec_web_url ("credit_facade_url",
     100              :                                    &tgr.details.ok.ad.credit_facade_url),
     101              :           NULL),
     102            0 :         TALER_JSON_spec_full_payto_uri ("payto_uri",
     103              :                                         &tgr.details.ok.ad.payto_uri),
     104            0 :         GNUNET_JSON_spec_fixed_auto ("h_wire",
     105              :                                      &tgr.details.ok.ad.h_wire),
     106            0 :         GNUNET_JSON_spec_bool ("active",
     107              :                                &tgr.details.ok.ad.active),
     108            0 :         GNUNET_JSON_spec_end ()
     109              :       };
     110              : 
     111            0 :       if (GNUNET_OK ==
     112            0 :           GNUNET_JSON_parse (json,
     113              :                              spec,
     114              :                              NULL, NULL))
     115              :       {
     116            0 :         tgh->cb (tgh->cb_cls,
     117              :                  &tgr);
     118            0 :         TALER_MERCHANT_account_get_cancel (tgh);
     119            0 :         return;
     120              :       }
     121            0 :       tgr.hr.http_status = 0;
     122            0 :       tgr.hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
     123            0 :       break;
     124              :     }
     125            0 :   case MHD_HTTP_UNAUTHORIZED:
     126            0 :     tgr.hr.ec = TALER_JSON_get_error_code (json);
     127            0 :     tgr.hr.hint = TALER_JSON_get_error_hint (json);
     128              :     /* Nothing really to verify, merchant says we need to authenticate. */
     129            0 :     break;
     130            0 :   case MHD_HTTP_NOT_FOUND:
     131            0 :     tgr.hr.ec = TALER_JSON_get_error_code (json);
     132            0 :     tgr.hr.hint = TALER_JSON_get_error_hint (json);
     133            0 :     break;
     134            0 :   default:
     135              :     /* unexpected response code */
     136            0 :     tgr.hr.ec = TALER_JSON_get_error_code (json);
     137            0 :     tgr.hr.hint = TALER_JSON_get_error_hint (json);
     138            0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     139              :                 "Unexpected response code %u/%d\n",
     140              :                 (unsigned int) response_code,
     141              :                 (int) tgr.hr.ec);
     142            0 :     break;
     143              :   }
     144            0 :   tgh->cb (tgh->cb_cls,
     145              :            &tgr);
     146            0 :   TALER_MERCHANT_account_get_cancel (tgh);
     147              : }
     148              : 
     149              : 
     150              : struct TALER_MERCHANT_AccountGetHandle *
     151            0 : TALER_MERCHANT_account_get (
     152              :   struct GNUNET_CURL_Context *ctx,
     153              :   const char *backend_url,
     154              :   const char *instance_id,
     155              :   const struct TALER_MerchantWireHashP *h_wire,
     156              :   TALER_MERCHANT_AccountGetCallback cb,
     157              :   void *cb_cls)
     158              : {
     159              :   struct TALER_MERCHANT_AccountGetHandle *tgh;
     160              :   CURL *eh;
     161              : 
     162            0 :   tgh = GNUNET_new (struct TALER_MERCHANT_AccountGetHandle);
     163            0 :   tgh->ctx = ctx;
     164            0 :   tgh->cb = cb;
     165            0 :   tgh->cb_cls = cb_cls;
     166              :   {
     167              :     char w_str[sizeof (*h_wire) * 2];
     168              :     char *path;
     169              :     char *end;
     170              : 
     171            0 :     end = GNUNET_STRINGS_data_to_string (h_wire,
     172              :                                          sizeof (*h_wire),
     173              :                                          w_str,
     174              :                                          sizeof (w_str));
     175            0 :     *end = '\0';
     176            0 :     GNUNET_asprintf (&path,
     177              :                      "private/accounts/%s",
     178              :                      w_str);
     179            0 :     tgh->url = TALER_url_join (backend_url,
     180              :                                path,
     181              :                                NULL);
     182            0 :     GNUNET_free (path);
     183              :   }
     184            0 :   if (NULL == tgh->url)
     185              :   {
     186            0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     187              :                 "Could not construct request URL.\n");
     188            0 :     GNUNET_free (tgh);
     189            0 :     return NULL;
     190              :   }
     191            0 :   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
     192              :               "Requesting URL '%s'\n",
     193              :               tgh->url);
     194            0 :   eh = TALER_MERCHANT_curl_easy_get_ (tgh->url);
     195            0 :   tgh->job = GNUNET_CURL_job_add (ctx,
     196              :                                   eh,
     197              :                                   &handle_get_account_finished,
     198              :                                   tgh);
     199            0 :   return tgh;
     200              : }
     201              : 
     202              : 
     203              : void
     204            0 : TALER_MERCHANT_account_get_cancel (
     205              :   struct TALER_MERCHANT_AccountGetHandle *tgh)
     206              : {
     207            0 :   if (NULL != tgh->job)
     208            0 :     GNUNET_CURL_job_cancel (tgh->job);
     209            0 :   GNUNET_free (tgh->url);
     210            0 :   GNUNET_free (tgh);
     211            0 : }
        
               |