challenger

OAuth 2.0-based authentication service that validates user can receive messages at a certain address
Log | Files | Refs | Submodules | README | LICENSE

validate_solve_pin.c (4200B)


      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/validate_solve_pin.c
     18  * @brief Implementation of the validate_solve_pin function for Postgres
     19  * @author Christian Grothoff
     20  */
     21 #include "platform.h"
     22 #include <taler/taler_error_codes.h>
     23 #include <taler/taler_dbevents.h>
     24 #include <taler/taler_pq_lib.h>
     25 #include "validate_solve_pin.h"
     26 #include "pg_helper.h"
     27 
     28 
     29 enum GNUNET_DB_QueryStatus
     30 CHALLENGERDB_validate_solve_pin (
     31   struct CHALLENGERDB_PostgresContext *ctx,
     32   const struct CHALLENGER_ValidationNonceP *nonce,
     33   uint32_t new_pin,
     34   bool *solved,
     35   bool *exhausted,
     36   bool *no_challenge,
     37   char **state,
     38   uint32_t *addr_left,
     39   uint32_t *auth_attempts_left,
     40   uint32_t *pin_transmissions_left,
     41   char **client_redirect_uri)
     42 {
     43   struct GNUNET_TIME_Absolute now
     44     = GNUNET_TIME_absolute_get ();
     45   struct GNUNET_PQ_QueryParam params[] = {
     46     GNUNET_PQ_query_param_auto_from_type (nonce),
     47     GNUNET_PQ_query_param_uint32 (&new_pin),
     48     GNUNET_PQ_query_param_absolute_time (&now),
     49     GNUNET_PQ_query_param_end
     50   };
     51   bool not_found;
     52   struct GNUNET_PQ_ResultSpec rs[] = {
     53     GNUNET_PQ_result_spec_bool ("not_found",
     54                                 &not_found),
     55     GNUNET_PQ_result_spec_bool ("solved",
     56                                 solved),
     57     GNUNET_PQ_result_spec_bool ("exhausted",
     58                                 exhausted),
     59     GNUNET_PQ_result_spec_bool ("no_challenge",
     60                                 no_challenge),
     61     GNUNET_PQ_result_spec_uint32 ("address_attempts_left",
     62                                   addr_left),
     63     GNUNET_PQ_result_spec_uint32 ("auth_attempts_left",
     64                                   auth_attempts_left),
     65     GNUNET_PQ_result_spec_uint32 ("pin_transmissions_left",
     66                                   pin_transmissions_left),
     67     GNUNET_PQ_result_spec_allow_null (
     68       GNUNET_PQ_result_spec_string ("client_redirect_uri",
     69                                     client_redirect_uri),
     70       NULL),
     71     GNUNET_PQ_result_spec_allow_null (
     72       GNUNET_PQ_result_spec_string ("state",
     73                                     state),
     74       NULL),
     75     GNUNET_PQ_result_spec_end
     76   };
     77   enum GNUNET_DB_QueryStatus qs;
     78 
     79   *client_redirect_uri = NULL;
     80   PREPARE (ctx,
     81            "do_validate_solve_pin",
     82            "SELECT "
     83            " out_not_found AS not_found"
     84            ",out_solved AS solved"
     85            ",out_exhausted AS exhausted"
     86            ",out_no_challenge AS no_challenge"
     87            ",out_state AS state"
     88            ",out_address_attempts_left AS address_attempts_left"
     89            ",out_auth_attempts_left AS auth_attempts_left"
     90            ",out_pin_transmissions_left AS pin_transmissions_left"
     91            ",out_client_redirect_uri AS client_redirect_uri"
     92            " FROM challenger_do_validate_and_solve_pin"
     93            " ($1,$2,$3);");
     94   qs = GNUNET_PQ_eval_prepared_singleton_select (ctx->conn,
     95                                                  "do_validate_solve_pin",
     96                                                  params,
     97                                                  rs);
     98   if (qs <= 0)
     99     return qs;
    100   if (not_found)
    101     return GNUNET_DB_STATUS_SUCCESS_NO_RESULTS;
    102   /* On the solved path the stored procedure returns the -1 sentinel (kept in
    103      the DB to mark the challenge as solved), which the uint32 result spec reads
    104      as 4294967295.  That value is not meaningful to the caller (which only
    105      reads it when !solved); normalize it to 0 to avoid leaking the sentinel. */
    106   if (*solved)
    107     *auth_attempts_left = 0;
    108   return qs;
    109 }