/*
  This file is part of TALER
  Copyright (C) 2025-2026 Taler Systems SA

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

  TALER 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 Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public License along with
  TALER; see the file COPYING.LGPL.  If not, see
  <http://www.gnu.org/licenses/>
*/
/**
 * @file src/lib/merchant_api_common_mfa_challenge.c
 * @brief Shared parsing of HTTP 202 MFA challenge responses
 * @author Christian Grothoff
 */
#include "platform.h"
#include "taler/taler_merchant_service.h"
#include <gnunet/gnunet_json_lib.h>
#include "taler/taler_json_lib.h"
#include "merchant_api_common.h"


enum GNUNET_GenericReturnValue
TALER_MERCHANT_parse_mfa_challenge_response_ (
  const json_t *json,
  struct TALER_MERCHANT_MfaChallengeResponse *cr)
{
  const json_t *challenges;
  struct GNUNET_JSON_Specification spec[] = {
    GNUNET_JSON_spec_bool ("combi_and",
                           &cr->combi_and),
    GNUNET_JSON_spec_array_const ("challenges",
                                  &challenges),
    GNUNET_JSON_spec_end ()
  };

  memset (cr,
          0,
          sizeof (*cr));
  if (GNUNET_OK !=
      GNUNET_JSON_parse (json,
                         spec,
                         NULL, NULL))
  {
    GNUNET_break_op (0);
    return GNUNET_SYSERR;
  }
  cr->challenges_length = (unsigned int) json_array_size (challenges);
  if (0 == cr->challenges_length)
  {
    GNUNET_break_op (0);
    return GNUNET_SYSERR;
  }
  cr->challenges = GNUNET_new_array (
    cr->challenges_length,
    struct TALER_MERCHANT_MfaChallengeEntry);
  for (unsigned int i = 0; i < cr->challenges_length; i++)
  {
    const json_t *jentry = json_array_get (challenges,
                                           i);
    struct TALER_MERCHANT_MfaChallengeEntry *entry = &cr->challenges[i];
    struct GNUNET_JSON_Specification espec[] = {
      GNUNET_JSON_spec_string ("tan_info",
                               &entry->tan_info),
      GNUNET_JSON_spec_string ("tan_channel",
                               &entry->tan_channel),
      TALER_JSON_spec_slug ("challenge_id",
                            &entry->challenge_id),
      GNUNET_JSON_spec_end ()
    };

    if (GNUNET_OK !=
        GNUNET_JSON_parse (jentry,
                           espec,
                           NULL, NULL))
    {
      GNUNET_break_op (0);
      GNUNET_free (cr->challenges);
      cr->challenges_length = 0;
      return GNUNET_SYSERR;
    }
  }
  return GNUNET_OK;
}


void
TALER_MERCHANT_mfa_challenge_response_free (
  struct TALER_MERCHANT_MfaChallengeResponse *cr)
{
  GNUNET_free (cr->challenges);
  cr->challenges_length = 0;
}


/* end of merchant_api_common_mfa_challenge.c */
