Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1859)

Unified Diff: chrome/browser/chromeos/extensions/quick_unlock_private/quick_unlock_private_api.cc

Issue 2374303002: cros: Added a new function to quick unlock api for checking unfinished pins. (Closed)
Patch Set: Fixed patch set 4 errors. Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/browser/chromeos/extensions/quick_unlock_private/quick_unlock_private_api.cc
diff --git a/chrome/browser/chromeos/extensions/quick_unlock_private/quick_unlock_private_api.cc b/chrome/browser/chromeos/extensions/quick_unlock_private/quick_unlock_private_api.cc
index 50bdfb92b393bb1028d07ee2ed4c58c2058f0e0c..c70cbcc24a5e1459d22507bd2074053f9ec20ca8 100644
--- a/chrome/browser/chromeos/extensions/quick_unlock_private/quick_unlock_private_api.cc
+++ b/chrome/browser/chromeos/extensions/quick_unlock_private/quick_unlock_private_api.cc
@@ -7,8 +7,10 @@
#include "chrome/browser/chromeos/login/quick_unlock/pin_storage.h"
#include "chrome/browser/chromeos/login/quick_unlock/pin_storage_factory.h"
#include "chrome/browser/chromeos/profiles/profile_helper.h"
+#include "chrome/common/pref_names.h"
#include "chromeos/login/auth/extended_authenticator.h"
#include "chromeos/login/auth/user_context.h"
+#include "components/prefs/pref_service.h"
#include "extensions/browser/event_router.h"
namespace extensions {
@@ -16,8 +18,12 @@ namespace extensions {
namespace quick_unlock_private = api::quick_unlock_private;
namespace SetModes = quick_unlock_private::SetModes;
namespace GetActiveModes = quick_unlock_private::GetActiveModes;
+namespace IsCredentialUsable = quick_unlock_private::IsCredentialUsable;
namespace GetAvailableModes = quick_unlock_private::GetAvailableModes;
namespace OnActiveModesChanged = quick_unlock_private::OnActiveModesChanged;
+using CredentialRequirementFailure =
+ quick_unlock_private::CredentialRequirementFailure;
+using CredentialRequirement = quick_unlock_private::CredentialRequirement;
using QuickUnlockMode = quick_unlock_private::QuickUnlockMode;
using QuickUnlockModeList = std::vector<QuickUnlockMode>;
@@ -27,6 +33,9 @@ const char kModesAndCredentialsLengthMismatch[] =
"|modes| and |credentials| must have the same number of elements";
const char kMultipleModesNotSupported[] =
"At most one quick unlock mode can be active.";
+// Pins greater in length than |kPinCheckWeakThreshold| will be checked for
+// weakness.
+const int kPinCheckWeakThreshold = 2;
// Returns the active set of quick unlock modes.
QuickUnlockModeList ComputeActiveModes(Profile* profile) {
@@ -56,6 +65,84 @@ bool AreModesEqual(const QuickUnlockModeList& a, const QuickUnlockModeList& b) {
return true;
}
+// Check if a given |pin| is valid given the policies in the |profile|. If
+// |result| is a valid pointer, the failures are added to |result|. Pass a
+// null for |result| if all we want to do is check the validity of |pin|.
jdufault 2016/10/05 21:47:38 Update comment
sammiequon 2016/10/14 20:58:53 Done.
+std::vector<CredentialRequirementFailure> IsPinLengthValid(
+ const std::string& pin,
+ Profile* profile,
+ int* out_min_length,
+ int* out_max_length) {
+ std::vector<CredentialRequirementFailure> failures;
+ PrefService* pref_service = profile->GetPrefs();
+ int min_length = pref_service->GetInteger(prefs::kPinUnlockMinimumLength);
+ int max_length = pref_service->GetInteger(prefs::kPinUnlockMaximumLength);
+ if (out_min_length)
+ *out_min_length = min_length;
+ if (out_max_length)
+ *out_max_length = max_length;
+
+ // Check if the pin is shorter than the minimum specified length.
+ if (int{pin.size()} < min_length) {
jdufault 2016/10/05 21:47:37 Compilation fails without this cast? What if you d
sammiequon 2016/10/14 20:58:53 Yeah it doesn't compile without the cast both ways
+ failures.push_back(
+ CredentialRequirementFailure::CREDENTIAL_REQUIREMENT_FAILURE_TOO_SHORT);
+ }
+
+ // If the maximum lenght is nonzero and shorter than the minimum length, the
jdufault 2016/10/05 21:47:38 lenght -> length
sammiequon 2016/10/14 20:58:53 Done.
+ // maximum length is the minimum length.
+ if (max_length > 0 && max_length < min_length)
jdufault 2016/10/05 21:47:38 This check should happen before assigning out_* pa
sammiequon 2016/10/14 20:58:53 Done.
+ max_length = min_length;
+
+ // If the maximum specified length is zero, there is no maximum length.
+ // Otherwise check if the pin is longer than the maximum specified length.
+ if (max_length > 0 && int{pin.size()} > max_length) {
+ failures.push_back(
+ CredentialRequirementFailure::CREDENTIAL_REQUIREMENT_FAILURE_TOO_LONG);
+ }
+
+ return failures;
+}
+
+// Check if a given |pin| is valid given the policies in the |profile|. If
+// |result| is a valid pointer, the failures are added to |result|. Pass a
jdufault 2016/10/05 21:47:38 Update comment
sammiequon 2016/10/14 20:58:53 Done.
+// null for |result| if all we want to do is check the validity of |pin|.
+std::vector<CredentialRequirementFailure> IsPinDifficultEnough(
+ const std::string& pin,
+ Profile* profile,
+ bool* out_allow_weak_pin) {
+ std::vector<CredentialRequirementFailure> failures;
+ PrefService* pref_service = profile->GetPrefs();
+ bool allow_weak = pref_service->GetBoolean(prefs::kPinUnlockAllowWeakPins);
+ if (out_allow_weak_pin)
+ *out_allow_weak_pin = allow_weak;
+
+ // If the pin length is |kPinCheckWeakThreshold| or less, there is no need to
+ // check for same character and increasing pin.
+ if (int{pin.size()} <= kPinCheckWeakThreshold)
+ return failures;
+
+ // Check for same digits, increasing pin simutaneously.
+ bool is_same = true;
+ bool is_increasing = true;
+ bool is_decreasing = true;
+ for (int i = 1; i < int{pin.length()}; ++i) {
+ const char previous = pin[i - 1];
+ const char current = pin[i];
+
+ is_same = is_same && (current == previous);
+ is_increasing = is_increasing && (current == previous + 1);
+ is_decreasing = is_decreasing && (current == previous - 1);
+ }
+
+ // Pin is considered weak if either of these is met.
+ if ((is_same || is_increasing || is_decreasing)) {
+ failures.push_back(
+ CredentialRequirementFailure::CREDENTIAL_REQUIREMENT_FAILURE_TOO_WEAK);
+ }
+
+ return failures;
+}
+
} // namespace
// quickUnlockPrivate.getAvailableModes
@@ -93,6 +180,50 @@ QuickUnlockPrivateGetActiveModesFunction::Run() {
return RespondNow(ArgumentList(GetActiveModes::Results::Create(modes)));
}
+// quickUnlockPrivate.isCredentialUsable
+
+QuickUnlockPrivateIsCredentialUsableFunction::
+ QuickUnlockPrivateIsCredentialUsableFunction()
+ : chrome_details_(this) {}
+
+QuickUnlockPrivateIsCredentialUsableFunction::
+ ~QuickUnlockPrivateIsCredentialUsableFunction() {}
+
+ExtensionFunction::ResponseAction
+QuickUnlockPrivateIsCredentialUsableFunction::Run() {
+ params_ = IsCredentialUsable::Params::Create(*args_);
+ EXTENSION_FUNCTION_VALIDATE(params_.get());
+
+ auto result = base::MakeUnique<CredentialRequirement>();
+ result->min_length = base::MakeUnique<int>();
+ result->max_length = base::MakeUnique<int>();
+ result->allow_weak_pins = base::MakeUnique<bool>();
+
+ // Only handles pins for now.
+ if (params_->mode != QuickUnlockMode::QUICK_UNLOCK_MODE_PIN) {
+ return RespondNow(
+ ArgumentList(IsCredentialUsable::Results::Create(*result)));
+ }
+
+ std::string tried_password = params_->credential;
+ if (!std::all_of(tried_password.begin(), tried_password.end(), ::isdigit))
+ return RespondNow(
+ ArgumentList(IsCredentialUsable::Results::Create(*result)));
+
+ Profile* profile = chrome_details_.GetProfile();
+ auto length_failures =
+ IsPinLengthValid(tried_password, profile, result->min_length.get(),
+ result->max_length.get());
+ auto weak_failures = IsPinDifficultEnough(tried_password, profile,
+ result->allow_weak_pins.get());
+ result->failures.insert(result->failures.end(), length_failures.begin(),
+ length_failures.end());
+ result->failures.insert(result->failures.end(), weak_failures.begin(),
+ weak_failures.end());
+
+ return RespondNow(ArgumentList(IsCredentialUsable::Results::Create(*result)));
+}
+
// quickUnlockPrivate.setModes
QuickUnlockPrivateSetModesFunction::QuickUnlockPrivateSetModesFunction()
@@ -127,6 +258,19 @@ ExtensionFunction::ResponseAction QuickUnlockPrivateSetModesFunction::Run() {
return RespondNow(ArgumentList(SetModes::Results::Create(false)));
}
+ // Verify every credential is valid based on policies.
+ for (size_t j = 0; j < params_->modes.size(); ++j) {
+ if (params_->modes[j] == QuickUnlockMode::QUICK_UNLOCK_MODE_PIN &&
+ !IsPinLengthValid(params_->credentials[j], chrome_details_.GetProfile(),
+ nullptr, nullptr)
+ .empty() &&
+ !IsPinDifficultEnough(params_->credentials[j],
+ chrome_details_.GetProfile(), nullptr)
+ .empty()) {
+ return RespondNow(ArgumentList(SetModes::Results::Create(false)));
+ }
+ }
+
user_manager::User* user = chromeos::ProfileHelper::Get()->GetUserByProfile(
chrome_details_.GetProfile());
chromeos::UserContext user_context(user->GetAccountId());

Powered by Google App Engine
This is Rietveld 408576698