fixed http request types

reformatted sources
This commit is contained in:
gulliver 2022-12-11 17:04:21 +01:00
parent ce784c38dd
commit 79094c8a6d
3 changed files with 371 additions and 334 deletions

View File

@ -1,94 +1,127 @@
#include "crow.h"
#include "crow/common.h"
#include "crow/http_parser_merged.h"
#include "crow/http_response.h"
#include "model.hpp"
int main()
{
int main() {
crow::SimpleApp app;
std::shared_ptr<Model> model = Model::getModel("simple");
CROW_ROUTE(app, "/cddc")
([&model](const crow::request& req){
.methods(crow::HTTPMethod::POST)([&model](const crow::request &req) {
auto req_cddc = RequestCDDC::from_string(req.body);
if (!req_cddc) {
return crow::response(crow::status::BAD_REQUEST);
} else {
ResponseCDDC res;
// TBD use serial from req
res.cddc = model->getCDDC();
res.message_reference = req_cddc->message_reference;
auto cddc = model->getCDDC(req_cddc->cdd_serial);
if (!cddc) {
res.status_code = crow::status::NOT_FOUND;
} else {
res.cddc = *cddc.value();
res.status_code = crow::status::OK;
}
return crow::response(res.to_json());
}
});
CROW_ROUTE(app, "/cddc/serial")
([&model](const crow::request& request){
.methods(crow::HTTPMethod::POST)([&model](const crow::request &request) {
auto req = RequestCDDSerial::from_string(request.body);
if (!req) {
return crow::response(crow::status::BAD_REQUEST);
} else {
// \todo check serial input
ResponseCDDSerial res;
res.cdd_serial = model->getCurrentCDDC().cdd.cdd_serial;
res.message_reference = req->message_reference;
auto cddc = model->getCurrentCDDC();
if (!cddc) {
res.status_code = crow::status::NOT_FOUND;
} else {
res.cdd_serial = (*cddc)->cdd.cdd_serial;
res.status_code = crow::status::OK;
}
return crow::response(res.to_json());
}
});
CROW_ROUTE(app, "/mkcs")
([&model](const crow::request& request){
.methods(crow::HTTPMethod::POST)([&model](const crow::request &request) {
auto req = RequestMKCs::from_string(request.body);
if (!req) {
return crow::response(crow::status::BAD_REQUEST);
} else {
// \todo implement request
return crow::response(crow::status::NOT_IMPLEMENTED);
ResponseMKCs res;
res.message_reference = req->message_reference;
res.keys = model->getMKCs(req->denominations, req->mint_key_ids);
res.status_code = crow::status::OK;
return crow::response(res.to_json());
}
});
CROW_ROUTE(app, "/mint").methods(crow::HTTPMethod::GET)
([&model](const crow::request& request){
CROW_ROUTE(app, "/mint")
.methods(crow::HTTPMethod::POST)([&model](const crow::request &request) {
auto req = RequestMint::from_string(request.body);
if (!req) {
return crow::response(crow::status::BAD_REQUEST);
} else {
// \todo implement request
return crow::response(crow::status::NOT_IMPLEMENTED);
ResponseMint res;
res.message_reference = req->message_reference;
auto minted = model->mint(req->transaction_reference, req->blinds);
res.blind_signatures = minted;
res.status_code = crow::status::OK;
return crow::response(res.to_json());
}
});
CROW_ROUTE(app, "/renew").methods(crow::HTTPMethod::GET)
([&model](const crow::request& request){
auto req = RequestMint::from_string(request.body);
CROW_ROUTE(app, "/renew")
.methods(crow::HTTPMethod::POST)([&model](const crow::request &request) {
auto req = RequestRenew::from_string(request.body);
if (!req) {
return crow::response(crow::status::BAD_REQUEST);
} else {
// \todo implement request
return crow::response(crow::status::NOT_IMPLEMENTED);
// \todo implement ResponseDelay
ResponseMint res;
res.message_reference = req->message_reference;
res.status_code = crow::status::OK;
res.blind_signatures =
model->mint(req->transaction_reference, req->blinds);
return crow::response(res.to_json());
}
});
CROW_ROUTE(app, "/resume").methods(crow::HTTPMethod::GET)
([&model](const crow::request& request){
CROW_ROUTE(app, "/resume")
.methods(crow::HTTPMethod::POST)([](const crow::request &request) {
auto req = RequestResume::from_string(request.body);
if (!req) {
return crow::response(crow::status::BAD_REQUEST);
} else {
// \todo implement request
ResponseMint res;
res.message_reference = req->message_reference;
res.status_code = crow::status::NOT_IMPLEMENTED; // crow::status::OK;
return crow::response(crow::status::NOT_IMPLEMENTED);
}
});
CROW_ROUTE(app, "/redeem").methods(crow::HTTPMethod::GET)
([&model](const crow::request& request){
CROW_ROUTE(app, "/redeem")
.methods(crow::HTTPMethod::POST)([&model](const crow::request &request) {
auto req = RequestRedeem::from_string(request.body);
if (!req) {
return crow::response(crow::status::BAD_REQUEST);
} else {
// \todo implement request
return crow::response(crow::status::NOT_IMPLEMENTED);
ResponseRedeem res;
res.message_reference = req->message_reference;
bool success = model->redeem(req->coins);
res.status_code =
success ? crow::status::OK : crow::status::NOT_FOUND;
return crow::response(res.to_json());
}
});

View File

@ -20,7 +20,6 @@ crow::json::wvalue list_to_json(const std::vector<unsigned int>& array) {
return crow::json::wvalue(l);
}
crow::json::wvalue PublicKey::to_json() const {
crow::json::wvalue r;
TO_JSON(modulus);
@ -29,8 +28,7 @@ crow::json::wvalue PublicKey::to_json() const {
return r;
}
crow::json::wvalue WeightedUrl::to_json() const
{
crow::json::wvalue WeightedUrl::to_json() const {
crow::json::wvalue::list l;
crow::json::wvalue w(weight);
@ -88,7 +86,6 @@ crow::json::wvalue MintKey::to_json() const {
return r;
}
crow::json::wvalue MintKeyCert::to_json() const {
crow::json::wvalue r;
TO_JSON_JSON(mint_key);
@ -97,8 +94,7 @@ crow::json::wvalue MintKeyCert::to_json() const {
return r;
}
crow::json::wvalue Response::to_json() const
{
crow::json::wvalue Response::to_json() const {
crow::json::wvalue r;
TO_JSON(message_reference);
TO_JSON(status_code);
@ -106,9 +102,7 @@ crow::json::wvalue Response::to_json() const
return r;
}
crow::json::wvalue ResponseCDDSerial::to_json() const
{
crow::json::wvalue ResponseCDDSerial::to_json() const {
crow::json::wvalue r = Response::to_json();
TO_JSON(cdd_serial);
@ -117,8 +111,7 @@ crow::json::wvalue ResponseCDDSerial::to_json() const
}
tl::expected<RequestCDDSerial, eError>
RequestCDDSerial::from_string(const std::string& str)
{
RequestCDDSerial::from_string(const std::string &str) {
auto json = crow::json::load(str);
if (!json) {
return tl::make_unexpected(eError::JSON_PARSE_ERROR);
@ -133,8 +126,7 @@ RequestCDDSerial::from_string(const std::string& str)
}
}
crow::json::wvalue ResponseCDDC::to_json() const
{
crow::json::wvalue ResponseCDDC::to_json() const {
crow::json::wvalue r = Response::to_json();
TO_JSON_JSON(cddc);
r["type"] = "response cdd serial";
@ -142,15 +134,12 @@ crow::json::wvalue ResponseCDDC::to_json() const
}
tl::expected<RequestCDDC, eError>
RequestCDDC::from_string(const std::string& str)
{
RequestCDDC::from_string(const std::string &str) {
auto json = crow::json::load(str);
if (!json) {
return tl::make_unexpected(eError::JSON_PARSE_ERROR);
} else if ( !( json.has("type")
&& json.has("message_reference")
&& json.has("cdd_serial")
) ) {
} else if (!(json.has("type") && json.has("message_reference") &&
json.has("cdd_serial"))) {
return tl::make_unexpected(eError::JSON_MISSING_KEY);
} else if (json["type"] != "request cddc") {
return tl::make_unexpected(eError::JSON_WRONG_REQUEST_TYPE);
@ -167,11 +156,8 @@ RequestMKCs::from_string(const std::string& str) {
auto json = crow::json::load(str);
if (!json) {
return tl::make_unexpected(eError::JSON_PARSE_ERROR);
} else if ( !(json.has("denominations")
&& json.has("message_reference")
&& json.has("mint_key_ids")
&& json.has("type")
) ) {
} else if (!(json.has("denominations") && json.has("message_reference") &&
json.has("mint_key_ids") && json.has("type"))) {
return tl::make_unexpected(eError::JSON_MISSING_KEY);
} else if (json["type"] != "request mint key certificates") {
return tl::make_unexpected(eError::JSON_WRONG_REQUEST_TYPE);
@ -199,7 +185,6 @@ RequestMKCs::from_string(const std::string& str) {
}
}
crow::json::wvalue ResponseMKCs::to_json() const {
crow::json::wvalue r = Response::to_json();
TO_JSON_ARRAY(keys);
@ -216,13 +201,9 @@ crow::json::wvalue Blind::to_json() const {
return r;
}
tl::expected<Blind,eError> Blind::from_json(const crow::json::rvalue& json)
{
if ( !( json.has("type")
&& json.has("blinded_payload_hash")
&& json.has("mint_key_id")
&& json.has("reference")
) ) {
tl::expected<Blind, eError> Blind::from_json(const crow::json::rvalue &json) {
if (!(json.has("type") && json.has("blinded_payload_hash") &&
json.has("mint_key_id") && json.has("reference"))) {
return tl::make_unexpected(eError::JSON_ERROR);
} else if (json["type"] != "blinded payload hash") {
return tl::make_unexpected(eError::JSON_ERROR);
@ -241,7 +222,6 @@ crow::json::wvalue BlindSignature::to_json() const {
TO_JSON(reference);
r["type"] = "blind signature";
return r;
}
tl::expected<RequestMint, eError>
@ -251,11 +231,8 @@ RequestMint::from_string(const std::string& str){
auto json = crow::json::load(str);
if (!json) {
return tl::make_unexpected(eError::JSON_PARSE_ERROR);
} else if ( !( json.has("type")
&& json.has("message_reference")
&& json.has("transaction_reference")
&& json.has("blinds")
) ) {
} else if (!(json.has("type") && json.has("message_reference") &&
json.has("transaction_reference") && json.has("blinds"))) {
return tl::make_unexpected(eError::JSON_ERROR);
} else if (json["type"] != "request mint") {
return tl::make_unexpected(eError::JSON_ERROR);
@ -299,8 +276,7 @@ crow::json::wvalue Coin::Payload::to_json() const {
return r;
}
crow::json::wvalue Coin::to_json() const
{
crow::json::wvalue Coin::to_json() const {
crow::json::wvalue r;
TO_JSON_JSON(payload);
TO_JSON(signature);
@ -308,14 +284,12 @@ crow::json::wvalue Coin::to_json() const
return r;
}
tl::expected<Coin::Payload,eError> Coin::Payload::from_json(const crow::json::rvalue& json) {
if ( !( json.has("cdd_location")
&& json.has("denomination")
&& json.has("issuer_id")
&& json.has("mint_key_id")
&& json.has("protocol_version")
&& json.has("serial")
&& json.has("type"))) {
tl::expected<Coin::Payload, eError>
Coin::Payload::from_json(const crow::json::rvalue &json) {
if (!(json.has("cdd_location") && json.has("denomination") &&
json.has("issuer_id") && json.has("mint_key_id") &&
json.has("protocol_version") && json.has("serial") &&
json.has("type"))) {
return tl::make_unexpected(eError::JSON_ERROR);
} else if (json["type"] != "payload") {
return tl::make_unexpected(eError::JSON_ERROR);
@ -332,10 +306,7 @@ tl::expected<Coin::Payload,eError> Coin::Payload::from_json(const crow::json::rv
}
tl::expected<Coin, eError> Coin::from_json(const crow::json::rvalue &json) {
if ( !( json.has("type")
&& json.has("payload")
&& json.has("signature")
) ) {
if (!(json.has("type") && json.has("payload") && json.has("signature"))) {
return tl::make_unexpected(eError::JSON_ERROR);
} else if (json["type"] != "coin") {
return tl::make_unexpected(eError::JSON_ERROR);
@ -360,24 +331,20 @@ crow::json::wvalue CoinStack::to_json() const {
return r;
}
tl::expected<RequestRenew, eError>
RequestRenew::from_string(const std::string &str) {
auto json = crow::json::load(str);
if (!json) {
return tl::make_unexpected(eError::JSON_PARSE_ERROR);
} else if ( !( json.has("blinds")
&& json.has("coins")
&& json.has("transaction_reference")
&& json.has("message_reference")
&& json.has("type")
) ) {
} else if (!(json.has("blinds") && json.has("coins") &&
json.has("transaction_reference") &&
json.has("message_reference") && json.has("type"))) {
return tl::make_unexpected(eError::JSON_MISSING_KEY);
} else if (json["type"] != "request renew") {
return tl::make_unexpected(eError::JSON_WRONG_REQUEST_TYPE);
} else if ( (json["coins"].t()!=crow::json::type::List)
|| (json["blinds"].t()!=crow::json::type::List) ) {
} else if ((json["coins"].t() != crow::json::type::List) ||
(json["blinds"].t() != crow::json::type::List)) {
return tl::make_unexpected(eError::JSON_WRONG_VALUE_TYPE);
} else {
RequestRenew r;
@ -417,10 +384,8 @@ RequestResume::from_string(const std::string& str) {
auto json = crow::json::load(str);
if (!json) {
return tl::make_unexpected(eError::JSON_PARSE_ERROR);
} else if ( !( json.has("transaction_reference")
&& json.has("message_reference")
&& json.has("type")
) ) {
} else if (!(json.has("transaction_reference") &&
json.has("message_reference") && json.has("type"))) {
return tl::make_unexpected(eError::JSON_MISSING_KEY);
} else if (json["type"] != "request resume") {
return tl::make_unexpected(eError::JSON_WRONG_REQUEST_TYPE);
@ -438,10 +403,8 @@ RequestRedeem::from_string(const std::string& str) {
auto json = crow::json::load(str);
if (!json) {
return tl::make_unexpected(eError::JSON_PARSE_ERROR);
} else if ( !( json.has("coins")
&& json.has("message_reference")
&& json.has("type")
) ) {
} else if (!(json.has("coins") && json.has("message_reference") &&
json.has("type"))) {
return tl::make_unexpected(eError::JSON_MISSING_KEY);
} else if (json["type"] != "request redeem") {
return tl::make_unexpected(eError::JSON_WRONG_REQUEST_TYPE);
@ -464,28 +427,57 @@ auto json = crow::json::load(str);
}
}
crow::json::wvalue ResponseReedem::to_json() const {
crow::json::wvalue ResponseRedeem::to_json() const {
crow::json::wvalue r = Response::to_json();
r["type"] = "response redeem";
return r;
}
using std::cout;
using std::endl;
/** this is for now our sample model */
class DefaultModel : public Model {
public:
DefaultModel() {}
const CDDC& getCDDC() override {return m_cddc; };
const CDDC& getCurrentCDDC() override {return m_cddc; };
void mint() override {};
tl::expected<CDDC *, bool> getCDDC(unsigned int cdd_serial) override {
cout << __FUNCTION__ << "(" << cdd_serial << ")" << endl;
return &m_cddc;
};
tl::expected<CDDC *, bool> getCurrentCDDC() override {
cout << __FUNCTION__ << "()" << endl;
return &m_cddc;
}
std::vector<BlindSignature> mint(const std::string &transaction_reference,
const std::vector<Blind> &blinds) override {
std::vector<BlindSignature> res;
cout << __FUNCTION__ << "("
<< ")" << endl;
return res;
}
const std::vector<MintKeyCert>
getMKCs(const std::vector<unsigned int> &denominations,
const std::vector<unsigned int> &mint_key_ids) override {
std::vector<MintKeyCert> res;
cout << __FUNCTION__ << endl;
return res;
}
bool redeem(const std::vector<Coin> &coins) override {
cout << __FUNCTION__ << endl;
return false;
}
private:
CDDC m_cddc;
};
std::unique_ptr<Model> Model::getModel(const std::string& /*backend_name*/)
{
std::unique_ptr<Model> Model::getModel(const std::string & /*backend_name*/) {
cout << __FUNCTION__ << endl;
//:wq
// if (backend_name=="default")
return std::make_unique<DefaultModel>();

View File

@ -1,16 +1,15 @@
#ifndef MODEL_HPP
#define MODEL_HPP
#include <memory>
#include <string>
#include <chrono>
#include <memory>
#include <optional>
#include <string>
#include "crow/json.h"
#include "tl/expected.hpp"
struct PublicKey {
std::string modulus; //: "daaa63ddda38c189b8c49020c8276adbe0a695685a...",
std::string public_exponent; //: 65537,
@ -39,8 +38,10 @@ struct CDD {
std::string id; //: 23ed956e629ba35f0002eaf833ea436aea7db5c2,
std::vector<WeightedUrl> info_service;
/* eCipherSuite*/ std::string issuer_cipher_suite; //: RSA-SHA256-PSS-CHAUM82,
PublicKey issuer_public_master_key;//: {
// modulus: daaa63ddda38c189b8c49020c8276adbe0a695685a...,
PublicKey
issuer_public_master_key; //: {
// modulus:
// daaa63ddda38c189b8c49020c8276adbe0a695685a...,
// public_exponent: 65537,
// type: rsa public key
//},
@ -100,7 +101,8 @@ struct Response {
struct RequestCDDSerial {
unsigned int message_reference; /// Client internal message reference.
/// (Integer)
static tl::expected<RequestCDDSerial,eError> from_string(const std::string& str);
static tl::expected<RequestCDDSerial, eError>
from_string(const std::string &str);
};
struct ResponseCDDSerial : Response {
@ -166,7 +168,6 @@ struct ResponseMint : Response {
crow::json::wvalue to_json() const override;
};
struct Coin {
struct Payload {
std::string cdd_location;
@ -177,7 +178,8 @@ struct Coin {
std::string serial;
crow::json::wvalue to_json() const;
static tl::expected<Payload,eError> from_json(const crow::json::rvalue& json);
static tl::expected<Payload, eError>
from_json(const crow::json::rvalue &json);
};
Payload payload;
@ -213,7 +215,8 @@ struct RequestResume {
/// (Integer)
std::string transaction_reference;
// "type": "request resume"
static tl::expected<RequestResume,eError> from_string(const std::string& str);
static tl::expected<RequestResume, eError>
from_string(const std::string &str);
};
struct RequestRedeem {
@ -221,10 +224,11 @@ struct RequestRedeem {
unsigned int message_reference; /// Client internal message reference.
/// (Integer)
// "type": "request redeem"
static tl::expected<RequestRedeem,eError> from_string(const std::string& str);
static tl::expected<RequestRedeem, eError>
from_string(const std::string &str);
};
struct ResponseReedem : Response {
struct ResponseRedeem : Response {
crow::json::wvalue to_json() const override;
};
@ -232,13 +236,21 @@ class Model {
public:
virtual ~Model(){};
virtual const CDDC& getCDDC() = 0;
virtual const CDDC& getCurrentCDDC() = 0;
virtual void mint() = 0;
virtual tl::expected<CDDC *, bool> getCDDC(unsigned int cdd_serial) = 0;
virtual tl::expected<CDDC *, bool> getCurrentCDDC() = 0;
virtual const std::vector<MintKeyCert>
getMKCs(const std::vector<unsigned int> &denominations,
const std::vector<unsigned int> &mint_key_ids) = 0;
virtual std::vector<BlindSignature>
mint(const std::string &transaction_reference,
const std::vector<Blind> &blinds) = 0;
virtual bool redeem(const std::vector<Coin> &coins) = 0;
static std::unique_ptr<Model> getModel(const std::string &backend_name);
private:
};
#endif // #ifndef MODEL_HPP