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

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 21 errors. Created 4 years 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 |kMinLengthForWeakPin| will be checked for
36 // weakness.
37 const int kMinLengthForWeakPin = 2;
stevenjb 2016/11/30 18:13:58 This should really be kMinLengthForNonWeakPin, or
sammiequon 2016/11/30 23:03:42 Done.
38 // A list of the top-10 most commmonly used PINs. This list is taken from
39 // www.datagenetics.com/blog/september32012/.
40 const char* kMostCommonPins[] = {"1234", "1111", "0000", "1212", "7777",
41 "1004", "2000", "4444", "2222", "6969"};
stevenjb 2016/11/30 18:13:58 Aren't 1234, 1111, 0000, 7777, 4444, and 2222 alre
sammiequon 2016/11/30 23:03:42 Done.
30 42
31 // Returns the active set of quick unlock modes. 43 // Returns the active set of quick unlock modes.
32 QuickUnlockModeList ComputeActiveModes(Profile* profile) { 44 QuickUnlockModeList ComputeActiveModes(Profile* profile) {
33 QuickUnlockModeList modes; 45 QuickUnlockModeList modes;
34 46
35 chromeos::PinStorage* pin_storage = 47 chromeos::PinStorage* pin_storage =
36 chromeos::PinStorageFactory::GetForProfile(profile); 48 chromeos::PinStorageFactory::GetForProfile(profile);
37 if (pin_storage && pin_storage->IsPinSet()) 49 if (pin_storage && pin_storage->IsPinSet())
38 modes.push_back(quick_unlock_private::QUICK_UNLOCK_MODE_PIN); 50 modes.push_back(quick_unlock_private::QUICK_UNLOCK_MODE_PIN);
39 51
40 return modes; 52 return modes;
41 } 53 }
42 54
43 // Returns true if |a| and |b| contain the same elements. The elements do not 55 // Returns true if |a| and |b| contain the same elements. The elements do not
44 // need to be in the same order. 56 // need to be in the same order.
45 bool AreModesEqual(const QuickUnlockModeList& a, const QuickUnlockModeList& b) { 57 bool AreModesEqual(const QuickUnlockModeList& a, const QuickUnlockModeList& b) {
46 if (a.size() != b.size()) 58 if (a.size() != b.size())
47 return false; 59 return false;
48 60
49 // This is a slow comparison algorithm, but the number of entries in |a| and 61 // 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. 62 // |b| will always be very low (0-3 items) so it doesn't matter.
51 for (size_t i = 0; i < a.size(); ++i) { 63 for (size_t i = 0; i < a.size(); ++i) {
52 if (std::find(b.begin(), b.end(), a[i]) == b.end()) 64 if (std::find(b.begin(), b.end(), a[i]) == b.end())
53 return false; 65 return false;
54 } 66 }
55 67
56 return true; 68 return true;
57 } 69 }
58 70
71 bool IsPinNumeric(const std::string& pin) {
72 return std::all_of(pin.begin(), pin.end(), ::isdigit);
73 }
74
75 // Checks whether a given |pin| is valid given the PIN min/max policies in
76 // |pref_service|. If |out_min_length| and/or |out_max_length| are valid
77 // pointers, return the profile min/max length as well. If |length_problem| is
78 // a valid pointer return the type of problem.
79 bool IsPinLengthValid(const std::string& pin,
80 PrefService* pref_service,
81 int* out_min_length,
82 int* out_max_length,
83 CredentialProblem* length_problem) {
84 int min_length = pref_service->GetInteger(prefs::kPinUnlockMinimumLength);
85 int max_length = pref_service->GetInteger(prefs::kPinUnlockMaximumLength);
86
87 // Sanitize the policy input.
88 if (min_length < 1)
89 min_length = 1;
90 if (max_length < 0)
91 max_length = 0;
92
93 // If the maximum length is nonzero and shorter than the minimum length, the
94 // maximum length is the minimum length.
stevenjb 2016/11/30 18:13:58 nit: comment is unnecessary
sammiequon 2016/11/30 23:03:42 Done.
95 if (max_length != 0 && max_length < min_length)
96 max_length = min_length;
97
98 if (out_min_length)
99 *out_min_length = min_length;
100 if (out_max_length)
101 *out_max_length = max_length;
stevenjb 2016/11/30 18:13:58 This method seems a bit over complicated. Could we
sammiequon 2016/11/30 23:03:42 Done.
102
103 // Check if the PIN is shorter than the minimum specified length.
104 if (int{pin.size()} < min_length) {
105 if (length_problem)
106 *length_problem = CredentialProblem::CREDENTIAL_PROBLEM_TOO_SHORT;
107 return false;
108 }
109
110 // If the maximum specified length is zero, there is no maximum length.
111 // Otherwise check if the PIN is longer than the maximum specified length.
112 if (max_length != 0 && static_cast<int>(pin.size()) > max_length) {
113 if (length_problem)
114 *length_problem = CredentialProblem::CREDENTIAL_PROBLEM_TOO_LONG;
115 return false;
116 }
117
118 return true;
119 }
120
121 // Checks if a given |pin| is weak or not. A PIN is considered weak if it:
122 // a) has all the same digits
123 // b) each digit is one larger than the previous digit
124 // c) each digit is one smaller than the previous digit
125 // d) is on the top ten of this list;
126 // www.datagenetics.com/blog/september32012/
127 // Note: A 9 followed by a 0 is not considered increasing, and a 0 followed by
128 // a 9 is not considered decreasing.
129 bool IsPinDifficultEnough(const std::string& pin) {
130 // If the pin length is |kMinLengthForWeakPin| or less, there is no need to
131 // check for same character and increasing pin.
stevenjb 2016/11/30 18:13:58 It would be nice if these tests matched the order
sammiequon 2016/11/30 23:03:42 Done.
132 if (int{pin.size()} <= kMinLengthForWeakPin)
133 return true;
134
135 // Check if it is on the list of most common PINs.
136 if (std::find(kMostCommonPins, std::end(kMostCommonPins), pin) !=
137 std::end(kMostCommonPins)) {
138 return false;
139 }
140
141 // Check for same digits, increasing PIN simutaneously.
stevenjb 2016/11/30 18:13:58 simultaneously
sammiequon 2016/11/30 23:03:42 Done.
142 bool is_same = true;
143 // TODO(sammiequon): Should longer PINs (5+) be still subjected to this?
144 bool is_increasing = true;
145 bool is_decreasing = true;
146 for (int i = 1; i < int{pin.length()}; ++i) {
147 const char previous = pin[i - 1];
148 const char current = pin[i];
149
150 is_same = is_same && (current == previous);
151 is_increasing = is_increasing && (current == previous + 1);
152 is_decreasing = is_decreasing && (current == previous - 1);
153 }
154
155 // PIN is considered weak if any of these conditions is met.
156 if (is_same || is_increasing || is_decreasing)
157 return false;
158
159 return true;
160 }
161
59 } // namespace 162 } // namespace
60 163
61 // quickUnlockPrivate.getAvailableModes 164 // quickUnlockPrivate.getAvailableModes
62 165
63 QuickUnlockPrivateGetAvailableModesFunction:: 166 QuickUnlockPrivateGetAvailableModesFunction::
64 QuickUnlockPrivateGetAvailableModesFunction() 167 QuickUnlockPrivateGetAvailableModesFunction()
65 : chrome_details_(this) {} 168 : chrome_details_(this) {}
66 169
67 QuickUnlockPrivateGetAvailableModesFunction:: 170 QuickUnlockPrivateGetAvailableModesFunction::
68 ~QuickUnlockPrivateGetAvailableModesFunction() {} 171 ~QuickUnlockPrivateGetAvailableModesFunction() {}
(...skipping 17 matching lines...) Expand all
86 QuickUnlockPrivateGetActiveModesFunction:: 189 QuickUnlockPrivateGetActiveModesFunction::
87 ~QuickUnlockPrivateGetActiveModesFunction() {} 190 ~QuickUnlockPrivateGetActiveModesFunction() {}
88 191
89 ExtensionFunction::ResponseAction 192 ExtensionFunction::ResponseAction
90 QuickUnlockPrivateGetActiveModesFunction::Run() { 193 QuickUnlockPrivateGetActiveModesFunction::Run() {
91 const QuickUnlockModeList modes = 194 const QuickUnlockModeList modes =
92 ComputeActiveModes(chrome_details_.GetProfile()); 195 ComputeActiveModes(chrome_details_.GetProfile());
93 return RespondNow(ArgumentList(GetActiveModes::Results::Create(modes))); 196 return RespondNow(ArgumentList(GetActiveModes::Results::Create(modes)));
94 } 197 }
95 198
199 // quickUnlockPrivate.checkCredential
200
201 QuickUnlockPrivateCheckCredentialFunction::
202 QuickUnlockPrivateCheckCredentialFunction() {}
203
204 QuickUnlockPrivateCheckCredentialFunction::
205 ~QuickUnlockPrivateCheckCredentialFunction() {}
206
207 ExtensionFunction::ResponseAction
208 QuickUnlockPrivateCheckCredentialFunction::Run() {
209 std::unique_ptr<CheckCredential::Params> params_ =
210 CheckCredential::Params::Create(*args_);
211 EXTENSION_FUNCTION_VALIDATE(params_);
212
213 auto result = base::MakeUnique<CredentialCheck>();
214
215 // Only handles pins for now.
216 if (params_->mode != QuickUnlockMode::QUICK_UNLOCK_MODE_PIN)
217 return RespondNow(ArgumentList(CheckCredential::Results::Create(*result)));
218
219 const std::string& credential = params_->credential;
220
221 Profile* profile = Profile::FromBrowserContext(browser_context());
222 PrefService* pref_service = profile->GetPrefs();
223 bool allow_weak = pref_service->GetBoolean(prefs::kPinUnlockAllowWeakPins);
224
225 // Check and return the problems.
226 if (!IsPinNumeric(credential)) {
227 result->errors.push_back(
228 CredentialProblem::CREDENTIAL_PROBLEM_CONTAINS_NONDIGIT);
229 }
230
231 CredentialProblem length_problem = CredentialProblem::CREDENTIAL_PROBLEM_NONE;
232 // Note: This function call writes values to |result->min_length|,
233 // |result->max_length| and |length_problem|.
234 if (!IsPinLengthValid(credential, pref_service, &result->min_length,
235 &result->max_length, &length_problem)) {
stevenjb 2016/11/30 18:13:58 This is a file local function, right? Why not just
sammiequon 2016/11/30 23:03:42 Function changed.
236 DCHECK_NE(length_problem, CredentialProblem::CREDENTIAL_PROBLEM_NONE);
237 result->errors.push_back(length_problem);
238 }
239
240 if (!IsPinDifficultEnough(credential)) {
241 if (allow_weak) {
242 result->warnings.push_back(
243 CredentialProblem::CREDENTIAL_PROBLEM_TOO_WEAK);
244 } else {
245 result->errors.push_back(CredentialProblem::CREDENTIAL_PROBLEM_TOO_WEAK);
246 }
247 }
248
249 return RespondNow(ArgumentList(CheckCredential::Results::Create(*result)));
250 }
251
96 // quickUnlockPrivate.setModes 252 // quickUnlockPrivate.setModes
97 253
98 QuickUnlockPrivateSetModesFunction::QuickUnlockPrivateSetModesFunction() 254 QuickUnlockPrivateSetModesFunction::QuickUnlockPrivateSetModesFunction()
99 : chrome_details_(this) {} 255 : chrome_details_(this) {}
100 256
101 QuickUnlockPrivateSetModesFunction::~QuickUnlockPrivateSetModesFunction() {} 257 QuickUnlockPrivateSetModesFunction::~QuickUnlockPrivateSetModesFunction() {}
102 258
103 void QuickUnlockPrivateSetModesFunction::SetAuthenticatorAllocatorForTesting( 259 void QuickUnlockPrivateSetModesFunction::SetAuthenticatorAllocatorForTesting(
104 const QuickUnlockPrivateSetModesFunction::AuthenticatorAllocator& 260 const QuickUnlockPrivateSetModesFunction::AuthenticatorAllocator&
105 allocator) { 261 allocator) {
106 authenticator_allocator_ = allocator; 262 authenticator_allocator_ = allocator;
107 } 263 }
108 264
109 void QuickUnlockPrivateSetModesFunction::SetModesChangedEventHandlerForTesting( 265 void QuickUnlockPrivateSetModesFunction::SetModesChangedEventHandlerForTesting(
110 const ModesChangedEventHandler& handler) { 266 const ModesChangedEventHandler& handler) {
111 modes_changed_handler_ = handler; 267 modes_changed_handler_ = handler;
112 } 268 }
113 269
114 ExtensionFunction::ResponseAction QuickUnlockPrivateSetModesFunction::Run() { 270 ExtensionFunction::ResponseAction QuickUnlockPrivateSetModesFunction::Run() {
115 params_ = SetModes::Params::Create(*args_); 271 params_ = SetModes::Params::Create(*args_);
116 EXTENSION_FUNCTION_VALIDATE(params_.get()); 272 EXTENSION_FUNCTION_VALIDATE(params_);
117 273
118 if (params_->modes.size() != params_->credentials.size()) 274 if (params_->modes.size() != params_->credentials.size())
119 return RespondNow(Error(kModesAndCredentialsLengthMismatch)); 275 return RespondNow(Error(kModesAndCredentialsLengthMismatch));
120 276
121 if (params_->modes.size() > 1) 277 if (params_->modes.size() > 1)
122 return RespondNow(Error(kMultipleModesNotSupported)); 278 return RespondNow(Error(kMultipleModesNotSupported));
123 279
124 // Verify every credential is numeric. 280 // Verify every credential is valid based on policies.
125 for (const std::string& credential : params_->credentials) { 281 Profile::Profile* profile = Profile::FromBrowserContext(browser_context());
126 if (!std::all_of(credential.begin(), credential.end(), ::isdigit)) 282 bool allow_weak =
127 return RespondNow(ArgumentList(SetModes::Results::Create(false))); 283 profile->GetPrefs()->GetBoolean(prefs::kPinUnlockAllowWeakPins);
284
285 for (size_t j = 0; j < params_->modes.size(); ++j) {
286 if (params_->credentials[j].empty())
287 continue;
288
289 if (params_->modes[j] == QuickUnlockMode::QUICK_UNLOCK_MODE_PIN) {
stevenjb 2016/11/30 18:13:58 invert and continue
sammiequon 2016/11/30 23:03:42 Done.
290 if (!IsPinNumeric(params_->credentials[j]))
291 return RespondNow(ArgumentList(SetModes::Results::Create(false)));
292
293 if (!IsPinLengthValid(params_->credentials[j], profile->GetPrefs(),
294 nullptr, nullptr, nullptr)) {
295 return RespondNow(ArgumentList(SetModes::Results::Create(false)));
296 }
297 if (!allow_weak && !IsPinDifficultEnough(params_->credentials[j])) {
298 return RespondNow(ArgumentList(SetModes::Results::Create(false)));
299 }
300 }
128 } 301 }
129 302
130 user_manager::User* user = chromeos::ProfileHelper::Get()->GetUserByProfile( 303 user_manager::User* user = chromeos::ProfileHelper::Get()->GetUserByProfile(
131 chrome_details_.GetProfile()); 304 chrome_details_.GetProfile());
132 chromeos::UserContext user_context(user->GetAccountId()); 305 chromeos::UserContext user_context(user->GetAccountId());
133 user_context.SetKey(chromeos::Key(params_->account_password)); 306 user_context.SetKey(chromeos::Key(params_->account_password));
134 307
135 // Lazily allocate the authenticator. We do this here, instead of in the ctor, 308 // Lazily allocate the authenticator. We do this here, instead of in the ctor,
136 // so that tests can install a fake. 309 // so that tests can install a fake.
137 if (authenticator_allocator_.is_null()) 310 if (authenticator_allocator_.is_null())
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after
223 } 396 }
224 397
225 std::unique_ptr<base::ListValue> args = OnActiveModesChanged::Create(modes); 398 std::unique_ptr<base::ListValue> args = OnActiveModesChanged::Create(modes);
226 std::unique_ptr<Event> event( 399 std::unique_ptr<Event> event(
227 new Event(events::QUICK_UNLOCK_PRIVATE_ON_ACTIVE_MODES_CHANGED, 400 new Event(events::QUICK_UNLOCK_PRIVATE_ON_ACTIVE_MODES_CHANGED,
228 OnActiveModesChanged::kEventName, std::move(args))); 401 OnActiveModesChanged::kEventName, std::move(args)));
229 EventRouter::Get(browser_context())->BroadcastEvent(std::move(event)); 402 EventRouter::Get(browser_context())->BroadcastEvent(std::move(event));
230 } 403 }
231 404
232 } // namespace extensions 405 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698