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

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 6 errors. Created 4 years, 2 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 "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
36 // weakness.
37 const int kPinCheckWeakThreshold = 2;
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 // Returns a list of problems (if any) for a given |pin| given the policies in
68 // the |profile|. If |out_min_length| and/or |out_max_length| are valid
69 // pointers, return the profile min/max length as well.
70 std::vector<CredentialProblem> IsPinLengthValid(const std::string& pin,
71 Profile* profile,
72 int* out_min_length,
73 int* out_max_length) {
74 std::vector<CredentialProblem> problems;
75 PrefService* pref_service = profile->GetPrefs();
76 int min_length = pref_service->GetInteger(prefs::kPinUnlockMinimumLength);
77 int max_length = pref_service->GetInteger(prefs::kPinUnlockMaximumLength);
78 // If the maximum length is nonzero and shorter than the minimum length, the
79 // maximum length is the minimum length.
80 if (max_length != 0 && max_length < min_length)
81 max_length = min_length;
82
83 if (out_min_length)
84 *out_min_length = min_length;
85 if (out_max_length)
86 *out_max_length = max_length;
87
88 // Check if the pin is shorter than the minimum specified length.
89 if (int{pin.size()} < min_length) {
90 problems.push_back(CredentialProblem::CREDENTIAL_PROBLEM_TOO_SHORT);
91 }
92
93 // If the maximum specified length is zero, there is no maximum length.
94 // Otherwise check if the pin is longer than the maximum specified length.
95 if (max_length != 0 && int{pin.size()} > max_length) {
96 problems.push_back(CredentialProblem::CREDENTIAL_PROBLEM_TOO_LONG);
97 }
98
99 return problems;
100 }
101
102 // Returns a list of problems (if any) for a given |pin| given the policies in
103 // the |profile|. If |out_allow_weak_pin| is a valid pointer, return the whether
104 // the |profile| allows weak pins as well.
105 std::vector<CredentialProblem> IsPinDifficultEnough(const std::string& pin,
106 Profile* profile,
107 bool* out_allow_weak_pin) {
108 std::vector<CredentialProblem> problems;
109 PrefService* pref_service = profile->GetPrefs();
110 bool allow_weak = pref_service->GetBoolean(prefs::kPinUnlockAllowWeakPins);
111 if (out_allow_weak_pin)
112 *out_allow_weak_pin = allow_weak;
113
114 // If the pin length is |kPinCheckWeakThreshold| or less, there is no need to
115 // check for same character and increasing pin.
116 if (int{pin.size()} <= kPinCheckWeakThreshold)
117 return problems;
118
119 // Check for same digits, increasing pin simutaneously.
120 bool is_same = true;
121 bool is_increasing = true;
122 bool is_decreasing = true;
123 for (int i = 1; i < int{pin.length()}; ++i) {
124 const char previous = pin[i - 1];
125 const char current = pin[i];
126
127 is_same = is_same && (current == previous);
128 is_increasing = is_increasing && (current == previous + 1);
129 is_decreasing = is_decreasing && (current == previous - 1);
130 }
131
132 // Pin is considered weak if either of these is met.
133 if ((is_same || is_increasing || is_decreasing)) {
134 problems.push_back(CredentialProblem::CREDENTIAL_PROBLEM_TOO_WEAK);
135 }
136
137 return problems;
138 }
139
59 } // namespace 140 } // namespace
60 141
61 // quickUnlockPrivate.getAvailableModes 142 // quickUnlockPrivate.getAvailableModes
62 143
63 QuickUnlockPrivateGetAvailableModesFunction:: 144 QuickUnlockPrivateGetAvailableModesFunction::
64 QuickUnlockPrivateGetAvailableModesFunction() 145 QuickUnlockPrivateGetAvailableModesFunction()
65 : chrome_details_(this) {} 146 : chrome_details_(this) {}
66 147
67 QuickUnlockPrivateGetAvailableModesFunction:: 148 QuickUnlockPrivateGetAvailableModesFunction::
68 ~QuickUnlockPrivateGetAvailableModesFunction() {} 149 ~QuickUnlockPrivateGetAvailableModesFunction() {}
(...skipping 17 matching lines...) Expand all
86 QuickUnlockPrivateGetActiveModesFunction:: 167 QuickUnlockPrivateGetActiveModesFunction::
87 ~QuickUnlockPrivateGetActiveModesFunction() {} 168 ~QuickUnlockPrivateGetActiveModesFunction() {}
88 169
89 ExtensionFunction::ResponseAction 170 ExtensionFunction::ResponseAction
90 QuickUnlockPrivateGetActiveModesFunction::Run() { 171 QuickUnlockPrivateGetActiveModesFunction::Run() {
91 const QuickUnlockModeList modes = 172 const QuickUnlockModeList modes =
92 ComputeActiveModes(chrome_details_.GetProfile()); 173 ComputeActiveModes(chrome_details_.GetProfile());
93 return RespondNow(ArgumentList(GetActiveModes::Results::Create(modes))); 174 return RespondNow(ArgumentList(GetActiveModes::Results::Create(modes)));
94 } 175 }
95 176
177 // quickUnlockPrivate.checkCredential
178
179 QuickUnlockPrivateCheckCredentialFunction::
180 QuickUnlockPrivateCheckCredentialFunction()
181 : chrome_details_(this) {}
182
183 QuickUnlockPrivateCheckCredentialFunction::
184 ~QuickUnlockPrivateCheckCredentialFunction() {}
185
186 ExtensionFunction::ResponseAction
187 QuickUnlockPrivateCheckCredentialFunction::Run() {
188 params_ = CheckCredential::Params::Create(*args_);
189 EXTENSION_FUNCTION_VALIDATE(params_.get());
190
191 auto result = base::MakeUnique<CredentialCheck>();
192 result->min_length = base::MakeUnique<int>();
193 result->max_length = base::MakeUnique<int>();
194 result->allow_weak_pins = base::MakeUnique<bool>();
195
196 // Only handles pins for now.
197 if (params_->mode != QuickUnlockMode::QUICK_UNLOCK_MODE_PIN) {
198 return RespondNow(ArgumentList(CheckCredential::Results::Create(*result)));
199 }
200
201 std::string tried_password = params_->credential;
202 if (!std::all_of(tried_password.begin(), tried_password.end(), ::isdigit))
203 return RespondNow(ArgumentList(CheckCredential::Results::Create(*result)));
204
205 Profile* profile = chrome_details_.GetProfile();
206 auto length_problems =
207 IsPinLengthValid(tried_password, profile, result->min_length.get(),
208 result->max_length.get());
209 auto weak_problems = IsPinDifficultEnough(tried_password, profile,
210 result->allow_weak_pins.get());
211 result->problems.insert(result->problems.end(), length_problems.begin(),
212 length_problems.end());
213 result->problems.insert(result->problems.end(), weak_problems.begin(),
214 weak_problems.end());
215
216 return RespondNow(ArgumentList(CheckCredential::Results::Create(*result)));
217 }
218
96 // quickUnlockPrivate.setModes 219 // quickUnlockPrivate.setModes
97 220
98 QuickUnlockPrivateSetModesFunction::QuickUnlockPrivateSetModesFunction() 221 QuickUnlockPrivateSetModesFunction::QuickUnlockPrivateSetModesFunction()
99 : chrome_details_(this) {} 222 : chrome_details_(this) {}
100 223
101 QuickUnlockPrivateSetModesFunction::~QuickUnlockPrivateSetModesFunction() {} 224 QuickUnlockPrivateSetModesFunction::~QuickUnlockPrivateSetModesFunction() {}
102 225
103 void QuickUnlockPrivateSetModesFunction::SetAuthenticatorAllocatorForTesting( 226 void QuickUnlockPrivateSetModesFunction::SetAuthenticatorAllocatorForTesting(
104 const QuickUnlockPrivateSetModesFunction::AuthenticatorAllocator& 227 const QuickUnlockPrivateSetModesFunction::AuthenticatorAllocator&
105 allocator) { 228 allocator) {
(...skipping 14 matching lines...) Expand all
120 243
121 if (params_->modes.size() > 1) 244 if (params_->modes.size() > 1)
122 return RespondNow(Error(kMultipleModesNotSupported)); 245 return RespondNow(Error(kMultipleModesNotSupported));
123 246
124 // Verify every credential is numeric. 247 // Verify every credential is numeric.
125 for (const std::string& credential : params_->credentials) { 248 for (const std::string& credential : params_->credentials) {
126 if (!std::all_of(credential.begin(), credential.end(), ::isdigit)) 249 if (!std::all_of(credential.begin(), credential.end(), ::isdigit))
127 return RespondNow(ArgumentList(SetModes::Results::Create(false))); 250 return RespondNow(ArgumentList(SetModes::Results::Create(false)));
128 } 251 }
129 252
253 // Verify every credential is valid based on policies.
254 for (size_t j = 0; j < params_->modes.size(); ++j) {
255 if (params_->modes[j] == QuickUnlockMode::QUICK_UNLOCK_MODE_PIN &&
256 !IsPinLengthValid(params_->credentials[j], chrome_details_.GetProfile(),
257 nullptr, nullptr)
258 .empty() &&
259 !IsPinDifficultEnough(params_->credentials[j],
260 chrome_details_.GetProfile(), nullptr)
261 .empty()) {
262 return RespondNow(ArgumentList(SetModes::Results::Create(false)));
263 }
264 }
265
130 user_manager::User* user = chromeos::ProfileHelper::Get()->GetUserByProfile( 266 user_manager::User* user = chromeos::ProfileHelper::Get()->GetUserByProfile(
131 chrome_details_.GetProfile()); 267 chrome_details_.GetProfile());
132 chromeos::UserContext user_context(user->GetAccountId()); 268 chromeos::UserContext user_context(user->GetAccountId());
133 user_context.SetKey(chromeos::Key(params_->account_password)); 269 user_context.SetKey(chromeos::Key(params_->account_password));
134 270
135 // Lazily allocate the authenticator. We do this here, instead of in the ctor, 271 // Lazily allocate the authenticator. We do this here, instead of in the ctor,
136 // so that tests can install a fake. 272 // so that tests can install a fake.
137 if (authenticator_allocator_.is_null()) 273 if (authenticator_allocator_.is_null())
138 extended_authenticator_ = chromeos::ExtendedAuthenticator::Create(this); 274 extended_authenticator_ = chromeos::ExtendedAuthenticator::Create(this);
139 else 275 else
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after
223 } 359 }
224 360
225 std::unique_ptr<base::ListValue> args = OnActiveModesChanged::Create(modes); 361 std::unique_ptr<base::ListValue> args = OnActiveModesChanged::Create(modes);
226 std::unique_ptr<Event> event( 362 std::unique_ptr<Event> event(
227 new Event(events::QUICK_UNLOCK_PRIVATE_ON_ACTIVE_MODES_CHANGED, 363 new Event(events::QUICK_UNLOCK_PRIVATE_ON_ACTIVE_MODES_CHANGED,
228 OnActiveModesChanged::kEventName, std::move(args))); 364 OnActiveModesChanged::kEventName, std::move(args)));
229 EventRouter::Get(browser_context())->BroadcastEvent(std::move(event)); 365 EventRouter::Get(browser_context())->BroadcastEvent(std::move(event));
230 } 366 }
231 367
232 } // namespace extensions 368 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698