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..3024cced870f75e2c07f9151338023e4ec8cc8ca 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,11 @@ namespace extensions { |
namespace quick_unlock_private = api::quick_unlock_private; |
namespace SetModes = quick_unlock_private::SetModes; |
namespace GetActiveModes = quick_unlock_private::GetActiveModes; |
+namespace CheckCredential = quick_unlock_private::CheckCredential; |
namespace GetAvailableModes = quick_unlock_private::GetAvailableModes; |
namespace OnActiveModesChanged = quick_unlock_private::OnActiveModesChanged; |
+using CredentialProblem = quick_unlock_private::CredentialProblem; |
+using CredentialCheck = quick_unlock_private::CredentialCheck; |
using QuickUnlockMode = quick_unlock_private::QuickUnlockMode; |
using QuickUnlockModeList = std::vector<QuickUnlockMode>; |
@@ -27,6 +32,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 +64,71 @@ bool AreModesEqual(const QuickUnlockModeList& a, const QuickUnlockModeList& b) { |
return true; |
} |
+// Returns a list of problems (if any) for a given |pin| given the policies in |
+// the |profile|. If |out_min_length| and/or |out_max_length| are valid |
+// pointers, return the profile min/max length as well. |
+std::vector<CredentialProblem> IsPinLengthValid(const std::string& pin, |
+ Profile* profile, |
+ int* out_min_length, |
+ int* out_max_length) { |
+ std::vector<CredentialProblem> problems; |
+ PrefService* pref_service = profile->GetPrefs(); |
+ int min_length = pref_service->GetInteger(prefs::kPinUnlockMinimumLength); |
+ int max_length = pref_service->GetInteger(prefs::kPinUnlockMaximumLength); |
+ // If the maximum length is nonzero and shorter than the minimum length, the |
+ // maximum length is the minimum length. |
+ if (max_length != 0 && max_length < min_length) |
jdufault
2016/10/31 22:03:07
Should this be <= 0?
What behavior do we want for
sammiequon
2016/11/01 18:08:31
Would people be allowed to enter negative numbers,
jdufault
2016/11/01 18:56:43
Policy UI hopefully will sanitize input, but we sh
sammiequon
2016/11/02 18:05:24
Done.
|
+ max_length = min_length; |
+ |
+ if (out_min_length) |
+ *out_min_length = min_length; |
+ if (out_max_length) |
+ *out_max_length = max_length; |
+ |
jdufault
2016/10/31 22:03:07
declare problems here
sammiequon
2016/11/01 18:08:31
Done.
|
+ // Check if the pin is shorter than the minimum specified length. |
+ if (int{pin.size()} < min_length) { |
jdufault
2016/10/31 22:03:07
nit: drop {}
sammiequon
2016/11/01 18:08:31
Done.
|
+ problems.push_back(CredentialProblem::CREDENTIAL_PROBLEM_TOO_SHORT); |
+ } |
+ |
+ // 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) { |
jdufault
2016/10/31 22:03:07
nit: drop {}
sammiequon
2016/11/01 18:08:31
Done.
|
+ problems.push_back(CredentialProblem::CREDENTIAL_PROBLEM_TOO_LONG); |
+ } |
+ |
+ return problems; |
+} |
+ |
+// Returns a list of problems (if any) for a given |pin| given the policies in |
+// the |profile|. |
+std::vector<CredentialProblem> IsPinDifficultEnough(const std::string& pin) { |
jdufault
2016/10/31 22:03:07
Maybe return base::Optional<CredentialProblem> ins
sammiequon
2016/11/01 18:08:31
Would this be an base::Optional<vector<CredentialP
jdufault
2016/11/01 18:56:43
base::Optional<std::vector<CredentialProblem>> wou
sammiequon
2016/11/02 18:05:24
I see, I decided to stick with the vector implemen
|
+ std::vector<CredentialProblem> problems; |
+ // If the pin length is |kPinCheckWeakThreshold| or less, there is no need to |
jdufault
2016/10/31 22:03:07
nit: newline above
sammiequon
2016/11/01 18:08:31
Done.
|
+ // check for same character and increasing pin. |
+ if (int{pin.size()} <= kPinCheckWeakThreshold) |
+ return problems; |
+ |
+ // 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)) { |
jdufault
2016/10/31 22:03:07
drop extraneous ()
sammiequon
2016/11/01 18:08:31
Done.
|
+ problems.push_back(CredentialProblem::CREDENTIAL_PROBLEM_TOO_WEAK); |
jdufault
2016/10/31 22:03:07
drop {}
sammiequon
2016/11/01 18:08:31
Done.
|
+ } |
+ |
+ return problems; |
+} |
+ |
} // namespace |
// quickUnlockPrivate.getAvailableModes |
@@ -93,6 +166,53 @@ QuickUnlockPrivateGetActiveModesFunction::Run() { |
return RespondNow(ArgumentList(GetActiveModes::Results::Create(modes))); |
} |
+// quickUnlockPrivate.checkCredential |
+ |
+QuickUnlockPrivateCheckCredentialFunction:: |
+ QuickUnlockPrivateCheckCredentialFunction() |
+ : chrome_details_(this) {} |
+ |
+QuickUnlockPrivateCheckCredentialFunction:: |
+ ~QuickUnlockPrivateCheckCredentialFunction() {} |
+ |
+ExtensionFunction::ResponseAction |
+QuickUnlockPrivateCheckCredentialFunction::Run() { |
+ params_ = CheckCredential::Params::Create(*args_); |
+ EXTENSION_FUNCTION_VALIDATE(params_.get()); |
+ |
+ auto result = base::MakeUnique<CredentialCheck>(); |
+ result->min_length = base::MakeUnique<int>(); |
+ result->max_length = base::MakeUnique<int>(); |
+ |
+ // Only handles pins for now. |
+ if (params_->mode != QuickUnlockMode::QUICK_UNLOCK_MODE_PIN) { |
+ return RespondNow(ArgumentList(CheckCredential::Results::Create(*result))); |
+ } |
+ |
+ std::string tried_password = params_->credential; |
jdufault
2016/10/31 22:03:07
What about reusing the name credential instead of
sammiequon
2016/11/01 18:08:31
Done.
|
+ if (!std::all_of(tried_password.begin(), tried_password.end(), ::isdigit)) |
+ return RespondNow(ArgumentList(CheckCredential::Results::Create(*result))); |
jdufault
2016/10/31 22:03:07
This should probably report an error that the inpu
sammiequon
2016/11/01 18:08:31
Done.
|
+ |
+ Profile* profile = chrome_details_.GetProfile(); |
+ PrefService* pref_service = profile->GetPrefs(); |
+ bool allow_weak = pref_service->GetBoolean(prefs::kPinUnlockAllowWeakPins); |
+ auto length_problems = |
jdufault
2016/10/31 22:03:07
Only use auto when the type of the variable is evi
sammiequon
2016/11/01 18:08:31
Done.
|
+ IsPinLengthValid(tried_password, profile, result->min_length.get(), |
jdufault
2016/10/31 22:03:07
It's not clear that min_length and max_length are
sammiequon
2016/11/01 18:08:31
Done.
|
+ result->max_length.get()); |
+ auto weak_problems = IsPinDifficultEnough(tried_password); |
jdufault
2016/10/31 22:03:07
Specify type instead of auto
sammiequon
2016/11/01 18:08:31
Done.
|
+ result->errors.insert(result->errors.end(), length_problems.begin(), |
+ length_problems.end()); |
+ if (allow_weak) { |
+ result->warnings.insert(result->warnings.end(), weak_problems.begin(), |
jdufault
2016/10/31 22:03:07
Maybe you want to add a helper method?
AppendTo
sammiequon
2016/11/01 18:08:31
Done.
|
+ weak_problems.end()); |
+ } else { |
+ result->errors.insert(result->errors.end(), weak_problems.begin(), |
+ weak_problems.end()); |
+ } |
+ |
+ return RespondNow(ArgumentList(CheckCredential::Results::Create(*result))); |
+} |
+ |
// quickUnlockPrivate.setModes |
QuickUnlockPrivateSetModesFunction::QuickUnlockPrivateSetModesFunction() |
@@ -127,6 +247,20 @@ ExtensionFunction::ResponseAction QuickUnlockPrivateSetModesFunction::Run() { |
return RespondNow(ArgumentList(SetModes::Results::Create(false))); |
} |
+ // Verify every credential is valid based on policies. |
+ bool allow_weak = chrome_details_.GetProfile()->GetPrefs()->GetBoolean( |
+ prefs::kPinUnlockAllowWeakPins); |
+ for (size_t j = 0; j < params_->modes.size(); ++j) { |
+ if (params_->modes[j] == QuickUnlockMode::QUICK_UNLOCK_MODE_PIN && |
+ (!IsPinLengthValid(params_->credentials[j], |
jdufault
2016/10/31 22:03:08
This if statement is pretty challenging to read. Y
sammiequon
2016/11/01 18:08:31
Done.
|
+ chrome_details_.GetProfile(), nullptr, nullptr) |
+ .empty() || |
+ (!IsPinDifficultEnough(params_->credentials[j]).empty() && |
+ !allow_weak))) { |
+ return RespondNow(ArgumentList(SetModes::Results::Create(false))); |
+ } |
+ } |
+ |
user_manager::User* user = chromeos::ProfileHelper::Get()->GetUserByProfile( |
chrome_details_.GetProfile()); |
chromeos::UserContext user_context(user->GetAccountId()); |