Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/chromeos/extensions/quick_unlock_private/quick_unlock_p rivate_api.h" | |
| 6 | |
| 7 #include "chrome/browser/chromeos/login/quick_unlock/pin_storage.h" | |
| 8 #include "chrome/browser/chromeos/login/quick_unlock/pin_storage_factory.h" | |
| 9 #include "chrome/browser/chromeos/profiles/profile_helper.h" | |
| 10 #include "chrome/common/extensions/api/quick_unlock_private.h" | |
| 11 #include "chromeos/login/auth/extended_authenticator.h" | |
| 12 #include "chromeos/login/auth/user_context.h" | |
| 13 | |
| 14 namespace CheckPassword = extensions::api::quick_unlock_private::CheckPassword; | |
| 15 namespace SetModes = extensions::api::quick_unlock_private::SetModes; | |
| 16 namespace GetActiveModes = | |
| 17 extensions::api::quick_unlock_private::GetActiveModes; | |
| 18 namespace GetAvailableModes = | |
| 19 extensions::api::quick_unlock_private::GetAvailableModes; | |
| 20 namespace quick_unlock_private = extensions::api::quick_unlock_private; | |
| 21 | |
| 22 namespace extensions { | |
| 23 | |
| 24 namespace { | |
| 25 const char kModesAndPasswordLengthMismatch[] = | |
| 26 "|modes| and |passwords| must have the same number of elements"; | |
| 27 const char kMultipleModesNotSupported[] = | |
| 28 "Currently at most one quick unlock mode can be active."; | |
| 29 | |
| 30 QuickUnlockPrivateCheckPasswordFunction::CreateAuthenticator | |
| 31 create_authenticator_ = nullptr; | |
| 32 } | |
| 33 | |
| 34 //////////// quickUnlockPrivate.checkPassword | |
| 35 | |
| 36 /* static */ | |
| 37 void QuickUnlockPrivateCheckPasswordFunction::SetCreateAuthenticatorForTesting( | |
| 38 QuickUnlockPrivateCheckPasswordFunction::CreateAuthenticator allocator) { | |
| 39 create_authenticator_ = allocator; | |
| 40 } | |
| 41 | |
| 42 QuickUnlockPrivateCheckPasswordFunction:: | |
| 43 QuickUnlockPrivateCheckPasswordFunction() | |
| 44 : chrome_details_(this) { | |
| 45 // Setup a fake authenticator if we are testing. | |
| 46 if (create_authenticator_) | |
| 47 extended_authenticator_ = create_authenticator_(this); | |
| 48 else | |
| 49 extended_authenticator_ = chromeos::ExtendedAuthenticator::Create(this); | |
| 50 } | |
| 51 | |
| 52 QuickUnlockPrivateCheckPasswordFunction:: | |
| 53 ~QuickUnlockPrivateCheckPasswordFunction() {} | |
| 54 | |
| 55 ExtensionFunction::ResponseAction | |
| 56 QuickUnlockPrivateCheckPasswordFunction::Run() { | |
| 57 std::unique_ptr<CheckPassword::Params> params( | |
| 58 CheckPassword::Params::Create(*args_)); | |
| 59 EXTENSION_FUNCTION_VALIDATE(params.get()); | |
| 60 | |
| 61 user_manager::User* user = chromeos::ProfileHelper::Get()->GetUserByProfile( | |
| 62 chrome_details_.GetProfile()); | |
| 63 chromeos::UserContext user_context(user->GetAccountId()); | |
| 64 user_context.SetKey(chromeos::Key(params->password)); | |
| 65 | |
| 66 // The extension function needs to stay alive while the authenticator is | |
| 67 // running the password check. Add a ref before the authenticator starts, and | |
| 68 // remove the ref after it invokes one of the OnAuth* callbacks. | |
| 69 AddRef(); | |
| 70 | |
| 71 content::BrowserThread::PostTask( | |
| 72 content::BrowserThread::UI, FROM_HERE, | |
| 73 base::Bind(&chromeos::ExtendedAuthenticator::AuthenticateToCheck, | |
| 74 extended_authenticator_.get(), user_context, base::Closure())); | |
| 75 | |
| 76 return RespondLater(); | |
| 77 } | |
| 78 | |
| 79 void QuickUnlockPrivateCheckPasswordFunction::OnAuthFailure( | |
| 80 const chromeos::AuthFailure& error) { | |
| 81 results_ = CheckPassword::Results::Create(false); | |
| 82 SendResponse(true); | |
| 83 Release(); | |
| 84 } | |
| 85 | |
| 86 void QuickUnlockPrivateCheckPasswordFunction::OnAuthSuccess( | |
| 87 const chromeos::UserContext& user_context) { | |
| 88 results_ = CheckPassword::Results::Create(true); | |
| 89 SendResponse(true); | |
| 90 Release(); | |
| 91 } | |
| 92 | |
| 93 //////////// quickUnlockPrivate.setModes | |
| 94 | |
| 95 QuickUnlockPrivateSetModesFunction::QuickUnlockPrivateSetModesFunction() | |
| 96 : chrome_details_(this) {} | |
| 97 | |
| 98 QuickUnlockPrivateSetModesFunction::~QuickUnlockPrivateSetModesFunction() {} | |
| 99 | |
| 100 ExtensionFunction::ResponseAction QuickUnlockPrivateSetModesFunction::Run() { | |
| 101 std::unique_ptr<SetModes::Params> params(SetModes::Params::Create(*args_)); | |
| 102 EXTENSION_FUNCTION_VALIDATE(params.get()); | |
| 103 | |
| 104 if (params->modes.size() != params->passwords.size()) | |
| 105 return RespondNow(Error(kModesAndPasswordLengthMismatch)); | |
| 106 | |
| 107 if (params->modes.size() > 1) | |
| 108 return RespondNow(Error(kMultipleModesNotSupported)); | |
| 109 | |
| 110 // This function is setup so it is easy to add another quick unlock mode while | |
| 111 // following all of the invariants, which are: | |
| 112 // | |
| 113 // 1: If an unlock type is not specified, it should be deactivated. | |
| 114 // 2: If a password for an unlock type is empty, it should not be touched. | |
| 115 // 3: Otherwise, the password should be set to the new value. | |
| 116 | |
| 117 bool update_pin = true; | |
| 118 std::string pin_password; | |
| 119 | |
| 120 // Compute values for update_pin, pin_password, etc. | |
| 121 for (size_t i = 0; i < params->modes.size(); ++i) { | |
| 122 const quick_unlock_private::QuickUnlockMode mode = params->modes[i]; | |
| 123 const std::string password = params->passwords[i]; | |
| 124 | |
| 125 if (mode == quick_unlock_private::QUICK_UNLOCK_MODE_PIN) { | |
| 126 update_pin = !password.empty(); | |
| 127 pin_password = password; | |
| 128 } | |
| 129 } | |
| 130 | |
| 131 // Use the computed update_pin, pin_password, etc, values. | |
| 132 if (update_pin) { | |
| 133 chromeos::PinStorage* pin_storage = | |
| 134 chromeos::PinStorageFactory::GetForProfile( | |
| 135 chrome_details_.GetProfile()); | |
| 136 if (pin_password.empty()) | |
| 137 pin_storage->RemovePin(); | |
| 138 else | |
| 139 pin_storage->SetPin(pin_password); | |
| 140 } | |
| 141 | |
| 142 return RespondNow(ArgumentList(SetModes::Results::Create(true))); | |
| 143 } | |
| 144 | |
| 145 //////////// quickUnlockPrivate.getActiveModes | |
| 146 | |
| 147 QuickUnlockPrivateGetActiveModesFunction:: | |
| 148 QuickUnlockPrivateGetActiveModesFunction() | |
| 149 : chrome_details_(this) {} | |
| 150 | |
| 151 QuickUnlockPrivateGetActiveModesFunction:: | |
| 152 ~QuickUnlockPrivateGetActiveModesFunction() {} | |
| 153 | |
| 154 ExtensionFunction::ResponseAction | |
| 155 QuickUnlockPrivateGetActiveModesFunction::Run() { | |
| 156 std::vector<quick_unlock_private::QuickUnlockMode> modes; | |
| 157 | |
| 158 chromeos::PinStorage* pin_storage = | |
| 159 chromeos::PinStorageFactory::GetForProfile(chrome_details_.GetProfile()); | |
| 160 if (pin_storage->IsPinSet()) | |
| 161 modes.push_back(quick_unlock_private::QUICK_UNLOCK_MODE_PIN); | |
| 162 | |
| 163 return RespondNow(ArgumentList(GetActiveModes::Results::Create(modes))); | |
| 164 } | |
| 165 | |
| 166 //////////// quickUnlockPrivate.getAvailableModes | |
| 167 | |
| 168 QuickUnlockPrivateGetAvailableModesFunction:: | |
| 169 QuickUnlockPrivateGetAvailableModesFunction() | |
| 170 : chrome_details_(this) {} | |
| 171 | |
| 172 QuickUnlockPrivateGetAvailableModesFunction:: | |
| 173 ~QuickUnlockPrivateGetAvailableModesFunction() {} | |
| 174 | |
| 175 ExtensionFunction::ResponseAction | |
| 176 QuickUnlockPrivateGetAvailableModesFunction::Run() { | |
| 177 // TODO(jdufault): Check for policy and do not return PIN if policy makes it | |
| 178 // unavailable. See crbug.com/612271. | |
| 179 std::vector<quick_unlock_private::QuickUnlockMode> modes = { | |
|
achuithb
2016/05/23 19:10:40
const?
jdufault
2016/05/27 20:00:21
Done.
| |
| 180 quick_unlock_private::QUICK_UNLOCK_MODE_PIN}; | |
| 181 | |
| 182 return RespondNow(ArgumentList(GetAvailableModes::Results::Create(modes))); | |
| 183 } | |
| 184 | |
| 185 } // namespace extensions | |
| OLD | NEW |