|
| 1 | +/////////////////////////////////////////////////////////////////////////////// |
| 2 | +// |
| 3 | +// Copyright (c) Crossbar.io Technologies GmbH and contributors |
| 4 | +// |
| 5 | +// Boost Software License - Version 1.0 - August 17th, 2003 |
| 6 | +// |
| 7 | +// Permission is hereby granted, free of charge, to any person or organization |
| 8 | +// obtaining a copy of the software and accompanying documentation covered by |
| 9 | +// this license (the "Software") to use, reproduce, display, distribute, |
| 10 | +// execute, and transmit the Software, and to prepare derivative works of the |
| 11 | +// Software, and to permit third-parties to whom the Software is furnished to |
| 12 | +// do so, all subject to the following: |
| 13 | +// |
| 14 | +// The copyright notices in the Software and this entire statement, including |
| 15 | +// the above license grant, this restriction and the following disclaimer, |
| 16 | +// must be included in all copies of the Software, in whole or in part, and |
| 17 | +// all derivative works of the Software, unless such copies or derivative |
| 18 | +// works are solely in the form of machine-executable object code generated by |
| 19 | +// a source language processor. |
| 20 | +// |
| 21 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 22 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 23 | +// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT |
| 24 | +// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE |
| 25 | +// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, |
| 26 | +// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 27 | +// DEALINGS IN THE SOFTWARE. |
| 28 | +// |
| 29 | +/////////////////////////////////////////////////////////////////////////////// |
| 30 | + |
| 31 | +#include "parameters.hpp" |
| 32 | + |
| 33 | +#include <botan/auto_rng.h> |
| 34 | +#include <botan/ecdsa.h> |
| 35 | +#include <botan/ec_group.h> |
| 36 | +#include <botan/pubkey.h> |
| 37 | +#include <botan/hex.h> |
| 38 | +#include <botan/ed25519.h> |
| 39 | + |
| 40 | +#include <autobahn/autobahn.hpp> |
| 41 | +#include <utility> |
| 42 | +#include <autobahn/wamp_websocketpp_websocket_transport.hpp> |
| 43 | +#include <websocketpp/config/asio_no_tls_client.hpp> |
| 44 | +#include <websocketpp/client.hpp> |
| 45 | +#include <boost/version.hpp> |
| 46 | +#include <iostream> |
| 47 | +#include <memory> |
| 48 | +#include <string> |
| 49 | +#include <tuple> |
| 50 | + |
| 51 | +void add2(autobahn::wamp_invocation invocation) |
| 52 | +{ |
| 53 | + auto a = invocation->argument<uint64_t>(0); |
| 54 | + auto b = invocation->argument<uint64_t>(1); |
| 55 | + |
| 56 | + std::cerr << "Procedure com.examples.calculator.add2 invoked: " << a << ", " << b << std::endl; |
| 57 | + |
| 58 | + invocation->result(std::make_tuple(a + b)); |
| 59 | +} |
| 60 | + |
| 61 | +class auth_wamp_session : |
| 62 | + public autobahn::wamp_session |
| 63 | +{ |
| 64 | +public: |
| 65 | + boost::promise<autobahn::wamp_authenticate> challenge_future; |
| 66 | + Botan::Ed25519_PrivateKey m_private_key; |
| 67 | + |
| 68 | + auth_wamp_session( |
| 69 | + boost::asio::io_service& io, |
| 70 | + bool debug_enabled, |
| 71 | + const Botan::secure_vector<uint8_t>& private_key) : |
| 72 | + autobahn::wamp_session(io, debug_enabled), |
| 73 | + m_private_key(private_key) |
| 74 | + { |
| 75 | + } |
| 76 | + |
| 77 | + boost::future<autobahn::wamp_authenticate> on_challenge(const autobahn::wamp_challenge& challenge) |
| 78 | + { |
| 79 | + Botan::AutoSeeded_RNG rng; |
| 80 | + Botan::PK_Signer signer(m_private_key, rng, "Pure"); |
| 81 | + signer.update(Botan::hex_decode(challenge.challenge())); |
| 82 | + std::cerr << "responding to auth challenge: " << challenge.challenge() << std::endl; |
| 83 | + std::string signature = Botan::hex_encode(signer.signature(rng)); |
| 84 | + challenge_future.set_value(autobahn::wamp_authenticate(signature + challenge.challenge())); |
| 85 | + std::cerr << "signature: " << signature << std::endl; |
| 86 | + return challenge_future.get_future(); |
| 87 | + } |
| 88 | +}; |
| 89 | + |
| 90 | +typedef websocketpp::client<websocketpp::config::asio_client> client; |
| 91 | + |
| 92 | +int main(int argc, char** argv) |
| 93 | +{ |
| 94 | + std::cerr << "Boost: " << BOOST_VERSION << std::endl; |
| 95 | + try { |
| 96 | + auto parameters = get_parameters(argc, argv); |
| 97 | + |
| 98 | + std::cerr << "Connecting to realm: " << parameters->realm() << std::endl; |
| 99 | + |
| 100 | + boost::asio::io_service io; |
| 101 | + bool debug = parameters->debug(); |
| 102 | + |
| 103 | + client ws_clinet; |
| 104 | + ws_clinet.init_asio(&io); |
| 105 | + auto transport = std::make_shared < autobahn::wamp_websocketpp_websocket_transport<websocketpp::config::asio_client> >( |
| 106 | + ws_clinet, "ws://127.0.0.1:8080/ws", debug); |
| 107 | + |
| 108 | + Botan::secure_vector<uint8_t> privateKey(32, 0); |
| 109 | + auto session = std::make_shared<auth_wamp_session>(io, debug, privateKey); |
| 110 | + |
| 111 | + // Create a thread to run the telemetry loop |
| 112 | + transport->attach(std::static_pointer_cast<autobahn::wamp_transport_handler>(session)); |
| 113 | + |
| 114 | + // Make sure the continuation futures we use do not run out of scope prematurely. |
| 115 | + // Since we are only using one thread here this can cause the io service to block |
| 116 | + // as a future generated by a continuation will block waiting for its promise to be |
| 117 | + // fulfilled when it goes out of scope. This would prevent the session from receiving |
| 118 | + // responses from the router. |
| 119 | + boost::future<void> connect_future; |
| 120 | + boost::future<void> start_future; |
| 121 | + boost::future<void> join_future; |
| 122 | + boost::future<void> provide_future; |
| 123 | + |
| 124 | + connect_future = transport->connect().then([&](boost::future<void> connected) { |
| 125 | + try { |
| 126 | + connected.get(); |
| 127 | + } catch (const std::exception& e) { |
| 128 | + std::cerr << e.what() << std::endl; |
| 129 | + io.stop(); |
| 130 | + return; |
| 131 | + } |
| 132 | + |
| 133 | + std::cerr << "transport connected" << std::endl; |
| 134 | + |
| 135 | + start_future = session->start().then([&](boost::future<void> started) { |
| 136 | + try { |
| 137 | + started.get(); |
| 138 | + } catch (const std::exception& e) { |
| 139 | + std::cerr << e.what() << std::endl; |
| 140 | + io.stop(); |
| 141 | + return; |
| 142 | + } |
| 143 | + |
| 144 | + std::cerr << "session started" << std::endl; |
| 145 | + |
| 146 | + std::vector<std::string> authmethods{"cryptosign"}; |
| 147 | + std::string pubkey{"3b6a27bcceb6a42d62a3a8d02a6f0d73653215771de243a63ac048a18b59da29"}; |
| 148 | + join_future = session->join(parameters->realm(), authmethods, "", {{"pubkey", pubkey}}).then([&](boost::future<uint64_t> joined) { |
| 149 | + try { |
| 150 | + std::cerr << "joined realm: " << joined.get() << std::endl; |
| 151 | + } catch (const std::exception& e) { |
| 152 | + std::cerr << e.what() << std::endl; |
| 153 | + io.stop(); |
| 154 | + return; |
| 155 | + } |
| 156 | + |
| 157 | + provide_future = session->provide("com.examples.calculator.add2", &add2).then( |
| 158 | + [&](boost::future<autobahn::wamp_registration> registration) { |
| 159 | + try { |
| 160 | + std::cerr << "registered procedure:" << registration.get().id() << std::endl; |
| 161 | + } catch (const std::exception& e) { |
| 162 | + std::cerr << e.what() << std::endl; |
| 163 | + io.stop(); |
| 164 | + return; |
| 165 | + } |
| 166 | + }); |
| 167 | + }); |
| 168 | + }); |
| 169 | + }); |
| 170 | + |
| 171 | + std::cerr << "starting io service" << std::endl; |
| 172 | + |
| 173 | + io.run(); |
| 174 | + |
| 175 | + std::cerr << "stopped io service" << std::endl; |
| 176 | + } |
| 177 | + catch (const std::exception& e) { |
| 178 | + std::cerr << "exception: " << e.what() << std::endl; |
| 179 | + return -1; |
| 180 | + } |
| 181 | + |
| 182 | + return 0; |
| 183 | +} |
0 commit comments