authorize_start.c (4341B)
1 /* 2 This file is part of Challenger 3 Copyright (C) 2023 Taler Systems SA 4 5 Challenger is free software; you can redistribute it and/or modify it under the 6 terms of the GNU 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 General Public License for more details. 12 13 You should have received a copy of the GNU General Public License along with 14 Challenger; see the file COPYING. If not, see <http://www.gnu.org/licenses/> 15 */ 16 /** 17 * @file src/challengerdb/authorize_start.c 18 * @brief Implementation of the authorize_start function for Postgres 19 * @author Christian Grothoff 20 * @author Bohdan Potuzhnyi 21 * @author Vlada Svirsh 22 */ 23 #include "platform.h" 24 #include <taler/taler_error_codes.h> 25 #include <taler/taler_dbevents.h> 26 #include <taler/taler_pq_lib.h> 27 #include "authorize_start.h" 28 #include "pg_helper.h" 29 30 31 enum GNUNET_DB_QueryStatus 32 CHALLENGERDB_authorize_start ( 33 struct CHALLENGERDB_PostgresContext *ctx, 34 const struct CHALLENGER_ValidationNonceP *nonce, 35 uint64_t client_id, 36 const char *client_scope, 37 const char *client_state, 38 const char *client_redirect_uri, 39 const char *code_challenge, 40 uint32_t code_challenge_method, 41 json_t **last_address, 42 uint32_t *address_attempts_left, 43 uint32_t *pin_transmissions_left, 44 uint32_t *auth_attempts_left, 45 bool *solved, 46 struct GNUNET_TIME_Absolute *last_tx_time) 47 { 48 struct GNUNET_TIME_Absolute now 49 = GNUNET_TIME_absolute_get (); 50 struct GNUNET_PQ_QueryParam params[] = { 51 GNUNET_PQ_query_param_auto_from_type (nonce), 52 GNUNET_PQ_query_param_uint64 (&client_id), 53 NULL != client_scope 54 ? GNUNET_PQ_query_param_string (client_scope) 55 : GNUNET_PQ_query_param_null (), 56 NULL != client_state 57 ? GNUNET_PQ_query_param_string (client_state) 58 : GNUNET_PQ_query_param_null (), 59 NULL != client_redirect_uri 60 ? GNUNET_PQ_query_param_string (client_redirect_uri) 61 : GNUNET_PQ_query_param_null (), 62 NULL != code_challenge 63 ? GNUNET_PQ_query_param_string (code_challenge) 64 : GNUNET_PQ_query_param_null (), 65 GNUNET_PQ_query_param_uint32 (&code_challenge_method), 66 GNUNET_PQ_query_param_absolute_time (&now), 67 GNUNET_PQ_query_param_end 68 }; 69 struct GNUNET_PQ_ResultSpec rs[] = { 70 GNUNET_PQ_result_spec_allow_null ( 71 TALER_PQ_result_spec_json ("address", 72 last_address), 73 NULL), 74 GNUNET_PQ_result_spec_uint32 ("address_attempts_left", 75 address_attempts_left), 76 GNUNET_PQ_result_spec_uint32 ("pin_transmissions_left", 77 pin_transmissions_left), 78 GNUNET_PQ_result_spec_uint32 ("auth_attempts_left", 79 auth_attempts_left), 80 GNUNET_PQ_result_spec_bool ("solved", 81 solved), 82 GNUNET_PQ_result_spec_absolute_time ("last_tx_time", 83 last_tx_time), 84 GNUNET_PQ_result_spec_end 85 }; 86 87 *last_address = NULL; 88 PREPARE (ctx, 89 "authorize_start_validation", 90 "UPDATE validations SET" 91 " client_scope=$3" 92 " ,client_state=$4" 93 " ,client_redirect_uri=COALESCE($5::VARCHAR,client_redirect_uri)" 94 " ,code_challenge=$6" 95 " ,code_challenge_method=$7" 96 " WHERE nonce=$1" 97 " AND client_serial_id=$2" 98 " AND expiration_time > $8" 99 " AND ( ($5::VARCHAR=client_redirect_uri)" 100 " OR ( ($5::VARCHAR IS NULL)" 101 " AND (client_redirect_uri IS NOT NULL) ) )" 102 " RETURNING" 103 " address" 104 " ,address_attempts_left" 105 " ,pin_transmissions_left" 106 " ,GREATEST(0, auth_attempts_left) AS auth_attempts_left" 107 " ,auth_attempts_left = -1 AS solved" 108 " ,last_tx_time;"); 109 return GNUNET_PQ_eval_prepared_singleton_select (ctx->conn, 110 "authorize_start_validation", 111 params, 112 rs); 113 }