challenger

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

insert_client.c (3647B)


      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/insert_client.c
     18  * @brief Implementation of the insert_client 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 "insert_client.h"
     26 #include "pg_helper.h"
     27 
     28 
     29 enum GNUNET_DB_QueryStatus
     30 CHALLENGERDB_insert_client (struct CHALLENGERDB_PostgresContext *ctx,
     31                             const char *client_redirect_uri,
     32                             const char *client_secret,
     33                             uint64_t *client_id,
     34                             bool *uri_taken)
     35 {
     36   struct GNUNET_PQ_QueryParam params[] = {
     37     GNUNET_PQ_query_param_string (client_redirect_uri),
     38     GNUNET_PQ_query_param_string (client_secret),
     39     GNUNET_PQ_query_param_end
     40   };
     41   bool inserted;
     42   bool taken;
     43   struct GNUNET_PQ_ResultSpec rs[] = {
     44     GNUNET_PQ_result_spec_allow_null (
     45       GNUNET_PQ_result_spec_uint64 ("client_serial_id",
     46                                     client_id),
     47       NULL),
     48     GNUNET_PQ_result_spec_bool ("inserted",
     49                                 &inserted),
     50     GNUNET_PQ_result_spec_bool ("uri_taken",
     51                                 &taken),
     52     GNUNET_PQ_result_spec_end
     53   };
     54   enum GNUNET_DB_QueryStatus qs;
     55 
     56   *client_id = 0;
     57   *uri_taken = false;
     58   /* The ON CONFLICT arbiter must be spelled out: a bare 'ON CONFLICT DO
     59      NOTHING' swallows *any* unique violation, including one on the primary
     60      key, which happens when the identity sequence of 'clients' lags behind
     61      max(client_serial_id) (e.g. after a restore that did not restore the
     62      sequence).  As GNUnet maps every unique violation to NO_RESULTS, the
     63      caller could not tell that apart from "this URI is already registered"
     64      either, so report whether the URI was taken; if it was not, the INSERT
     65      failed for a different reason and the caller must say so. */
     66   PREPARE (ctx,
     67            "insert_client",
     68            "WITH ins AS ("
     69            "  INSERT INTO clients"
     70            "   (uri"
     71            "   ,client_secret"
     72            "  ) VALUES "
     73            "  ($1, $2)"
     74            "  ON CONFLICT (uri) DO NOTHING"
     75            "  RETURNING client_serial_id"
     76            ") SELECT"
     77            "   (SELECT client_serial_id FROM ins) AS client_serial_id"
     78            "  ,EXISTS (SELECT 1 FROM ins) AS inserted"
     79            "  ,EXISTS (SELECT 1 FROM clients WHERE uri=$1) AS uri_taken;");
     80   qs = GNUNET_PQ_eval_prepared_singleton_select (ctx->conn,
     81                                                  "insert_client",
     82                                                  params,
     83                                                  rs);
     84   if (0 > qs)
     85     return qs;
     86   if (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS == qs)
     87     return qs;
     88   *uri_taken = taken;
     89   if (! inserted)
     90   {
     91     *client_id = 0;
     92     return GNUNET_DB_STATUS_SUCCESS_NO_RESULTS;
     93   }
     94   return GNUNET_DB_STATUS_SUCCESS_ONE_RESULT;
     95 }