rublon-ssh/PAM/ssh/include/rublon/init.hpp
rublon-bwi 6a3882fa47
Bwi/v2.0.2 rc1 (#10)
* Remove unused options from rublon default config

* Remove safe|secure options

* Allow 9 digits long passcode for passcode bypass

* Change name of 'Mobile Passcode' to 'Passcode'

* Do not display any prompt when user is waiting

* remove unused alloca.h header

* Add autopushPrompt option

* Change name OTP method

* Change enrolement message handling

* ad static string ctor

* Addded postrm script

* Rename 01_rublon_ssh.conf to 01-rublon-ssh.conf

* restart sshd service after rublon package instalation

* Fix sshd not restarting bug on ubuntu 24.04

* disable logging from websocket-io

* change package name to match old package name

* Fix compilation issue when using non owning ptr

* Set version to 2.0.0
2024-06-17 08:57:26 +02:00

139 lines
5.3 KiB
C++
Executable File

#pragma once
#include <rublon/json.hpp>
#include <rublon/pam.hpp>
#include <rublon/session.hpp>
#include <rublon/authentication_step_interface.hpp>
#include <rublon/configuration.hpp>
#include <rublon/method/method_select.hpp>
#include <string>
#include <sys/utsname.h>
namespace rublon {
/// TODO move to some other place
using PAM = LinuxPam;
using Session = SessionBase< PAM, CoreHandler< CURL > >;
using Transaction = TransactionBase< Session >;
} // namespace rublon
extern std::string g_tid;
namespace rublon {
template < class MethodSelect_t = MethodSelect >
class Init : public AuthenticationStep {
using base_t = AuthenticationStep;
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();
g_tid = tid; ///TODO set tid in session
return MethodSelect_t{this->_systemToken, tid, rublonResponse["methods"], _session.config().prompt, _session.config().autopushPrompt};
}
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);
}
template < typename PamInfo_t >
void addParams(Document & coreRequest, const PamInfo_t & pam) const {
memory::MonotonicStackResource< 512 > stackResource;
std::pmr::string releaseInfo{&stackResource};
auto & alloc = coreRequest.GetAllocator();
const auto os = details::osName(&stackResource);
if(os == "unknown") {
log(LogLevel::Warning, "No OS information available");
}
Value osNamePretty{os.data(), static_cast< unsigned >(os.size()), alloc};
Value ip{pam.ip().get(), alloc};
Value params{rapidjson::kObjectType};
params.AddMember("userIP", ip, alloc);
params.AddMember("appVer", "2.0.0", alloc); /// TODO add version to cmake
params.AddMember("os", osNamePretty, alloc);
coreRequest.AddMember("params", std::move(params), alloc);
}
template<typename Pam_T>
tl::expected< std::reference_wrapper< const Document >, Error > checkEnrolement(const Document & coreResponse, const Pam_T pam) const {
using namespace std::string_view_literals;
const auto & resp = coreResponse;
///TODO refactor this
if(resp.HasMember("result") and resp["result"].IsObject() and resp["result"].HasMember("status")) {
const auto & status = resp["result"]["status"].GetString();
log(LogLevel::Warning, "Got enrolement message with stats %s", status);
if(status == "pending"sv ) {
if(resp["result"].HasMember("webURI")){
const auto & weburi = resp["result"]["webURI"].GetString();
pam.print("Visit %s", weburi);
}
}
else if(status == "waiting"sv){
return tl::unexpected{Error{RublonAuthenticationInterrupt{RublonAuthenticationInterrupt::UserWaiting}}};
}
else if(status == "denied"sv) {
return tl::unexpected{Error{RublonAuthenticationInterrupt{RublonAuthenticationInterrupt::UserDenied}}};
}
}
return coreResponse;
}
public:
const char * _name = "Initialization";
const Session & _session;
Init(const Session & session) : base_t(std::string{session.config().systemToken.data(), 32}, ""), _session{session} {
log(LogLevel::Debug, "Init");
}
template < typename Hander_t, typename PamInfo_t = LinuxPam >
[[deprecated]] 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 checkEnrolement = [&](const auto & coreResponse) { return this->checkEnrolement(coreResponse,pam); };
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(checkEnrolement)
.and_then(createMethod);
}
// tl::expected< Transaction, Error > openTransaction() const {
// const auto createTransaction = [&](const auto & coreResponse) { return this->createTransaction(coreResponse); };
// const auto checkEnrolement = [&](const auto & coreResponse) { return this->checkEnrolement(coreResponse, _session.pam()); };
// RapidJSONPMRStackAlloc< 2048 > alloc{};
// Document body{rapidjson::kObjectType, &alloc};
// this->addSystemToken(body);
// this->addPamInfo(body, _session.pam());
// this->addParams(body, _session.pam());
// return _session.coreHandler()
// .request(alloc, apiPath, body) //
// .and_then(checkEnrolement)
// .and_then(createTransaction);
// }
};
} // namespace rublon