OLD | NEW |
---|---|
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."; |
36 // Pins greater in length than |kPinCheckWeakThreshold| will be checked for | |
37 // weakness. | |
38 const int kPinCheckWeakThreshold = 2; | |
30 | 39 |
31 // Returns the active set of quick unlock modes. | 40 // Returns the active set of quick unlock modes. |
32 QuickUnlockModeList ComputeActiveModes(Profile* profile) { | 41 QuickUnlockModeList ComputeActiveModes(Profile* profile) { |
33 QuickUnlockModeList modes; | 42 QuickUnlockModeList modes; |
34 | 43 |
35 chromeos::PinStorage* pin_storage = | 44 chromeos::PinStorage* pin_storage = |
36 chromeos::PinStorageFactory::GetForProfile(profile); | 45 chromeos::PinStorageFactory::GetForProfile(profile); |
37 if (pin_storage && pin_storage->IsPinSet()) | 46 if (pin_storage && pin_storage->IsPinSet()) |
38 modes.push_back(quick_unlock_private::QUICK_UNLOCK_MODE_PIN); | 47 modes.push_back(quick_unlock_private::QUICK_UNLOCK_MODE_PIN); |
39 | 48 |
40 return modes; | 49 return modes; |
41 } | 50 } |
42 | 51 |
43 // Returns true if |a| and |b| contain the same elements. The elements do not | 52 // Returns true if |a| and |b| contain the same elements. The elements do not |
44 // need to be in the same order. | 53 // need to be in the same order. |
45 bool AreModesEqual(const QuickUnlockModeList& a, const QuickUnlockModeList& b) { | 54 bool AreModesEqual(const QuickUnlockModeList& a, const QuickUnlockModeList& b) { |
46 if (a.size() != b.size()) | 55 if (a.size() != b.size()) |
47 return false; | 56 return false; |
48 | 57 |
49 // This is a slow comparison algorithm, but the number of entries in |a| and | 58 // 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. | 59 // |b| will always be very low (0-3 items) so it doesn't matter. |
51 for (size_t i = 0; i < a.size(); ++i) { | 60 for (size_t i = 0; i < a.size(); ++i) { |
52 if (std::find(b.begin(), b.end(), a[i]) == b.end()) | 61 if (std::find(b.begin(), b.end(), a[i]) == b.end()) |
53 return false; | 62 return false; |
54 } | 63 } |
55 | 64 |
56 return true; | 65 return true; |
57 } | 66 } |
58 | 67 |
68 // Check if a given |pin| is valid given the policies in the |profile|. If | |
69 // |result| is a valid pointer, the failures are added to |result|. Pass a | |
70 // null for |result| if all we want to do is check the validity of |pin|. | |
jdufault
2016/10/05 21:47:38
Update comment
sammiequon
2016/10/14 20:58:53
Done.
| |
71 std::vector<CredentialRequirementFailure> IsPinLengthValid( | |
72 const std::string& pin, | |
73 Profile* profile, | |
74 int* out_min_length, | |
75 int* out_max_length) { | |
76 std::vector<CredentialRequirementFailure> failures; | |
77 PrefService* pref_service = profile->GetPrefs(); | |
78 int min_length = pref_service->GetInteger(prefs::kPinUnlockMinimumLength); | |
79 int max_length = pref_service->GetInteger(prefs::kPinUnlockMaximumLength); | |
80 if (out_min_length) | |
81 *out_min_length = min_length; | |
82 if (out_max_length) | |
83 *out_max_length = max_length; | |
84 | |
85 // Check if the pin is shorter than the minimum specified length. | |
86 if (int{pin.size()} < min_length) { | |
jdufault
2016/10/05 21:47:37
Compilation fails without this cast? What if you d
sammiequon
2016/10/14 20:58:53
Yeah it doesn't compile without the cast both ways
| |
87 failures.push_back( | |
88 CredentialRequirementFailure::CREDENTIAL_REQUIREMENT_FAILURE_TOO_SHORT); | |
89 } | |
90 | |
91 // If the maximum lenght is nonzero and shorter than the minimum length, the | |
jdufault
2016/10/05 21:47:38
lenght -> length
sammiequon
2016/10/14 20:58:53
Done.
| |
92 // maximum length is the minimum length. | |
93 if (max_length > 0 && max_length < min_length) | |
jdufault
2016/10/05 21:47:38
This check should happen before assigning out_* pa
sammiequon
2016/10/14 20:58:53
Done.
| |
94 max_length = min_length; | |
95 | |
96 // If the maximum specified length is zero, there is no maximum length. | |
97 // Otherwise check if the pin is longer than the maximum specified length. | |
98 if (max_length > 0 && int{pin.size()} > max_length) { | |
99 failures.push_back( | |
100 CredentialRequirementFailure::CREDENTIAL_REQUIREMENT_FAILURE_TOO_LONG); | |
101 } | |
102 | |
103 return failures; | |
104 } | |
105 | |
106 // Check if a given |pin| is valid given the policies in the |profile|. If | |
107 // |result| is a valid pointer, the failures are added to |result|. Pass a | |
jdufault
2016/10/05 21:47:38
Update comment
sammiequon
2016/10/14 20:58:53
Done.
| |
108 // null for |result| if all we want to do is check the validity of |pin|. | |
109 std::vector<CredentialRequirementFailure> IsPinDifficultEnough( | |
110 const std::string& pin, | |
111 Profile* profile, | |
112 bool* out_allow_weak_pin) { | |
113 std::vector<CredentialRequirementFailure> failures; | |
114 PrefService* pref_service = profile->GetPrefs(); | |
115 bool allow_weak = pref_service->GetBoolean(prefs::kPinUnlockAllowWeakPins); | |
116 if (out_allow_weak_pin) | |
117 *out_allow_weak_pin = allow_weak; | |
118 | |
119 // If the pin length is |kPinCheckWeakThreshold| or less, there is no need to | |
120 // check for same character and increasing pin. | |
121 if (int{pin.size()} <= kPinCheckWeakThreshold) | |
122 return failures; | |
123 | |
124 // Check for same digits, increasing pin simutaneously. | |
125 bool is_same = true; | |
126 bool is_increasing = true; | |
127 bool is_decreasing = true; | |
128 for (int i = 1; i < int{pin.length()}; ++i) { | |
129 const char previous = pin[i - 1]; | |
130 const char current = pin[i]; | |
131 | |
132 is_same = is_same && (current == previous); | |
133 is_increasing = is_increasing && (current == previous + 1); | |
134 is_decreasing = is_decreasing && (current == previous - 1); | |
135 } | |
136 | |
137 // Pin is considered weak if either of these is met. | |
138 if ((is_same || is_increasing || is_decreasing)) { | |
139 failures.push_back( | |
140 CredentialRequirementFailure::CREDENTIAL_REQUIREMENT_FAILURE_TOO_WEAK); | |
141 } | |
142 | |
143 return failures; | |
144 } | |
145 | |
59 } // namespace | 146 } // namespace |
60 | 147 |
61 // quickUnlockPrivate.getAvailableModes | 148 // quickUnlockPrivate.getAvailableModes |
62 | 149 |
63 QuickUnlockPrivateGetAvailableModesFunction:: | 150 QuickUnlockPrivateGetAvailableModesFunction:: |
64 QuickUnlockPrivateGetAvailableModesFunction() | 151 QuickUnlockPrivateGetAvailableModesFunction() |
65 : chrome_details_(this) {} | 152 : chrome_details_(this) {} |
66 | 153 |
67 QuickUnlockPrivateGetAvailableModesFunction:: | 154 QuickUnlockPrivateGetAvailableModesFunction:: |
68 ~QuickUnlockPrivateGetAvailableModesFunction() {} | 155 ~QuickUnlockPrivateGetAvailableModesFunction() {} |
(...skipping 17 matching lines...) Expand all Loading... | |
86 QuickUnlockPrivateGetActiveModesFunction:: | 173 QuickUnlockPrivateGetActiveModesFunction:: |
87 ~QuickUnlockPrivateGetActiveModesFunction() {} | 174 ~QuickUnlockPrivateGetActiveModesFunction() {} |
88 | 175 |
89 ExtensionFunction::ResponseAction | 176 ExtensionFunction::ResponseAction |
90 QuickUnlockPrivateGetActiveModesFunction::Run() { | 177 QuickUnlockPrivateGetActiveModesFunction::Run() { |
91 const QuickUnlockModeList modes = | 178 const QuickUnlockModeList modes = |
92 ComputeActiveModes(chrome_details_.GetProfile()); | 179 ComputeActiveModes(chrome_details_.GetProfile()); |
93 return RespondNow(ArgumentList(GetActiveModes::Results::Create(modes))); | 180 return RespondNow(ArgumentList(GetActiveModes::Results::Create(modes))); |
94 } | 181 } |
95 | 182 |
183 // quickUnlockPrivate.isCredentialUsable | |
184 | |
185 QuickUnlockPrivateIsCredentialUsableFunction:: | |
186 QuickUnlockPrivateIsCredentialUsableFunction() | |
187 : chrome_details_(this) {} | |
188 | |
189 QuickUnlockPrivateIsCredentialUsableFunction:: | |
190 ~QuickUnlockPrivateIsCredentialUsableFunction() {} | |
191 | |
192 ExtensionFunction::ResponseAction | |
193 QuickUnlockPrivateIsCredentialUsableFunction::Run() { | |
194 params_ = IsCredentialUsable::Params::Create(*args_); | |
195 EXTENSION_FUNCTION_VALIDATE(params_.get()); | |
196 | |
197 auto result = base::MakeUnique<CredentialRequirement>(); | |
198 result->min_length = base::MakeUnique<int>(); | |
199 result->max_length = base::MakeUnique<int>(); | |
200 result->allow_weak_pins = base::MakeUnique<bool>(); | |
201 | |
202 // Only handles pins for now. | |
203 if (params_->mode != QuickUnlockMode::QUICK_UNLOCK_MODE_PIN) { | |
204 return RespondNow( | |
205 ArgumentList(IsCredentialUsable::Results::Create(*result))); | |
206 } | |
207 | |
208 std::string tried_password = params_->credential; | |
209 if (!std::all_of(tried_password.begin(), tried_password.end(), ::isdigit)) | |
210 return RespondNow( | |
211 ArgumentList(IsCredentialUsable::Results::Create(*result))); | |
212 | |
213 Profile* profile = chrome_details_.GetProfile(); | |
214 auto length_failures = | |
215 IsPinLengthValid(tried_password, profile, result->min_length.get(), | |
216 result->max_length.get()); | |
217 auto weak_failures = IsPinDifficultEnough(tried_password, profile, | |
218 result->allow_weak_pins.get()); | |
219 result->failures.insert(result->failures.end(), length_failures.begin(), | |
220 length_failures.end()); | |
221 result->failures.insert(result->failures.end(), weak_failures.begin(), | |
222 weak_failures.end()); | |
223 | |
224 return RespondNow(ArgumentList(IsCredentialUsable::Results::Create(*result))); | |
225 } | |
226 | |
96 // quickUnlockPrivate.setModes | 227 // quickUnlockPrivate.setModes |
97 | 228 |
98 QuickUnlockPrivateSetModesFunction::QuickUnlockPrivateSetModesFunction() | 229 QuickUnlockPrivateSetModesFunction::QuickUnlockPrivateSetModesFunction() |
99 : chrome_details_(this) {} | 230 : chrome_details_(this) {} |
100 | 231 |
101 QuickUnlockPrivateSetModesFunction::~QuickUnlockPrivateSetModesFunction() {} | 232 QuickUnlockPrivateSetModesFunction::~QuickUnlockPrivateSetModesFunction() {} |
102 | 233 |
103 void QuickUnlockPrivateSetModesFunction::SetAuthenticatorAllocatorForTesting( | 234 void QuickUnlockPrivateSetModesFunction::SetAuthenticatorAllocatorForTesting( |
104 const QuickUnlockPrivateSetModesFunction::AuthenticatorAllocator& | 235 const QuickUnlockPrivateSetModesFunction::AuthenticatorAllocator& |
105 allocator) { | 236 allocator) { |
(...skipping 14 matching lines...) Expand all Loading... | |
120 | 251 |
121 if (params_->modes.size() > 1) | 252 if (params_->modes.size() > 1) |
122 return RespondNow(Error(kMultipleModesNotSupported)); | 253 return RespondNow(Error(kMultipleModesNotSupported)); |
123 | 254 |
124 // Verify every credential is numeric. | 255 // Verify every credential is numeric. |
125 for (const std::string& credential : params_->credentials) { | 256 for (const std::string& credential : params_->credentials) { |
126 if (!std::all_of(credential.begin(), credential.end(), ::isdigit)) | 257 if (!std::all_of(credential.begin(), credential.end(), ::isdigit)) |
127 return RespondNow(ArgumentList(SetModes::Results::Create(false))); | 258 return RespondNow(ArgumentList(SetModes::Results::Create(false))); |
128 } | 259 } |
129 | 260 |
261 // Verify every credential is valid based on policies. | |
262 for (size_t j = 0; j < params_->modes.size(); ++j) { | |
263 if (params_->modes[j] == QuickUnlockMode::QUICK_UNLOCK_MODE_PIN && | |
264 !IsPinLengthValid(params_->credentials[j], chrome_details_.GetProfile(), | |
265 nullptr, nullptr) | |
266 .empty() && | |
267 !IsPinDifficultEnough(params_->credentials[j], | |
268 chrome_details_.GetProfile(), nullptr) | |
269 .empty()) { | |
270 return RespondNow(ArgumentList(SetModes::Results::Create(false))); | |
271 } | |
272 } | |
273 | |
130 user_manager::User* user = chromeos::ProfileHelper::Get()->GetUserByProfile( | 274 user_manager::User* user = chromeos::ProfileHelper::Get()->GetUserByProfile( |
131 chrome_details_.GetProfile()); | 275 chrome_details_.GetProfile()); |
132 chromeos::UserContext user_context(user->GetAccountId()); | 276 chromeos::UserContext user_context(user->GetAccountId()); |
133 user_context.SetKey(chromeos::Key(params_->account_password)); | 277 user_context.SetKey(chromeos::Key(params_->account_password)); |
134 | 278 |
135 // Lazily allocate the authenticator. We do this here, instead of in the ctor, | 279 // Lazily allocate the authenticator. We do this here, instead of in the ctor, |
136 // so that tests can install a fake. | 280 // so that tests can install a fake. |
137 if (authenticator_allocator_.is_null()) | 281 if (authenticator_allocator_.is_null()) |
138 extended_authenticator_ = chromeos::ExtendedAuthenticator::Create(this); | 282 extended_authenticator_ = chromeos::ExtendedAuthenticator::Create(this); |
139 else | 283 else |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
223 } | 367 } |
224 | 368 |
225 std::unique_ptr<base::ListValue> args = OnActiveModesChanged::Create(modes); | 369 std::unique_ptr<base::ListValue> args = OnActiveModesChanged::Create(modes); |
226 std::unique_ptr<Event> event( | 370 std::unique_ptr<Event> event( |
227 new Event(events::QUICK_UNLOCK_PRIVATE_ON_ACTIVE_MODES_CHANGED, | 371 new Event(events::QUICK_UNLOCK_PRIVATE_ON_ACTIVE_MODES_CHANGED, |
228 OnActiveModesChanged::kEventName, std::move(args))); | 372 OnActiveModesChanged::kEventName, std::move(args))); |
229 EventRouter::Get(browser_context())->BroadcastEvent(std::move(event)); | 373 EventRouter::Get(browser_context())->BroadcastEvent(std::move(event)); |
230 } | 374 } |
231 | 375 |
232 } // namespace extensions | 376 } // namespace extensions |
OLD | NEW |