Skip to content

Commit 0913a5c

Browse files
authored
Add LastSuccessfulLogin time point to TSidRecord (#13254)
1 parent ceed259 commit 0913a5c

File tree

5 files changed

+17
-9
lines changed

5 files changed

+17
-9
lines changed

ydb/core/tx/schemeshard/schemeshard__init.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3884,6 +3884,7 @@ struct TSchemeShard::TTxInit : public TTransactionBase<TSchemeShard> {
38843884
sid.SetType(rowset.GetValue<Schema::LoginSids::SidType>());
38853885
sid.SetHash(rowset.GetValue<Schema::LoginSids::SidHash>());
38863886
sid.SetCreatedAt(rowset.GetValueOrDefault<Schema::LoginSids::CreatedAt>());
3887+
sid.SetLastSuccessfulLogin(rowset.GetValue<Schema::LoginSids::LastSuccessfulAttempt>());
38873888
sidIndex[sid.name()] = securityState.SidsSize() - 1;
38883889
if (!rowset.Next()) {
38893890
return false;

ydb/core/tx/schemeshard/schemeshard__login.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,8 +204,8 @@ struct TSchemeShard::TTxLogin : TTransactionBase<TSchemeShard> {
204204
ResetFailedAttemptCount(loginRequest, db);
205205
}
206206

207-
void HandleLoginAuthSuccess(const NLogin::TLoginProvider::TLoginUserRequest& loginRequest, const NLogin::TLoginProvider::TLoginUserResponse& /* loginResponse */, NIceDb::TNiceDb& db) {
208-
db.Table<Schema::LoginSids>().Key(loginRequest.User).Update<Schema::LoginSids::LastSuccessfulAttempt, Schema::LoginSids::FailedAttemptCount>(TAppData::TimeProvider->Now().MicroSeconds(), Schema::LoginSids::FailedAttemptCount::Default);
207+
void HandleLoginAuthSuccess(const NLogin::TLoginProvider::TLoginUserRequest& loginRequest, const NLogin::TLoginProvider::TLoginUserResponse& loginResponse, NIceDb::TNiceDb& db) {
208+
db.Table<Schema::LoginSids>().Key(loginRequest.User).Update<Schema::LoginSids::LastSuccessfulAttempt, Schema::LoginSids::FailedAttemptCount>(loginResponse.LoginAttemptTime, Schema::LoginSids::FailedAttemptCount::Default);
209209
}
210210

211211
void HandleLoginAuthInvalidPassword(const NLogin::TLoginProvider::TLoginUserRequest& loginRequest, const NLogin::TLoginProvider::TLoginUserResponse& /* loginResponse */, NIceDb::TNiceDb& db) {

ydb/library/login/login.cpp

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,16 @@ std::vector<TString> TLoginProvider::GetGroupsMembership(const TString& member)
318318
}
319319

320320
TLoginProvider::TLoginUserResponse TLoginProvider::LoginUser(const TLoginUserRequest& request) {
321+
auto now = std::chrono::system_clock::now();
321322
TLoginUserResponse response;
323+
response.LoginAttemptTime = std::chrono::time_point_cast<std::chrono::microseconds>(now).time_since_epoch().count();
324+
325+
if (Keys.empty() || Keys.back().PrivateKey.empty()) {
326+
response.Status = TLoginUserResponse::EStatus::UNAVAILABLE_KEY;
327+
response.Error = "No key to generate token";
328+
return response;
329+
}
330+
322331
if (!request.ExternalAuth) {
323332
auto itUser = Sids.find(request.User);
324333
if (itUser == Sids.end() || itUser->second.Type != ESidType::USER) {
@@ -332,22 +341,16 @@ TLoginProvider::TLoginUserResponse TLoginProvider::LoginUser(const TLoginUserReq
332341
response.Error = "Invalid password";
333342
return response;
334343
}
335-
}
336344

337-
if (Keys.empty() || Keys.back().PrivateKey.empty()) {
338-
response.Status = TLoginUserResponse::EStatus::UNAVAILABLE_KEY;
339-
response.Error = "No key to generate token";
340-
return response;
345+
itUser->second.LastSuccessfulLogin = response.LoginAttemptTime;
341346
}
342347

343348
const TKeyRecord& key = Keys.back();
344-
345349
auto keyId = ToString(key.KeyId);
346350
const auto& publicKey = key.PublicKey;
347351
const auto& privateKey = key.PrivateKey;
348352

349353
// encode jwt
350-
auto now = std::chrono::system_clock::now();
351354
auto expires_at = now + MAX_TOKEN_EXPIRE_TIME;
352355
if (request.Options.ExpiresAfter != std::chrono::system_clock::duration::zero()) {
353356
expires_at = std::min(expires_at, now + request.Options.ExpiresAfter);
@@ -668,6 +671,7 @@ void TLoginProvider::UpdateSecurityState(const NLoginProto::TSecurityState& stat
668671
sid.Type = pbSid.GetType();
669672
sid.Name = pbSid.GetName();
670673
sid.Hash = pbSid.GetHash();
674+
sid.LastSuccessfulLogin = pbSid.GetLastSuccessfulLogin();
671675
for (const auto& pbSubSid : pbSid.GetMembers()) {
672676
sid.Members.emplace(pbSubSid);
673677
ChildToParentIndex[pbSubSid].emplace(sid.Name);

ydb/library/login/login.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ class TLoginProvider {
5959
TString Token;
6060
TString SanitizedToken; // Token for audit logs
6161
EStatus Status = EStatus::UNSPECIFIED;
62+
ui64 LoginAttemptTime; // microseconds
6263
};
6364

6465
struct TValidateTokenRequest : TBasicRequest {
@@ -146,6 +147,7 @@ class TLoginProvider {
146147
TString Hash;
147148
std::unordered_set<TString> Members;
148149
std::chrono::system_clock::time_point CreatedAt; // CreatedAt does not need in describe result. We will not add to security state
150+
ui64 LastSuccessfulLogin;
149151
};
150152

151153
// our current audience (database name)

ydb/library/login/protos/login.proto

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ message TSid {
2323
string Hash = 3;
2424
repeated string Members = 4;
2525
uint64 CreatedAt = 5;
26+
uint64 LastSuccessfulLogin = 6; // microseconds
2627
}
2728

2829
message TSecurityState {

0 commit comments

Comments
 (0)