exchange

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

exchange_api_post-management-aml-officers.c (7810B)


      1 /*
      2   This file is part of TALER
      3   Copyright (C) 2023-2026 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_post-management-aml-officers.c
     19  * @brief functions to update AML officer status via POST /management/aml-officers
     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 "taler/taler-exchange/post-management-aml-officers.h"
     28 #include "exchange_api_curl_defaults.h"
     29 #include "taler/taler_signatures.h"
     30 #include "taler/taler_curl_lib.h"
     31 
     32 
     33 /**
     34  * Handle for a POST /management/aml-officers request.
     35  */
     36 struct TALER_EXCHANGE_PostManagementAmlOfficersHandle
     37 {
     38 
     39   /**
     40    * The base URL for this request.
     41    */
     42   char *base_url;
     43 
     44   /**
     45    * The full URL for this request, set during _start.
     46    */
     47   char *url;
     48 
     49   /**
     50    * Minor context that holds body and headers.
     51    */
     52   struct TALER_CURL_PostContext post_ctx;
     53 
     54   /**
     55    * Handle for the request.
     56    */
     57   struct GNUNET_CURL_Job *job;
     58 
     59   /**
     60    * Function to call with the result.
     61    */
     62   TALER_EXCHANGE_PostManagementAmlOfficersCallback cb;
     63 
     64   /**
     65    * Closure for @a cb.
     66    */
     67   TALER_EXCHANGE_POST_MANAGEMENT_AML_OFFICERS_RESULT_CLOSURE *cb_cls;
     68 
     69   /**
     70    * Reference to the execution context.
     71    */
     72   struct GNUNET_CURL_Context *ctx;
     73 
     74   /**
     75    * Public key of the officer.
     76    */
     77   struct TALER_AmlOfficerPublicKeyP officer_pub;
     78 
     79   /**
     80    * Name of the officer.
     81    */
     82   char *officer_name;
     83 
     84   /**
     85    * When to affect the status change.
     86    */
     87   struct GNUNET_TIME_Timestamp change_date;
     88 
     89   /**
     90    * Is the officer active?
     91    */
     92   bool is_active;
     93 
     94   /**
     95    * Is the officer read-only?
     96    */
     97   bool read_only;
     98 
     99   /**
    100    * Master signature affirming the change.
    101    */
    102   struct TALER_MasterSignatureP master_sig;
    103 
    104 };
    105 
    106 
    107 /**
    108  * Function called when we're done processing the
    109  * HTTP POST /management/aml-officers request.
    110  *
    111  * @param cls the `struct TALER_EXCHANGE_PostManagementAmlOfficersHandle`
    112  * @param response_code HTTP response code, 0 on error
    113  * @param response response body, NULL if not in JSON
    114  */
    115 static void
    116 handle_post_aml_officer_finished (void *cls,
    117                                   long response_code,
    118                                   const void *response)
    119 {
    120   struct TALER_EXCHANGE_PostManagementAmlOfficersHandle *pmaoh = cls;
    121   const json_t *json = response;
    122   struct TALER_EXCHANGE_PostManagementAmlOfficersResponse res = {
    123     .hr.http_status = (unsigned int) response_code,
    124     .hr.reply = json
    125   };
    126 
    127   pmaoh->job = NULL;
    128   switch (response_code)
    129   {
    130   case 0:
    131     /* no reply */
    132     res.hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
    133     res.hr.hint = "server offline?";
    134     break;
    135   case MHD_HTTP_NO_CONTENT:
    136     break;
    137   case MHD_HTTP_FORBIDDEN:
    138     res.hr.ec = TALER_JSON_get_error_code (json);
    139     res.hr.hint = TALER_JSON_get_error_hint (json);
    140     break;
    141   case MHD_HTTP_CONFLICT:
    142     res.hr.ec = TALER_JSON_get_error_code (json);
    143     res.hr.hint = TALER_JSON_get_error_hint (json);
    144     break;
    145   default:
    146     /* unexpected response code */
    147     GNUNET_break_op (0);
    148     res.hr.ec = TALER_JSON_get_error_code (json);
    149     res.hr.hint = TALER_JSON_get_error_hint (json);
    150     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    151                 "Unexpected response code %u/%d for exchange POST management AML officer\n",
    152                 (unsigned int) response_code,
    153                 (int) res.hr.ec);
    154     break;
    155   }
    156   if (NULL != pmaoh->cb)
    157   {
    158     pmaoh->cb (pmaoh->cb_cls,
    159                &res);
    160     pmaoh->cb = NULL;
    161   }
    162   TALER_EXCHANGE_post_management_aml_officers_cancel (pmaoh);
    163 }
    164 
    165 
    166 struct TALER_EXCHANGE_PostManagementAmlOfficersHandle *
    167 TALER_EXCHANGE_post_management_aml_officers_create (
    168   struct GNUNET_CURL_Context *ctx,
    169   const char *url,
    170   const struct TALER_AmlOfficerPublicKeyP *officer_pub,
    171   const char *officer_name,
    172   struct GNUNET_TIME_Timestamp change_date,
    173   bool is_active,
    174   bool read_only,
    175   const struct TALER_MasterSignatureP *master_sig)
    176 {
    177   struct TALER_EXCHANGE_PostManagementAmlOfficersHandle *pmaoh;
    178 
    179   pmaoh = GNUNET_new (struct TALER_EXCHANGE_PostManagementAmlOfficersHandle);
    180   pmaoh->ctx = ctx;
    181   pmaoh->base_url = GNUNET_strdup (url);
    182   pmaoh->officer_pub = *officer_pub;
    183   pmaoh->officer_name = GNUNET_strdup (officer_name);
    184   pmaoh->change_date = change_date;
    185   pmaoh->is_active = is_active;
    186   pmaoh->read_only = read_only;
    187   pmaoh->master_sig = *master_sig;
    188   return pmaoh;
    189 }
    190 
    191 
    192 enum TALER_ErrorCode
    193 TALER_EXCHANGE_post_management_aml_officers_start (
    194   struct TALER_EXCHANGE_PostManagementAmlOfficersHandle *pmaoh,
    195   TALER_EXCHANGE_PostManagementAmlOfficersCallback cb,
    196   TALER_EXCHANGE_POST_MANAGEMENT_AML_OFFICERS_RESULT_CLOSURE *cb_cls)
    197 {
    198   CURL *eh;
    199   json_t *body;
    200 
    201   pmaoh->cb = cb;
    202   pmaoh->cb_cls = cb_cls;
    203   pmaoh->url = TALER_url_join (pmaoh->base_url,
    204                                "management/aml-officers",
    205                                NULL);
    206   if (NULL == pmaoh->url)
    207   {
    208     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    209                 "Could not construct request URL.\n");
    210     return TALER_EC_GENERIC_CONFIGURATION_INVALID;
    211   }
    212   body = GNUNET_JSON_PACK (
    213     GNUNET_JSON_pack_string ("officer_name",
    214                              pmaoh->officer_name),
    215     GNUNET_JSON_pack_data_auto ("officer_pub",
    216                                 &pmaoh->officer_pub),
    217     GNUNET_JSON_pack_data_auto ("master_sig",
    218                                 &pmaoh->master_sig),
    219     GNUNET_JSON_pack_bool ("is_active",
    220                            pmaoh->is_active),
    221     GNUNET_JSON_pack_bool ("read_only",
    222                            pmaoh->read_only),
    223     GNUNET_JSON_pack_timestamp ("change_date",
    224                                 pmaoh->change_date));
    225   eh = TALER_EXCHANGE_curl_easy_get_ (pmaoh->url);
    226   if ( (NULL == eh) ||
    227        (GNUNET_OK !=
    228         TALER_curl_easy_post (&pmaoh->post_ctx,
    229                               eh,
    230                               body)) )
    231   {
    232     GNUNET_break (0);
    233     if (NULL != eh)
    234       curl_easy_cleanup (eh);
    235     json_decref (body);
    236     GNUNET_free (pmaoh->url);
    237     pmaoh->url = NULL;
    238     return TALER_EC_GENERIC_INTERNAL_INVARIANT_FAILURE;
    239   }
    240   json_decref (body);
    241   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
    242               "Requesting URL '%s'\n",
    243               pmaoh->url);
    244   pmaoh->job = GNUNET_CURL_job_add2 (pmaoh->ctx,
    245                                      eh,
    246                                      pmaoh->post_ctx.headers,
    247                                      &handle_post_aml_officer_finished,
    248                                      pmaoh);
    249   if (NULL == pmaoh->job)
    250   {
    251     TALER_curl_easy_post_finished (&pmaoh->post_ctx);
    252     GNUNET_free (pmaoh->url);
    253     pmaoh->url = NULL;
    254     return TALER_EC_GENERIC_INTERNAL_INVARIANT_FAILURE;
    255   }
    256   return TALER_EC_NONE;
    257 }
    258 
    259 
    260 void
    261 TALER_EXCHANGE_post_management_aml_officers_cancel (
    262   struct TALER_EXCHANGE_PostManagementAmlOfficersHandle *pmaoh)
    263 {
    264   if (NULL != pmaoh->job)
    265   {
    266     GNUNET_CURL_job_cancel (pmaoh->job);
    267     pmaoh->job = NULL;
    268   }
    269   TALER_curl_easy_post_finished (&pmaoh->post_ctx);
    270   GNUNET_free (pmaoh->url);
    271   GNUNET_free (pmaoh->officer_name);
    272   GNUNET_free (pmaoh->base_url);
    273   GNUNET_free (pmaoh);
    274 }
    275 
    276 
    277 /* end of exchange_api_post-management-aml-officers.c */