Commit 808f0414 authored by Lutz Justen's avatar Lutz Justen Committed by Commit Bot

Make FakeKerberosClient behave more like daemon

Changes FakeKerberosClient's internal storage from a map to a vector,
so the order in which accounts are added is preserved. Also remembers
whether the login password was used.

BUG=chromium:952240
TEST=Manually tested on Linux desktop

Change-Id: Icfb576e3f54f97cdb849b41ea6dd8fa49b3101ae
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1674084
Commit-Queue: Lutz Justen <ljusten@chromium.org>
Auto-Submit: Lutz Justen <ljusten@chromium.org>
Reviewed-by: default avatarSteven Bennetts <stevenjb@chromium.org>
Cr-Commit-Position: refs/heads/master@{#673354}
parent 7bde8d9e
file://chrome/browser/chromeos/kerberos/OWNERS
# COMPONENT: Enterprise>ActiveDirectory
......@@ -62,26 +62,31 @@ FakeKerberosClient::~FakeKerberosClient() = default;
void FakeKerberosClient::AddAccount(const kerberos::AddAccountRequest& request,
AddAccountCallback callback) {
auto it = accounts_.find(request.principal_name());
auto it = std::find(accounts_.begin(), accounts_.end(),
AccountData(request.principal_name()));
if (it != accounts_.end()) {
it->second.is_managed |= request.is_managed();
it->is_managed |= request.is_managed();
PostResponse(std::move(callback), kerberos::ERROR_DUPLICATE_PRINCIPAL_NAME);
return;
}
AccountData data;
AccountData data(request.principal_name());
data.is_managed = request.is_managed();
accounts_[request.principal_name()] = data;
accounts_.push_back(data);
PostResponse(std::move(callback), kerberos::ERROR_NONE);
}
void FakeKerberosClient::RemoveAccount(
const kerberos::RemoveAccountRequest& request,
RemoveAccountCallback callback) {
kerberos::ErrorType error = accounts_.erase(request.principal_name()) == 0
? kerberos::ERROR_UNKNOWN_PRINCIPAL_NAME
: kerberos::ERROR_NONE;
PostResponse(std::move(callback), error);
auto it = std::find(accounts_.begin(), accounts_.end(),
AccountData(request.principal_name()));
if (it == accounts_.end()) {
PostResponse(std::move(callback), kerberos::ERROR_UNKNOWN_PRINCIPAL_NAME);
return;
}
accounts_.erase(it);
PostResponse(std::move(callback), kerberos::ERROR_NONE);
}
void FakeKerberosClient::ClearAccounts(
......@@ -95,12 +100,9 @@ void FakeKerberosClient::ListAccounts(
const kerberos::ListAccountsRequest& request,
ListAccountsCallback callback) {
kerberos::ListAccountsResponse response;
for (const auto& it : accounts_) {
const std::string& principal_name = it.first;
const AccountData& data = it.second;
for (const AccountData& data : accounts_) {
kerberos::Account* account = response.add_accounts();
account->set_principal_name(principal_name);
account->set_principal_name(data.principal_name);
account->set_krb5conf(data.krb5conf);
account->set_tgt_validity_seconds(data.has_tgt ? kTgtValidity.InSeconds()
: 0);
......@@ -108,6 +110,7 @@ void FakeKerberosClient::ListAccounts(
: 0);
account->set_is_managed(data.is_managed);
account->set_password_was_remembered(!data.password.empty());
account->set_use_login_password(data.use_login_password);
}
response.set_error(kerberos::ERROR_NONE);
PostProtoResponse(std::move(callback), response);
......@@ -135,6 +138,9 @@ void FakeKerberosClient::AcquireKerberosTgt(
return;
}
// Remember whether to use the login password.
data->use_login_password = request.use_login_password();
std::string password;
if (request.use_login_password()) {
// "Retrieve" login password.
......@@ -200,10 +206,25 @@ void FakeKerberosClient::ConnectToKerberosTicketExpiringSignal(
FakeKerberosClient::AccountData* FakeKerberosClient::GetAccountData(
const std::string& principal_name) {
auto it = accounts_.find(principal_name);
if (it == accounts_.end())
return nullptr;
return &it->second;
auto it = std::find(accounts_.begin(), accounts_.end(),
AccountData(principal_name));
return it != accounts_.end() ? &*it : nullptr;
}
FakeKerberosClient::AccountData::AccountData(const std::string& principal_name)
: principal_name(principal_name) {}
FakeKerberosClient::AccountData::AccountData(const AccountData& other) =
default;
bool FakeKerberosClient::AccountData::operator==(
const AccountData& other) const {
return principal_name == other.principal_name;
}
bool FakeKerberosClient::AccountData::operator!=(
const AccountData& other) const {
return !(*this == other);
}
} // namespace chromeos
......@@ -7,7 +7,7 @@
#include <memory>
#include <string>
#include <unordered_map>
#include <vector>
#include "chromeos/dbus/kerberos/kerberos_client.h"
#include "chromeos/dbus/kerberos/kerberos_service.pb.h"
......@@ -44,6 +44,9 @@ class COMPONENT_EXPORT(CHROMEOS_DBUS) FakeKerberosClient
private:
struct AccountData {
// User principal (user@EXAMPLE.COM) that identifies this account.
std::string principal_name;
// Kerberos configuration file.
std::string krb5conf;
......@@ -53,8 +56,18 @@ class COMPONENT_EXPORT(CHROMEOS_DBUS) FakeKerberosClient
// True if the account was added by policy.
bool is_managed = false;
// True if login password was used during last AcquireKerberosTgt() call.
bool use_login_password = false;
// Remembered password, if any.
std::string password;
explicit AccountData(const std::string& principal_name);
AccountData(const AccountData& other);
// Only compares principal_name. For finding and erasing in vectors.
bool operator==(const AccountData& other) const;
bool operator!=(const AccountData& other) const;
};
// Returns the AccountData for |principal_name| if available or nullptr
......@@ -62,7 +75,7 @@ class COMPONENT_EXPORT(CHROMEOS_DBUS) FakeKerberosClient
AccountData* GetAccountData(const std::string& principal_name);
// Maps principal name (user@REALM.COM) to account data.
using AccountsMap = std::unordered_map<std::string, AccountData>;
using AccountsMap = std::vector<AccountData>;
AccountsMap accounts_;
KerberosFilesChangedCallback kerberos_files_changed_callback_;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment