LCOV - code coverage report
Current view: top level - backend - taler-merchant-httpd_get-private-tokens.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 65.5 % 29 19
Test Date: 2026-09-04 23:42:01 Functions: 100.0 % 2 2

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

Generated by: LCOV version 2.0-1