LCOV - code coverage report
Current view: top level - backend - taler-merchant-httpd_private-get-instances-ID-tokens.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 17 23 73.9 %
Date: 2025-08-28 06:06:54 Functions: 2 2 100.0 %

          Line data    Source code
       1             : /*
       2             :   This file is part of TALER
       3             :   (C) 2025 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-get-instances-ID-tokens.c
      18             :  * @brief implement GET /tokens
      19             :  * @author Martin Schanzenbach
      20             :  */
      21             : #include "platform.h"
      22             : #include "taler-merchant-httpd_private-get-instances-ID-tokens.h"
      23             : 
      24             : 
      25             : /**
      26             :  * Add token details to our JSON array.
      27             :  *
      28             :  * @param cls a `json_t *` JSON array to build
      29             :  * @param creation_time when the token was created
      30             :  * @param expiration_time when the token will expire
      31             :  * @param scope internal scope identifier for the token (mapped to string)
      32             :  * @param description human-readable purpose or context of the token
      33             :  * @param serial serial (row) number of the product in the database
      34             :  */
      35             : static void
      36           2 : add_token (void *cls,
      37             :            struct GNUNET_TIME_Timestamp creation_time,
      38             :            struct GNUNET_TIME_Timestamp expiration_time,
      39             :            uint32_t scope,
      40             :            const char *description,
      41             :            uint64_t serial)
      42             : {
      43           2 :   json_t *pa = cls;
      44             :   bool refreshable;
      45             :   const char *as;
      46             : 
      47           2 :   as = TMH_get_name_by_scope (scope,
      48             :                               &refreshable);
      49           2 :   if (NULL == as)
      50             :   {
      51           0 :     GNUNET_break (0);
      52           0 :     return;
      53             :   }
      54           2 :   GNUNET_assert (0 ==
      55             :                  json_array_append_new (
      56             :                    pa,
      57             :                    GNUNET_JSON_PACK (
      58             :                      GNUNET_JSON_pack_timestamp ("creation_time",
      59             :                                                  creation_time),
      60             :                      GNUNET_JSON_pack_timestamp ("expiration",
      61             :                                                  expiration_time),
      62             :                      GNUNET_JSON_pack_string ("scope",
      63             :                                               as),
      64             :                      GNUNET_JSON_pack_bool ("refreshable",
      65             :                                             refreshable),
      66             :                      GNUNET_JSON_pack_string ("description",
      67             :                                               description),
      68             :                      GNUNET_JSON_pack_uint64 ("serial",
      69             :                                               serial))));
      70             : }
      71             : 
      72             : 
      73             : MHD_RESULT
      74           1 : TMH_private_get_instances_ID_tokens (const struct TMH_RequestHandler *rh,
      75             :                                      struct MHD_Connection *connection,
      76             :                                      struct TMH_HandlerContext *hc)
      77             : {
      78             :   json_t *ta;
      79             :   enum GNUNET_DB_QueryStatus qs;
      80           1 :   int64_t limit = -20; /* default */
      81             :   uint64_t offset;
      82             : 
      83           1 :   TALER_MHD_parse_request_snumber (connection,
      84             :                                    "limit",
      85             :                                    &limit);
      86           1 :   if (limit > 0)
      87           0 :     offset = 0;
      88             :   else
      89           1 :     offset = INT64_MAX;
      90           1 :   TALER_MHD_parse_request_number (connection,
      91             :                                   "offset",
      92             :                                   &offset);
      93           1 :   ta = json_array ();
      94           1 :   GNUNET_assert (NULL != ta);
      95           1 :   qs = TMH_db->lookup_login_tokens (TMH_db->cls,
      96           1 :                                     hc->instance->settings.id,
      97             :                                     offset,
      98             :                                     limit,
      99             :                                     &add_token,
     100             :                                     ta);
     101           1 :   if (0 > qs)
     102             :   {
     103           0 :     GNUNET_break (0);
     104           0 :     json_decref (ta);
     105           0 :     return TALER_MHD_reply_with_error (connection,
     106             :                                        MHD_HTTP_INTERNAL_SERVER_ERROR,
     107             :                                        TALER_EC_GENERIC_DB_FETCH_FAILED,
     108             :                                        NULL);
     109             :   }
     110           1 :   return TALER_MHD_REPLY_JSON_PACK (connection,
     111             :                                     MHD_HTTP_OK,
     112             :                                     GNUNET_JSON_pack_array_steal ("tokens",
     113             :                                                                   ta));
     114             : }
     115             : 
     116             : 
     117             : /* end of taler-merchant-httpd_private-get-instances-ID-tokens.c */

Generated by: LCOV version 1.16