/*
   This file is part of Challenger
   Copyright (C) 2023 Taler Systems SA

   Challenger is free software; you can redistribute it and/or modify it under the
   terms of the GNU General Public License as published by the Free Software
   Foundation; either version 3, or (at your option) any later version.

   Challenger is distributed in the hope that it will be useful, but WITHOUT ANY
   WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
   A PARTICULAR PURPOSE.  See the GNU General Public License for more details.

   You should have received a copy of the GNU General Public License along with
   Challenger; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
 */
/**
 * @file src/include/challenger-database/do_insert_token.h
 * @brief implementation of the do_insert_token function for Postgres
 * @author Christian Grothoff
 */
#ifndef CHALLENGER_DATABASE_DO_INSERT_TOKEN_H
#define CHALLENGER_DATABASE_DO_INSERT_TOKEN_H

#include <taler/taler_util.h>
#include <taler/taler_json_lib.h>
#include "challenger_util.h"
#include "challenger_database_lib.h"


/**
 * Add access @a grant to address under @a nonce, consuming the validation in
 * the same statement so that the authorization code cannot be replayed.
 *
 * Only *solved*, unexpired validations of @a client_id that carry an address
 * are consumed; anything else yields #GNUNET_DB_STATUS_SUCCESS_NO_RESULTS,
 * which the caller maps to the OAuth 2.0 ``invalid_grant`` error.
 *
 * @param cls closure
 * @param nonce validation process to grant access to
 * @param client_id serial id of the authenticated client redeeming the code
 * @param grant grant token that grants access
 * @param grant_expiration for how long should the grant be valid
 * @param address_expiration for how long from now do we consider the address
 *        to be valid
 * @return transaction status:
 *   #GNUNET_DB_STATUS_SUCCESS_ONE_RESULT if the token was minted
 *   #GNUNET_DB_STATUS_SUCCESS_NO_RESULTS if there is no solved validation
 *     under @a nonce (unknown, unsolved, expired, of another client or
 *     already redeemed)
 *   #GNUNET_DB_STATUS_HARD_ERROR on failure
 */
enum GNUNET_DB_QueryStatus
CHALLENGERDB_do_insert_token (
  struct CHALLENGERDB_PostgresContext *ctx,
  const struct CHALLENGER_ValidationNonceP *nonce,
  uint64_t client_id,
  const struct CHALLENGER_AccessTokenP *grant,
  struct GNUNET_TIME_Relative grant_expiration,
  struct GNUNET_TIME_Relative address_expiration);


#endif
