rublon-ssh/PAM/ssh/include/rublon/method/OTP.hpp
Bartosz Wieczorek e9b65c294e Fix tests
2023-08-01 15:44:23 +02:00

61 lines
2.1 KiB
C++

#pragma once
#include <tl/expected.hpp>
#include <rublon/authentication_step_interface.hpp>
#include <rublon/pam.hpp>
#include <rublon/pam_action.hpp>
namespace rublon {
class Confirmation {};
}; // namespace rublon
namespace rublon::method {
template < typename PamInfo_t = LinuxPam >
class OTP : public AuthenticationStep< OTP< PamInfo_t > > {
using base_t = AuthenticationStep< OTP< PamInfo_t > >;
const char * uri = "/api/transaction/confirmCode";
protected:
const PamInfo_t & _pamInfo;
public:
const char * name = "One Time Password";
OTP(std::string systemToken, std::string tid, const PamInfo_t & pam) : base_t(std::move(systemToken), std::move(tid)), _pamInfo{pam} {}
template < typename Hander_t >
tl::expected< Confirmation, PamAction > handle(const CoreHandlerInterface< Hander_t > & coreHandler) const {
log(Debug, "OTP fired");
const auto passcode =
_pamInfo.scan([](const char * userInput) { return std::string{userInput}; }, "Mobile TOTP from Rublon Authenticator:");
char _buffer[1024];
std::pmr::monotonic_buffer_resource mr{_buffer, 1024};
RapidJSONPMRAlloc alloc{&mr};
Document body{rapidjson::kObjectType, &alloc};
body.AddMember("systemToken", Value{this->_systemToken.c_str(), alloc}, alloc);
body.AddMember("tid", Value{this->_tid.c_str(), alloc}, alloc);
body.AddMember("vericode", Value{passcode.value().c_str(), alloc}, alloc); /// TODO proper username
auto coreResponse = coreHandler.request(uri, body);
if(coreResponse.has_value()) {
log(LogLevel::Info, "[TMP] has response, processing", __PRETTY_FUNCTION__);
const auto & rublonResponse = coreResponse.value()["result"];
std::string tid = rublonResponse["tid"].GetString();
return tl::unexpected{PamAction::accept};
} else {
return tl::unexpected{this->coreErrorHandler(coreResponse)};
}
return tl::unexpected{PamAction::accept};
}
};
} // namespace rublon::method