rublon-ssh/PAM/ssh/include/rublon/init.hpp
Bartosz Wieczorek 96b11a1b74 refactor
2023-08-01 13:26:42 +02:00

79 lines
3.2 KiB
C++

#pragma once
#include <rublon/json.hpp>
#include <rublon/pam.hpp>
#include <rublon/authentication_step_interface.hpp>
#include <rublon/configuration.hpp>
#include <rublon/method/method_factory.hpp>
namespace rublon{
class Verify{};
}
namespace rublon {
template < class MethodSelect_t = MethodSelect, typename Pam_t = LinuxPam >
class Init : public AuthenticationStep< Init< MethodSelect_t, Pam_t > > {
const char * apiPath = "/api/transaction/init";
const std::string & _systemToken;
protected:
const Pam_t & _pamInfo;
public:
const char * name = "Initialization";
Init(const Pam_t & pamHandler, const rublon::Configuration & config)
: _systemToken{config.parameters.systemToken}, _pamInfo{pamHandler} {}
/// TODO add core handler interface
template < typename Hander_t >
tl::expected< MethodSelect_t, PamAction > handle(const CoreHandlerInterface< Hander_t > & coreHandler) const {
char _buffer[1024];
std::pmr::monotonic_buffer_resource mr{_buffer, 1024};
RapidJSONPMRAlloc alloc{&mr};
Document body{rapidjson::kObjectType, &alloc};
body.AddMember("systemToken", Value{_systemToken.c_str(), alloc}, alloc);
body.AddMember("username", Value{_pamInfo.username().get(), alloc}, alloc);
body.AddMember("userEmail", "bwi@rublon.com", alloc); /// TODO proper username
Value params{rapidjson::kObjectType};
params.AddMember("userIP", Value{_pamInfo.ip().get(), alloc}, alloc);
params.AddMember("appVer", "v.1.6", alloc); /// TODO add version to cmake
params.AddMember("os", "Ubuntu 23.04", alloc); /// TODO add version to cmake
body.AddMember("params", std::move(params), alloc);
auto coreResponse = coreHandler.request(apiPath, 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 MethodSelect_t{_systemToken, tid, rublonResponse["methods"]};
} else {
// mostly connectio errors
switch(coreResponse.error().errorClass) {
case CoreHandlerError::ErrorClass::BadSigature:
log(LogLevel::Error, "ErrorClass::BadSigature");
return tl::unexpected{PamAction::decline};
case CoreHandlerError::ErrorClass::CoreException: /// TODO exception handling
log(LogLevel::Error, "ErrorClass::CoreException");
return tl::unexpected{PamAction::decline}; /// TODO accept?
case CoreHandlerError::ErrorClass::ConnectionError:
log(LogLevel::Error, "ErrorClass::ConnectionError");
return tl::unexpected{PamAction::decline}; /// TODO decline?
case CoreHandlerError::ErrorClass::BrokenData:
log(LogLevel::Error, "ErrorClass::BrokenData");
return tl::unexpected{PamAction::decline};
}
}
return tl::unexpected{PamAction::decline};
}
};
} // namespace rublon