83 lines
3.0 KiB
C++
83 lines
3.0 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>
|
|
#include <sys/utsname.h>
|
|
|
|
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 & coreRequest, const PamInfo_t & pam) const {
|
|
auto & alloc = coreRequest.GetAllocator();
|
|
coreRequest.AddMember("username", Value{pam.username().get(), alloc}, alloc);
|
|
// coreRequest.AddMember("userEmail", "bwi@rublon.com", alloc); /// TODO proper useremail
|
|
}
|
|
|
|
static std::string osNameImpl() {
|
|
struct utsname uts;
|
|
uname(&uts);
|
|
return uts.sysname;
|
|
}
|
|
|
|
template < typename PamInfo_t >
|
|
void addParams(Document & coreRequest, const PamInfo_t & pam) const {
|
|
auto & alloc = coreRequest.GetAllocator();
|
|
|
|
Value params{rapidjson::kObjectType};
|
|
|
|
params.AddMember("userIP", Value{pam.ip().get(), alloc}, alloc);
|
|
params.AddMember("appVer", "v.0.0.1", alloc); /// TODO add version to cmake
|
|
params.AddMember("os", Value{osNameImpl().c_str(), alloc}, alloc); /// TODO add version to cmake
|
|
|
|
coreRequest.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< 2048 > alloc{};
|
|
Document body{rapidjson::kObjectType, &alloc};
|
|
|
|
this->addSystemToken(body);
|
|
this->addPamInfo(body, pam);
|
|
this->addParams(body, pam);
|
|
|
|
return coreHandler
|
|
.request(alloc, apiPath, body) //
|
|
.and_then(createMethod)
|
|
.or_else(handleInitError);
|
|
}
|
|
};
|
|
} // namespace rublon
|