LCOV - code coverage report
Current view: top level - backend - taler-merchant-httpd_private-post-reserves-ID-authorize-tip.c (source / functions) Hit Total Coverage
Test: GNU Taler merchant coverage report Lines: 37 51 72.5 %
Date: 2021-08-30 06:54:17 Functions: 3 3 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :   This file is part of TALER
       3             :   (C) 2014-2020 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 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 <http://www.gnu.org/licenses/>
      15             : */
      16             : /**
      17             :  * @file taler-merchant-httpd_private-post-reserves-ID-authorize-tip.c
      18             :  * @brief implement API for authorizing tips to be paid to visitors
      19             :  * @author Christian Grothoff
      20             :  */
      21             : #include "platform.h"
      22             : #include <jansson.h>
      23             : #include <taler/taler_util.h>
      24             : #include <taler/taler_json_lib.h>
      25             : #include "taler-merchant-httpd.h"
      26             : #include "taler-merchant-httpd_mhd.h"
      27             : #include "taler-merchant-httpd_get-tips-ID.h"
      28             : #include "taler-merchant-httpd_private-post-reserves-ID-authorize-tip.h"
      29             : 
      30             : 
      31             : /**
      32             :  * Handle a "tip-authorize" request.
      33             :  *
      34             :  * @param rh context of the handler
      35             :  * @param connection the MHD connection to handle
      36             :  * @param[in,out] hc context with further information about the request
      37             :  * @param reserve_pub reserve to use, or NULL for "any"
      38             :  * @return MHD result code
      39             :  */
      40             : static MHD_RESULT
      41           4 : authorize_tip (const struct TMH_RequestHandler *rh,
      42             :                struct MHD_Connection *connection,
      43             :                struct TMH_HandlerContext *hc,
      44             :                const struct TALER_ReservePublicKeyP *reserve_pub)
      45             : {
      46             :   enum TALER_ErrorCode ec;
      47             :   struct GNUNET_TIME_Absolute expiration;
      48             :   struct GNUNET_HashCode tip_id;
      49             :   const char *justification;
      50             :   const char *next_url;
      51             :   struct TALER_Amount amount;
      52             :   {
      53             :     struct GNUNET_JSON_Specification spec[] = {
      54           4 :       TALER_JSON_spec_amount ("amount",
      55             :                               TMH_currency,
      56             :                               &amount),
      57           4 :       GNUNET_JSON_spec_string ("justification",
      58             :                                &justification),
      59           4 :       GNUNET_JSON_spec_string ("next_url",
      60             :                                &next_url),
      61           4 :       GNUNET_JSON_spec_end ()
      62             :     };
      63             :     enum GNUNET_GenericReturnValue res;
      64             : 
      65           4 :     res = TALER_MHD_parse_json_data (connection,
      66           4 :                                      hc->request_body,
      67             :                                      spec);
      68           4 :     if (GNUNET_YES != res)
      69             :     {
      70           0 :       GNUNET_break_op (0);
      71             :       return (GNUNET_NO == res)
      72             :              ? MHD_YES
      73           0 :              : MHD_NO;
      74             :     }
      75             :   }
      76           4 :   TMH_db->preflight (TMH_db->cls);
      77           4 :   ec = TMH_db->authorize_tip (TMH_db->cls,
      78           4 :                               hc->instance->settings.id,
      79             :                               reserve_pub,
      80             :                               &amount,
      81             :                               justification,
      82             :                               next_url,
      83             :                               &tip_id,
      84             :                               &expiration);
      85             :   /* handle errors */
      86           4 :   if (TALER_EC_NONE != ec)
      87             :   {
      88             :     unsigned int http_status;
      89             : 
      90           2 :     switch (ec)
      91             :     {
      92           1 :     case TALER_EC_MERCHANT_PRIVATE_POST_TIP_AUTHORIZE_INSUFFICIENT_FUNDS:
      93           1 :       http_status = MHD_HTTP_PRECONDITION_FAILED;
      94           1 :       break;
      95           0 :     case TALER_EC_MERCHANT_PRIVATE_POST_TIP_AUTHORIZE_RESERVE_EXPIRED:
      96           0 :       http_status = MHD_HTTP_GONE;
      97           0 :       break;
      98           0 :     case TALER_EC_MERCHANT_PRIVATE_POST_TIP_AUTHORIZE_RESERVE_UNKNOWN:
      99           0 :       http_status = MHD_HTTP_SERVICE_UNAVAILABLE;
     100           0 :       break;
     101           1 :     case TALER_EC_MERCHANT_PRIVATE_POST_TIP_AUTHORIZE_RESERVE_NOT_FOUND:
     102           1 :       http_status = MHD_HTTP_NOT_FOUND;
     103           1 :       break;
     104           0 :     default:
     105           0 :       http_status = MHD_HTTP_INTERNAL_SERVER_ERROR;
     106           0 :       break;
     107             :     }
     108             : 
     109           2 :     return TALER_MHD_reply_with_error (connection,
     110             :                                        http_status,
     111             :                                        ec,
     112             :                                        NULL);
     113             :   }
     114             : 
     115             :   /* generate success response */
     116             :   {
     117             :     char *taler_tip_uri;
     118             :     char *tip_status_url;
     119             :     struct GNUNET_CRYPTO_HashAsciiEncoded hash_enc;
     120             :     MHD_RESULT res;
     121             : 
     122           2 :     GNUNET_CRYPTO_hash_to_enc (&tip_id,
     123             :                                &hash_enc);
     124           2 :     taler_tip_uri = TMH_make_taler_tip_uri (connection,
     125             :                                             &tip_id,
     126           2 :                                             hc->instance->settings.id);
     127           2 :     tip_status_url = TMH_make_tip_status_url (connection,
     128             :                                               &tip_id,
     129           2 :                                               hc->instance->settings.id);
     130           2 :     GNUNET_TIME_round_abs (&expiration);
     131           2 :     res = TALER_MHD_REPLY_JSON_PACK (
     132             :       connection,
     133             :       MHD_HTTP_OK,
     134             :       GNUNET_JSON_pack_string ("tip_id",
     135             :                                (const char *) hash_enc.encoding),
     136             :       GNUNET_JSON_pack_string ("taler_tip_uri",
     137             :                                taler_tip_uri),
     138             :       GNUNET_JSON_pack_string ("tip_status_url",
     139             :                                tip_status_url),
     140             :       GNUNET_JSON_pack_time_abs ("tip_expiration",
     141             :                                  expiration));
     142           2 :     GNUNET_free (taler_tip_uri);
     143           2 :     GNUNET_free (tip_status_url);
     144           2 :     return res;
     145             :   }
     146             : }
     147             : 
     148             : 
     149             : MHD_RESULT
     150           3 : TMH_private_post_reserves_ID_authorize_tip (const struct TMH_RequestHandler *rh,
     151             :                                             struct MHD_Connection *connection,
     152             :                                             struct TMH_HandlerContext *hc)
     153             : {
     154             :   struct TALER_ReservePublicKeyP reserve_pub;
     155             : 
     156           3 :   if (GNUNET_OK !=
     157           3 :       GNUNET_STRINGS_string_to_data (hc->infix,
     158           3 :                                      strlen (hc->infix),
     159             :                                      &reserve_pub,
     160             :                                      sizeof (reserve_pub)))
     161             :   {
     162           0 :     GNUNET_break_op (0);
     163           0 :     return TALER_MHD_reply_with_error (connection,
     164             :                                        MHD_HTTP_BAD_REQUEST,
     165             :                                        TALER_EC_MERCHANT_GENERIC_RESERVE_PUB_MALFORMED,
     166           0 :                                        hc->infix);
     167             :   }
     168           3 :   return authorize_tip (rh,
     169             :                         connection,
     170             :                         hc,
     171             :                         &reserve_pub);
     172             : }
     173             : 
     174             : 
     175             : MHD_RESULT
     176           1 : TMH_private_post_tips (const struct TMH_RequestHandler *rh,
     177             :                        struct MHD_Connection *connection,
     178             :                        struct TMH_HandlerContext *hc)
     179             : {
     180           1 :   return authorize_tip (rh,
     181             :                         connection,
     182             :                         hc,
     183             :                         NULL);
     184             : }
     185             : 
     186             : 
     187             : /* end of taler-merchant-httpd_private-post-reserves-ID-authorize-tip.c */

Generated by: LCOV version 1.14