76 lines
3.3 KiB
C++
76 lines
3.3 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{
|
|
template < template < typename > class MethodFactory_t = MethodFactory, typename PamInfo_t = LinuxPam >
|
|
class Init : public AuthenticationStep< Init< MethodFactory_t, PamInfo_t > > {
|
|
const char * apiPath = "/api/transaction/init";
|
|
const std::string & _systemToken;
|
|
|
|
protected:
|
|
PamInfo_t _pamInfo;
|
|
MethodFactory_t< PamInfo_t > _methodFactory;
|
|
|
|
public:
|
|
const char * name = "Initialization";
|
|
|
|
Init(pam_handle_t * pamHandler, const rublon::Configuration & config)
|
|
: _systemToken{config.parameters.systemToken}, _pamInfo{pamHandler}, _methodFactory{_pamInfo} {}
|
|
|
|
/// TODO add core handler interface
|
|
template < typename Hander_t >
|
|
tl::expected< Method, 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 httpResponse = coreHandler.request(apiPath, body);
|
|
|
|
if(httpResponse.has_value()) {
|
|
log(LogLevel::Info, "[TMP] has response, processing", __PRETTY_FUNCTION__);
|
|
const auto & rublonResponse = httpResponse.value()["result"];
|
|
std::string tid = rublonResponse["tid"].GetString();
|
|
return _methodFactory.create(rublonResponse["methods"].GetArray(), std::move(tid));
|
|
} else {
|
|
// mostly connectio errors
|
|
switch(httpResponse.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};
|
|
}
|
|
};
|
|
}
|