From ccb8c161a227e8b3678472ce7006e911073bbdfb Mon Sep 17 00:00:00 2001 From: Marcello Stanisci Date: Thu, 2 Feb 2017 17:18:49 +0100 Subject: First steps in renaming --- doc/version.texi | 2 +- src/backend/Makefile.am | 2 +- src/backend/taler-merchant-httpd_proposal.c | 281 ++++++++++++++++++++++++++++ src/backend/taler-merchant-httpd_propose.c | 281 ---------------------------- src/lib/Makefile.am | 2 +- src/lib/merchant_api_contract.c | 239 ----------------------- src/lib/merchant_api_proposal.c | 240 ++++++++++++++++++++++++ src/lib/test_merchant_api.c | 58 +----- 8 files changed, 531 insertions(+), 574 deletions(-) create mode 100644 src/backend/taler-merchant-httpd_proposal.c delete mode 100644 src/backend/taler-merchant-httpd_propose.c delete mode 100644 src/lib/merchant_api_contract.c create mode 100644 src/lib/merchant_api_proposal.c diff --git a/doc/version.texi b/doc/version.texi index 13bfa00e..0b47203a 100644 --- a/doc/version.texi +++ b/doc/version.texi @@ -1,4 +1,4 @@ -@set UPDATED 8 January 2017 +@set UPDATED 27 January 2017 @set UPDATED-MONTH January 2017 @set EDITION 0.2.0 @set VERSION 0.2.0 diff --git a/src/backend/Makefile.am b/src/backend/Makefile.am index 9a5b2d6b..db782fe7 100644 --- a/src/backend/Makefile.am +++ b/src/backend/Makefile.am @@ -19,7 +19,7 @@ taler_merchant_httpd_SOURCES = \ taler-merchant-httpd_mhd.c taler-merchant-httpd_mhd.h \ taler-merchant-httpd_auditors.c taler-merchant-httpd_auditors.h \ taler-merchant-httpd_exchanges.c taler-merchant-httpd_exchanges.h \ - taler-merchant-httpd_propose.c taler-merchant-httpd_propose.h \ + taler-merchant-httpd_proposal.c taler-merchant-httpd_proposal.h \ taler-merchant-httpd_pay.c taler-merchant-httpd_pay.h \ taler-merchant-httpd_history.c taler-merchant-httpd_history.h \ taler-merchant-httpd_track-transaction.c taler-merchant-httpd_track-transaction.h \ diff --git a/src/backend/taler-merchant-httpd_proposal.c b/src/backend/taler-merchant-httpd_proposal.c new file mode 100644 index 00000000..ed222c97 --- /dev/null +++ b/src/backend/taler-merchant-httpd_proposal.c @@ -0,0 +1,281 @@ +/* + This file is part of TALER + (C) 2014, 2015, 2016 INRIA + + TALER is free software; you can redistribute it and/or modify it under the + terms of the GNU Affero 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 backend/taler-merchant-httpd_propose.c + * @brief HTTP serving layer mainly intended to communicate with the frontend + * @author Marcello Stanisci + */ +#include "platform.h" +#include +#include +#include +#include "taler-merchant-httpd.h" +#include "taler-merchant-httpd_parsing.h" +#include "taler-merchant-httpd_auditors.h" +#include "taler-merchant-httpd_exchanges.h" +#include "taler-merchant-httpd_responses.h" + + +/** + * Check that the given JSON array of products is well-formed. + * + * @param products JSON array to check + * @return #GNUNET_OK if all is fine + */ +static int +check_products (json_t *products) +{ + size_t index; + json_t *value; + int res; + + if (! json_is_array (products)) + { + GNUNET_break (0); + return GNUNET_SYSERR; + } + json_array_foreach (products, index, value) { + const char *description; + const char *error_name; + unsigned int error_line; + struct GNUNET_JSON_Specification spec[] = { + GNUNET_JSON_spec_string ("description", &description), + /* FIXME: there are other fields in the product specification + that are currently not labeled as optional. Maybe check + those as well, or make them truly optional. */ + GNUNET_JSON_spec_end() + }; + + /* extract fields we need to sign separately */ + res = GNUNET_JSON_parse (value, + spec, + &error_name, + &error_line); + if (GNUNET_OK != res) + { + GNUNET_break (0); + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + "Product description parsing failed at #%u: %s:%u\n", + (unsigned int) index, + error_name, + error_line); + return GNUNET_SYSERR; + } + GNUNET_JSON_parse_free (spec); + } + return GNUNET_OK; +} + + +/** + * Information we keep for individual calls + * to requests that parse JSON, but keep no other state. + */ +struct TMH_JsonParseContext +{ + + /** + * This field MUST be first. + * FIXME: Explain why! + */ + struct TM_HandlerContext hc; + + /** + * Placeholder for #TMH_PARSE_post_json() to keep its internal state. + */ + void *json_parse_context; +}; + + +/** + * Custom cleanup routine for a `struct TMH_JsonParseContext`. + * + * @param hc the `struct TMH_JsonParseContext` to clean up. + */ +static void +json_parse_cleanup (struct TM_HandlerContext *hc) +{ + struct TMH_JsonParseContext *jpc = (struct TMH_JsonParseContext *) hc; + + TMH_PARSE_post_cleanup_callback (jpc->json_parse_context); + GNUNET_free (jpc); +} + + +extern struct MerchantInstance * +get_instance (struct json_t *json); + + +/** + * Manage a contract request. In practical terms, it adds the fields + * 'exchanges', 'merchant_pub', and 'H_wire' to the contract 'proposition' + * gotten from the frontend. Finally, it adds (outside of the + * contract) a signature of the (hashed stringification) of the + * contract (and the hashed stringification of this contract as well + * to aid diagnostics) to the final bundle, which is then send back to + * the frontend. + * + * @param rh context of the handler + * @param connection the MHD connection to handle + * @param[in,out] connection_cls the connection's closure (can be updated) + * @param upload_data upload data + * @param[in,out] upload_data_size number of bytes (left) in @a upload_data + * @return MHD result code + */ +int +MH_handler_propose (struct TMH_RequestHandler *rh, + struct MHD_Connection *connection, + void **connection_cls, + const char *upload_data, + size_t *upload_data_size) +{ + + json_t *root; + json_t *jcontract; + int res; + struct MerchantInstance *mi; + struct TMH_JsonParseContext *ctx; + struct TALER_ContractPS contract; + struct GNUNET_CRYPTO_EddsaSignature contract_sig; + struct TALER_Amount total; + struct TALER_Amount max_fee; + uint64_t transaction_id; + json_t *products; + json_t *merchant; + struct GNUNET_TIME_Absolute timestamp; + struct GNUNET_TIME_Absolute refund_deadline; + struct GNUNET_TIME_Absolute pay_deadline; + struct GNUNET_JSON_Specification spec[] = { + TALER_JSON_spec_amount ("amount", &total), + TALER_JSON_spec_amount ("max_fee", &max_fee), + GNUNET_JSON_spec_uint64 ("transaction_id", &transaction_id), + /* The following entries we don't actually need, except to check that + the contract is well-formed */ + GNUNET_JSON_spec_json ("products", &products), + GNUNET_JSON_spec_json ("merchant", &merchant), + GNUNET_JSON_spec_absolute_time ("timestamp", ×tamp), + GNUNET_JSON_spec_absolute_time ("refund_deadline", &refund_deadline), + GNUNET_JSON_spec_absolute_time ("pay_deadline", &pay_deadline), + GNUNET_JSON_spec_end () + }; + + if (NULL == *connection_cls) + { + ctx = GNUNET_new (struct TMH_JsonParseContext); + ctx->hc.cc = &json_parse_cleanup; + *connection_cls = ctx; + } + else + { + ctx = *connection_cls; + } + + res = TMH_PARSE_post_json (connection, + &ctx->json_parse_context, + upload_data, + upload_data_size, + &root); + if (GNUNET_SYSERR == res) + return MHD_NO; + /* the POST's body has to be further fetched */ + if ((GNUNET_NO == res) || (NULL == root)) + return MHD_YES; + + jcontract = json_object_get (root, + "contract"); + if (NULL == jcontract) + { + json_decref (root); + return TMH_RESPONSE_reply_arg_missing (connection, + TALER_EC_PARAMETER_MISSING, + "contract"); + } + /* extract fields we need to sign separately */ + res = TMH_PARSE_json_data (connection, + jcontract, + spec); + if (GNUNET_NO == res) + { + json_decref (root); + return MHD_YES; + } + if (GNUNET_SYSERR == res) + { + json_decref (root); + return TMH_RESPONSE_reply_internal_error (connection, + TALER_EC_NONE, + "Impossible to parse contract"); + } + /* check contract is well-formed */ + if (GNUNET_OK != check_products (products)) + { + GNUNET_JSON_parse_free (spec); + json_decref (root); + return TMH_RESPONSE_reply_arg_invalid (connection, + TALER_EC_PARAMETER_MALFORMED, + "contract:products"); + } + + mi = get_instance (merchant); + if (NULL == mi) + { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + "Not able to find the specified instance\n"); + json_decref (root); + return TMH_RESPONSE_reply_not_found (connection, + TALER_EC_CONTRACT_INSTANCE_UNKNOWN, + "Unknown instance given"); + } + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "Signing contract on behalf of instance '%s'\n", + mi->id); + /* add fields to the contract that the backend should provide */ + json_object_set (jcontract, + "exchanges", + trusted_exchanges); + json_object_set (jcontract, + "auditors", + j_auditors); + json_object_set_new (jcontract, + "H_wire", + GNUNET_JSON_from_data_auto (&mi->h_wire)); + json_object_set_new (jcontract, + "merchant_pub", + GNUNET_JSON_from_data_auto (&mi->pubkey)); + + /* create contract signature */ + contract.purpose.purpose = htonl (TALER_SIGNATURE_MERCHANT_CONTRACT); + contract.purpose.size = htonl (sizeof (contract)); + GNUNET_assert (GNUNET_OK == + TALER_JSON_hash (jcontract, + &contract.h_contract)); + GNUNET_CRYPTO_eddsa_sign (&mi->privkey.eddsa_priv, + &contract.purpose, + &contract_sig); + + /* return final response */ + res = TMH_RESPONSE_reply_json_pack (connection, + MHD_HTTP_OK, + "{s:O, s:o s:o}", + "contract", jcontract, + "merchant_sig", GNUNET_JSON_from_data_auto (&contract_sig), + "H_contract", GNUNET_JSON_from_data_auto (&contract.h_contract)); + GNUNET_JSON_parse_free (spec); + json_decref (root); + return res; +} + +/* end of taler-merchant-httpd_contract.c */ diff --git a/src/backend/taler-merchant-httpd_propose.c b/src/backend/taler-merchant-httpd_propose.c deleted file mode 100644 index ed222c97..00000000 --- a/src/backend/taler-merchant-httpd_propose.c +++ /dev/null @@ -1,281 +0,0 @@ -/* - This file is part of TALER - (C) 2014, 2015, 2016 INRIA - - TALER is free software; you can redistribute it and/or modify it under the - terms of the GNU Affero 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 backend/taler-merchant-httpd_propose.c - * @brief HTTP serving layer mainly intended to communicate with the frontend - * @author Marcello Stanisci - */ -#include "platform.h" -#include -#include -#include -#include "taler-merchant-httpd.h" -#include "taler-merchant-httpd_parsing.h" -#include "taler-merchant-httpd_auditors.h" -#include "taler-merchant-httpd_exchanges.h" -#include "taler-merchant-httpd_responses.h" - - -/** - * Check that the given JSON array of products is well-formed. - * - * @param products JSON array to check - * @return #GNUNET_OK if all is fine - */ -static int -check_products (json_t *products) -{ - size_t index; - json_t *value; - int res; - - if (! json_is_array (products)) - { - GNUNET_break (0); - return GNUNET_SYSERR; - } - json_array_foreach (products, index, value) { - const char *description; - const char *error_name; - unsigned int error_line; - struct GNUNET_JSON_Specification spec[] = { - GNUNET_JSON_spec_string ("description", &description), - /* FIXME: there are other fields in the product specification - that are currently not labeled as optional. Maybe check - those as well, or make them truly optional. */ - GNUNET_JSON_spec_end() - }; - - /* extract fields we need to sign separately */ - res = GNUNET_JSON_parse (value, - spec, - &error_name, - &error_line); - if (GNUNET_OK != res) - { - GNUNET_break (0); - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - "Product description parsing failed at #%u: %s:%u\n", - (unsigned int) index, - error_name, - error_line); - return GNUNET_SYSERR; - } - GNUNET_JSON_parse_free (spec); - } - return GNUNET_OK; -} - - -/** - * Information we keep for individual calls - * to requests that parse JSON, but keep no other state. - */ -struct TMH_JsonParseContext -{ - - /** - * This field MUST be first. - * FIXME: Explain why! - */ - struct TM_HandlerContext hc; - - /** - * Placeholder for #TMH_PARSE_post_json() to keep its internal state. - */ - void *json_parse_context; -}; - - -/** - * Custom cleanup routine for a `struct TMH_JsonParseContext`. - * - * @param hc the `struct TMH_JsonParseContext` to clean up. - */ -static void -json_parse_cleanup (struct TM_HandlerContext *hc) -{ - struct TMH_JsonParseContext *jpc = (struct TMH_JsonParseContext *) hc; - - TMH_PARSE_post_cleanup_callback (jpc->json_parse_context); - GNUNET_free (jpc); -} - - -extern struct MerchantInstance * -get_instance (struct json_t *json); - - -/** - * Manage a contract request. In practical terms, it adds the fields - * 'exchanges', 'merchant_pub', and 'H_wire' to the contract 'proposition' - * gotten from the frontend. Finally, it adds (outside of the - * contract) a signature of the (hashed stringification) of the - * contract (and the hashed stringification of this contract as well - * to aid diagnostics) to the final bundle, which is then send back to - * the frontend. - * - * @param rh context of the handler - * @param connection the MHD connection to handle - * @param[in,out] connection_cls the connection's closure (can be updated) - * @param upload_data upload data - * @param[in,out] upload_data_size number of bytes (left) in @a upload_data - * @return MHD result code - */ -int -MH_handler_propose (struct TMH_RequestHandler *rh, - struct MHD_Connection *connection, - void **connection_cls, - const char *upload_data, - size_t *upload_data_size) -{ - - json_t *root; - json_t *jcontract; - int res; - struct MerchantInstance *mi; - struct TMH_JsonParseContext *ctx; - struct TALER_ContractPS contract; - struct GNUNET_CRYPTO_EddsaSignature contract_sig; - struct TALER_Amount total; - struct TALER_Amount max_fee; - uint64_t transaction_id; - json_t *products; - json_t *merchant; - struct GNUNET_TIME_Absolute timestamp; - struct GNUNET_TIME_Absolute refund_deadline; - struct GNUNET_TIME_Absolute pay_deadline; - struct GNUNET_JSON_Specification spec[] = { - TALER_JSON_spec_amount ("amount", &total), - TALER_JSON_spec_amount ("max_fee", &max_fee), - GNUNET_JSON_spec_uint64 ("transaction_id", &transaction_id), - /* The following entries we don't actually need, except to check that - the contract is well-formed */ - GNUNET_JSON_spec_json ("products", &products), - GNUNET_JSON_spec_json ("merchant", &merchant), - GNUNET_JSON_spec_absolute_time ("timestamp", ×tamp), - GNUNET_JSON_spec_absolute_time ("refund_deadline", &refund_deadline), - GNUNET_JSON_spec_absolute_time ("pay_deadline", &pay_deadline), - GNUNET_JSON_spec_end () - }; - - if (NULL == *connection_cls) - { - ctx = GNUNET_new (struct TMH_JsonParseContext); - ctx->hc.cc = &json_parse_cleanup; - *connection_cls = ctx; - } - else - { - ctx = *connection_cls; - } - - res = TMH_PARSE_post_json (connection, - &ctx->json_parse_context, - upload_data, - upload_data_size, - &root); - if (GNUNET_SYSERR == res) - return MHD_NO; - /* the POST's body has to be further fetched */ - if ((GNUNET_NO == res) || (NULL == root)) - return MHD_YES; - - jcontract = json_object_get (root, - "contract"); - if (NULL == jcontract) - { - json_decref (root); - return TMH_RESPONSE_reply_arg_missing (connection, - TALER_EC_PARAMETER_MISSING, - "contract"); - } - /* extract fields we need to sign separately */ - res = TMH_PARSE_json_data (connection, - jcontract, - spec); - if (GNUNET_NO == res) - { - json_decref (root); - return MHD_YES; - } - if (GNUNET_SYSERR == res) - { - json_decref (root); - return TMH_RESPONSE_reply_internal_error (connection, - TALER_EC_NONE, - "Impossible to parse contract"); - } - /* check contract is well-formed */ - if (GNUNET_OK != check_products (products)) - { - GNUNET_JSON_parse_free (spec); - json_decref (root); - return TMH_RESPONSE_reply_arg_invalid (connection, - TALER_EC_PARAMETER_MALFORMED, - "contract:products"); - } - - mi = get_instance (merchant); - if (NULL == mi) - { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - "Not able to find the specified instance\n"); - json_decref (root); - return TMH_RESPONSE_reply_not_found (connection, - TALER_EC_CONTRACT_INSTANCE_UNKNOWN, - "Unknown instance given"); - } - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Signing contract on behalf of instance '%s'\n", - mi->id); - /* add fields to the contract that the backend should provide */ - json_object_set (jcontract, - "exchanges", - trusted_exchanges); - json_object_set (jcontract, - "auditors", - j_auditors); - json_object_set_new (jcontract, - "H_wire", - GNUNET_JSON_from_data_auto (&mi->h_wire)); - json_object_set_new (jcontract, - "merchant_pub", - GNUNET_JSON_from_data_auto (&mi->pubkey)); - - /* create contract signature */ - contract.purpose.purpose = htonl (TALER_SIGNATURE_MERCHANT_CONTRACT); - contract.purpose.size = htonl (sizeof (contract)); - GNUNET_assert (GNUNET_OK == - TALER_JSON_hash (jcontract, - &contract.h_contract)); - GNUNET_CRYPTO_eddsa_sign (&mi->privkey.eddsa_priv, - &contract.purpose, - &contract_sig); - - /* return final response */ - res = TMH_RESPONSE_reply_json_pack (connection, - MHD_HTTP_OK, - "{s:O, s:o s:o}", - "contract", jcontract, - "merchant_sig", GNUNET_JSON_from_data_auto (&contract_sig), - "H_contract", GNUNET_JSON_from_data_auto (&contract.h_contract)); - GNUNET_JSON_parse_free (spec); - json_decref (root); - return res; -} - -/* end of taler-merchant-httpd_contract.c */ diff --git a/src/lib/Makefile.am b/src/lib/Makefile.am index d862365e..34388bc9 100644 --- a/src/lib/Makefile.am +++ b/src/lib/Makefile.am @@ -14,7 +14,7 @@ libtalermerchant_la_LDFLAGS = \ -no-undefined libtalermerchant_la_SOURCES = \ - merchant_api_contract.c \ + merchant_api_proposal.c \ merchant_api_pay.c \ merchant_api_track_transaction.c \ merchant_api_track_transfer.c \ diff --git a/src/lib/merchant_api_contract.c b/src/lib/merchant_api_contract.c deleted file mode 100644 index c5db8d3b..00000000 --- a/src/lib/merchant_api_contract.c +++ /dev/null @@ -1,239 +0,0 @@ -/* - This file is part of TALER - Copyright (C) 2014, 2015, 2016 GNUnet e.V. and INRIA - - 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 - -*/ -/** - * @file lib/merchant_api_contract.c - * @brief Implementation of the /contract request of the merchant's HTTP API - * @author Christian Grothoff - */ -#include "platform.h" -#include -#include -#include /* just for HTTP status codes */ -#include -#include -#include "taler_merchant_service.h" -#include -#include - - -/** - * @brief A Contract Operation Handle - */ -struct TALER_MERCHANT_ContractOperation -{ - - /** - * The url for this request. - */ - char *url; - - /** - * JSON encoding of the request to POST. - */ - char *json_enc; - - /** - * Handle for the request. - */ - struct GNUNET_CURL_Job *job; - - /** - * Function to call with the result. - */ - TALER_MERCHANT_ContractCallback cb; - - /** - * Closure for @a cb. - */ - void *cb_cls; - - /** - * Reference to the execution context. - */ - struct GNUNET_CURL_Context *ctx; -}; - - -/** - * Function called when we're done processing the - * HTTP /contract request. - * - * @param cls the `struct TALER_MERCHANT_Pay` - * @param response_code HTTP response code, 0 on error - * @param json response body, NULL if not in JSON - */ -static void -handle_contract_finished (void *cls, - long response_code, - const json_t *json) -{ - struct TALER_MERCHANT_ContractOperation *co = cls; - json_t *contract; - const struct TALER_MerchantSignatureP *sigp; - const struct GNUNET_HashCode *h_contractp; - struct TALER_MerchantSignatureP sig; - struct GNUNET_HashCode h_contract; - - co->job = NULL; - contract = NULL; - sigp = NULL; - h_contractp = NULL; - switch (response_code) - { - case 0: - break; - case MHD_HTTP_OK: - { - struct GNUNET_JSON_Specification spec[] = { - GNUNET_JSON_spec_json ("contract", &contract), - GNUNET_JSON_spec_fixed_auto ("merchant_sig", &sig), - GNUNET_JSON_spec_fixed_auto ("H_contract", &h_contract), - GNUNET_JSON_spec_end() - }; - - if (GNUNET_OK != - GNUNET_JSON_parse (json, - spec, - NULL, NULL)) - { - GNUNET_break_op (0); - response_code = 0; - break; - } - h_contractp = &h_contract; - sigp = &sig; - } - break; - case MHD_HTTP_BAD_REQUEST: - /* This should never happen, either us or the merchant is buggy - (or API version conflict); just pass JSON reply to the application */ - break; - case MHD_HTTP_FORBIDDEN: - break; - case MHD_HTTP_UNAUTHORIZED: - /* Nothing really to verify, merchant says one of the signatures is - invalid; as we checked them, this should never happen, we - should pass the JSON reply to the application */ - break; - case MHD_HTTP_NOT_FOUND: - /* Nothing really to verify, this should never - happen, we should pass the JSON reply to the application */ - break; - case MHD_HTTP_INTERNAL_SERVER_ERROR: - /* Server had an internal issue; we should retry, but this API - leaves this to the application */ - break; - default: - /* unexpected response code */ - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - "Unexpected response code %u\n", - (unsigned int) response_code); - GNUNET_break (0); - response_code = 0; - } - co->cb (co->cb_cls, - response_code, - TALER_JSON_get_error_code (json), - json, - contract, - sigp, - h_contractp); - if (NULL != contract) - json_decref (contract); -} - - -/** - * Request backend to sign a contract (and add fields like wire transfer - * details). - * - * @param ctx execution context - * @param backend_uri URI of the backend - * @param contract prototype of the contract - * @param contract_cb the callback to call when a reply for this request is available - * @param contract_cb_cls closure for @a contract_cb - * @return a handle for this request - */ -struct TALER_MERCHANT_ContractOperation * -TALER_MERCHANT_contract_sign (struct GNUNET_CURL_Context *ctx, - const char *backend_uri, - const json_t *contract, - TALER_MERCHANT_ContractCallback contract_cb, - void *contract_cb_cls) -{ - struct TALER_MERCHANT_ContractOperation *co; - json_t *req; - CURL *eh; - - co = GNUNET_new (struct TALER_MERCHANT_ContractOperation); - co->ctx = ctx; - co->cb = contract_cb; - co->cb_cls = contract_cb_cls; - GNUNET_asprintf (&co->url, - "%s%s", - backend_uri, - "/contract"); - - req = json_pack ("{s:O}", - "contract", (json_t *) contract); - eh = curl_easy_init (); - GNUNET_assert (NULL != (co->json_enc = - json_dumps (req, - JSON_COMPACT))); - json_decref (req); - GNUNET_assert (CURLE_OK == - curl_easy_setopt (eh, - CURLOPT_URL, - co->url)); - GNUNET_assert (CURLE_OK == - curl_easy_setopt (eh, - CURLOPT_POSTFIELDS, - co->json_enc)); - GNUNET_assert (CURLE_OK == - curl_easy_setopt (eh, - CURLOPT_POSTFIELDSIZE, - strlen (co->json_enc))); - co->job = GNUNET_CURL_job_add (ctx, - eh, - GNUNET_YES, - &handle_contract_finished, - co); - return co; -} - - -/** - * Cancel a /contract request. This function cannot be used - * on a request handle if a response is already served for it. - * - * @param co the contract operation request handle - */ -void -TALER_MERCHANT_contract_sign_cancel (struct TALER_MERCHANT_ContractOperation *co) -{ - if (NULL != co->job) - { - GNUNET_CURL_job_cancel (co->job); - co->job = NULL; - } - GNUNET_free (co->url); - GNUNET_free (co->json_enc); - GNUNET_free (co); -} - - -/* end of merchant_api_contract.c */ diff --git a/src/lib/merchant_api_proposal.c b/src/lib/merchant_api_proposal.c new file mode 100644 index 00000000..f1858906 --- /dev/null +++ b/src/lib/merchant_api_proposal.c @@ -0,0 +1,240 @@ +/* + This file is part of TALER + Copyright (C) 2014, 2015, 2016 GNUnet e.V. and INRIA + + 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 + +*/ +/** + * @file lib/merchant_api_proposal.c + * @brief Implementation of the /proposal PUT and GET + * @author Christian Grothoff + * @author Marcello Stanisci + */ +#include "platform.h" +#include +#include +#include /* just for HTTP status codes */ +#include +#include +#include "taler_merchant_service.h" +#include +#include + + +/** + * @brief A Contract Operation Handle + */ +struct TALER_MERCHANT_ContractOperation +{ + + /** + * The url for this request. + */ + char *url; + + /** + * JSON encoding of the request to POST. + */ + char *json_enc; + + /** + * Handle for the request. + */ + struct GNUNET_CURL_Job *job; + + /** + * Function to call with the result. + */ + TALER_MERCHANT_ContractCallback cb; + + /** + * Closure for @a cb. + */ + void *cb_cls; + + /** + * Reference to the execution context. + */ + struct GNUNET_CURL_Context *ctx; +}; + + +/** + * Function called when we're done processing the + * HTTP /contract request. + * + * @param cls the `struct TALER_MERCHANT_Pay` + * @param response_code HTTP response code, 0 on error + * @param json response body, NULL if not in JSON + */ +static void +handle_contract_finished (void *cls, + long response_code, + const json_t *json) +{ + struct TALER_MERCHANT_ContractOperation *co = cls; + json_t *contract; + const struct TALER_MerchantSignatureP *sigp; + const struct GNUNET_HashCode *h_contractp; + struct TALER_MerchantSignatureP sig; + struct GNUNET_HashCode h_contract; + + co->job = NULL; + contract = NULL; + sigp = NULL; + h_contractp = NULL; + switch (response_code) + { + case 0: + break; + case MHD_HTTP_OK: + { + struct GNUNET_JSON_Specification spec[] = { + GNUNET_JSON_spec_json ("contract", &contract), + GNUNET_JSON_spec_fixed_auto ("merchant_sig", &sig), + GNUNET_JSON_spec_fixed_auto ("H_contract", &h_contract), + GNUNET_JSON_spec_end() + }; + + if (GNUNET_OK != + GNUNET_JSON_parse (json, + spec, + NULL, NULL)) + { + GNUNET_break_op (0); + response_code = 0; + break; + } + h_contractp = &h_contract; + sigp = &sig; + } + break; + case MHD_HTTP_BAD_REQUEST: + /* This should never happen, either us or the merchant is buggy + (or API version conflict); just pass JSON reply to the application */ + break; + case MHD_HTTP_FORBIDDEN: + break; + case MHD_HTTP_UNAUTHORIZED: + /* Nothing really to verify, merchant says one of the signatures is + invalid; as we checked them, this should never happen, we + should pass the JSON reply to the application */ + break; + case MHD_HTTP_NOT_FOUND: + /* Nothing really to verify, this should never + happen, we should pass the JSON reply to the application */ + break; + case MHD_HTTP_INTERNAL_SERVER_ERROR: + /* Server had an internal issue; we should retry, but this API + leaves this to the application */ + break; + default: + /* unexpected response code */ + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + "Unexpected response code %u\n", + (unsigned int) response_code); + GNUNET_break (0); + response_code = 0; + } + co->cb (co->cb_cls, + response_code, + TALER_JSON_get_error_code (json), + json, + contract, + sigp, + h_contractp); + if (NULL != contract) + json_decref (contract); +} + + +/** + * PUT an order to the backend and receives the related + * proposal. + * + * @param ctx execution context + * @param backend_uri URI of the backend + * @param contract prototype of the contract + * @param contract_cb the callback to call when a reply for this request is available + * @param contract_cb_cls closure for @a contract_cb + * @return a handle for this request + */ +struct TALER_MERCHANT_ContractOperation * +TALER_MERCHANT_put_order (struct GNUNET_CURL_Context *ctx, + const char *backend_uri, + const json_t *order, + TALER_MERCHANT_ProposalCallback proposal_cb, + void *proposal_cb_cls) +{ + struct TALER_MERCHANT_ContractOperation *co; + json_t *req; + CURL *eh; + + co = GNUNET_new (struct TALER_MERCHANT_ContractOperation); + co->ctx = ctx; + co->cb = contract_cb; + co->cb_cls = contract_cb_cls; + GNUNET_asprintf (&co->url, + "%s%s", + backend_uri, + "/contract"); + + req = json_pack ("{s:O}", + "contract", (json_t *) contract); + eh = curl_easy_init (); + GNUNET_assert (NULL != (co->json_enc = + json_dumps (req, + JSON_COMPACT))); + json_decref (req); + GNUNET_assert (CURLE_OK == + curl_easy_setopt (eh, + CURLOPT_URL, + co->url)); + GNUNET_assert (CURLE_OK == + curl_easy_setopt (eh, + CURLOPT_POSTFIELDS, + co->json_enc)); + GNUNET_assert (CURLE_OK == + curl_easy_setopt (eh, + CURLOPT_POSTFIELDSIZE, + strlen (co->json_enc))); + co->job = GNUNET_CURL_job_add (ctx, + eh, + GNUNET_YES, + &handle_contract_finished, + co); + return co; +} + + +/** + * Cancel a /contract request. This function cannot be used + * on a request handle if a response is already served for it. + * + * @param co the contract operation request handle + */ +void +TALER_MERCHANT_contract_sign_cancel (struct TALER_MERCHANT_ContractOperation *co) +{ + if (NULL != co->job) + { + GNUNET_CURL_job_cancel (co->job); + co->job = NULL; + } + GNUNET_free (co->url); + GNUNET_free (co->json_enc); + GNUNET_free (co); +} + + +/* end of merchant_api_contract.c */ diff --git a/src/lib/test_merchant_api.c b/src/lib/test_merchant_api.c index f2081c2d..2705e862 100644 --- a/src/lib/test_merchant_api.c +++ b/src/lib/test_merchant_api.c @@ -131,12 +131,6 @@ enum OpCode */ OC_END = 0, - /** - * Ask the backend to store a contract and its hashcode into - * the database. - */ - OC_MAP_IN, - /** * Ask the backend to retrieve a contract from the database, according * to its hashcode. @@ -1499,7 +1493,6 @@ interpreter_run (void *cls) is); return; - case OC_MAP_IN: case OC_MAP_OUT: { struct GNUNET_HashCode h_proposal; @@ -1521,29 +1514,13 @@ interpreter_run (void *cls) GNUNET_assert (GNUNET_SYSERR != TALER_JSON_hash (proposal, &h_proposal)); - if (OC_MAP_IN == cmd->oc) - { - - if (MHD_HTTP_UNPROCESSABLE_ENTITY == cmd->expected_response_code) - RND_BLK (&h_proposal); - - GNUNET_assert (NULL != - (cmd->details.map.mo - = TALER_MERCHANT_map_in (ctx, - MERCHANT_URI, - proposal, - &h_proposal, - map_cb, - is))); - } - else - GNUNET_assert (NULL != - (cmd->details.map.mo - = TALER_MERCHANT_map_out (ctx, - MERCHANT_URI, - &h_proposal, - map_cb, - is))); + GNUNET_assert (NULL != + (cmd->details.map.mo + = TALER_MERCHANT_map_out (ctx, + MERCHANT_URI, + &h_proposal, + map_cb, + is))); } return; @@ -2033,7 +2010,6 @@ do_shutdown (void *cls) case OC_END: GNUNET_assert (0); break; - case OC_MAP_IN: case OC_MAP_OUT: if (NULL != cmd->details.map.mo) { @@ -2301,12 +2277,6 @@ run (void *cls) .details.pay.amount_with_fee = "EUR:5", .details.pay.amount_without_fee = "EUR:4.99" }, - /* Store contract-1 */ - { - .oc = OC_MAP_IN, - .label = "store-contract-1", - .expected_response_code = MHD_HTTP_OK, - .details.map.contract_reference = "create-contract-1" }, /* Create another contract */ { .oc = OC_CONTRACT, @@ -2341,14 +2311,6 @@ run (void *cls) .details.admin_add_incoming.transfer_details = "{ \"uuid\": 2}", .details.admin_add_incoming.amount = "EUR:1" }, - /* Store contract-1 */ - { - .oc = OC_MAP_IN, - .label = "store-contract-2", - .expected_response_code = MHD_HTTP_OK, - .details.map.contract_reference = "create-contract-2", - }, - /* Add another 4.01 EUR to reserve #2 */ { .oc = OC_ADMIN_ADD_INCOMING, .label = "create-reserve-2b", @@ -2523,12 +2485,6 @@ run (void *cls) \"products\":\ [ {\"description\":\"bogus\", \"value\":\"{EUR:1}\"} ] }" }, - /* Try to store a contract passing a bogus hashcode. */ - { - .oc = OC_MAP_IN, - .label = "store-contract-bogus", - .expected_response_code = MHD_HTTP_UNPROCESSABLE_ENTITY, - .details.map.contract_reference = "create-contract-3" }, /* end of testcase */ { .oc = OC_END } }; -- cgit v1.2.3