94 lines
3.2 KiB
C++
94 lines
3.2 KiB
C++
#include "AuthServiceServer.h"
|
|
|
|
#include "lib/persistence/SqliteStore.h"
|
|
#include "lib/logging/logging.h"
|
|
#include "lib/api/services/AuthService.pb.h"
|
|
#include "lib/api/services/AdminService.client.pb.h"
|
|
#include "lib/utils/PBUtils.h"
|
|
#include "lib/utils/AuthToken.h"
|
|
#include "lib/api/service_helpers/AdminServiceUtils.h"
|
|
|
|
#include <boost/tokenizer.hpp>
|
|
#include <fmt/format.h>
|
|
#include <map>
|
|
#include <set>
|
|
|
|
namespace bts
|
|
{
|
|
namespace user
|
|
{
|
|
|
|
const api::VoidMessage vod;
|
|
struct AuthServiceServer::PImpl
|
|
{
|
|
api::admin::AdminService admin;
|
|
|
|
PImpl(bts::api::ServiceContext& context) :
|
|
admin(context.locate<api::admin::AdminService>()) { }
|
|
};
|
|
|
|
AuthServiceServer::AuthServiceServer (bts::api::ServiceContext& context) : AuthServiceBase(context), me (new PImpl(context)) {
|
|
LOG(User,Warn,"Starting user service");
|
|
}
|
|
|
|
AuthServiceServer::~AuthServiceServer () {}
|
|
|
|
void AuthServiceServer::get_users(const api::user::GetUsersRequest& ,
|
|
const std::function<void(const api::user::GetUsersResponse&)>& respond)
|
|
{
|
|
me->admin.get_admin_data(vod, [respond](const api::Result<bts::api::admin::AdminData>& admin_data) {
|
|
api::user::GetUsersResponse resp;
|
|
if (admin_data.success()) {
|
|
for (const auto& inuser : admin_data.value()->users()) {
|
|
if (inuser.is_present()) {
|
|
api::user::User& outuser = *resp.add_users();
|
|
outuser.set_login(inuser.name());
|
|
outuser.set_id(inuser.id());
|
|
}
|
|
}
|
|
}
|
|
else {
|
|
resp.set_error("Invalid response from AdminService: " + std::string(admin_data.error_message()));
|
|
}
|
|
respond(resp);
|
|
});
|
|
}
|
|
|
|
void AuthServiceServer::authenticate_login(const api::user::AuthenticateLoginRequest& req,
|
|
const std::function<void(const api::user::AuthenticateLoginResponse&)>& respond)
|
|
{
|
|
me->admin.get_admin_data(vod, [=](const api::Result<bts::api::admin::AdminData>& admin_data) {
|
|
api::user::AuthenticateLoginResponse resp;
|
|
resp.set_is_valid(false);
|
|
if (admin_data.success()) {
|
|
for (const auto& user : admin_data.value()->users()) {
|
|
if (user.name() == req.login() && user.password() == req.password() && user.is_present()) {
|
|
std::string msg;
|
|
resp.mutable_token()->CopyFrom(api::auth::generate_token(user, &msg));
|
|
api::user::TokenPayload payload;
|
|
api::auth::decode_token(resp.token(), &payload);
|
|
|
|
resp.set_can_create(payload.wheel() || user.can_create());
|
|
resp.set_is_valid(payload.wheel() || user.is_active());
|
|
if(!resp.is_valid())
|
|
resp.set_reason("User login privledges restricted, please contact your risk manager");
|
|
resp.set_user_id(user.id());
|
|
if(msg != "")
|
|
resp.set_reason(msg);
|
|
respond(resp);
|
|
return;
|
|
}
|
|
}
|
|
resp.set_reason("Invalid Username/Password");
|
|
}
|
|
else {
|
|
resp.set_reason("Invalid response from AdminService: " + std::string(admin_data.error_message()));
|
|
}
|
|
respond(resp);
|
|
});
|
|
}
|
|
|
|
|
|
|
|
} }
|