rublon-ssh/PAM/ssh/include/rublon/method/method_factory.hpp
2023-07-24 13:25:48 +02:00

83 lines
2.3 KiB
C++

#pragma once
#include <tl/expected.hpp>
#include <variant>
#include <rublon/core_handler.hpp>
#include <rublon/pam.hpp>
#include <rublon/pam_action.hpp>
#include <rublon/method/OTP.hpp>
#include <rublon/method/SMS.hpp>
namespace rublon {
class Method {
std::string tid;
public:
Method() {}
template < typename Handler_t >
tl::expected< int, PamAction > fire(const CoreHandlerInterface< Handler_t > & coreHandler) {
return std::visit([&](const auto & method) { return method.fire(coreHandler); }, _impl);
}
private:
std::variant< method::OTP, method::SMS > _impl;
};
template < typename Pam_t = LinuxPam >
class MethodFactory {
const Pam_t & pam;
public:
MethodFactory(const Pam_t & pam) : pam{pam} {}
template < typename Array_t >
tl::expected< Method, PamAction > create(const Array_t & methods, const std::string & tid) const {
std::pmr::map< int, std::pmr::string > methods_id;
pam.print("%s", "");
int i;
for(const auto & method : methods) {
rublon::log(LogLevel::Debug, "method %s found at pos %d", method.GetString(), i);
if(method == "email") {
pam.print("%d: Email Link", i + 1);
methods_id[++i] = "email";
}
if(method == "qrcode") {
pam.print("%d: QR Code", i + 1);
methods_id[++i] = "qrcode";
}
if(method == "totp") {
pam.print("%d: Mobile TOTP", i + 1);
methods_id[++i] = "totp";
}
if(method == "push") {
pam.print("%d: Mobile Push", i + 1);
methods_id[++i] = "push";
}
if(method == "sms") {
pam.print("%d: SMS code", i + 1);
methods_id[++i] = "sms";
}
}
auto methodid = pam.scan(
[](char * userinput) {
rublon::log(LogLevel::Debug, "User input: %s", userinput);
return std::stoi(userinput);
},
"\nSelect method [1-%d]: ",
methods.Size());
pam.print(
"you selected: %s", methods_id.count(methodid.value_or(0)) ? methods_id.at(methodid.value_or(0)).c_str() : "unknown option");
return tl::unexpected{PamAction::accept};
}
};
} // namespace rublon