challenger-httpd_authorize.c (13445B)
1 /* 2 This file is part of Challenger 3 Copyright (C) 2023, 2024 Taler Systems SA 4 5 Challenger 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 Challenger 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 Affero General Public License for more details. 12 13 You should have received a copy of the GNU Affero General Public License along with 14 Challenger; see the file COPYING. If not, see <http://www.gnu.org/licenses/> 15 */ 16 /** 17 * @file challenger-httpd_authorize.c 18 * @brief functions to handle incoming requests for authorizations 19 * @author Christian Grothoff 20 */ 21 #include "platform.h" 22 #include "challenger-httpd.h" 23 #include <gnunet/gnunet_util_lib.h> 24 #include <gnunet/gnunet_db_lib.h> 25 #include <taler/taler_templating_lib.h> 26 #include "challenger-httpd_authorize.h" 27 #include "challenger-httpd_common.h" 28 #include "challenger-httpd_spa.h" 29 #include "challenger-database/update_validation.h" 30 #include "challenger_cm_enums.h" 31 32 /** 33 * Maximum number of retries for the database interaction. 34 */ 35 #define MAX_RETRIES 3 36 37 /** 38 * Generate error reply. 39 * 40 * @param hc our context 41 * @param http_status HTTP status to return 42 * @param ec error code to return 43 * @param hint human-readable hint to give 44 */ 45 static enum MHD_Result 46 reply_error (struct CH_HandlerContext *hc, 47 unsigned int http_status, 48 enum TALER_ErrorCode ec, 49 const char *hint) 50 { 51 return TALER_MHD_reply_with_error ( 52 hc->connection, 53 http_status, 54 ec, 55 hint); 56 } 57 58 59 enum MHD_Result 60 CH_handler_authorize (struct CH_HandlerContext *hc, 61 const char *upload_data, 62 size_t *upload_data_size) 63 { 64 const char *response_type; 65 unsigned long long client_id; 66 const char *redirect_uri; 67 const char *state; 68 const char *scope; 69 const char *code_challenge; 70 enum CHALLENGER_CM code_challenge_method_enum; 71 struct CHALLENGER_ValidationNonceP nonce; 72 73 (void) upload_data; 74 (void) upload_data_size; 75 if (GNUNET_OK != 76 GNUNET_STRINGS_string_to_data (hc->path, 77 strlen (hc->path), 78 &nonce, 79 sizeof (nonce))) 80 { 81 GNUNET_break_op (0); 82 /* The nonce is present but unusable; that is a malformed request, 83 not a missing parameter and not an unknown validation. /challenge 84 and /solve answer the same way. */ 85 return reply_error ( 86 hc, 87 MHD_HTTP_BAD_REQUEST, 88 TALER_EC_GENERIC_PARAMETER_MALFORMED, 89 "nonce"); 90 } 91 response_type 92 = MHD_lookup_connection_value (hc->connection, 93 MHD_GET_ARGUMENT_KIND, 94 "response_type"); 95 if (NULL == response_type) 96 { 97 GNUNET_break_op (0); 98 return reply_error ( 99 hc, 100 MHD_HTTP_BAD_REQUEST, 101 TALER_EC_GENERIC_PARAMETER_MISSING, 102 "response_type"); 103 } 104 if (0 != strcmp (response_type, 105 "code")) 106 { 107 GNUNET_break_op (0); 108 return reply_error ( 109 hc, 110 MHD_HTTP_BAD_REQUEST, 111 TALER_EC_GENERIC_PARAMETER_MALFORMED, 112 "response_type (must be 'code')"); 113 } 114 115 { 116 const char *client_id_str; 117 char dummy; 118 119 client_id_str 120 = MHD_lookup_connection_value (hc->connection, 121 MHD_GET_ARGUMENT_KIND, 122 "client_id"); 123 if (NULL == client_id_str) 124 { 125 GNUNET_break_op (0); 126 return reply_error ( 127 hc, 128 MHD_HTTP_BAD_REQUEST, 129 TALER_EC_GENERIC_PARAMETER_MISSING, 130 "client_id"); 131 } 132 if (1 != sscanf (client_id_str, 133 "%llu%c", 134 &client_id, 135 &dummy)) 136 { 137 GNUNET_break_op (0); 138 return reply_error ( 139 hc, 140 MHD_HTTP_BAD_REQUEST, 141 TALER_EC_GENERIC_PARAMETER_MALFORMED, 142 "client_id"); 143 } 144 } 145 redirect_uri 146 = MHD_lookup_connection_value (hc->connection, 147 MHD_GET_ARGUMENT_KIND, 148 "redirect_uri"); 149 150 { 151 const char *code_challenge_method; 152 153 code_challenge_method 154 = MHD_lookup_connection_value (hc->connection, 155 MHD_GET_ARGUMENT_KIND, 156 "code_challenge_method"); 157 code_challenge_method_enum 158 = CHALLENGER_cm_from_string ( 159 code_challenge_method); 160 } 161 if (CHALLENGER_CM_UNKNOWN == code_challenge_method_enum) 162 { 163 GNUNET_break_op (0); 164 return reply_error (hc, 165 MHD_HTTP_BAD_REQUEST, 166 TALER_EC_GENERIC_PARAMETER_MALFORMED, 167 "Unsupported code_challenge_method, supported only \"plain\", \"S256\"."); 168 } 169 code_challenge = MHD_lookup_connection_value (hc->connection, 170 MHD_GET_ARGUMENT_KIND, 171 "code_challenge"); 172 if ( (NULL == code_challenge) && 173 (CHALLENGER_CM_EMPTY != code_challenge_method_enum) ) 174 { 175 /* Client specified code challenge method (plain or S256) but then did 176 not give the code_challenge. Reject bad request. */ 177 GNUNET_break_op (0); 178 return reply_error ( 179 hc, 180 MHD_HTTP_BAD_REQUEST, 181 TALER_EC_GENERIC_PARAMETER_MISSING, 182 "code_challenge"); 183 } 184 if ( (NULL != code_challenge) && 185 (CHALLENGER_CM_EMPTY == code_challenge_method_enum) ) 186 code_challenge_method_enum = CHALLENGER_CM_PLAIN; 187 188 /** 189 * Safety check to not allow public clients without s256 code_challenge 190 */ 191 if ( (NULL != redirect_uri) && 192 (! TALER_is_web_url (redirect_uri)) && 193 ( (CHALLENGER_CM_EMPTY == code_challenge_method_enum) || 194 (CHALLENGER_CM_PLAIN == code_challenge_method_enum) ) ) 195 { 196 GNUNET_break_op (0); 197 return reply_error ( 198 hc, 199 MHD_HTTP_BAD_REQUEST, 200 TALER_EC_GENERIC_PARAMETER_MALFORMED, 201 "redirect_uri (has to start with 'http://' or 'https://' or not use 'plain'/NULL as code_challenge)"); 202 } 203 204 state 205 = MHD_lookup_connection_value (hc->connection, 206 MHD_GET_ARGUMENT_KIND, 207 "state"); 208 scope 209 = MHD_lookup_connection_value (hc->connection, 210 MHD_GET_ARGUMENT_KIND, 211 "scope"); 212 /* Everything below is stored verbatim in text columns. Postgres rejects 213 invalid UTF-8 with SQLSTATE 22021, which we would surface as a 500; 214 catch it here so a bad request is reported as one. */ 215 { 216 const struct 217 { 218 const char *name; 219 const char *value; 220 } texts[] = { 221 { "redirect_uri", redirect_uri }, 222 { "state", state }, 223 { "scope", scope }, 224 { "code_challenge", code_challenge }, 225 { NULL, NULL } 226 }; 227 228 for (unsigned int i = 0; NULL != texts[i].name; i++) 229 { 230 json_t *probe; 231 232 if (NULL == texts[i].value) 233 continue; 234 /* json_string() returns NULL for anything that is not valid UTF-8; 235 this is the same check /challenge applies to its form fields. */ 236 probe = json_string (texts[i].value); 237 if (NULL == probe) 238 { 239 GNUNET_break_op (0); 240 return reply_error ( 241 hc, 242 MHD_HTTP_BAD_REQUEST, 243 TALER_EC_GENERIC_PARAMETER_MALFORMED, 244 texts[i].name); 245 } 246 json_decref (probe); 247 } 248 } 249 { 250 json_t *last_address = NULL; 251 uint32_t address_attempts_left; 252 uint32_t pin_transmissions_left; 253 uint32_t auth_attempts_left; 254 struct GNUNET_TIME_Absolute last_tx_time; 255 bool solved; 256 bool failed; 257 char *final_redirect_uri = NULL; 258 enum GNUNET_DB_QueryStatus qs; 259 260 /* update_validation will return 0 if a 'redirect_uri' was 261 configured for the client and this one differs. */ 262 for (unsigned int r = 0; r<MAX_RETRIES; r++) 263 { 264 qs = CHALLENGERDB_update_validation (CH_context, 265 &nonce, 266 client_id, 267 scope, 268 state, 269 redirect_uri, 270 code_challenge, 271 (uint32_t) code_challenge_method_enum 272 , 273 &last_address, 274 &address_attempts_left, 275 &pin_transmissions_left, 276 &auth_attempts_left, 277 &solved, 278 &failed, 279 &final_redirect_uri, 280 &last_tx_time); 281 switch (qs) 282 { 283 case GNUNET_DB_STATUS_HARD_ERROR: 284 GNUNET_break (0); 285 return reply_error ( 286 hc, 287 MHD_HTTP_INTERNAL_SERVER_ERROR, 288 TALER_EC_GENERIC_DB_STORE_FAILED, 289 "update_validation"); 290 case GNUNET_DB_STATUS_SOFT_ERROR: 291 if (r < MAX_RETRIES - 1) 292 continue; 293 GNUNET_break (0); 294 return reply_error ( 295 hc, 296 MHD_HTTP_INTERNAL_SERVER_ERROR, 297 TALER_EC_GENERIC_DB_STORE_FAILED, 298 "update_validation"); 299 case GNUNET_DB_STATUS_SUCCESS_NO_RESULTS: 300 GNUNET_break_op (0); 301 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 302 "Failed to find authorization process of client %llu for nonce `%s'\n", 303 client_id, 304 hc->path); 305 return reply_error ( 306 hc, 307 MHD_HTTP_NOT_FOUND, 308 TALER_EC_CHALLENGER_GENERIC_VALIDATION_UNKNOWN, 309 NULL); 310 case GNUNET_DB_STATUS_SUCCESS_ONE_RESULT: 311 break; 312 } 313 break; 314 } 315 if (failed) 316 { 317 enum MHD_Result res; 318 319 /* The user came back to a validation they can no longer pass; 320 send them (or tell the SPA to send them) to the client. */ 321 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 322 "Authorization requested for failed validation\n"); 323 json_decref (last_address); 324 res = CH_reply_validation_failed (hc->connection, 325 final_redirect_uri, 326 state); 327 GNUNET_free (final_redirect_uri); 328 return res; 329 } 330 GNUNET_free (final_redirect_uri); 331 if (0 == CH_get_output_type (hc->connection)) 332 { 333 char *prev_full_url = hc->full_url; 334 const char *rparams = strchr (hc->full_url, '?'); 335 336 if (NULL == rparams) 337 GNUNET_asprintf (&hc->full_url, 338 "%s?nonce=%s", 339 prev_full_url, 340 hc->path); 341 342 else 343 GNUNET_asprintf (&hc->full_url, 344 "%s&nonce=%s", 345 prev_full_url, 346 hc->path); 347 348 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 349 "Redirect before %s now `%s'\n", 350 prev_full_url, 351 hc->full_url); 352 GNUNET_free (prev_full_url); 353 json_decref (last_address); 354 return CH_spa_redirect (hc, 355 NULL, 356 0); 357 } 358 { 359 json_t *args; 360 struct MHD_Response *resp; 361 enum MHD_Result res; 362 json_t *ro; 363 364 ro = json_object_get (last_address, 365 "read_only"); 366 if ( (NULL != ro) && 367 (json_boolean_value (ro)) ) 368 address_attempts_left = 0; 369 370 args = GNUNET_JSON_PACK ( 371 GNUNET_JSON_pack_bool ("fix_address", 372 0 == address_attempts_left), 373 GNUNET_JSON_pack_allow_null ( 374 GNUNET_JSON_pack_object_steal ("last_address", 375 last_address)), 376 GNUNET_JSON_pack_bool ("solved", 377 solved), 378 GNUNET_JSON_pack_uint64 ("pin_transmissions_left", 379 pin_transmissions_left), 380 GNUNET_JSON_pack_uint64 ("auth_attempts_left", 381 auth_attempts_left), 382 GNUNET_JSON_pack_timestamp ("retransmission_time", 383 GNUNET_TIME_absolute_to_timestamp ( 384 GNUNET_TIME_absolute_add ( 385 last_tx_time, 386 CH_pin_retransmission_frequency))), 387 GNUNET_JSON_pack_uint64 ("changes_left", 388 address_attempts_left) 389 ); 390 resp = TALER_MHD_make_json_steal (args); 391 GNUNET_break (MHD_YES == 392 MHD_add_response_header (resp, 393 MHD_HTTP_HEADER_CACHE_CONTROL, 394 "no-store,no-cache")); 395 res = MHD_queue_response (hc->connection, 396 MHD_HTTP_OK, 397 resp); 398 MHD_destroy_response (resp); 399 return res; 400 } 401 } 402 }