Line data Source code
1 : /* 2 : This file is part of TALER 3 : (C) 2021 Taler Systems SA 4 : 5 : TALER is free software; you can redistribute it and/or modify 6 : it under the terms of the GNU General Public License as 7 : published by the Free Software Foundation; either version 3, or 8 : (at your option) any later version. 9 : 10 : TALER is distributed in the hope that it will be useful, but 11 : WITHOUT ANY WARRANTY; without even the implied warranty of 12 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 : GNU General Public License for more details. 14 : 15 : You should have received a copy of the GNU General Public 16 : License along with TALER; see the file COPYING. If not, see 17 : <http://www.gnu.org/licenses/> 18 : */ 19 : /** 20 : * @file testing/testing_api_cmd_change_auth.c 21 : * @brief command(s) to change CURL context authorization header 22 : * @author Christian Grothoff 23 : */ 24 : #include "platform.h" 25 : #include "taler_json_lib.h" 26 : #include <gnunet/gnunet_curl_lib.h> 27 : #include "taler_testing_lib.h" 28 : 29 : 30 : /** 31 : * State for a "authchange" CMD. 32 : */ 33 : struct AuthchangeState 34 : { 35 : 36 : /** 37 : * What is the new authorization token to send? 38 : */ 39 : const char *auth_token; 40 : 41 : /** 42 : * Old context, clean up on termination. 43 : */ 44 : struct GNUNET_CURL_Context *old_ctx; 45 : }; 46 : 47 : 48 : /** 49 : * Run the command. 50 : * 51 : * @param cls closure. 52 : * @param cmd the command to execute. 53 : * @param is the interpreter state. 54 : */ 55 : static void 56 0 : authchange_run (void *cls, 57 : const struct TALER_TESTING_Command *cmd, 58 : struct TALER_TESTING_Interpreter *is) 59 : { 60 0 : struct AuthchangeState *ss = cls; 61 : 62 : (void) cmd; 63 0 : ss->old_ctx = is->ctx; 64 0 : if (NULL != is->rc) 65 : { 66 0 : GNUNET_CURL_gnunet_rc_destroy (is->rc); 67 0 : is->rc = NULL; 68 : } 69 0 : is->ctx = GNUNET_CURL_init (&GNUNET_CURL_gnunet_scheduler_reschedule, 70 0 : &is->rc); 71 0 : GNUNET_CURL_enable_async_scope_header (is->ctx, 72 : "Taler-Correlation-Id"); 73 0 : GNUNET_assert (NULL != is->ctx); 74 0 : is->rc = GNUNET_CURL_gnunet_rc_create (is->ctx); 75 0 : if (NULL != ss->auth_token) 76 : { 77 : char *authorization; 78 : 79 0 : GNUNET_asprintf (&authorization, 80 : "%s: %s", 81 : MHD_HTTP_HEADER_AUTHORIZATION, 82 : ss->auth_token); 83 0 : GNUNET_assert (GNUNET_OK == 84 : GNUNET_CURL_append_header (is->ctx, 85 : authorization)); 86 0 : GNUNET_free (authorization); 87 : } 88 0 : TALER_TESTING_interpreter_next (is); 89 0 : } 90 : 91 : 92 : /** 93 : * Call GNUNET_CURL_fini(). Done as a separate task to 94 : * ensure that all of the command's cleanups have been 95 : * executed first. See #7151. 96 : * 97 : * @param cls a `struct GNUNET_CURL_Context *` to clean up. 98 : */ 99 : static void 100 0 : deferred_cleanup_cb (void *cls) 101 : { 102 0 : struct GNUNET_CURL_Context *ctx = cls; 103 : 104 0 : GNUNET_CURL_fini (ctx); 105 0 : } 106 : 107 : 108 : /** 109 : * Cleanup the state from a "authchange" CMD. 110 : * 111 : * @param cls closure. 112 : * @param cmd the command which is being cleaned up. 113 : */ 114 : static void 115 0 : authchange_cleanup (void *cls, 116 : const struct TALER_TESTING_Command *cmd) 117 : { 118 0 : struct AuthchangeState *ss = cls; 119 : 120 : (void) cmd; 121 0 : if (NULL != ss->old_ctx) 122 : { 123 0 : (void) GNUNET_SCHEDULER_add_now (&deferred_cleanup_cb, 124 0 : ss->old_ctx); 125 0 : ss->old_ctx = NULL; 126 : } 127 0 : GNUNET_free (ss); 128 0 : } 129 : 130 : 131 : struct TALER_TESTING_Command 132 0 : TALER_TESTING_cmd_set_authorization (const char *label, 133 : const char *auth_token) 134 : { 135 : struct AuthchangeState *ss; 136 : 137 0 : ss = GNUNET_new (struct AuthchangeState); 138 0 : ss->auth_token = auth_token; 139 : 140 : { 141 0 : struct TALER_TESTING_Command cmd = { 142 : .cls = ss, 143 : .label = label, 144 : .run = &authchange_run, 145 : .cleanup = &authchange_cleanup 146 : }; 147 : 148 0 : return cmd; 149 : } 150 : } 151 : 152 : 153 : /* end of testing_api_cmd_change_auth.c */