SPARK-1901 Rename UserService->AuthService
This commit is contained in:
1 parent
c7097faba2
commit
d6c1285ada
20 files changed
+57
-40
No files matched your search
@@ -0,0 +1,90 @@
|
||||
#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.result()->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.result()->users()) {
|
||||
if (user.name() == req.login() && user.password() == req.password() && user.is_present()) {
|
||||
resp.set_is_valid(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());
|
||||
resp.mutable_token()->CopyFrom(api::auth::generate_token(user));
|
||||
|
||||
api::user::TokenPayload payload;
|
||||
api::auth::decode_token(resp.token().token(), &payload);
|
||||
resp.set_can_create(payload.wheel() || user.can_create());
|
||||
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);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
|
||||
} }
|
||||
Reference in new issue
Block a user