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

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 2 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 IsCredentialUsable = quick_unlock_private::IsCredentialUsable;
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 CredentialRequirementFailure =
25 quick_unlock_private::CredentialRequirementFailure;
26 using CredentialRequirement = quick_unlock_private::CredentialRequirement;
21 using QuickUnlockMode = quick_unlock_private::QuickUnlockMode; 27 using QuickUnlockMode = quick_unlock_private::QuickUnlockMode;
22 using QuickUnlockModeList = std::vector<QuickUnlockMode>; 28 using QuickUnlockModeList = std::vector<QuickUnlockMode>;
23 29
24 namespace { 30 namespace {
25 31
26 const char kModesAndCredentialsLengthMismatch[] = 32 const char kModesAndCredentialsLengthMismatch[] =
27 "|modes| and |credentials| must have the same number of elements"; 33 "|modes| and |credentials| must have the same number of elements";
28 const char kMultipleModesNotSupported[] = 34 const char kMultipleModesNotSupported[] =
29 "At most one quick unlock mode can be active."; 35 "At most one quick unlock mode can be active.";
30 36
(...skipping 18 matching lines...) Expand all
49 // This is a slow comparison algorithm, but the number of entries in |a| and 55 // 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. 56 // |b| will always be very low (0-3 items) so it doesn't matter.
51 for (size_t i = 0; i < a.size(); ++i) { 57 for (size_t i = 0; i < a.size(); ++i) {
52 if (std::find(b.begin(), b.end(), a[i]) == b.end()) 58 if (std::find(b.begin(), b.end(), a[i]) == b.end())
53 return false; 59 return false;
54 } 60 }
55 61
56 return true; 62 return true;
57 } 63 }
58 64
65 // Check if a given |pin| is valid given the policies in the |profile|. If
66 // |result| is a valid pointer, the failures are added to |result|. Pass a
67 // nullptr for |result| if all we want to do is check the validity of |pin|.
jdufault 2016/10/04 22:57:27 nit: 'a nullptr' => 'null' null is preferred insi
sammiequon 2016/10/05 19:48:11 Done.
68 bool IsPinLengthValid(const std::string& pin,
69 Profile* profile,
70 CredentialRequirement* result) {
71 bool is_valid = true;
72 PrefService* pref_service = profile->GetPrefs();
73 int minimum_length = pref_service->GetInteger(prefs::kPinUnlockMinimumLength);
jdufault 2016/10/04 22:57:27 Name these min_length, max_length?
sammiequon 2016/10/05 19:48:11 Done.
74 int maximum_length = pref_service->GetInteger(prefs::kPinUnlockMaximumLength);
75 if (result) {
76 result->min_length = base::MakeUnique<int>(minimum_length);
77 result->max_length = base::MakeUnique<int>(maximum_length);
78 }
79
80 // Check if the pin is shorter than the minimum specified length.
81 if (int{pin.size()} < minimum_length) {
82 is_valid = false;
83 if (result) {
84 result->failures.push_back(CredentialRequirementFailure::
85 CREDENTIAL_REQUIREMENT_FAILURE_TOO_SHORT);
86 }
87 }
88
89 // If the maximum specified length is shorter or equal to the minimum
90 // specified length, there is no maximum length. Otherwise check if the pin is
91 // longer than the maximum specified length.
jdufault 2016/10/04 22:57:27 I think if the maximum length is shorter than the
sammiequon 2016/10/05 19:48:11 Done.
92 if (maximum_length > minimum_length && int{pin.size()} > maximum_length) {
93 is_valid = true;
94 if (result) {
95 result->failures.push_back(CredentialRequirementFailure::
96 CREDENTIAL_REQUIREMENT_FAILURE_TOO_LONG);
97 }
98 }
99
100 return is_valid;
101 }
102
103 // Check if a given |pin| is valid given the policies in the |profile|. If
104 // |result| is a valid pointer, the failures are added to |result|. Pass a
105 // nullptr for |result| if all we want to do is check the validity of |pin|.
jdufault 2016/10/04 22:57:27 nullptr => null (see above comment)
sammiequon 2016/10/05 19:48:11 Done.
106 bool IsPinDifficultEnough(const std::string& pin,
jdufault 2016/10/04 22:57:28 If result->allow_easy_pins is eliminated, this fun
sammiequon 2016/10/05 19:48:11 Done.
107 Profile* profile,
108 CredentialRequirement* result) {
109 PrefService* pref_service = profile->GetPrefs();
110 bool allow_easy = pref_service->GetBoolean(prefs::kPinUnlockAllowEasyPins);
111 if (result)
112 result->allow_easy_pins = base::MakeUnique<bool>(allow_easy);
113
114 // If the pin length is two or less, there is no need to check for same
115 // character and increasing pin.
116 const int pin_check_easy_threshold = 2;
jdufault 2016/10/04 22:57:27 Move this constant the top of the file and fix the
sammiequon 2016/10/05 19:48:11 Done.
117 if (int{pin.size()} <= pin_check_easy_threshold)
118 return allow_easy;
jdufault 2016/10/04 22:57:28 Shouldn't this always return true?
sammiequon 2016/10/05 19:48:11 Done.
119
120 // Check for same digits, increasing pin simutaneously.
121 bool is_same = true;
122 bool is_increasing = true;
123 bool first = true;
124 char last_char;
125 for (const char& c : pin) {
jdufault 2016/10/04 22:57:28 I'd remove the const&, the pointer is bigger than
sammiequon 2016/10/05 19:48:11 Done.
126 if (first) {
127 first = false;
128 last_char = c;
129 continue;
130 }
131 is_same = is_same && (c == last_char);
132 is_increasing = is_increasing && (c == last_char + 1);
133 last_char = c;
134 }
135
136 // Pin is considered weak if either of these is met.
137 if ((is_same || is_increasing) && result) {
138 result->failures.push_back(
139 CredentialRequirementFailure::CREDENTIAL_REQUIREMENT_FAILURE_TOO_WEAK);
140 }
141
142 return is_same || is_increasing;
143 }
144
59 } // namespace 145 } // namespace
60 146
61 // quickUnlockPrivate.getAvailableModes 147 // quickUnlockPrivate.getAvailableModes
62 148
63 QuickUnlockPrivateGetAvailableModesFunction:: 149 QuickUnlockPrivateGetAvailableModesFunction::
64 QuickUnlockPrivateGetAvailableModesFunction() 150 QuickUnlockPrivateGetAvailableModesFunction()
65 : chrome_details_(this) {} 151 : chrome_details_(this) {}
66 152
67 QuickUnlockPrivateGetAvailableModesFunction:: 153 QuickUnlockPrivateGetAvailableModesFunction::
68 ~QuickUnlockPrivateGetAvailableModesFunction() {} 154 ~QuickUnlockPrivateGetAvailableModesFunction() {}
(...skipping 17 matching lines...) Expand all
86 QuickUnlockPrivateGetActiveModesFunction:: 172 QuickUnlockPrivateGetActiveModesFunction::
87 ~QuickUnlockPrivateGetActiveModesFunction() {} 173 ~QuickUnlockPrivateGetActiveModesFunction() {}
88 174
89 ExtensionFunction::ResponseAction 175 ExtensionFunction::ResponseAction
90 QuickUnlockPrivateGetActiveModesFunction::Run() { 176 QuickUnlockPrivateGetActiveModesFunction::Run() {
91 const QuickUnlockModeList modes = 177 const QuickUnlockModeList modes =
92 ComputeActiveModes(chrome_details_.GetProfile()); 178 ComputeActiveModes(chrome_details_.GetProfile());
93 return RespondNow(ArgumentList(GetActiveModes::Results::Create(modes))); 179 return RespondNow(ArgumentList(GetActiveModes::Results::Create(modes)));
94 } 180 }
95 181
182 // quickUnlockPrivate.isCredentialUsable
183
184 QuickUnlockPrivateIsCredentialUsableFunction::
185 QuickUnlockPrivateIsCredentialUsableFunction()
186 : chrome_details_(this) {}
187
188 QuickUnlockPrivateIsCredentialUsableFunction::
189 ~QuickUnlockPrivateIsCredentialUsableFunction() {}
190
191 ExtensionFunction::ResponseAction
192 QuickUnlockPrivateIsCredentialUsableFunction::Run() {
193 params_ = IsCredentialUsable::Params::Create(*args_);
194 EXTENSION_FUNCTION_VALIDATE(params_.get());
195
196 auto result = base::MakeUnique<CredentialRequirement>();
197
198 // Only handles pins for now.
199 if (params_->mode != QuickUnlockMode::QUICK_UNLOCK_MODE_PIN) {
200 return RespondNow(
201 ArgumentList(IsCredentialUsable::Results::Create(*result)));
202 }
203
204 std::string tried_password = params_->credential;
205
206 Profile* profile = chrome_details_.GetProfile();
207 IsPinLengthValid(tried_password, profile, result.get());
208 IsPinDifficultEnough(tried_password, profile, result.get());
209
210 return RespondNow(ArgumentList(IsCredentialUsable::Results::Create(*result)));
211 }
212
96 // quickUnlockPrivate.setModes 213 // quickUnlockPrivate.setModes
97 214
98 QuickUnlockPrivateSetModesFunction::QuickUnlockPrivateSetModesFunction() 215 QuickUnlockPrivateSetModesFunction::QuickUnlockPrivateSetModesFunction()
99 : chrome_details_(this) {} 216 : chrome_details_(this) {}
100 217
101 QuickUnlockPrivateSetModesFunction::~QuickUnlockPrivateSetModesFunction() {} 218 QuickUnlockPrivateSetModesFunction::~QuickUnlockPrivateSetModesFunction() {}
102 219
103 void QuickUnlockPrivateSetModesFunction::SetAuthenticatorAllocatorForTesting( 220 void QuickUnlockPrivateSetModesFunction::SetAuthenticatorAllocatorForTesting(
104 const QuickUnlockPrivateSetModesFunction::AuthenticatorAllocator& 221 const QuickUnlockPrivateSetModesFunction::AuthenticatorAllocator&
105 allocator) { 222 allocator) {
(...skipping 14 matching lines...) Expand all
120 237
121 if (params_->modes.size() > 1) 238 if (params_->modes.size() > 1)
122 return RespondNow(Error(kMultipleModesNotSupported)); 239 return RespondNow(Error(kMultipleModesNotSupported));
123 240
124 // Verify every credential is numeric. 241 // Verify every credential is numeric.
125 for (const std::string& credential : params_->credentials) { 242 for (const std::string& credential : params_->credentials) {
126 if (!std::all_of(credential.begin(), credential.end(), ::isdigit)) 243 if (!std::all_of(credential.begin(), credential.end(), ::isdigit))
127 return RespondNow(ArgumentList(SetModes::Results::Create(false))); 244 return RespondNow(ArgumentList(SetModes::Results::Create(false)));
128 } 245 }
129 246
247 // Verify every credential is valid based on policies.
248 for (size_t j = 0; j < params_->modes.size(); ++j) {
249 if (params_->modes[j] == QuickUnlockMode::QUICK_UNLOCK_MODE_PIN &&
250 !IsPinLengthValid(params_->credentials[j], chrome_details_.GetProfile(),
251 nullptr) &&
252 !IsPinDifficultEnough(params_->credentials[j],
253 chrome_details_.GetProfile(), nullptr)) {
254 return RespondNow(ArgumentList(SetModes::Results::Create(false)));
255 }
256 }
257
130 user_manager::User* user = chromeos::ProfileHelper::Get()->GetUserByProfile( 258 user_manager::User* user = chromeos::ProfileHelper::Get()->GetUserByProfile(
131 chrome_details_.GetProfile()); 259 chrome_details_.GetProfile());
132 chromeos::UserContext user_context(user->GetAccountId()); 260 chromeos::UserContext user_context(user->GetAccountId());
133 user_context.SetKey(chromeos::Key(params_->account_password)); 261 user_context.SetKey(chromeos::Key(params_->account_password));
134 262
135 // Lazily allocate the authenticator. We do this here, instead of in the ctor, 263 // Lazily allocate the authenticator. We do this here, instead of in the ctor,
136 // so that tests can install a fake. 264 // so that tests can install a fake.
137 if (authenticator_allocator_.is_null()) 265 if (authenticator_allocator_.is_null())
138 extended_authenticator_ = chromeos::ExtendedAuthenticator::Create(this); 266 extended_authenticator_ = chromeos::ExtendedAuthenticator::Create(this);
139 else 267 else
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after
223 } 351 }
224 352
225 std::unique_ptr<base::ListValue> args = OnActiveModesChanged::Create(modes); 353 std::unique_ptr<base::ListValue> args = OnActiveModesChanged::Create(modes);
226 std::unique_ptr<Event> event( 354 std::unique_ptr<Event> event(
227 new Event(events::QUICK_UNLOCK_PRIVATE_ON_ACTIVE_MODES_CHANGED, 355 new Event(events::QUICK_UNLOCK_PRIVATE_ON_ACTIVE_MODES_CHANGED,
228 OnActiveModesChanged::kEventName, std::move(args))); 356 OnActiveModesChanged::kEventName, std::move(args)));
229 EventRouter::Get(browser_context())->BroadcastEvent(std::move(event)); 357 EventRouter::Get(browser_context())->BroadcastEvent(std::move(event));
230 } 358 }
231 359
232 } // namespace extensions 360 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698