Line data    Source code 
       1              : /*
       2              :   This file is part of TALER
       3              :   Copyright (C) 2023 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_delete_account.c
      19              :  * @brief Implementation of the DELETE /private/account/$H_WIRE 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 DELETE /accounts/$ID operation.
      36              :  */
      37              : struct TALER_MERCHANT_AccountDeleteHandle
      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_AccountDeleteCallback 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 DELETE /accounts/$H_WIRE request.
      70              :  *
      71              :  * @param cls the `struct TALER_MERCHANT_AccountDeleteHandle`
      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_delete_account_finished (void *cls,
      77              :                                 long response_code,
      78              :                                 const void *response)
      79              : {
      80            0 :   struct TALER_MERCHANT_AccountDeleteHandle *adh = cls;
      81            0 :   const json_t *json = response;
      82            0 :   struct TALER_MERCHANT_AccountDeleteResponse adr = {
      83            0 :     .hr.http_status = (unsigned int) response_code,
      84              :     .hr.reply = json
      85              :   };
      86              : 
      87            0 :   adh->job = NULL;
      88            0 :   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
      89              :               "Got /accounts/$H_WIRE response with status code %u\n",
      90              :               (unsigned int) response_code);
      91            0 :   switch (response_code)
      92              :   {
      93            0 :   case MHD_HTTP_NO_CONTENT:
      94            0 :     break;
      95            0 :   case MHD_HTTP_UNAUTHORIZED:
      96            0 :     adr.hr.ec = TALER_JSON_get_error_code (json);
      97            0 :     adr.hr.hint = TALER_JSON_get_error_hint (json);
      98              :     /* Nothing really to verify, merchant says we need to authenticate. */
      99            0 :     break;
     100            0 :   case MHD_HTTP_NOT_FOUND:
     101            0 :     break;
     102            0 :   default:
     103              :     /* unexpected response code */
     104            0 :     adr.hr.ec = TALER_JSON_get_error_code (json);
     105            0 :     adr.hr.hint = TALER_JSON_get_error_hint (json);
     106            0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     107              :                 "Unexpected response code %u/%d for DELETE /account/ID\n",
     108              :                 (unsigned int) response_code,
     109              :                 (int) adr.hr.ec);
     110            0 :     break;
     111              :   }
     112            0 :   adh->cb (adh->cb_cls,
     113              :            &adr);
     114            0 :   TALER_MERCHANT_account_delete_cancel (adh);
     115            0 : }
     116              : 
     117              : 
     118              : struct TALER_MERCHANT_AccountDeleteHandle *
     119            0 : TALER_MERCHANT_account_delete (
     120              :   struct GNUNET_CURL_Context *ctx,
     121              :   const char *backend_url,
     122              :   const struct TALER_MerchantWireHashP *h_wire,
     123              :   TALER_MERCHANT_AccountDeleteCallback cb,
     124              :   void *cb_cls)
     125              : {
     126              :   struct TALER_MERCHANT_AccountDeleteHandle *adh;
     127              : 
     128            0 :   adh = GNUNET_new (struct TALER_MERCHANT_AccountDeleteHandle);
     129            0 :   adh->ctx = ctx;
     130            0 :   adh->cb = cb;
     131            0 :   adh->cb_cls = cb_cls;
     132              :   {
     133              :     char h_wire_str[sizeof (*h_wire) * 2];
     134              :     char *path;
     135              :     char *end;
     136              : 
     137            0 :     end = GNUNET_STRINGS_data_to_string (h_wire,
     138              :                                          sizeof (*h_wire),
     139              :                                          h_wire_str,
     140              :                                          sizeof (h_wire_str));
     141            0 :     *end = '\0';
     142            0 :     GNUNET_asprintf (&path,
     143              :                      "private/account/%s",
     144              :                      h_wire_str);
     145            0 :     adh->url = TALER_url_join (backend_url,
     146              :                                path,
     147              :                                NULL);
     148            0 :     GNUNET_free (path);
     149              :   }
     150            0 :   if (NULL == adh->url)
     151              :   {
     152            0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     153              :                 "Could not construct request URL.\n");
     154            0 :     GNUNET_free (adh);
     155            0 :     return NULL;
     156              :   }
     157            0 :   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
     158              :               "Requesting URL '%s'\n",
     159              :               adh->url);
     160              :   {
     161              :     CURL *eh;
     162              : 
     163            0 :     eh = TALER_MERCHANT_curl_easy_get_ (adh->url);
     164            0 :     GNUNET_assert (CURLE_OK ==
     165              :                    curl_easy_setopt (eh,
     166              :                                      CURLOPT_CUSTOMREQUEST,
     167              :                                      MHD_HTTP_METHOD_DELETE));
     168            0 :     adh->job = GNUNET_CURL_job_add (ctx,
     169              :                                     eh,
     170              :                                     &handle_delete_account_finished,
     171              :                                     adh);
     172              :   }
     173            0 :   return adh;
     174              : }
     175              : 
     176              : 
     177              : void
     178            0 : TALER_MERCHANT_account_delete_cancel (
     179              :   struct TALER_MERCHANT_AccountDeleteHandle *adh)
     180              : {
     181            0 :   if (NULL != adh->job)
     182            0 :     GNUNET_CURL_job_cancel (adh->job);
     183            0 :   GNUNET_free (adh->url);
     184            0 :   GNUNET_free (adh);
     185            0 : }
        
               |