Skip to content

Commit

Permalink
refactor: login
Browse files Browse the repository at this point in the history
  • Loading branch information
huerni committed Nov 21, 2024
1 parent 3ea910b commit a9b1f63
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 81 deletions.
16 changes: 12 additions & 4 deletions protos/Crane.proto
Original file line number Diff line number Diff line change
Expand Up @@ -754,7 +754,7 @@ message LoginRequest {
message LoginReply {
bool ok = 1;
string token = 2;
string reason = 3;
ErrCode reason = 3;
}

// Todo: Divide service into two parts: one for Craned and one for Crun
Expand All @@ -779,10 +779,18 @@ service CraneCtld {
rpc AddUser(AddUserRequest) returns (AddUserReply);
rpc AddQos(AddQosRequest) returns (AddQosReply);

rpc DeleteEntity(DeleteEntityRequest) returns (DeleteEntityReply);
rpc DeleteAccount(DeleteAccountRequest) returns (DeleteAccountReply);
rpc DeleteUser(DeleteUserRequest) returns (DeleteUserReply);
rpc DeleteQos(DeleteQosRequest) returns (DeleteQosReply);

rpc QueryAccountInfo(QueryAccountInfoRequest) returns (QueryAccountInfoReply);
rpc QueryUserInfo(QueryUserInfoRequest) returns (QueryUserInfoReply);
rpc QueryQosInfo(QueryQosInfoRequest) returns (QueryQosInfoReply);

rpc QueryEntityInfo(QueryEntityInfoRequest) returns (QueryEntityInfoReply);
rpc ModifyEntity(ModifyEntityRequest) returns (ModifyEntityReply);
rpc ModifyAccount(ModifyAccountRequest) returns (ModifyAccountReply);
rpc ModifyUser(ModifyUserRequest) returns (ModifyUserReply);
rpc ModifyQos(ModifyQosRequest) returns (ModifyQosReply);

rpc BlockAccountOrUser(BlockAccountOrUserRequest) returns (BlockAccountOrUserReply);

/* RPCs called from cinfo */
Expand Down
61 changes: 31 additions & 30 deletions protos/PublicDefs.proto
Original file line number Diff line number Diff line change
Expand Up @@ -329,36 +329,37 @@ enum ErrCode {
ERR_INVALID_ADMIN_LEVEL = 10007;
ERR_USER_ACCOUNT_MISMATCH = 10008;
ERR_NO_ACCOUNT_SPECIFIED = 10009;

ERR_INVALID_ACCOUNT = 10010;
ERR_DUPLICATE_ACCOUNT = 10011;
ERR_INVALID_PARENTACCOUNT = 10012;
ERR_DELETE_ACCOUNT = 10013;

ERR_INVALID_PARTITION = 10014;
ERR_ALLOWED_PARTITION = 10015;
ERR_DUPLICATE_PARTITION = 10016;
ERR_PARENT_ALLOWED_PARTITION = 10017;
ERR_USER_EMPTY_PARTITION = 10018;
ERR_CHILD_HAS_PARTITION = 10019;

ERR_INVALID_QOS = 10020;
ERR_DB_DUPLICATE_QOS = 10021;
ERR_DELETE_QOS = 10022;
ERR_CONVERT_TO_INTERGER = 10023;
ERR_TIME_LIMIT = 10024;
ERR_ALLOWED_QOS = 10025;
ERR_DUPLICATE_QOS = 10026;
ERR_PARENT_ALLOWED_QOS = 10027;
ERR_SET_ALLOWED_QOS = 10028;
ERR_ALLOWED_DEFAULT_QOS = 10029;
ERR_DUPLICATE_DEFAULT_QOS = 10030;
ERR_CHILD_HAS_DEFAULT_QOS = 10031;
ERR_SET_ACCOUNT_QOS = 10032;
ERR_SET_DEFAULT_QOS = 10033;
ERR_IS_DEFAULT_QOS = 10034;

ERR_UPDATE_DATABASE = 10035;
ERR_PASSWORD_MISMATCH = 100010;

ERR_INVALID_ACCOUNT = 10011;
ERR_DUPLICATE_ACCOUNT = 10012;
ERR_INVALID_PARENTACCOUNT = 10013;
ERR_DELETE_ACCOUNT = 10014;

ERR_INVALID_PARTITION = 10015;
ERR_ALLOWED_PARTITION = 10016;
ERR_DUPLICATE_PARTITION = 10017;
ERR_PARENT_ALLOWED_PARTITION = 10018;
ERR_USER_EMPTY_PARTITION = 10019;
ERR_CHILD_HAS_PARTITION = 10020;

ERR_INVALID_QOS = 10021;
ERR_DB_DUPLICATE_QOS = 10022;
ERR_DELETE_QOS = 10023;
ERR_CONVERT_TO_INTERGER = 10024;
ERR_TIME_LIMIT = 10025;
ERR_ALLOWED_QOS = 10026;
ERR_DUPLICATE_QOS = 10027;
ERR_PARENT_ALLOWED_QOS = 10028;
ERR_SET_ALLOWED_QOS = 10029;
ERR_ALLOWED_DEFAULT_QOS = 10030;
ERR_DUPLICATE_DEFAULT_QOS = 10031;
ERR_CHILD_HAS_DEFAULT_QOS = 10032;
ERR_SET_ACCOUNT_QOS = 10033;
ERR_SET_DEFAULT_QOS = 10034;
ERR_IS_DEFAULT_QOS = 10035;

ERR_UPDATE_DATABASE = 10036;

ERR_GENERIC_FAILURE = 10100;
ERR_NO_RESOURCE = 10101;
Expand Down
45 changes: 7 additions & 38 deletions src/CraneCtld/AccountManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

#include "AccountManager.h"

#include "CtldPublicDefs.h"
#include "crane/Jwt.h"
#include "crane/PasswordEntry.h"
#include "protos/PublicDefs.pb.h"
Expand All @@ -28,53 +27,23 @@ namespace Ctld {

AccountManager::AccountManager() { InitDataMap_(); }

AccountManager::Result AccountManager::Login(uint32_t uid,
const std::string& password) {
AccountManager::CraneExpected<std::string> AccountManager::Login(
uint32_t uid, const std::string& password) {
util::read_lock_guard user_guard(m_rw_user_mutex_);

PasswordEntry entry(uid);
if (!entry.Valid()) {
return Result{false, fmt::format("Uid {} not existed", uid)};
}
auto user_result = GetUserInfoByUidNoLock_(uid);
if (!user_result) return std::unexpected(user_result.error());
const User* user = user_result.value();

const User* user = GetExistedUserInfoNoLock_(entry.Username());
if (!user) {
return Result{false, "user not existed"};
}
if (password != user->password) {
return Result{false, "Incorrect password"};
return std::unexpected(CraneErrCode::ERR_PASSWORD_MISMATCH);
}
std::unordered_map<std::string, std::string> claims{
{"UID", std::to_string(uid)}};
const std::string& token =
util::GenerateToken(g_config.ListenConf.JwtSecretContent, claims);

return Result{true, token};
}

AccountManager::Result AccountManager::Login(uint32_t uid,
const std::string& password) {
util::read_lock_guard user_guard(m_rw_user_mutex_);

PasswordEntry entry(uid);
if (!entry.Valid()) {
return Result{false, fmt::format("Uid {} not existed", uid)};
}

const User* user = GetExistedUserInfoNoLock_(entry.Username());
if (!user) {
return Result{false, "user not existed"};
}

if (password != user->password) {
return Result{false, "Incorrect password"};
}
std::unordered_map<std::string, std::string> claims{
{"UID", std::to_string(uid)}};
const std::string& token =
util::GenerateToken(g_config.JwtSecretContent, claims);

return Result{true, token};
return token;
}

AccountManager::CraneExpected<void> AccountManager::AddUser(
Expand Down
2 changes: 1 addition & 1 deletion src/CraneCtld/AccountManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class AccountManager {

~AccountManager() = default;

Result Login(uint32_t uid, const std::string& password);
CraneExpected<std::string> Login(uint32_t uid, const std::string& password);

CraneExpected<void> AddUser(uint32_t uid, const User& new_user);

Expand Down
12 changes: 6 additions & 6 deletions src/CraneCtld/RpcService/CtldGrpcServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -252,14 +252,14 @@ grpc::Status CraneCtldServiceImpl::QueryTasksInfo(
grpc::Status CraneCtldServiceImpl::Login(
grpc::ServerContext *context, const crane::grpc::LoginRequest *request,
crane::grpc::LoginReply *response) {
AccountManager::Result result =
g_account_manager->Login(request->uid(), request->password());
auto result = g_account_manager->Login(request->uid(), request->password());

response->set_ok(result.ok);
if (result.ok) {
response->set_token(result.reason);
if (result) {
response->set_ok(true);
response->set_token(result.value());
} else {
response->set_reason(result.reason);
response->set_ok(false);
response->set_reason(result.error());
}
return grpc::Status::OK;
}
Expand Down
4 changes: 2 additions & 2 deletions src/CraneCtld/TaskScheduler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1068,8 +1068,8 @@ TaskScheduler::SubmitTaskToScheduler(std::unique_ptr<TaskInCtld> task) {
task->Username(), task->partition_id, task->account));
}

auto enable_res =
g_account_manager->CheckEnableState(task->account, task->Username());
auto enable_res = g_account_manager->CheckIfUserOfAccountIsEnabled(
task->Username(), task->account);
if (enable_res.has_error()) {
return result::fail(enable_res.error());
}
Expand Down

0 comments on commit a9b1f63

Please sign in to comment.