rublon-ssh/PAM/ssh/include/rublon/init.hpp
rublon-bwi 51b14c57d2
Bwi/memory management (#2)
Improve memory management
2023-09-21 16:52:20 +02:00

77 lines
2.7 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_select.hpp>
namespace rublon {
class Verify {};
} // namespace rublon
namespace rublon {
template < class MethodSelect_t = MethodSelect >
class Init : public AuthenticationStep< Init< MethodSelect_t > > {
using base_t = AuthenticationStep< Init< MethodSelect_t > >;
const char * apiPath = "/api/transaction/init";
tl::expected< MethodSelect_t, Error > createMethod(const Document & coreResponse) const {
const auto & rublonResponse = coreResponse["result"];
std::string tid = rublonResponse["tid"].GetString();
return MethodSelect_t{this->_systemToken, tid, rublonResponse["methods"]};
}
tl::expected< MethodSelect_t, Error > handleInitError(const Error & error) const {
return tl::unexpected{this->coreErrorHandler(error)};
}
template < typename PamInfo_t >
void addPamInfo(Document & body, const PamInfo_t & pam) const {
auto & alloc = body.GetAllocator();
body.AddMember("username", Value{pam.username().get(), alloc}, alloc);
body.AddMember("userEmail", "bwi@rublon.com", alloc); /// TODO proper useremail
}
template < typename PamInfo_t >
void addParams(Document & body, const PamInfo_t & pam) const {
auto & alloc = body.GetAllocator();
Value params{rapidjson::kObjectType};
params.AddMember("userIP", Value{pam.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);
}
public:
const char * name = "Initialization";
Init(const rublon::Configuration & config) : base_t(config.parameters.systemToken, "") {}
/// TODO add core handler interface
template < typename Hander_t, typename PamInfo_t = LinuxPam >
tl::expected< MethodSelect_t, Error > handle(const CoreHandlerInterface< Hander_t > & coreHandler, const PamInfo_t & pam) const {
const auto createMethod = [&](const auto & coreResponse) { return this->createMethod(coreResponse); };
const auto handleInitError = [&](const auto & error) { return this->handleInitError(error); };
RapidJSONPMRStackAlloc< 1024 > alloc{};
Document body{rapidjson::kObjectType, &alloc};
this->addSystemToken(body);
this->addPamInfo(body, pam);
this->addParams(body, pam);
return coreHandler
.request(apiPath, body) //
.and_then(createMethod)
.or_else(handleInitError);
}
};
} // namespace rublon