challenger

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

challenger-httpd_setup.c (8994B)


      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_setup.c
     18  * @brief functions to handle incoming requests for setups
     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 "challenger-httpd_setup.h"
     26 #include "challenger-httpd_common.h"
     27 #include "challenger-database/do_insert_validation.h"
     28 
     29 
     30 /**
     31  * Set to 1 to dump addresses into the log.
     32  */
     33 #define DEBUG 0
     34 
     35 /**
     36  * Maximum number of retries for the database interaction.
     37  */
     38 #define MAX_RETRIES 3
     39 
     40 struct SetupContext
     41 {
     42 
     43   /**
     44    * Our handler context.
     45    */
     46   struct CH_HandlerContext *hc;
     47 
     48   /**
     49    * Opaque parsing context.
     50    */
     51   void *opaque_post_parsing_context;
     52 
     53   /**
     54    * Uploaded JSON data, NULL if upload is not yet complete.
     55    */
     56   json_t *root;
     57 
     58   /**
     59    * Did the client actually send a request body?  The body is optional
     60    * for this endpoint, so we must not hand an empty upload to the JSON
     61    * parser (which would reject it as malformed).
     62    */
     63   bool have_upload;
     64 
     65 };
     66 
     67 
     68 /**
     69  * Callback to clean up the request context.
     70  *
     71  * @param[in,out] ctx a `struct SetupContext` to clean up
     72  */
     73 static void
     74 request_done (void *ctx)
     75 {
     76   struct SetupContext *sc = ctx;
     77 
     78   if (NULL == ctx)
     79     return;
     80   if (NULL != sc->root)
     81   {
     82     json_decref (sc->root);
     83     sc->root = NULL;
     84   }
     85   TALER_MHD_parse_post_cleanup_callback (sc->opaque_post_parsing_context);
     86   GNUNET_free (sc);
     87 }
     88 
     89 
     90 enum MHD_Result
     91 CH_handler_setup (struct CH_HandlerContext *hc,
     92                   const char *upload_data,
     93                   size_t *upload_data_size)
     94 {
     95   struct SetupContext *sc = hc->ctx;
     96   unsigned long long client_id;
     97   const char *client_secret;
     98 
     99   if (NULL == sc)
    100   {
    101     /* Fresh request, do initial setup */
    102     sc = GNUNET_new (struct SetupContext);
    103     sc->hc = hc;
    104     hc->cc = &request_done;
    105     hc->ctx = sc;
    106     return MHD_YES;
    107   }
    108   /* Remember that a body is coming.  We must keep calling the parser after
    109      this point even once *upload_data_size has dropped back to zero: that
    110      final call is the one where GNUNET_MHD_post_parser() actually decodes
    111      the buffered bytes.  Conversely, if no body ever arrives we must not
    112      call the parser at all, as it would report an empty document as
    113      malformed JSON -- the body of this endpoint is optional. */
    114   if (0 != *upload_data_size)
    115     sc->have_upload = true;
    116   if ( (NULL == sc->root) &&
    117        (sc->have_upload) )
    118   {
    119     /* parse byte stream upload into JSON */
    120     enum GNUNET_GenericReturnValue res;
    121 
    122 #if DEBUG
    123     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
    124                 "Handling upload of `%.*s'\n",
    125                 (int) *upload_data_size,
    126                 upload_data);
    127 #endif
    128     res = TALER_MHD_parse_post_json (hc->connection,
    129                                      &sc->opaque_post_parsing_context,
    130                                      upload_data,
    131                                      upload_data_size,
    132                                      &sc->root);
    133     if (GNUNET_SYSERR == res)
    134     {
    135       GNUNET_assert (NULL == sc->root);
    136       return MHD_NO; /* bad upload, could not even generate error */
    137     }
    138     if ( (GNUNET_NO == res) ||
    139          (NULL == sc->root) )
    140     {
    141       GNUNET_assert (NULL == sc->root);
    142       return MHD_YES; /* so far incomplete upload or parser error */
    143     }
    144     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
    145                 "Address data uploaded in /setup\n");
    146 #if DEBUG
    147     json_dumpf (sc->root,
    148                 stderr,
    149                 JSON_INDENT (2));
    150 #endif
    151     if (! json_is_object (sc->root))
    152     {
    153       /* The address is stored verbatim and later packed with
    154          GNUNET_JSON_pack_object_steal() by /authorize, which asserts on
    155          anything that is not an object; refuse it here instead. */
    156       GNUNET_break_op (0);
    157       return TALER_MHD_reply_with_error (hc->connection,
    158                                          MHD_HTTP_BAD_REQUEST,
    159                                          TALER_EC_GENERIC_PARAMETER_MALFORMED,
    160                                          "address");
    161     }
    162   }
    163 
    164   {
    165     char dummy;
    166 
    167     if (1 != sscanf (hc->path,
    168                      "%llu%c",
    169                      &client_id,
    170                      &dummy))
    171     {
    172       GNUNET_break_op (0);
    173       return TALER_MHD_reply_with_error (hc->connection,
    174                                          MHD_HTTP_BAD_REQUEST,
    175                                          TALER_EC_GENERIC_PARAMETER_MALFORMED,
    176                                          "client_id");
    177     }
    178   }
    179   {
    180     enum TALER_ErrorCode ec;
    181 
    182     client_secret = CH_get_client_secret (hc->connection,
    183                                           &ec);
    184     if (NULL == client_secret)
    185     {
    186       GNUNET_break_op (0);
    187       /* Credentials are required to use this endpoint at all, so refusing
    188          the request is a matter of authorization, not of a bad request. */
    189       return TALER_MHD_reply_with_error (hc->connection,
    190                                          MHD_HTTP_FORBIDDEN,
    191                                          ec,
    192                                          MHD_HTTP_HEADER_AUTHORIZATION);
    193     }
    194   }
    195   {
    196     const json_t *ro;
    197 
    198     ro = json_object_get (sc->root,
    199                           "read_only");
    200     if ( (NULL != ro) &&
    201          (! json_is_boolean (ro)) )
    202     {
    203       GNUNET_break_op (0);
    204       return TALER_MHD_reply_with_error (hc->connection,
    205                                          MHD_HTTP_BAD_REQUEST,
    206                                          TALER_EC_GENERIC_PARAMETER_MALFORMED,
    207                                          "read_only");
    208     }
    209   }
    210   {
    211     struct CHALLENGER_ValidationNonceP nonce;
    212     struct GNUNET_TIME_Absolute expiration_time;
    213     enum GNUNET_DB_QueryStatus qs;
    214 
    215     expiration_time = GNUNET_TIME_relative_to_absolute (CH_validation_duration);
    216     GNUNET_CRYPTO_random_block (&nonce,
    217                                 sizeof (nonce));
    218     /* Authenticating the client, charging it for the validation and
    219        creating the validation row all happen in this one statement, so
    220        the validation counter only moves if the setup really succeeded.
    221        As the session is SERIALIZABLE and every /setup of a given client
    222        updates the same 'validation_counter' row, serialization failures
    223        are routine here and must be retried instead of reported. */
    224     for (unsigned int r = 0; r<MAX_RETRIES; r++)
    225     {
    226       qs = CHALLENGERDB_do_insert_validation (CH_context,
    227                                               client_id,
    228                                               client_secret,
    229                                               &nonce,
    230                                               expiration_time,
    231                                               sc->root);
    232       switch (qs)
    233       {
    234       case GNUNET_DB_STATUS_HARD_ERROR:
    235         GNUNET_break (0);
    236         return TALER_MHD_reply_with_error (
    237           hc->connection,
    238           MHD_HTTP_INTERNAL_SERVER_ERROR,
    239           TALER_EC_GENERIC_DB_STORE_FAILED,
    240           "do_insert_validation");
    241       case GNUNET_DB_STATUS_SOFT_ERROR:
    242         if (r < MAX_RETRIES - 1)
    243           continue;
    244         GNUNET_break (0);
    245         return TALER_MHD_reply_with_error (
    246           hc->connection,
    247           MHD_HTTP_INTERNAL_SERVER_ERROR,
    248           TALER_EC_GENERIC_DB_STORE_FAILED,
    249           "do_insert_validation");
    250       case GNUNET_DB_STATUS_SUCCESS_NO_RESULTS:
    251         /* Nothing was inserted, so the client did not authenticate. */
    252         return TALER_MHD_reply_with_error (
    253           hc->connection,
    254           MHD_HTTP_NOT_FOUND,
    255           TALER_EC_CHALLENGER_GENERIC_CLIENT_UNKNOWN,
    256           NULL);
    257       case GNUNET_DB_STATUS_SUCCESS_ONE_RESULT:
    258         break;
    259       }
    260       break;
    261     } /* for MAX_RETRIES */
    262     {
    263       char *nstr;
    264 
    265       nstr = GNUNET_STRINGS_data_to_string_alloc (&nonce,
    266                                                   sizeof (nonce));
    267       GNUNET_log (GNUNET_ERROR_TYPE_INFO,
    268                   "/setup returns nonce `%s'\n",
    269                   nstr);
    270       GNUNET_free (nstr);
    271     }
    272     return TALER_MHD_REPLY_JSON_PACK (
    273       hc->connection,
    274       MHD_HTTP_OK,
    275       GNUNET_JSON_pack_data_auto ("nonce",
    276                                   &nonce),
    277       GNUNET_JSON_pack_timestamp (
    278         "expires",
    279         GNUNET_TIME_absolute_to_timestamp (expiration_time)));
    280   }
    281 }