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

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 10 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
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|.
jdufault 2016/11/01 18:56:44 Update name/comment to match impl.
sammiequon 2016/11/02 18:05:25 Done.
68 std::vector<CredentialProblem> IsPinValid(const std::string& pin) {
69 std::vector<CredentialProblem> problems;
70 if (!std::all_of(pin.begin(), pin.end(), ::isdigit))
71 problems.push_back(CredentialProblem::CREDENTIAL_PROBLEM_CONTAINS_NONDIGIT);
72 return problems;
73 }
74
75 // Returns a list of problems (if any) for a given |pin| given the policies in
76 // the |profile|. If |out_min_length| and/or |out_max_length| are valid
77 // pointers, return the profile min/max length as well.
jdufault 2016/11/01 18:56:43 Update the comments for all of these methods to de
sammiequon 2016/11/02 18:05:24 Done.
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)
88 max_length = min_length;
89
90 if (out_min_length)
91 *out_min_length = min_length;
92 if (out_max_length)
93 *out_max_length = max_length;
94
95 std::vector<CredentialProblem> problems;
96 // Check if the pin is shorter than the minimum specified length.
jdufault 2016/11/01 18:56:44 nit: newline above
sammiequon 2016/11/02 18:05:25 Done.
97 if (int{pin.size()} < min_length)
98 problems.push_back(CredentialProblem::CREDENTIAL_PROBLEM_TOO_SHORT);
99
100 // If the maximum specified length is zero, there is no maximum length.
101 // Otherwise check if the pin is longer than the maximum specified length.
102 if (max_length != 0 && int{pin.size()} > max_length)
103 problems.push_back(CredentialProblem::CREDENTIAL_PROBLEM_TOO_LONG);
104
105 return problems;
106 }
107
108 // Returns a list of problems (if any) for a given |pin| given the policies in
109 // the |profile|.
110 std::vector<CredentialProblem> IsPinDifficultEnough(const std::string& pin) {
111 std::vector<CredentialProblem> problems;
112
113 // If the pin length is |kPinCheckWeakThreshold| or less, there is no need to
114 // check for same character and increasing pin.
115 if (int{pin.size()} <= kPinCheckWeakThreshold)
116 return problems;
117
118 // Check for same digits, increasing pin simutaneously.
119 bool is_same = true;
jdufault 2016/11/01 18:56:43 We should be warning for the top-10 most common PI
sammiequon 2016/11/02 18:05:24 I added a TODO. I don't think adding this would ca
120 bool is_increasing = true;
121 bool is_decreasing = true;
122 for (int i = 1; i < int{pin.length()}; ++i) {
123 const char previous = pin[i - 1];
124 const char current = pin[i];
125
126 is_same = is_same && (current == previous);
127 is_increasing = is_increasing && (current == previous + 1);
128 is_decreasing = is_decreasing && (current == previous - 1);
129 }
130
131 // Pin is considered weak if either of these is met.
132 if (is_same || is_increasing || is_decreasing)
133 problems.push_back(CredentialProblem::CREDENTIAL_PROBLEM_TOO_WEAK);
134
135 return problems;
136 }
137
138 void AppendToProblemList(std::vector<CredentialProblem>& list_to_append_to,
139 const std::vector<CredentialProblem>& list_to_append) {
140 list_to_append_to.insert(list_to_append_to.end(), list_to_append.begin(),
141 list_to_append.end());
142 }
143
59 } // namespace 144 } // namespace
60 145
61 // quickUnlockPrivate.getAvailableModes 146 // quickUnlockPrivate.getAvailableModes
62 147
63 QuickUnlockPrivateGetAvailableModesFunction:: 148 QuickUnlockPrivateGetAvailableModesFunction::
64 QuickUnlockPrivateGetAvailableModesFunction() 149 QuickUnlockPrivateGetAvailableModesFunction()
65 : chrome_details_(this) {} 150 : chrome_details_(this) {}
66 151
67 QuickUnlockPrivateGetAvailableModesFunction:: 152 QuickUnlockPrivateGetAvailableModesFunction::
68 ~QuickUnlockPrivateGetAvailableModesFunction() {} 153 ~QuickUnlockPrivateGetAvailableModesFunction() {}
(...skipping 17 matching lines...) Expand all
86 QuickUnlockPrivateGetActiveModesFunction:: 171 QuickUnlockPrivateGetActiveModesFunction::
87 ~QuickUnlockPrivateGetActiveModesFunction() {} 172 ~QuickUnlockPrivateGetActiveModesFunction() {}
88 173
89 ExtensionFunction::ResponseAction 174 ExtensionFunction::ResponseAction
90 QuickUnlockPrivateGetActiveModesFunction::Run() { 175 QuickUnlockPrivateGetActiveModesFunction::Run() {
91 const QuickUnlockModeList modes = 176 const QuickUnlockModeList modes =
92 ComputeActiveModes(chrome_details_.GetProfile()); 177 ComputeActiveModes(chrome_details_.GetProfile());
93 return RespondNow(ArgumentList(GetActiveModes::Results::Create(modes))); 178 return RespondNow(ArgumentList(GetActiveModes::Results::Create(modes)));
94 } 179 }
95 180
181 // quickUnlockPrivate.checkCredential
182
183 QuickUnlockPrivateCheckCredentialFunction::
184 QuickUnlockPrivateCheckCredentialFunction()
185 : chrome_details_(this) {}
186
187 QuickUnlockPrivateCheckCredentialFunction::
188 ~QuickUnlockPrivateCheckCredentialFunction() {}
189
190 ExtensionFunction::ResponseAction
191 QuickUnlockPrivateCheckCredentialFunction::Run() {
192 params_ = CheckCredential::Params::Create(*args_);
193 EXTENSION_FUNCTION_VALIDATE(params_.get());
194
195 auto result = base::MakeUnique<CredentialCheck>();
196 result->min_length = base::MakeUnique<int>();
197 result->max_length = base::MakeUnique<int>();
198
199 // Only handles pins for now.
200 if (params_->mode != QuickUnlockMode::QUICK_UNLOCK_MODE_PIN) {
201 return RespondNow(ArgumentList(CheckCredential::Results::Create(*result)));
202 }
203
204 std::string credential = params_->credential;
205
206 Profile* profile = chrome_details_.GetProfile();
207 PrefService* pref_service = profile->GetPrefs();
208 bool allow_weak = pref_service->GetBoolean(prefs::kPinUnlockAllowWeakPins);
209
210 // Check and return the problems. |result->min_length| and
211 // |result->max_length| also get the minimum length and maximum length from
212 // policy here.
213 std::vector<CredentialProblem> validity_problems = IsPinValid(credential);
214 std::vector<CredentialProblem> length_problems = IsPinLengthValid(
jdufault 2016/11/01 18:56:43 // Note: This function call writes values to |resu
sammiequon 2016/11/02 18:05:25 Done.
215 credential, profile, result->min_length.get(), result->max_length.get());
216 std::vector<CredentialProblem> weak_problems =
217 IsPinDifficultEnough(credential);
218
219 // Append the results from various checks to the appropriate list in |result|.
220 // |validity_problems| and |length_problems| are always errors, while
221 // |weak_problems| are errors or warnings depending on the policy.
222 AppendToProblemList(result->errors, validity_problems);
jdufault 2016/11/01 18:56:43 What about dropping the temporaries and inlining t
sammiequon 2016/11/02 18:05:24 I think this make it harder to read because of wri
223 AppendToProblemList(result->errors, length_problems);
224 AppendToProblemList(allow_weak ? result->warnings : result->errors,
225 weak_problems);
226
227 return RespondNow(ArgumentList(CheckCredential::Results::Create(*result)));
228 }
229
96 // quickUnlockPrivate.setModes 230 // quickUnlockPrivate.setModes
97 231
98 QuickUnlockPrivateSetModesFunction::QuickUnlockPrivateSetModesFunction() 232 QuickUnlockPrivateSetModesFunction::QuickUnlockPrivateSetModesFunction()
99 : chrome_details_(this) {} 233 : chrome_details_(this) {}
100 234
101 QuickUnlockPrivateSetModesFunction::~QuickUnlockPrivateSetModesFunction() {} 235 QuickUnlockPrivateSetModesFunction::~QuickUnlockPrivateSetModesFunction() {}
102 236
103 void QuickUnlockPrivateSetModesFunction::SetAuthenticatorAllocatorForTesting( 237 void QuickUnlockPrivateSetModesFunction::SetAuthenticatorAllocatorForTesting(
104 const QuickUnlockPrivateSetModesFunction::AuthenticatorAllocator& 238 const QuickUnlockPrivateSetModesFunction::AuthenticatorAllocator&
105 allocator) { 239 allocator) {
(...skipping 10 matching lines...) Expand all
116 EXTENSION_FUNCTION_VALIDATE(params_.get()); 250 EXTENSION_FUNCTION_VALIDATE(params_.get());
117 251
118 if (params_->modes.size() != params_->credentials.size()) 252 if (params_->modes.size() != params_->credentials.size())
119 return RespondNow(Error(kModesAndCredentialsLengthMismatch)); 253 return RespondNow(Error(kModesAndCredentialsLengthMismatch));
120 254
121 if (params_->modes.size() > 1) 255 if (params_->modes.size() > 1)
122 return RespondNow(Error(kMultipleModesNotSupported)); 256 return RespondNow(Error(kMultipleModesNotSupported));
123 257
124 // Verify every credential is numeric. 258 // Verify every credential is numeric.
125 for (const std::string& credential : params_->credentials) { 259 for (const std::string& credential : params_->credentials) {
126 if (!std::all_of(credential.begin(), credential.end(), ::isdigit)) 260 if (!IsPinValid(credential).empty())
jdufault 2016/11/01 18:56:43 This should be in the loop below and happening on
sammiequon 2016/11/02 18:05:25 Done.
127 return RespondNow(ArgumentList(SetModes::Results::Create(false))); 261 return RespondNow(ArgumentList(SetModes::Results::Create(false)));
128 } 262 }
129 263
264 // Verify every credential is valid based on policies.
265 bool allow_weak = chrome_details_.GetProfile()->GetPrefs()->GetBoolean(
266 prefs::kPinUnlockAllowWeakPins);
267 for (size_t j = 0; j < params_->modes.size(); ++j) {
268 if (params_->credentials[j].empty())
269 continue;
270
271 if (params_->modes[j] != QuickUnlockMode::QUICK_UNLOCK_MODE_PIN)
jdufault 2016/11/01 18:56:43 I would invert the if (not do a continue), ie,
sammiequon 2016/11/02 18:05:24 Done.
272 continue;
273
274 // Check if there any errors with the length of the pin.
jdufault 2016/11/01 18:56:43 Drop comment; Generally, comments should call out
sammiequon 2016/11/02 18:05:24 Done.
275 if (!IsPinLengthValid(params_->credentials[j], chrome_details_.GetProfile(),
276 nullptr, nullptr)
277 .empty()) {
278 return RespondNow(ArgumentList(SetModes::Results::Create(false)));
279 }
280 // Check if there any errors with the difficulty of the pin.
jdufault 2016/11/01 18:56:43 Drop comment
sammiequon 2016/11/02 18:05:24 Done.
281 if (!allow_weak && !IsPinDifficultEnough(params_->credentials[j]).empty())
282 return RespondNow(ArgumentList(SetModes::Results::Create(false)));
283 }
284
130 user_manager::User* user = chromeos::ProfileHelper::Get()->GetUserByProfile( 285 user_manager::User* user = chromeos::ProfileHelper::Get()->GetUserByProfile(
131 chrome_details_.GetProfile()); 286 chrome_details_.GetProfile());
132 chromeos::UserContext user_context(user->GetAccountId()); 287 chromeos::UserContext user_context(user->GetAccountId());
133 user_context.SetKey(chromeos::Key(params_->account_password)); 288 user_context.SetKey(chromeos::Key(params_->account_password));
134 289
135 // Lazily allocate the authenticator. We do this here, instead of in the ctor, 290 // Lazily allocate the authenticator. We do this here, instead of in the ctor,
136 // so that tests can install a fake. 291 // so that tests can install a fake.
137 if (authenticator_allocator_.is_null()) 292 if (authenticator_allocator_.is_null())
138 extended_authenticator_ = chromeos::ExtendedAuthenticator::Create(this); 293 extended_authenticator_ = chromeos::ExtendedAuthenticator::Create(this);
139 else 294 else
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after
223 } 378 }
224 379
225 std::unique_ptr<base::ListValue> args = OnActiveModesChanged::Create(modes); 380 std::unique_ptr<base::ListValue> args = OnActiveModesChanged::Create(modes);
226 std::unique_ptr<Event> event( 381 std::unique_ptr<Event> event(
227 new Event(events::QUICK_UNLOCK_PRIVATE_ON_ACTIVE_MODES_CHANGED, 382 new Event(events::QUICK_UNLOCK_PRIVATE_ON_ACTIVE_MODES_CHANGED,
228 OnActiveModesChanged::kEventName, std::move(args))); 383 OnActiveModesChanged::kEventName, std::move(args)));
229 EventRouter::Get(browser_context())->BroadcastEvent(std::move(event)); 384 EventRouter::Get(browser_context())->BroadcastEvent(std::move(event));
230 } 385 }
231 386
232 } // namespace extensions 387 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698