Skip to content

Commit

Permalink
auth: bit of cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
PaideiaDilemma committed Dec 9, 2024
1 parent 4cd9891 commit a985218
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 43 deletions.
44 changes: 26 additions & 18 deletions src/auth/Auth.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ void CAuth::start() {
}
}

void CAuth::submitInput(const std::string& input) {
for (const auto& i : m_vImpls) {
i->handleInput(input);
}
}

bool CAuth::isAuthenticated() {
for (const auto& i : m_vImpls) {
if (i->isAuthenticated())
Expand All @@ -30,42 +36,44 @@ bool CAuth::isAuthenticated() {
return false;
}

void CAuth::submitInput(const std::string& input) {
bool CAuth::checkWaiting() {
for (const auto& i : m_vImpls) {
i->handleInput(input);
if (i->checkWaiting())
return true;
}

return false;
}

std::optional<std::string> CAuth::getLastFailText() {
std::string CAuth::getInlineFeedback() {
for (const auto& i : m_vImpls) {
const auto FAIL = i->getLastFailText();
if (FAIL.has_value())
return FAIL;
const auto FEEDBACK = (m_bDisplayFailText) ? i->getLastFailText() : i->getLastPrompt();
if (FEEDBACK.has_value())
return FEEDBACK.value();
}

return std::nullopt;
return "Ups, empty authentication feedack";
}

std::optional<std::string> CAuth::getLastPrompt() {
std::optional<std::string> CAuth::getFailText(eAuthImplementations implType) {
for (const auto& i : m_vImpls) {
const auto PROMPT = i->getLastPrompt();
if (PROMPT.has_value())
return PROMPT;
if (i->getImplType() == implType) {
return i->getLastFailText();
}
}

return std::nullopt;
}

bool CAuth::checkWaiting() {
std::optional<std::string> CAuth::getPrompt(eAuthImplementations implType) {
for (const auto& i : m_vImpls) {
if (i->checkWaiting())
return true;
if (i->getImplType() == implType) {
return i->getLastPrompt();
}
}

return false;
return std::nullopt;
}

std::shared_ptr<IAuthImplementation> CAuth::getImpl(const eAuthImplementations implType) {
std::shared_ptr<IAuthImplementation> CAuth::getImpl(eAuthImplementations implType) {
for (const auto& i : m_vImpls) {
if (i->getImplType() == implType)
return i;
Expand Down
25 changes: 15 additions & 10 deletions src/auth/Auth.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ class IAuthImplementation {

virtual eAuthImplementations getImplType() = 0;
virtual void init() = 0;
virtual bool isAuthenticated() = 0;
virtual void handleInput(const std::string& input) = 0;
virtual bool isAuthenticated() = 0;
virtual bool checkWaiting() = 0;
virtual std::optional<std::string> getLastFailText() = 0;
virtual std::optional<std::string> getLastPrompt() = 0;
virtual bool checkWaiting() = 0;
virtual void terminate() = 0;

friend class CAuth;
Expand All @@ -29,15 +29,20 @@ class CAuth {
public:
CAuth();

void start();
void start();

void submitInput(const std::string& input);
bool isAuthenticated();
bool checkWaiting();

// Used by the PasswordInput field. We are constraint to a single line for the authentication feedback there.
// Based on m_bDisplayFailText, this will return either the fail text or the prompt. This result can also be influence by the auth configuration.
std::string getInlineFeedback();

std::optional<std::string> getFailText(eAuthImplementations implType);
std::optional<std::string> getPrompt(eAuthImplementations implType);

bool isAuthenticated();
void submitInput(const std::string& input);
std::optional<std::string> getLastFailText();
std::optional<std::string> getLastPrompt();
std::vector<int> getPollFDs();
bool checkWaiting();
std::shared_ptr<IAuthImplementation> getImpl(const eAuthImplementations implType);
std::shared_ptr<IAuthImplementation> getImpl(eAuthImplementations implType);

void terminate();

Expand Down
4 changes: 2 additions & 2 deletions src/auth/Fingerprint.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ class CFingerprint : public IAuthImplementation {
return AUTH_IMPL_FINGERPRINT;
}
virtual void init();
virtual bool isAuthenticated();
virtual void handleInput(const std::string& input);
virtual bool isAuthenticated();
virtual bool checkWaiting();
virtual std::optional<std::string> getLastFailText();
virtual std::optional<std::string> getLastPrompt();
virtual bool checkWaiting();
virtual void terminate();

std::shared_ptr<sdbus::IConnection> getConnection();
Expand Down
4 changes: 2 additions & 2 deletions src/auth/Pam.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ class CPam : public IAuthImplementation {
return AUTH_IMPL_PAM;
}
virtual void init();
virtual bool isAuthenticated();
virtual void handleInput(const std::string& input);
virtual bool isAuthenticated();
virtual bool checkWaiting();
virtual std::optional<std::string> getLastFailText();
virtual std::optional<std::string> getLastPrompt();
virtual bool checkWaiting();
virtual void terminate();

private:
Expand Down
17 changes: 7 additions & 10 deletions src/renderer/widgets/IWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,14 +170,14 @@ IWidget::SFormatResult IWidget::formatString(std::string in) {
}

if (in.contains("$FAIL")) {
const auto FAIL = g_pAuth->getLastFailText();
replaceInString(in, "$FAIL", FAIL.has_value() ? FAIL.value() : "");
const auto FAIL = g_pAuth->getFailText(AUTH_IMPL_PAM);
replaceInString(in, "$FAIL", FAIL.value_or(""));
result.allowForceUpdate = true;
}

if (in.contains("$PROMPT")) {
const auto PROMPT = g_pAuth->getLastPrompt();
replaceInString(in, "$PROMPT", PROMPT.has_value() ? PROMPT.value() : "");
const auto PROMPT = g_pAuth->getPrompt(AUTH_IMPL_PAM);
replaceInString(in, "$PROMPT", PROMPT.value_or(""));
result.allowForceUpdate = true;
}

Expand All @@ -192,12 +192,9 @@ IWidget::SFormatResult IWidget::formatString(std::string in) {
}

if (in.contains("$FPRINTMESSAGE")) {
const auto fingerprintAuth = g_pAuth->getImpl(AUTH_IMPL_FINGERPRINT);
if (fingerprintAuth) {
const auto FPRINTMESSAGE = fingerprintAuth->getLastFailText();
replaceInString(in, "$FPRINTMESSAGE", FPRINTMESSAGE.has_value() ? FPRINTMESSAGE.value() : "");
result.allowForceUpdate = true;
}
const auto FPRINTMESSAGE = g_pAuth->getFailText(AUTH_IMPL_FINGERPRINT);
replaceInString(in, "$FPRINTMESSAGE", FPRINTMESSAGE.value_or(""));
result.allowForceUpdate = true;
}

if (in.starts_with("cmd[") && in.contains("]")) {
Expand Down
2 changes: 1 addition & 1 deletion src/renderer/widgets/PasswordInputField.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ void CPasswordInputField::updatePlaceholder() {
return;
}

const auto AUTHFEEDBACK = g_pAuth->m_bDisplayFailText ? g_pAuth->getLastFailText().value_or("Ups, no fail text?") : g_pAuth->getLastPrompt().value_or("Ups, no prompt?");
const auto AUTHFEEDBACK = g_pAuth->getInlineFeedback();
const auto ALLOWCOLORSWAP = outThick == 0 && colorConfig.swapFont;

if (!ALLOWCOLORSWAP && placeholder.lastAuthFeedback == AUTHFEEDBACK && g_pAuth->m_iFailedAttempts == placeholder.failedAttempts)
Expand Down

0 comments on commit a985218

Please sign in to comment.