/*
This file is part of TALER
(C) 2014--2021 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 3, 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 General Public License for more details.
You should have received a copy of the GNU General Public License along with
TALER; see the file COPYING. If not, see
*/
/**
* @file taler-merchant-httpd_helper.c
* @brief shared logic for various handlers
* @author Christian Grothoff
*/
#include "platform.h"
#include
#include
#include
#include
#include "taler-merchant-httpd_helper.h"
#include
#include
/**
* check @a payto_uris for well-formedness
*
* @param payto_uris JSON array of payto URIs (presumably)
* @return true if they are all valid URIs (and this is an array of strings)
*/
bool
TMH_payto_uri_array_valid (const json_t *payto_uris)
{
bool payto_ok = true;
if (! json_is_array (payto_uris))
{
GNUNET_break_op (0);
payto_ok = false;
}
else
{
unsigned int len = json_array_size (payto_uris);
for (unsigned int i = 0; iwire_salt,
sizeof (wm->wire_salt));
wm->payto_uri = GNUNET_strdup (payto_uri);
TALER_merchant_wire_signature_hash (payto_uri,
&wm->wire_salt,
&wm->h_wire);
wm->wire_method
= TALER_payto_get_method (payto_uri);
wm->active = true;
return wm;
}
enum GNUNET_GenericReturnValue
TMH_check_auth_config (struct MHD_Connection *connection,
const json_t *jauth,
const char **auth_token)
{
bool auth_wellformed = false;
const char *auth_method = json_string_value (json_object_get (jauth,
"method"));
*auth_token = NULL;
if (NULL == auth_method)
{
GNUNET_break_op (0);
}
else if (0 == strcmp (auth_method,
"external"))
{
auth_wellformed = true;
}
else if (0 == strcmp (auth_method,
"token"))
{
*auth_token = json_string_value (json_object_get (jauth,
"token"));
if (NULL == *auth_token)
{
GNUNET_break_op (0);
}
else
{
if (0 != strncasecmp (RFC_8959_PREFIX,
*auth_token,
strlen (RFC_8959_PREFIX)))
GNUNET_break_op (0);
else
auth_wellformed = true;
}
}
if (! auth_wellformed)
{
GNUNET_break_op (0);
return (MHD_YES ==
TALER_MHD_reply_with_error (connection,
MHD_HTTP_BAD_REQUEST,
TALER_EC_MERCHANT_PRIVATE_POST_INSTANCES_BAD_AUTH,
"bad authentication config")) ?
GNUNET_NO : GNUNET_SYSERR;
}
return GNUNET_OK;
}
/**
* Generate binary UUID from client-provided UUID-string.
*
* @param uuids string intpu
* @param[out] uuid set to binary UUID
*/
void
TMH_uuid_from_string (const char *uuids,
struct GNUNET_Uuid *uuid)
{
struct GNUNET_HashCode hc;
GNUNET_CRYPTO_hash (uuids,
strlen (uuids),
&hc);
GNUNET_static_assert (sizeof (hc) > sizeof (*uuid));
memcpy (uuid,
&hc,
sizeof (*uuid));
}
/**
* Closure for #trigger_webhook_cb.
*
* @param instance which is the instance we work with
* @param root JSON data to fill into the template
* @param rv, query of the TALER_TEMPLATEING_fill
*/
struct Trigger
{
const char *instance;
const json_t *root;
enum GNUNET_DB_QueryStatus rv;
};
/**
* Typically called by `TMH_trigger_webhook`.
*
* @param[in,out] cls a `struct Trigger` with information about the webhook
* @param webhook_serial reference to the configured webhook template.
* @param event_type is the event/action of the webhook
* @param url to make request to
* @param http_method use for the webhook
* @param header_template of the webhook
* @param body_template of the webhook
*/
static void
trigger_webhook_cb (void *cls,
uint64_t webhook_serial,
const char *event_type,
const char *url,
const char *http_method,
const char *header_template,
const char *body_template)
{
struct Trigger *t = cls;
void *header = NULL;
void *body = NULL;
size_t header_size;
size_t body_size;
if (NULL != header_template)
{
int ret;
ret = TALER_TEMPLATING_fill (header_template,
t->root,
&header,
&header_size);
if (0 != ret)
{
GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
"Failed to expand webhook header template for webhook %llu (%d)\n",
(unsigned long long) webhook_serial,
ret);
t->rv = GNUNET_DB_STATUS_HARD_ERROR;
return;
}
/* Note: header is actually header_size+1 bytes long here, see mustach.c::memfile_close() */
GNUNET_assert ('\0' == ((const char *) header)[header_size]);
}
if (NULL != body_template)
{
int ret;
ret = TALER_TEMPLATING_fill (body_template,
t->root,
&body,
&body_size);
if (0 != ret)
{
GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
"Failed to expand webhook body template for webhook %llu (%d)\n",
(unsigned long long) webhook_serial,
ret);
t->rv = GNUNET_DB_STATUS_HARD_ERROR;
return;
}
/* Note: body is actually body_size+1 bytes long here, see mustach.c::memfile_close() */
GNUNET_assert ('\0' == ((const char *) body)[body_size]);
}
t->rv = TMH_db->insert_pending_webhook (TMH_db->cls,
t->instance,
webhook_serial,
url,
http_method,
header,
body);
if (t->rv > 0)
{
struct GNUNET_DB_EventHeaderP es = {
.size = htons (sizeof(es)),
.type = htons (TALER_DBEVENT_MERCHANT_WEBHOOK_PENDING)
};
const void *extra = NULL;
size_t extra_size = 0;
TMH_db->event_notify (TMH_db->cls,
&es,
&extra,
extra_size);
}
free (header);
free (body);
}
/**
* TMH_trigger_webhook is a function that need to be use when someone
* pay. Merchant need to have a notification.
*
* @param instance that we need to send the webhook as a notification
* @param event of the webhook
* @param args argument of the function
*/
enum GNUNET_DB_QueryStatus
TMH_trigger_webhook (const char *instance,
const char *event,
const json_t *args)
{
struct Trigger t = {
.instance = instance,
.root = args
};
enum GNUNET_DB_QueryStatus qs;
qs = TMH_db->lookup_webhook_by_event (TMH_db->cls,
instance,
event,
&trigger_webhook_cb,
&t);
if (qs < 0)
return qs;
return t.rv;
}