Files

86 lines
2.3 KiB
C++
Raw Permalink Normal View History

2024-05-27 10:35:06 +02:00
//
2025-02-14 13:42:23 +01:00
// Copyright (c) 2023-2025 Ivica Siladic, Bruno Iljazovic, Korina Simicevic
2024-05-27 10:35:06 +02:00
//
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt)
//
2025-01-13 16:11:41 +01:00
#ifndef BOOST_MQTT5_TEST_TEST_AUTHENTICATORS_HPP
#define BOOST_MQTT5_TEST_TEST_AUTHENTICATORS_HPP
2024-01-19 09:26:54 +01:00
2025-01-13 16:11:41 +01:00
#include <boost/mqtt5/types.hpp>
2024-10-08 09:59:35 +02:00
#include <boost/asio/async_result.hpp>
2024-01-19 09:26:54 +01:00
#include <boost/asio/dispatch.hpp>
#include <boost/asio/prepend.hpp>
2024-10-08 09:59:35 +02:00
#include <boost/system/error_code.hpp>
2024-01-19 09:26:54 +01:00
2025-01-13 16:11:41 +01:00
#include <string>
#include <string_view>
2024-01-19 09:26:54 +01:00
2025-01-13 16:11:41 +01:00
namespace boost::mqtt5::test {
2024-01-19 09:26:54 +01:00
namespace asio = boost::asio;
struct test_authenticator {
2025-01-13 16:11:41 +01:00
test_authenticator() = default;
2024-01-19 09:26:54 +01:00
2025-01-13 16:11:41 +01:00
template <typename CompletionToken>
decltype(auto) async_auth(
auth_step_e step, std::string data,
CompletionToken&& token
) {
using error_code = boost::system::error_code;
using Signature = void (error_code, std::string);
2024-01-19 09:26:54 +01:00
2025-01-13 16:11:41 +01:00
auto initiate = [](auto handler, auth_step_e, std::string) {
asio::dispatch(
asio::prepend(std::move(handler), error_code {}, "")
);
};
2024-01-19 09:26:54 +01:00
2025-01-13 16:11:41 +01:00
return asio::async_initiate<CompletionToken, Signature>(
initiate, token, step, std::move(data)
);
}
2024-01-19 09:26:54 +01:00
2025-01-13 16:11:41 +01:00
std::string_view method() const {
return "method";
}
2024-01-19 09:26:54 +01:00
};
template <auth_step_e fail_on_step>
struct fail_test_authenticator {
2025-01-13 16:11:41 +01:00
fail_test_authenticator() = default;
2024-01-19 09:26:54 +01:00
2025-01-13 16:11:41 +01:00
template <typename CompletionToken>
decltype(auto) async_auth(
auth_step_e step, std::string data,
CompletionToken&& token
) {
using error_code = boost::system::error_code;
using Signature = void (error_code, std::string);
2024-01-19 09:26:54 +01:00
2025-01-13 16:11:41 +01:00
auto initiate = [](auto handler, auth_step_e step, std::string) {
error_code ec;
if (fail_on_step == step)
ec = asio::error::no_recovery;
2024-01-19 09:26:54 +01:00
2025-01-13 16:11:41 +01:00
asio::dispatch(
asio::prepend(std::move(handler), ec, "")
);
};
2024-01-19 09:26:54 +01:00
2025-01-13 16:11:41 +01:00
return asio::async_initiate<CompletionToken, Signature>(
initiate, token, step, std::move(data)
);
}
2024-01-19 09:26:54 +01:00
2025-01-13 16:11:41 +01:00
std::string_view method() const {
return "method";
}
2024-01-19 09:26:54 +01:00
};
2025-01-13 16:11:41 +01:00
} // end namespace boost::mqtt5::test
2024-01-19 09:26:54 +01:00
2025-01-13 16:11:41 +01:00
#endif // BOOST_MQTT5_TEST_TEST_AUTHENTICATORS_HPP