LCOV - code coverage report
Current view: top level - lib - merchant_api_post_instance_auth.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 42 65 64.6 %
Date: 2025-06-23 16:22:09 Functions: 3 3 100.0 %

          Line data    Source code
       1             : /*
       2             :   This file is part of TALER
       3             :   Copyright (C) 2014-2021 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_post_instance_auth.c
      19             :  * @brief Implementation of the POST /instance/$ID/private/auth request
      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             : #include <taler/taler_curl_lib.h>
      33             : 
      34             : 
      35             : /**
      36             :  * Handle for a POST /instances/$ID/private/auth operation.
      37             :  */
      38             : struct TALER_MERCHANT_InstanceAuthPostHandle
      39             : {
      40             :   /**
      41             :    * The url for this request.
      42             :    */
      43             :   char *url;
      44             : 
      45             :   /**
      46             :    * Handle for the request.
      47             :    */
      48             :   struct GNUNET_CURL_Job *job;
      49             : 
      50             :   /**
      51             :    * Function to call with the result.
      52             :    */
      53             :   TALER_MERCHANT_InstanceAuthPostCallback cb;
      54             : 
      55             :   /**
      56             :    * Closure for @a cb.
      57             :    */
      58             :   void *cb_cls;
      59             : 
      60             :   /**
      61             :    * Reference to the execution context.
      62             :    */
      63             :   struct GNUNET_CURL_Context *ctx;
      64             : 
      65             :   /**
      66             :    * Minor context that holds body and headers.
      67             :    */
      68             :   struct TALER_CURL_PostContext post_ctx;
      69             : 
      70             : };
      71             : 
      72             : 
      73             : /**
      74             :  * Function called when we're done processing the
      75             :  * HTTP GET /instances/$ID/private/auth request.
      76             :  *
      77             :  * @param cls the `struct TALER_MERCHANT_InstanceAuthPostHandle`
      78             :  * @param response_code HTTP response code, 0 on error
      79             :  * @param response response body, NULL if not in JSON
      80             :  */
      81             : static void
      82           6 : handle_post_instance_auth_finished (void *cls,
      83             :                                     long response_code,
      84             :                                     const void *response)
      85             : {
      86           6 :   struct TALER_MERCHANT_InstanceAuthPostHandle *iaph = cls;
      87           6 :   const json_t *json = response;
      88           6 :   struct TALER_MERCHANT_HttpResponse hr = {
      89           6 :     .http_status = (unsigned int) response_code,
      90             :     .reply = json
      91             :   };
      92             : 
      93           6 :   iaph->job = NULL;
      94           6 :   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
      95             :               "Got /instances/$ID response with status code %u\n",
      96             :               (unsigned int) response_code);
      97           6 :   switch (response_code)
      98             :   {
      99           6 :   case MHD_HTTP_NO_CONTENT:
     100           6 :     break;
     101           0 :   case MHD_HTTP_BAD_REQUEST:
     102             :     /* happens if the auth token is malformed */
     103           0 :     hr.ec = TALER_JSON_get_error_code (json);
     104           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     105             :     /* Nothing really to verify, merchant says we need to authenticate. */
     106           0 :     break;
     107           0 :   case MHD_HTTP_UNAUTHORIZED:
     108           0 :     hr.ec = TALER_JSON_get_error_code (json);
     109           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     110             :     /* Nothing really to verify, merchant says we need to authenticate. */
     111           0 :     break;
     112           0 :   default:
     113             :     /* unexpected response code */
     114           0 :     hr.ec = TALER_JSON_get_error_code (json);
     115           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     116           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     117             :                 "Unexpected response code %u/%d\n",
     118             :                 (unsigned int) response_code,
     119             :                 (int) hr.ec);
     120           0 :     break;
     121             :   }
     122           6 :   iaph->cb (iaph->cb_cls,
     123             :             &hr);
     124           6 :   TALER_MERCHANT_instance_auth_post_cancel (iaph);
     125           6 : }
     126             : 
     127             : 
     128             : struct TALER_MERCHANT_InstanceAuthPostHandle *
     129           6 : TALER_MERCHANT_instance_auth_post (
     130             :   struct GNUNET_CURL_Context *ctx,
     131             :   const char *backend_url,
     132             :   const char *instance_id,
     133             :   const char *auth_password,
     134             :   TALER_MERCHANT_InstanceAuthPostCallback cb,
     135             :   void *cb_cls)
     136             : {
     137             :   struct TALER_MERCHANT_InstanceAuthPostHandle *iaph;
     138             :   json_t *req_obj;
     139             : 
     140           6 :   iaph = GNUNET_new (struct TALER_MERCHANT_InstanceAuthPostHandle);
     141           6 :   iaph->ctx = ctx;
     142           6 :   iaph->cb = cb;
     143           6 :   iaph->cb_cls = cb_cls;
     144           6 :   if (NULL != instance_id)
     145             :   {
     146             :     char *path;
     147             : 
     148           2 :     GNUNET_asprintf (&path,
     149             :                      "management/instances/%s/auth",
     150             :                      instance_id);
     151           2 :     iaph->url = TALER_url_join (backend_url,
     152             :                                 path,
     153             :                                 NULL);
     154           2 :     GNUNET_free (path);
     155             :   }
     156             :   else
     157             :   {
     158             :     /* backend_url is already identifying the instance */
     159           4 :     iaph->url = TALER_url_join (backend_url,
     160             :                                 "private/auth",
     161             :                                 NULL);
     162             :   }
     163           6 :   if (NULL == iaph->url)
     164             :   {
     165           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     166             :                 "Could not construct request URL.\n");
     167           0 :     GNUNET_free (iaph);
     168           0 :     return NULL;
     169             :   }
     170           6 :   if (NULL == auth_password)
     171             :   {
     172           2 :     req_obj = GNUNET_JSON_PACK (
     173             :       GNUNET_JSON_pack_string ("method",
     174             :                                "external"));
     175             :   }
     176             :   else
     177             :   {
     178           4 :     req_obj = GNUNET_JSON_PACK (
     179             :       GNUNET_JSON_pack_string ("method",
     180             :                                "token"),
     181             :       GNUNET_JSON_pack_string ("password",
     182             :                                auth_password));
     183             :   }
     184           6 :   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
     185             :               "Requesting URL '%s'\n",
     186             :               iaph->url);
     187             :   {
     188             :     CURL *eh;
     189             : 
     190           6 :     eh = TALER_MERCHANT_curl_easy_get_ (iaph->url);
     191           6 :     if (GNUNET_OK !=
     192           6 :         TALER_curl_easy_post (&iaph->post_ctx,
     193             :                               eh,
     194             :                               req_obj))
     195             :     {
     196           0 :       GNUNET_break (0);
     197           0 :       curl_easy_cleanup (eh);
     198           0 :       json_decref (req_obj);
     199           0 :       GNUNET_free (iaph->url);
     200           0 :       GNUNET_free (iaph);
     201           0 :       return NULL;
     202             :     }
     203           6 :     json_decref (req_obj);
     204           6 :     GNUNET_assert (CURLE_OK ==
     205             :                    curl_easy_setopt (eh,
     206             :                                      CURLOPT_CUSTOMREQUEST,
     207             :                                      MHD_HTTP_METHOD_POST));
     208          12 :     iaph->job = GNUNET_CURL_job_add2 (ctx,
     209             :                                       eh,
     210           6 :                                       iaph->post_ctx.headers,
     211             :                                       &handle_post_instance_auth_finished,
     212             :                                       iaph);
     213             :   }
     214           6 :   return iaph;
     215             : }
     216             : 
     217             : 
     218             : void
     219           6 : TALER_MERCHANT_instance_auth_post_cancel (
     220             :   struct TALER_MERCHANT_InstanceAuthPostHandle *iaph)
     221             : {
     222           6 :   if (NULL != iaph->job)
     223           0 :     GNUNET_CURL_job_cancel (iaph->job);
     224           6 :   TALER_curl_easy_post_finished (&iaph->post_ctx);
     225           6 :   GNUNET_free (iaph->url);
     226           6 :   GNUNET_free (iaph);
     227           6 : }

Generated by: LCOV version 1.16