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

Side by Side Diff: chrome/browser/chromeos/extensions/quick_unlock_private/quick_unlock_private_api.cc

Issue 2809993004: cros: Implement cryptohome backend for pin.
Patch Set: Rebase, remove some extraneous LOG statements Created 3 years, 7 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 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 "base/memory/ptr_util.h" 7 #include "base/memory/ptr_util.h"
8 #include "chrome/browser/chromeos/login/quick_unlock/pin_backend.h"
8 #include "chrome/browser/chromeos/login/quick_unlock/quick_unlock_factory.h" 9 #include "chrome/browser/chromeos/login/quick_unlock/quick_unlock_factory.h"
9 #include "chrome/browser/chromeos/login/quick_unlock/quick_unlock_storage.h" 10 #include "chrome/browser/chromeos/login/quick_unlock/quick_unlock_storage.h"
10 #include "chrome/browser/chromeos/profiles/profile_helper.h" 11 #include "chrome/browser/chromeos/profiles/profile_helper.h"
11 #include "chrome/common/pref_names.h" 12 #include "chrome/common/pref_names.h"
12 #include "chromeos/login/auth/extended_authenticator.h" 13 #include "chromeos/login/auth/extended_authenticator.h"
13 #include "chromeos/login/auth/user_context.h" 14 #include "chromeos/login/auth/user_context.h"
14 #include "components/prefs/pref_service.h" 15 #include "components/prefs/pref_service.h"
15 #include "extensions/browser/event_router.h" 16 #include "extensions/browser/event_router.h"
16 17
17 namespace extensions { 18 namespace extensions {
(...skipping 20 matching lines...) Expand all
38 "At most one quick unlock mode can be active."; 39 "At most one quick unlock mode can be active.";
39 // PINs greater in length than |kMinLengthForWeakPin| will be checked for 40 // PINs greater in length than |kMinLengthForWeakPin| will be checked for
40 // weakness. 41 // weakness.
41 const int kMinLengthForNonWeakPin = 2; 42 const int kMinLengthForNonWeakPin = 2;
42 // A list of the most commmonly used PINs, whose digits are not all the same, 43 // A list of the most commmonly used PINs, whose digits are not all the same,
43 // increasing or decreasing. This list is taken from 44 // increasing or decreasing. This list is taken from
44 // www.datagenetics.com/blog/september32012/. 45 // www.datagenetics.com/blog/september32012/.
45 const char* kMostCommonPins[] = {"1212", "1004", "2000", "6969", 46 const char* kMostCommonPins[] = {"1212", "1004", "2000", "6969",
46 "1122", "1313", "2001", "1010"}; 47 "1122", "1313", "2001", "1010"};
47 48
49 using ActiveModeCallback = base::Callback<void(const QuickUnlockModeList&)>;
50
48 // Returns the active set of quick unlock modes. 51 // Returns the active set of quick unlock modes.
49 QuickUnlockModeList ComputeActiveModes(Profile* profile) { 52 void ComputeActiveModes(Profile* profile, const ActiveModeCallback& result) {
50 QuickUnlockModeList modes; 53 user_manager::User* user =
51 54 chromeos::ProfileHelper::Get()->GetUserByProfile(profile);
52 chromeos::quick_unlock::QuickUnlockStorage* quick_unlock_storage = 55 chromeos::quick_unlock::PinBackend::IsSet(
53 chromeos::quick_unlock::QuickUnlockFactory::GetForProfile(profile); 56 user->GetAccountId(),
54 if (quick_unlock_storage && quick_unlock_storage->pin_storage()->IsPinSet()) 57 base::Bind(
55 modes.push_back(quick_unlock_private::QUICK_UNLOCK_MODE_PIN); 58 [](const ActiveModeCallback& result, bool is_set) {
56 59 QuickUnlockModeList modes;
57 return modes; 60 if (is_set)
61 modes.push_back(quick_unlock_private::QUICK_UNLOCK_MODE_PIN);
62 result.Run(modes);
63 },
64 result));
58 } 65 }
59 66
60 // Returns true if |a| and |b| contain the same elements. The elements do not 67 // Returns true if |a| and |b| contain the same elements. The elements do not
61 // need to be in the same order. 68 // need to be in the same order.
62 bool AreModesEqual(const QuickUnlockModeList& a, const QuickUnlockModeList& b) { 69 bool AreModesEqual(const QuickUnlockModeList& a, const QuickUnlockModeList& b) {
63 if (a.size() != b.size()) 70 if (a.size() != b.size())
64 return false; 71 return false;
65 72
66 // This is a slow comparison algorithm, but the number of entries in |a| and 73 // This is a slow comparison algorithm, but the number of entries in |a| and
67 // |b| will always be very low (0-3 items) so it doesn't matter. 74 // |b| will always be very low (0-3 items) so it doesn't matter.
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after
190 197
191 QuickUnlockPrivateGetActiveModesFunction:: 198 QuickUnlockPrivateGetActiveModesFunction::
192 QuickUnlockPrivateGetActiveModesFunction() 199 QuickUnlockPrivateGetActiveModesFunction()
193 : chrome_details_(this) {} 200 : chrome_details_(this) {}
194 201
195 QuickUnlockPrivateGetActiveModesFunction:: 202 QuickUnlockPrivateGetActiveModesFunction::
196 ~QuickUnlockPrivateGetActiveModesFunction() {} 203 ~QuickUnlockPrivateGetActiveModesFunction() {}
197 204
198 ExtensionFunction::ResponseAction 205 ExtensionFunction::ResponseAction
199 QuickUnlockPrivateGetActiveModesFunction::Run() { 206 QuickUnlockPrivateGetActiveModesFunction::Run() {
200 const QuickUnlockModeList modes = 207 ComputeActiveModes(
201 ComputeActiveModes(chrome_details_.GetProfile()); 208 chrome_details_.GetProfile(),
202 return RespondNow(ArgumentList(GetActiveModes::Results::Create(modes))); 209 base::Bind(&QuickUnlockPrivateGetActiveModesFunction::OnGetActiveModes,
210 this));
211
212 return RespondLater();
213 }
214
215 void QuickUnlockPrivateGetActiveModesFunction::OnGetActiveModes(
216 const std::vector<api::quick_unlock_private::QuickUnlockMode>& modes) {
217 Respond(ArgumentList(GetActiveModes::Results::Create(modes)));
203 } 218 }
204 219
205 // quickUnlockPrivate.checkCredential 220 // quickUnlockPrivate.checkCredential
206 221
207 QuickUnlockPrivateCheckCredentialFunction:: 222 QuickUnlockPrivateCheckCredentialFunction::
208 QuickUnlockPrivateCheckCredentialFunction() {} 223 QuickUnlockPrivateCheckCredentialFunction() {}
209 224
210 QuickUnlockPrivateCheckCredentialFunction:: 225 QuickUnlockPrivateCheckCredentialFunction::
211 ~QuickUnlockPrivateCheckCredentialFunction() {} 226 ~QuickUnlockPrivateCheckCredentialFunction() {}
212 227
(...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after
355 } 370 }
356 371
357 void QuickUnlockPrivateSetModesFunction::OnAuthFailure( 372 void QuickUnlockPrivateSetModesFunction::OnAuthFailure(
358 const chromeos::AuthFailure& error) { 373 const chromeos::AuthFailure& error) {
359 Respond(ArgumentList(SetModes::Results::Create(false))); 374 Respond(ArgumentList(SetModes::Results::Create(false)));
360 Release(); // Balanced in Run(). 375 Release(); // Balanced in Run().
361 } 376 }
362 377
363 void QuickUnlockPrivateSetModesFunction::OnAuthSuccess( 378 void QuickUnlockPrivateSetModesFunction::OnAuthSuccess(
364 const chromeos::UserContext& user_context) { 379 const chromeos::UserContext& user_context) {
365 const QuickUnlockModeList initial_modes = 380 ComputeActiveModes(
366 ComputeActiveModes(chrome_details_.GetProfile()); 381 chrome_details_.GetProfile(),
367 ApplyModeChange(); 382 base::Bind(
368 const QuickUnlockModeList updated_modes = 383 &QuickUnlockPrivateSetModesFunction::OnGetActiveModesAfterAuthSuccess,
369 ComputeActiveModes(chrome_details_.GetProfile()); 384 base::Unretained(this), user_context));
385 }
370 386
387 void QuickUnlockPrivateSetModesFunction::OnGetActiveModesAfterAuthSuccess(
388 const chromeos::UserContext& user_context,
389 const std::vector<api::quick_unlock_private::QuickUnlockMode>&
390 initial_modes) {
391 std::vector<QuickUnlockMode> updated_modes = ApplyModeChange(user_context);
371 if (!AreModesEqual(initial_modes, updated_modes)) 392 if (!AreModesEqual(initial_modes, updated_modes))
372 FireEvent(updated_modes); 393 FireEvent(updated_modes);
373 394
374 Respond(ArgumentList(SetModes::Results::Create(true))); 395 Respond(ArgumentList(SetModes::Results::Create(true)));
375 Release(); // Balanced in Run(). 396 Release(); // Balanced in Run().
376 } 397 }
377 398
378 void QuickUnlockPrivateSetModesFunction::ApplyModeChange() { 399 std::vector<QuickUnlockMode>
400 QuickUnlockPrivateSetModesFunction::ApplyModeChange(
401 const chromeos::UserContext& user_context) {
379 // This function is setup so it is easy to add another quick unlock mode while 402 // This function is setup so it is easy to add another quick unlock mode while
380 // following all of the invariants, which are: 403 // following all of the invariants, which are:
381 // 404 //
382 // 1: If an unlock type is not specified, it should be deactivated. 405 // 1: If an unlock type is not specified, it should be deactivated.
383 // 2: If a credential for an unlock type is empty, it should not be touched. 406 // 2: If a credential for an unlock type is empty, it should not be touched.
384 // 3: Otherwise, the credential should be set to the new value. 407 // 3: Otherwise, the credential should be set to the new value.
385 408
409 std::vector<QuickUnlockMode> newly_active_modes;
410
386 bool update_pin = true; 411 bool update_pin = true;
387 std::string pin_credential; 412 std::string pin_credential;
388 413
389 // Compute needed changes. 414 // Compute needed changes.
390 for (size_t i = 0; i < params_->modes.size(); ++i) { 415 for (size_t i = 0; i < params_->modes.size(); ++i) {
391 const QuickUnlockMode mode = params_->modes[i]; 416 const QuickUnlockMode mode = params_->modes[i];
392 const std::string& credential = params_->credentials[i]; 417 const std::string& credential = params_->credentials[i];
393 418
394 if (mode == quick_unlock_private::QUICK_UNLOCK_MODE_PIN) { 419 if (mode == quick_unlock_private::QUICK_UNLOCK_MODE_PIN) {
395 update_pin = !credential.empty(); 420 update_pin = !credential.empty();
396 pin_credential = credential; 421 pin_credential = credential;
422 newly_active_modes.push_back(QuickUnlockMode::QUICK_UNLOCK_MODE_PIN);
397 } 423 }
398 } 424 }
399 425
400 // Apply changes. 426 // Apply changes.
401 if (update_pin) { 427 if (update_pin) {
402 Profile* profile = chrome_details_.GetProfile(); 428 if (pin_credential.empty()) {
403 chromeos::quick_unlock::QuickUnlockStorage* quick_unlock_storage = 429 chromeos::quick_unlock::PinBackend::Remove(user_context);
404 chromeos::quick_unlock::QuickUnlockFactory::GetForProfile(profile); 430 } else {
431 chromeos::quick_unlock::PinBackend::Set(user_context, pin_credential);
405 432
406 if (pin_credential.empty()) { 433 Profile* profile = chrome_details_.GetProfile();
407 quick_unlock_storage->pin_storage()->RemovePin(); 434 chromeos::quick_unlock::QuickUnlockStorage* quick_unlock_storage =
408 } else { 435 chromeos::quick_unlock::QuickUnlockFactory::GetForProfile(profile);
409 quick_unlock_storage->pin_storage()->SetPin(pin_credential);
410 quick_unlock_storage->MarkStrongAuth(); 436 quick_unlock_storage->MarkStrongAuth();
411 } 437 }
412 } 438 }
439
440 return newly_active_modes;
achuithb 2017/05/13 01:01:57 So that can only be empty or have the QUICK_UNLOCK
jdufault 2017/06/06 18:17:05 Yep.
413 } 441 }
414 442
415 // Triggers a quickUnlockPrivate.onActiveModesChanged change event. 443 // Triggers a quickUnlockPrivate.onActiveModesChanged change event.
416 void QuickUnlockPrivateSetModesFunction::FireEvent( 444 void QuickUnlockPrivateSetModesFunction::FireEvent(
417 const QuickUnlockModeList& modes) { 445 const QuickUnlockModeList& modes) {
418 // Allow unit tests to override how events are raised/handled. 446 // Allow unit tests to override how events are raised/handled.
419 if (!modes_changed_handler_.is_null()) { 447 if (!modes_changed_handler_.is_null()) {
420 modes_changed_handler_.Run(modes); 448 modes_changed_handler_.Run(modes);
421 return; 449 return;
422 } 450 }
423 451
424 std::unique_ptr<base::ListValue> args = OnActiveModesChanged::Create(modes); 452 std::unique_ptr<base::ListValue> args = OnActiveModesChanged::Create(modes);
425 std::unique_ptr<Event> event( 453 std::unique_ptr<Event> event(
426 new Event(events::QUICK_UNLOCK_PRIVATE_ON_ACTIVE_MODES_CHANGED, 454 new Event(events::QUICK_UNLOCK_PRIVATE_ON_ACTIVE_MODES_CHANGED,
427 OnActiveModesChanged::kEventName, std::move(args))); 455 OnActiveModesChanged::kEventName, std::move(args)));
428 EventRouter::Get(browser_context())->BroadcastEvent(std::move(event)); 456 EventRouter::Get(browser_context())->BroadcastEvent(std::move(event));
429 } 457 }
430 458
431 } // namespace extensions 459 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698