exchange

Base system with REST service to issue digital coins, run by the payment service provider
Log | Files | Refs | Submodules | README | LICENSE

exchange_api_management_update_aml_officer.c (6583B)


      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 General Public License as published by the Free Software
      7   Foundation; either version 3, 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 General Public License for more details.
     12 
     13   You should have received a copy of the GNU General Public License along with
     14   TALER; see the file COPYING.  If not, see
     15   <http://www.gnu.org/licenses/>
     16 */
     17 /**
     18  * @file lib/exchange_api_management_update_aml_officer.c
     19  * @brief functions to update AML officer status
     20  * @author Christian Grothoff
     21  */
     22 #include "taler/platform.h"
     23 #include "taler/taler_json_lib.h"
     24 #include <gnunet/gnunet_curl_lib.h>
     25 #include <microhttpd.h>
     26 #include "taler/taler_exchange_service.h"
     27 #include "exchange_api_curl_defaults.h"
     28 #include "taler/taler_signatures.h"
     29 #include "taler/taler_curl_lib.h"
     30 #include "taler/taler_json_lib.h"
     31 
     32 
     33 struct TALER_EXCHANGE_ManagementUpdateAmlOfficer
     34 {
     35 
     36   /**
     37    * The url for this request.
     38    */
     39   char *url;
     40 
     41   /**
     42    * Minor context that holds body and headers.
     43    */
     44   struct TALER_CURL_PostContext post_ctx;
     45 
     46   /**
     47    * Handle for the request.
     48    */
     49   struct GNUNET_CURL_Job *job;
     50 
     51   /**
     52    * Function to call with the result.
     53    */
     54   TALER_EXCHANGE_ManagementUpdateAmlOfficerCallback cb;
     55 
     56   /**
     57    * Closure for @a cb.
     58    */
     59   void *cb_cls;
     60 
     61   /**
     62    * Reference to the execution context.
     63    */
     64   struct GNUNET_CURL_Context *ctx;
     65 };
     66 
     67 
     68 /**
     69  * Function called when we're done processing the
     70  * HTTP /management/wire request.
     71  *
     72  * @param cls the `struct TALER_EXCHANGE_ManagementAuditorEnableHandle *`
     73  * @param response_code HTTP response code, 0 on error
     74  * @param response response body, NULL if not in JSON
     75  */
     76 static void
     77 handle_update_aml_officer_finished (void *cls,
     78                                     long response_code,
     79                                     const void *response)
     80 {
     81   struct TALER_EXCHANGE_ManagementUpdateAmlOfficer *wh = cls;
     82   const json_t *json = response;
     83   struct TALER_EXCHANGE_ManagementUpdateAmlOfficerResponse uar = {
     84     .hr.http_status = (unsigned int) response_code,
     85     .hr.reply = json
     86   };
     87 
     88   wh->job = NULL;
     89   switch (response_code)
     90   {
     91   case 0:
     92     /* no reply */
     93     uar.hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
     94     uar.hr.hint = "server offline?";
     95     break;
     96   case MHD_HTTP_NO_CONTENT:
     97     break;
     98   case MHD_HTTP_FORBIDDEN:
     99     uar.hr.ec = TALER_JSON_get_error_code (json);
    100     uar.hr.hint = TALER_JSON_get_error_hint (json);
    101     break;
    102   case MHD_HTTP_NOT_FOUND:
    103     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    104                 "Server did not find handler at `%s'. Did you configure the correct exchange base URL?\n",
    105                 wh->url);
    106     if (NULL != json)
    107     {
    108       uar.hr.ec = TALER_JSON_get_error_code (json);
    109       uar.hr.hint = TALER_JSON_get_error_hint (json);
    110     }
    111     else
    112     {
    113       uar.hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
    114       uar.hr.hint = TALER_ErrorCode_get_hint (uar.hr.ec);
    115     }
    116     break;
    117   case MHD_HTTP_CONFLICT:
    118     uar.hr.ec = TALER_JSON_get_error_code (json);
    119     uar.hr.hint = TALER_JSON_get_error_hint (json);
    120     break;
    121   default:
    122     /* unexpected response code */
    123     GNUNET_break_op (0);
    124     uar.hr.ec = TALER_JSON_get_error_code (json);
    125     uar.hr.hint = TALER_JSON_get_error_hint (json);
    126     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    127                 "Unexpected response code %u/%d for exchange management update AML officer\n",
    128                 (unsigned int) response_code,
    129                 (int) uar.hr.ec);
    130     break;
    131   }
    132   if (NULL != wh->cb)
    133   {
    134     wh->cb (wh->cb_cls,
    135             &uar);
    136     wh->cb = NULL;
    137   }
    138   TALER_EXCHANGE_management_update_aml_officer_cancel (wh);
    139 }
    140 
    141 
    142 struct TALER_EXCHANGE_ManagementUpdateAmlOfficer *
    143 TALER_EXCHANGE_management_update_aml_officer (
    144   struct GNUNET_CURL_Context *ctx,
    145   const char *url,
    146   const struct TALER_AmlOfficerPublicKeyP *officer_pub,
    147   const char *officer_name,
    148   struct GNUNET_TIME_Timestamp change_date,
    149   bool is_active,
    150   bool read_only,
    151   const struct TALER_MasterSignatureP *master_sig,
    152   TALER_EXCHANGE_ManagementUpdateAmlOfficerCallback cb,
    153   void *cb_cls)
    154 {
    155   struct TALER_EXCHANGE_ManagementUpdateAmlOfficer *wh;
    156   CURL *eh;
    157   json_t *body;
    158 
    159   wh = GNUNET_new (struct TALER_EXCHANGE_ManagementUpdateAmlOfficer);
    160   wh->cb = cb;
    161   wh->cb_cls = cb_cls;
    162   wh->ctx = ctx;
    163   wh->url = TALER_url_join (url,
    164                             "management/aml-officers",
    165                             NULL);
    166   if (NULL == wh->url)
    167   {
    168     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    169                 "Could not construct request URL.\n");
    170     GNUNET_free (wh);
    171     return NULL;
    172   }
    173   body = GNUNET_JSON_PACK (
    174     GNUNET_JSON_pack_string ("officer_name",
    175                              officer_name),
    176     GNUNET_JSON_pack_data_auto ("officer_pub",
    177                                 officer_pub),
    178     GNUNET_JSON_pack_data_auto ("master_sig",
    179                                 master_sig),
    180     GNUNET_JSON_pack_bool ("is_active",
    181                            is_active),
    182     GNUNET_JSON_pack_bool ("read_only",
    183                            read_only),
    184     GNUNET_JSON_pack_timestamp ("change_date",
    185                                 change_date));
    186   eh = TALER_EXCHANGE_curl_easy_get_ (wh->url);
    187   if ( (NULL == eh) ||
    188        (GNUNET_OK !=
    189         TALER_curl_easy_post (&wh->post_ctx,
    190                               eh,
    191                               body)) )
    192   {
    193     GNUNET_break (0);
    194     if (NULL != eh)
    195       curl_easy_cleanup (eh);
    196     json_decref (body);
    197     GNUNET_free (wh->url);
    198     GNUNET_free (wh);
    199     return NULL;
    200   }
    201   json_decref (body);
    202   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
    203               "Requesting URL '%s'\n",
    204               wh->url);
    205   wh->job = GNUNET_CURL_job_add2 (ctx,
    206                                   eh,
    207                                   wh->post_ctx.headers,
    208                                   &handle_update_aml_officer_finished,
    209                                   wh);
    210   if (NULL == wh->job)
    211   {
    212     TALER_EXCHANGE_management_update_aml_officer_cancel (wh);
    213     return NULL;
    214   }
    215   return wh;
    216 }
    217 
    218 
    219 void
    220 TALER_EXCHANGE_management_update_aml_officer_cancel (
    221   struct TALER_EXCHANGE_ManagementUpdateAmlOfficer *wh)
    222 {
    223   if (NULL != wh->job)
    224   {
    225     GNUNET_CURL_job_cancel (wh->job);
    226     wh->job = NULL;
    227   }
    228   TALER_curl_easy_post_finished (&wh->post_ctx);
    229   GNUNET_free (wh->url);
    230   GNUNET_free (wh);
    231 }