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