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

Side by Side 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 11 errors. Created 4 years, 1 month 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 unified diff | Download patch
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/chromeos/extensions/quick_unlock_private/quick_unlock_p rivate_api.h" 5 #include "chrome/browser/chromeos/extensions/quick_unlock_private/quick_unlock_p rivate_api.h"
6 6
7 #include "chrome/browser/chromeos/login/quick_unlock/pin_storage.h" 7 #include "chrome/browser/chromeos/login/quick_unlock/pin_storage.h"
8 #include "chrome/browser/chromeos/login/quick_unlock/pin_storage_factory.h" 8 #include "chrome/browser/chromeos/login/quick_unlock/pin_storage_factory.h"
9 #include "chrome/browser/chromeos/profiles/profile_helper.h" 9 #include "chrome/browser/chromeos/profiles/profile_helper.h"
10 #include "chrome/common/pref_names.h"
10 #include "chromeos/login/auth/extended_authenticator.h" 11 #include "chromeos/login/auth/extended_authenticator.h"
11 #include "chromeos/login/auth/user_context.h" 12 #include "chromeos/login/auth/user_context.h"
13 #include "components/prefs/pref_service.h"
12 #include "extensions/browser/event_router.h" 14 #include "extensions/browser/event_router.h"
13 15
14 namespace extensions { 16 namespace extensions {
15 17
16 namespace quick_unlock_private = api::quick_unlock_private; 18 namespace quick_unlock_private = api::quick_unlock_private;
17 namespace SetModes = quick_unlock_private::SetModes; 19 namespace SetModes = quick_unlock_private::SetModes;
18 namespace GetActiveModes = quick_unlock_private::GetActiveModes; 20 namespace GetActiveModes = quick_unlock_private::GetActiveModes;
21 namespace CheckCredential = quick_unlock_private::CheckCredential;
19 namespace GetAvailableModes = quick_unlock_private::GetAvailableModes; 22 namespace GetAvailableModes = quick_unlock_private::GetAvailableModes;
20 namespace OnActiveModesChanged = quick_unlock_private::OnActiveModesChanged; 23 namespace OnActiveModesChanged = quick_unlock_private::OnActiveModesChanged;
24 using CredentialProblem = quick_unlock_private::CredentialProblem;
25 using CredentialCheck = quick_unlock_private::CredentialCheck;
21 using QuickUnlockMode = quick_unlock_private::QuickUnlockMode; 26 using QuickUnlockMode = quick_unlock_private::QuickUnlockMode;
22 using QuickUnlockModeList = std::vector<QuickUnlockMode>; 27 using QuickUnlockModeList = std::vector<QuickUnlockMode>;
23 28
24 namespace { 29 namespace {
25 30
26 const char kModesAndCredentialsLengthMismatch[] = 31 const char kModesAndCredentialsLengthMismatch[] =
27 "|modes| and |credentials| must have the same number of elements"; 32 "|modes| and |credentials| must have the same number of elements";
28 const char kMultipleModesNotSupported[] = 33 const char kMultipleModesNotSupported[] =
29 "At most one quick unlock mode can be active."; 34 "At most one quick unlock mode can be active.";
35 // Pins greater in length than |kPinCheckWeakThreshold| will be checked for
jdufault 2016/11/02 19:22:59 nit: Pins => PINs
sammiequon 2016/11/03 16:54:12 Done.
36 // weakness.
37 const int kPinCheckWeakThreshold = 2;
jdufault 2016/11/02 19:22:59 Using Threshold as a name is a bit confusing here.
sammiequon 2016/11/03 16:54:12 Done.
30 38
31 // Returns the active set of quick unlock modes. 39 // Returns the active set of quick unlock modes.
32 QuickUnlockModeList ComputeActiveModes(Profile* profile) { 40 QuickUnlockModeList ComputeActiveModes(Profile* profile) {
33 QuickUnlockModeList modes; 41 QuickUnlockModeList modes;
34 42
35 chromeos::PinStorage* pin_storage = 43 chromeos::PinStorage* pin_storage =
36 chromeos::PinStorageFactory::GetForProfile(profile); 44 chromeos::PinStorageFactory::GetForProfile(profile);
37 if (pin_storage && pin_storage->IsPinSet()) 45 if (pin_storage && pin_storage->IsPinSet())
38 modes.push_back(quick_unlock_private::QUICK_UNLOCK_MODE_PIN); 46 modes.push_back(quick_unlock_private::QUICK_UNLOCK_MODE_PIN);
39 47
40 return modes; 48 return modes;
41 } 49 }
42 50
43 // Returns true if |a| and |b| contain the same elements. The elements do not 51 // Returns true if |a| and |b| contain the same elements. The elements do not
44 // need to be in the same order. 52 // need to be in the same order.
45 bool AreModesEqual(const QuickUnlockModeList& a, const QuickUnlockModeList& b) { 53 bool AreModesEqual(const QuickUnlockModeList& a, const QuickUnlockModeList& b) {
46 if (a.size() != b.size()) 54 if (a.size() != b.size())
47 return false; 55 return false;
48 56
49 // This is a slow comparison algorithm, but the number of entries in |a| and 57 // This is a slow comparison algorithm, but the number of entries in |a| and
50 // |b| will always be very low (0-3 items) so it doesn't matter. 58 // |b| will always be very low (0-3 items) so it doesn't matter.
51 for (size_t i = 0; i < a.size(); ++i) { 59 for (size_t i = 0; i < a.size(); ++i) {
52 if (std::find(b.begin(), b.end(), a[i]) == b.end()) 60 if (std::find(b.begin(), b.end(), a[i]) == b.end())
53 return false; 61 return false;
54 } 62 }
55 63
56 return true; 64 return true;
57 } 65 }
58 66
67 std::vector<CredentialProblem> IsPinNumeric(const std::string& pin) {
68 std::vector<CredentialProblem> problems;
69 if (!std::all_of(pin.begin(), pin.end(), ::isdigit))
70 problems.push_back(CredentialProblem::CREDENTIAL_PROBLEM_CONTAINS_NONDIGIT);
71 return problems;
72 }
73
74 // Returns a list of length related problems (if any) for a given |pin|
75 // given the PIN min/max policies in |profile|. If |out_min_length| and/or
76 // |out_max_length| are valid pointers, return the profile min/max length as
77 // well.
78 std::vector<CredentialProblem> IsPinLengthValid(const std::string& pin,
79 Profile* profile,
80 int* out_min_length,
81 int* out_max_length) {
82 PrefService* pref_service = profile->GetPrefs();
83 int min_length = pref_service->GetInteger(prefs::kPinUnlockMinimumLength);
84 int max_length = pref_service->GetInteger(prefs::kPinUnlockMaximumLength);
85 // If the maximum length is nonzero and shorter than the minimum length, the
86 // maximum length is the minimum length.
87 if (max_length != 0 && max_length < min_length)
jdufault 2016/11/02 19:22:59 This should happen after the sanitization check.
sammiequon 2016/11/03 16:54:12 Done.
88 max_length = min_length;
89
90 // Sanitize the policy input.
91 if (min_length < 1)
92 min_length = 1;
93 if (max_length < 0)
94 max_length = 0;
95
96 if (out_min_length)
97 *out_min_length = min_length;
98 if (out_max_length)
99 *out_max_length = max_length;
100
101 std::vector<CredentialProblem> problems;
102
103 // Check if the pin is shorter than the minimum specified length.
104 if (int{pin.size()} < min_length)
105 problems.push_back(CredentialProblem::CREDENTIAL_PROBLEM_TOO_SHORT);
106
107 // If the maximum specified length is zero, there is no maximum length.
108 // Otherwise check if the pin is longer than the maximum specified length.
109 if (max_length != 0 && int{pin.size()} > max_length)
110 problems.push_back(CredentialProblem::CREDENTIAL_PROBLEM_TOO_LONG);
111
112 return problems;
113 }
114
115 // Returns a list of problems regarding the difficulty of the pin (if any) for
116 // a given |pin|.
117 std::vector<CredentialProblem> IsPinDifficultEnough(const std::string& pin) {
118 std::vector<CredentialProblem> problems;
119 // TODO(sammiequon): Add a check for the top 10 used pins. See
120 // crbug.com/661649.
jdufault 2016/11/02 19:22:59 I don't think adding the check will be much work,
sammiequon 2016/11/03 16:54:12 Done.
121
122 // If the pin length is |kPinCheckWeakThreshold| or less, there is no need to
123 // check for same character and increasing pin.
124 if (int{pin.size()} <= kPinCheckWeakThreshold)
125 return problems;
126
127 // Check for same digits, increasing pin simutaneously.
128 bool is_same = true;
129 bool is_increasing = true;
130 bool is_decreasing = true;
131 for (int i = 1; i < int{pin.length()}; ++i) {
132 const char previous = pin[i - 1];
133 const char current = pin[i];
134
135 is_same = is_same && (current == previous);
136 is_increasing = is_increasing && (current == previous + 1);
137 is_decreasing = is_decreasing && (current == previous - 1);
138 }
139
140 // Pin is considered weak if either of these is met.
jdufault 2016/11/02 19:22:59 nit: either => any nit: Pin => PIN
sammiequon 2016/11/03 16:54:13 Done.
141 if (is_same || is_increasing || is_decreasing)
142 problems.push_back(CredentialProblem::CREDENTIAL_PROBLEM_TOO_WEAK);
143
144 return problems;
145 }
146
147 void AppendToProblemList(std::vector<CredentialProblem>& list_to_append_to,
jdufault 2016/11/02 19:22:59 What about a standard src/dest name? AppendToProb
sammiequon 2016/11/03 16:54:12 Done.
148 const std::vector<CredentialProblem>& list_to_append) {
149 list_to_append_to.insert(list_to_append_to.end(), list_to_append.begin(),
150 list_to_append.end());
151 }
152
59 } // namespace 153 } // namespace
60 154
61 // quickUnlockPrivate.getAvailableModes 155 // quickUnlockPrivate.getAvailableModes
62 156
63 QuickUnlockPrivateGetAvailableModesFunction:: 157 QuickUnlockPrivateGetAvailableModesFunction::
64 QuickUnlockPrivateGetAvailableModesFunction() 158 QuickUnlockPrivateGetAvailableModesFunction()
65 : chrome_details_(this) {} 159 : chrome_details_(this) {}
66 160
67 QuickUnlockPrivateGetAvailableModesFunction:: 161 QuickUnlockPrivateGetAvailableModesFunction::
68 ~QuickUnlockPrivateGetAvailableModesFunction() {} 162 ~QuickUnlockPrivateGetAvailableModesFunction() {}
(...skipping 17 matching lines...) Expand all
86 QuickUnlockPrivateGetActiveModesFunction:: 180 QuickUnlockPrivateGetActiveModesFunction::
87 ~QuickUnlockPrivateGetActiveModesFunction() {} 181 ~QuickUnlockPrivateGetActiveModesFunction() {}
88 182
89 ExtensionFunction::ResponseAction 183 ExtensionFunction::ResponseAction
90 QuickUnlockPrivateGetActiveModesFunction::Run() { 184 QuickUnlockPrivateGetActiveModesFunction::Run() {
91 const QuickUnlockModeList modes = 185 const QuickUnlockModeList modes =
92 ComputeActiveModes(chrome_details_.GetProfile()); 186 ComputeActiveModes(chrome_details_.GetProfile());
93 return RespondNow(ArgumentList(GetActiveModes::Results::Create(modes))); 187 return RespondNow(ArgumentList(GetActiveModes::Results::Create(modes)));
94 } 188 }
95 189
190 // quickUnlockPrivate.checkCredential
191
192 QuickUnlockPrivateCheckCredentialFunction::
193 QuickUnlockPrivateCheckCredentialFunction()
194 : chrome_details_(this) {}
195
196 QuickUnlockPrivateCheckCredentialFunction::
197 ~QuickUnlockPrivateCheckCredentialFunction() {}
198
199 ExtensionFunction::ResponseAction
200 QuickUnlockPrivateCheckCredentialFunction::Run() {
201 params_ = CheckCredential::Params::Create(*args_);
202 EXTENSION_FUNCTION_VALIDATE(params_.get());
203
204 auto result = base::MakeUnique<CredentialCheck>();
205
206 // Only handles pins for now.
207 if (params_->mode != QuickUnlockMode::QUICK_UNLOCK_MODE_PIN) {
208 return RespondNow(ArgumentList(CheckCredential::Results::Create(*result)));
jdufault 2016/11/02 19:22:59 nit: drop {}
sammiequon 2016/11/03 16:54:13 Done.
209 }
210
211 std::string credential = params_->credential;
212
213 Profile* profile = chrome_details_.GetProfile();
214 PrefService* pref_service = profile->GetPrefs();
215 bool allow_weak = pref_service->GetBoolean(prefs::kPinUnlockAllowWeakPins);
216
217 // Check and return the problems.
218 // Note: This function call writes values to |result->min_length| and
jdufault 2016/11/02 19:22:59 This comment should be above the IsPinLengthValid
sammiequon 2016/11/03 16:54:12 Done.
219 // |result->max_length|.
220 std::vector<CredentialProblem> validity_problems = IsPinNumeric(credential);
221 std::vector<CredentialProblem> length_problems = IsPinLengthValid(
222 credential, profile, &result->min_length, &result->max_length);
223 std::vector<CredentialProblem> weak_problems =
224 IsPinDifficultEnough(credential);
225
226 // Append the results from various checks to the appropriate list in |result|.
227 // |validity_problems| and |length_problems| are always errors, while
228 // |weak_problems| are errors or warnings depending on the policy.
229 AppendToProblemList(result->errors, validity_problems);
230 AppendToProblemList(result->errors, length_problems);
231 AppendToProblemList(allow_weak ? result->warnings : result->errors,
232 weak_problems);
233
234 return RespondNow(ArgumentList(CheckCredential::Results::Create(*result)));
235 }
236
96 // quickUnlockPrivate.setModes 237 // quickUnlockPrivate.setModes
97 238
98 QuickUnlockPrivateSetModesFunction::QuickUnlockPrivateSetModesFunction() 239 QuickUnlockPrivateSetModesFunction::QuickUnlockPrivateSetModesFunction()
99 : chrome_details_(this) {} 240 : chrome_details_(this) {}
100 241
101 QuickUnlockPrivateSetModesFunction::~QuickUnlockPrivateSetModesFunction() {} 242 QuickUnlockPrivateSetModesFunction::~QuickUnlockPrivateSetModesFunction() {}
102 243
103 void QuickUnlockPrivateSetModesFunction::SetAuthenticatorAllocatorForTesting( 244 void QuickUnlockPrivateSetModesFunction::SetAuthenticatorAllocatorForTesting(
104 const QuickUnlockPrivateSetModesFunction::AuthenticatorAllocator& 245 const QuickUnlockPrivateSetModesFunction::AuthenticatorAllocator&
105 allocator) { 246 allocator) {
106 authenticator_allocator_ = allocator; 247 authenticator_allocator_ = allocator;
107 } 248 }
108 249
109 void QuickUnlockPrivateSetModesFunction::SetModesChangedEventHandlerForTesting( 250 void QuickUnlockPrivateSetModesFunction::SetModesChangedEventHandlerForTesting(
110 const ModesChangedEventHandler& handler) { 251 const ModesChangedEventHandler& handler) {
111 modes_changed_handler_ = handler; 252 modes_changed_handler_ = handler;
112 } 253 }
113 254
114 ExtensionFunction::ResponseAction QuickUnlockPrivateSetModesFunction::Run() { 255 ExtensionFunction::ResponseAction QuickUnlockPrivateSetModesFunction::Run() {
115 params_ = SetModes::Params::Create(*args_); 256 params_ = SetModes::Params::Create(*args_);
116 EXTENSION_FUNCTION_VALIDATE(params_.get()); 257 EXTENSION_FUNCTION_VALIDATE(params_.get());
117 258
118 if (params_->modes.size() != params_->credentials.size()) 259 if (params_->modes.size() != params_->credentials.size())
119 return RespondNow(Error(kModesAndCredentialsLengthMismatch)); 260 return RespondNow(Error(kModesAndCredentialsLengthMismatch));
120 261
121 if (params_->modes.size() > 1) 262 if (params_->modes.size() > 1)
122 return RespondNow(Error(kMultipleModesNotSupported)); 263 return RespondNow(Error(kMultipleModesNotSupported));
123 264
124 // Verify every credential is numeric. 265 // Verify every credential is valid based on policies.
125 for (const std::string& credential : params_->credentials) { 266 bool allow_weak = chrome_details_.GetProfile()->GetPrefs()->GetBoolean(
126 if (!std::all_of(credential.begin(), credential.end(), ::isdigit)) 267 prefs::kPinUnlockAllowWeakPins);
127 return RespondNow(ArgumentList(SetModes::Results::Create(false))); 268 for (size_t j = 0; j < params_->modes.size(); ++j) {
269 if (params_->credentials[j].empty())
270 continue;
271
272 if (params_->modes[j] == QuickUnlockMode::QUICK_UNLOCK_MODE_PIN) {
273 if (!IsPinNumeric(params_->credentials[j]).empty())
274 return RespondNow(ArgumentList(SetModes::Results::Create(false)));
275
276 if (!IsPinLengthValid(params_->credentials[j],
277 chrome_details_.GetProfile(), nullptr, nullptr)
278 .empty()) {
279 return RespondNow(ArgumentList(SetModes::Results::Create(false)));
280 }
281 if (!allow_weak &&
282 !IsPinDifficultEnough(params_->credentials[j]).empty()) {
283 return RespondNow(ArgumentList(SetModes::Results::Create(false)));
284 }
285 }
128 } 286 }
129 287
130 user_manager::User* user = chromeos::ProfileHelper::Get()->GetUserByProfile( 288 user_manager::User* user = chromeos::ProfileHelper::Get()->GetUserByProfile(
131 chrome_details_.GetProfile()); 289 chrome_details_.GetProfile());
132 chromeos::UserContext user_context(user->GetAccountId()); 290 chromeos::UserContext user_context(user->GetAccountId());
133 user_context.SetKey(chromeos::Key(params_->account_password)); 291 user_context.SetKey(chromeos::Key(params_->account_password));
134 292
135 // Lazily allocate the authenticator. We do this here, instead of in the ctor, 293 // Lazily allocate the authenticator. We do this here, instead of in the ctor,
136 // so that tests can install a fake. 294 // so that tests can install a fake.
137 if (authenticator_allocator_.is_null()) 295 if (authenticator_allocator_.is_null())
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after
223 } 381 }
224 382
225 std::unique_ptr<base::ListValue> args = OnActiveModesChanged::Create(modes); 383 std::unique_ptr<base::ListValue> args = OnActiveModesChanged::Create(modes);
226 std::unique_ptr<Event> event( 384 std::unique_ptr<Event> event(
227 new Event(events::QUICK_UNLOCK_PRIVATE_ON_ACTIVE_MODES_CHANGED, 385 new Event(events::QUICK_UNLOCK_PRIVATE_ON_ACTIVE_MODES_CHANGED,
228 OnActiveModesChanged::kEventName, std::move(args))); 386 OnActiveModesChanged::kEventName, std::move(args)));
229 EventRouter::Get(browser_context())->BroadcastEvent(std::move(event)); 387 EventRouter::Get(browser_context())->BroadcastEvent(std::move(event));
230 } 388 }
231 389
232 } // namespace extensions 390 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698