exchange

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

taler-exchange-httpd_management_wire_disable.c (6380B)


      1 /*
      2   This file is part of TALER
      3   Copyright (C) 2020-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 Affero 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 Affero General Public License for more details.
     12 
     13   You should have received a copy of the GNU Affero General Public License along with
     14   TALER; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
     15 */
     16 /**
     17  * @file taler-exchange-httpd_management_wire_disable.c
     18  * @brief Handle request to disable wire account.
     19  * @author Christian Grothoff
     20  */
     21 #include "taler/platform.h"
     22 #include <gnunet/gnunet_util_lib.h>
     23 #include <gnunet/gnunet_json_lib.h>
     24 #include <jansson.h>
     25 #include <microhttpd.h>
     26 #include <pthread.h>
     27 #include "taler/taler_json_lib.h"
     28 #include "taler/taler_mhd_lib.h"
     29 #include "taler-exchange-httpd_management.h"
     30 #include "taler-exchange-httpd_responses.h"
     31 #include "taler-exchange-httpd_keys.h"
     32 
     33 
     34 /**
     35  * Closure for the #del_wire transaction.
     36  */
     37 struct DelWireContext
     38 {
     39   /**
     40    * Master signature affirming the WIRE DEL operation
     41    * (includes timestamp).
     42    */
     43   struct TALER_MasterSignatureP master_sig;
     44 
     45   /**
     46    * Payto:// URI this is about.
     47    */
     48   struct TALER_FullPayto payto_uri;
     49 
     50   /**
     51    * Timestamp for checking against replay attacks.
     52    */
     53   struct GNUNET_TIME_Timestamp validity_end;
     54 
     55 };
     56 
     57 
     58 /**
     59  * Function implementing database transaction to del an wire.  Runs the
     60  * transaction logic; IF it returns a non-error code, the transaction logic
     61  * MUST NOT queue a MHD response.  IF it returns an hard error, the
     62  * transaction logic MUST queue a MHD response and set @a mhd_ret.  IF it
     63  * returns the soft error code, the function MAY be called again to retry and
     64  * MUST not queue a MHD response.
     65  *
     66  * @param cls closure with a `struct DelWireContext`
     67  * @param connection MHD request which triggered the transaction
     68  * @param[out] mhd_ret set to MHD response status for @a connection,
     69  *             if transaction failed (!)
     70  * @return transaction status
     71  */
     72 static enum GNUNET_DB_QueryStatus
     73 del_wire (void *cls,
     74           struct MHD_Connection *connection,
     75           MHD_RESULT *mhd_ret)
     76 {
     77   struct DelWireContext *awc = cls;
     78   struct GNUNET_TIME_Timestamp last_date;
     79   enum GNUNET_DB_QueryStatus qs;
     80 
     81   qs = TEH_plugin->lookup_wire_timestamp (TEH_plugin->cls,
     82                                           awc->payto_uri,
     83                                           &last_date);
     84   if (qs < 0)
     85   {
     86     if (GNUNET_DB_STATUS_SOFT_ERROR == qs)
     87       return qs;
     88     GNUNET_break (0);
     89     *mhd_ret = TALER_MHD_reply_with_error (connection,
     90                                            MHD_HTTP_INTERNAL_SERVER_ERROR,
     91                                            TALER_EC_GENERIC_DB_FETCH_FAILED,
     92                                            "lookup wire");
     93     return qs;
     94   }
     95   if (GNUNET_TIME_timestamp_cmp (last_date,
     96                                  >,
     97                                  awc->validity_end))
     98   {
     99     *mhd_ret = TALER_MHD_reply_with_error (
    100       connection,
    101       MHD_HTTP_CONFLICT,
    102       TALER_EC_EXCHANGE_MANAGEMENT_WIRE_MORE_RECENT_PRESENT,
    103       NULL);
    104     return GNUNET_DB_STATUS_HARD_ERROR;
    105   }
    106   if (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS == qs)
    107   {
    108     *mhd_ret = TALER_MHD_reply_with_error (
    109       connection,
    110       MHD_HTTP_NOT_FOUND,
    111       TALER_EC_EXCHANGE_MANAGEMENT_WIRE_NOT_FOUND,
    112       NULL);
    113     return GNUNET_DB_STATUS_HARD_ERROR;
    114   }
    115   qs = TEH_plugin->update_wire (TEH_plugin->cls,
    116                                 awc->payto_uri,
    117                                 NULL,
    118                                 NULL,
    119                                 NULL,
    120                                 NULL,
    121                                 NULL,
    122                                 awc->validity_end,
    123                                 NULL,
    124                                 NULL,
    125                                 0,
    126                                 false);
    127   if (qs < 0)
    128   {
    129     GNUNET_break (0);
    130     if (GNUNET_DB_STATUS_SOFT_ERROR == qs)
    131       return qs;
    132     *mhd_ret = TALER_MHD_reply_with_error (connection,
    133                                            MHD_HTTP_INTERNAL_SERVER_ERROR,
    134                                            TALER_EC_GENERIC_DB_STORE_FAILED,
    135                                            "del wire");
    136     return qs;
    137   }
    138   return qs;
    139 }
    140 
    141 
    142 MHD_RESULT
    143 TEH_handler_management_post_wire_disable (
    144   struct MHD_Connection *connection,
    145   const json_t *root)
    146 {
    147   struct DelWireContext awc;
    148   struct GNUNET_JSON_Specification spec[] = {
    149     GNUNET_JSON_spec_fixed_auto ("master_sig_del",
    150                                  &awc.master_sig),
    151     TALER_JSON_spec_full_payto_uri ("payto_uri",
    152                                     &awc.payto_uri),
    153     GNUNET_JSON_spec_timestamp ("validity_end",
    154                                 &awc.validity_end),
    155     GNUNET_JSON_spec_end ()
    156   };
    157 
    158   {
    159     enum GNUNET_GenericReturnValue res;
    160 
    161     res = TALER_MHD_parse_json_data (connection,
    162                                      root,
    163                                      spec);
    164     if (GNUNET_SYSERR == res)
    165       return MHD_NO; /* hard failure */
    166     if (GNUNET_NO == res)
    167       return MHD_YES; /* failure */
    168   }
    169   if (GNUNET_OK !=
    170       TALER_exchange_offline_wire_del_verify (
    171         awc.payto_uri,
    172         awc.validity_end,
    173         &TEH_master_public_key,
    174         &awc.master_sig))
    175   {
    176     GNUNET_break_op (0);
    177     return TALER_MHD_reply_with_error (
    178       connection,
    179       MHD_HTTP_FORBIDDEN,
    180       TALER_EC_EXCHANGE_MANAGEMENT_WIRE_DEL_SIGNATURE_INVALID,
    181       NULL);
    182   }
    183 
    184   {
    185     enum GNUNET_GenericReturnValue res;
    186     MHD_RESULT ret;
    187 
    188     res = TEH_DB_run_transaction (connection,
    189                                   "del wire",
    190                                   TEH_MT_REQUEST_OTHER,
    191                                   &ret,
    192                                   &del_wire,
    193                                   &awc);
    194     if (GNUNET_SYSERR == res)
    195       return ret;
    196   }
    197   TEH_wire_update_state ();
    198   return TALER_MHD_reply_static (
    199     connection,
    200     MHD_HTTP_NO_CONTENT,
    201     NULL,
    202     NULL,
    203     0);
    204 }
    205 
    206 
    207 /* end of taler-exchange-httpd_management_wire_disable.c */