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 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) | |
jdufault
2016/10/31 22:03:07
Should this be <= 0?
What behavior do we want for
sammiequon
2016/11/01 18:08:31
Would people be allowed to enter negative numbers,
jdufault
2016/11/01 18:56:43
Policy UI hopefully will sanitize input, but we sh
sammiequon
2016/11/02 18:05:24
Done.
| |
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 | |
jdufault
2016/10/31 22:03:07
declare problems here
sammiequon
2016/11/01 18:08:31
Done.
| |
88 // Check if the pin is shorter than the minimum specified length. | |
89 if (int{pin.size()} < min_length) { | |
jdufault
2016/10/31 22:03:07
nit: drop {}
sammiequon
2016/11/01 18:08:31
Done.
| |
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) { | |
jdufault
2016/10/31 22:03:07
nit: drop {}
sammiequon
2016/11/01 18:08:31
Done.
| |
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|. | |
104 std::vector<CredentialProblem> IsPinDifficultEnough(const std::string& pin) { | |
jdufault
2016/10/31 22:03:07
Maybe return base::Optional<CredentialProblem> ins
sammiequon
2016/11/01 18:08:31
Would this be an base::Optional<vector<CredentialP
jdufault
2016/11/01 18:56:43
base::Optional<std::vector<CredentialProblem>> wou
sammiequon
2016/11/02 18:05:24
I see, I decided to stick with the vector implemen
| |
105 std::vector<CredentialProblem> problems; | |
106 // If the pin length is |kPinCheckWeakThreshold| or less, there is no need to | |
jdufault
2016/10/31 22:03:07
nit: newline above
sammiequon
2016/11/01 18:08:31
Done.
| |
107 // check for same character and increasing pin. | |
108 if (int{pin.size()} <= kPinCheckWeakThreshold) | |
109 return problems; | |
110 | |
111 // Check for same digits, increasing pin simutaneously. | |
112 bool is_same = true; | |
113 bool is_increasing = true; | |
114 bool is_decreasing = true; | |
115 for (int i = 1; i < int{pin.length()}; ++i) { | |
116 const char previous = pin[i - 1]; | |
117 const char current = pin[i]; | |
118 | |
119 is_same = is_same && (current == previous); | |
120 is_increasing = is_increasing && (current == previous + 1); | |
121 is_decreasing = is_decreasing && (current == previous - 1); | |
122 } | |
123 | |
124 // Pin is considered weak if either of these is met. | |
125 if ((is_same || is_increasing || is_decreasing)) { | |
jdufault
2016/10/31 22:03:07
drop extraneous ()
sammiequon
2016/11/01 18:08:31
Done.
| |
126 problems.push_back(CredentialProblem::CREDENTIAL_PROBLEM_TOO_WEAK); | |
jdufault
2016/10/31 22:03:07
drop {}
sammiequon
2016/11/01 18:08:31
Done.
| |
127 } | |
128 | |
129 return problems; | |
130 } | |
131 | |
59 } // namespace | 132 } // namespace |
60 | 133 |
61 // quickUnlockPrivate.getAvailableModes | 134 // quickUnlockPrivate.getAvailableModes |
62 | 135 |
63 QuickUnlockPrivateGetAvailableModesFunction:: | 136 QuickUnlockPrivateGetAvailableModesFunction:: |
64 QuickUnlockPrivateGetAvailableModesFunction() | 137 QuickUnlockPrivateGetAvailableModesFunction() |
65 : chrome_details_(this) {} | 138 : chrome_details_(this) {} |
66 | 139 |
67 QuickUnlockPrivateGetAvailableModesFunction:: | 140 QuickUnlockPrivateGetAvailableModesFunction:: |
68 ~QuickUnlockPrivateGetAvailableModesFunction() {} | 141 ~QuickUnlockPrivateGetAvailableModesFunction() {} |
(...skipping 17 matching lines...) Expand all Loading... | |
86 QuickUnlockPrivateGetActiveModesFunction:: | 159 QuickUnlockPrivateGetActiveModesFunction:: |
87 ~QuickUnlockPrivateGetActiveModesFunction() {} | 160 ~QuickUnlockPrivateGetActiveModesFunction() {} |
88 | 161 |
89 ExtensionFunction::ResponseAction | 162 ExtensionFunction::ResponseAction |
90 QuickUnlockPrivateGetActiveModesFunction::Run() { | 163 QuickUnlockPrivateGetActiveModesFunction::Run() { |
91 const QuickUnlockModeList modes = | 164 const QuickUnlockModeList modes = |
92 ComputeActiveModes(chrome_details_.GetProfile()); | 165 ComputeActiveModes(chrome_details_.GetProfile()); |
93 return RespondNow(ArgumentList(GetActiveModes::Results::Create(modes))); | 166 return RespondNow(ArgumentList(GetActiveModes::Results::Create(modes))); |
94 } | 167 } |
95 | 168 |
169 // quickUnlockPrivate.checkCredential | |
170 | |
171 QuickUnlockPrivateCheckCredentialFunction:: | |
172 QuickUnlockPrivateCheckCredentialFunction() | |
173 : chrome_details_(this) {} | |
174 | |
175 QuickUnlockPrivateCheckCredentialFunction:: | |
176 ~QuickUnlockPrivateCheckCredentialFunction() {} | |
177 | |
178 ExtensionFunction::ResponseAction | |
179 QuickUnlockPrivateCheckCredentialFunction::Run() { | |
180 params_ = CheckCredential::Params::Create(*args_); | |
181 EXTENSION_FUNCTION_VALIDATE(params_.get()); | |
182 | |
183 auto result = base::MakeUnique<CredentialCheck>(); | |
184 result->min_length = base::MakeUnique<int>(); | |
185 result->max_length = base::MakeUnique<int>(); | |
186 | |
187 // Only handles pins for now. | |
188 if (params_->mode != QuickUnlockMode::QUICK_UNLOCK_MODE_PIN) { | |
189 return RespondNow(ArgumentList(CheckCredential::Results::Create(*result))); | |
190 } | |
191 | |
192 std::string tried_password = params_->credential; | |
jdufault
2016/10/31 22:03:07
What about reusing the name credential instead of
sammiequon
2016/11/01 18:08:31
Done.
| |
193 if (!std::all_of(tried_password.begin(), tried_password.end(), ::isdigit)) | |
194 return RespondNow(ArgumentList(CheckCredential::Results::Create(*result))); | |
jdufault
2016/10/31 22:03:07
This should probably report an error that the inpu
sammiequon
2016/11/01 18:08:31
Done.
| |
195 | |
196 Profile* profile = chrome_details_.GetProfile(); | |
197 PrefService* pref_service = profile->GetPrefs(); | |
198 bool allow_weak = pref_service->GetBoolean(prefs::kPinUnlockAllowWeakPins); | |
199 auto length_problems = | |
jdufault
2016/10/31 22:03:07
Only use auto when the type of the variable is evi
sammiequon
2016/11/01 18:08:31
Done.
| |
200 IsPinLengthValid(tried_password, profile, result->min_length.get(), | |
jdufault
2016/10/31 22:03:07
It's not clear that min_length and max_length are
sammiequon
2016/11/01 18:08:31
Done.
| |
201 result->max_length.get()); | |
202 auto weak_problems = IsPinDifficultEnough(tried_password); | |
jdufault
2016/10/31 22:03:07
Specify type instead of auto
sammiequon
2016/11/01 18:08:31
Done.
| |
203 result->errors.insert(result->errors.end(), length_problems.begin(), | |
204 length_problems.end()); | |
205 if (allow_weak) { | |
206 result->warnings.insert(result->warnings.end(), weak_problems.begin(), | |
jdufault
2016/10/31 22:03:07
Maybe you want to add a helper method?
AppendTo
sammiequon
2016/11/01 18:08:31
Done.
| |
207 weak_problems.end()); | |
208 } else { | |
209 result->errors.insert(result->errors.end(), weak_problems.begin(), | |
210 weak_problems.end()); | |
211 } | |
212 | |
213 return RespondNow(ArgumentList(CheckCredential::Results::Create(*result))); | |
214 } | |
215 | |
96 // quickUnlockPrivate.setModes | 216 // quickUnlockPrivate.setModes |
97 | 217 |
98 QuickUnlockPrivateSetModesFunction::QuickUnlockPrivateSetModesFunction() | 218 QuickUnlockPrivateSetModesFunction::QuickUnlockPrivateSetModesFunction() |
99 : chrome_details_(this) {} | 219 : chrome_details_(this) {} |
100 | 220 |
101 QuickUnlockPrivateSetModesFunction::~QuickUnlockPrivateSetModesFunction() {} | 221 QuickUnlockPrivateSetModesFunction::~QuickUnlockPrivateSetModesFunction() {} |
102 | 222 |
103 void QuickUnlockPrivateSetModesFunction::SetAuthenticatorAllocatorForTesting( | 223 void QuickUnlockPrivateSetModesFunction::SetAuthenticatorAllocatorForTesting( |
104 const QuickUnlockPrivateSetModesFunction::AuthenticatorAllocator& | 224 const QuickUnlockPrivateSetModesFunction::AuthenticatorAllocator& |
105 allocator) { | 225 allocator) { |
106 authenticator_allocator_ = allocator; | 226 authenticator_allocator_ = allocator; |
107 } | 227 } |
108 | 228 |
109 void QuickUnlockPrivateSetModesFunction::SetModesChangedEventHandlerForTesting( | 229 void QuickUnlockPrivateSetModesFunction::SetModesChangedEventHandlerForTesting( |
110 const ModesChangedEventHandler& handler) { | 230 const ModesChangedEventHandler& handler) { |
111 modes_changed_handler_ = handler; | 231 modes_changed_handler_ = handler; |
112 } | 232 } |
113 | 233 |
114 ExtensionFunction::ResponseAction QuickUnlockPrivateSetModesFunction::Run() { | 234 ExtensionFunction::ResponseAction QuickUnlockPrivateSetModesFunction::Run() { |
115 params_ = SetModes::Params::Create(*args_); | 235 params_ = SetModes::Params::Create(*args_); |
116 EXTENSION_FUNCTION_VALIDATE(params_.get()); | 236 EXTENSION_FUNCTION_VALIDATE(params_.get()); |
117 | 237 |
118 if (params_->modes.size() != params_->credentials.size()) | 238 if (params_->modes.size() != params_->credentials.size()) |
119 return RespondNow(Error(kModesAndCredentialsLengthMismatch)); | 239 return RespondNow(Error(kModesAndCredentialsLengthMismatch)); |
120 | 240 |
121 if (params_->modes.size() > 1) | 241 if (params_->modes.size() > 1) |
122 return RespondNow(Error(kMultipleModesNotSupported)); | 242 return RespondNow(Error(kMultipleModesNotSupported)); |
123 | 243 |
124 // Verify every credential is numeric. | 244 // Verify every credential is numeric. |
125 for (const std::string& credential : params_->credentials) { | 245 for (const std::string& credential : params_->credentials) { |
jdufault
2016/10/31 22:03:07
If this is made a problem the check can be merged
sammiequon
2016/11/01 18:08:31
Done.
| |
126 if (!std::all_of(credential.begin(), credential.end(), ::isdigit)) | 246 if (!std::all_of(credential.begin(), credential.end(), ::isdigit)) |
127 return RespondNow(ArgumentList(SetModes::Results::Create(false))); | 247 return RespondNow(ArgumentList(SetModes::Results::Create(false))); |
128 } | 248 } |
129 | 249 |
250 // Verify every credential is valid based on policies. | |
251 bool allow_weak = chrome_details_.GetProfile()->GetPrefs()->GetBoolean( | |
252 prefs::kPinUnlockAllowWeakPins); | |
253 for (size_t j = 0; j < params_->modes.size(); ++j) { | |
254 if (params_->modes[j] == QuickUnlockMode::QUICK_UNLOCK_MODE_PIN && | |
255 (!IsPinLengthValid(params_->credentials[j], | |
jdufault
2016/10/31 22:03:08
This if statement is pretty challenging to read. Y
sammiequon
2016/11/01 18:08:31
Done.
| |
256 chrome_details_.GetProfile(), nullptr, nullptr) | |
257 .empty() || | |
258 (!IsPinDifficultEnough(params_->credentials[j]).empty() && | |
259 !allow_weak))) { | |
260 return RespondNow(ArgumentList(SetModes::Results::Create(false))); | |
261 } | |
262 } | |
263 | |
130 user_manager::User* user = chromeos::ProfileHelper::Get()->GetUserByProfile( | 264 user_manager::User* user = chromeos::ProfileHelper::Get()->GetUserByProfile( |
131 chrome_details_.GetProfile()); | 265 chrome_details_.GetProfile()); |
132 chromeos::UserContext user_context(user->GetAccountId()); | 266 chromeos::UserContext user_context(user->GetAccountId()); |
133 user_context.SetKey(chromeos::Key(params_->account_password)); | 267 user_context.SetKey(chromeos::Key(params_->account_password)); |
134 | 268 |
135 // Lazily allocate the authenticator. We do this here, instead of in the ctor, | 269 // Lazily allocate the authenticator. We do this here, instead of in the ctor, |
136 // so that tests can install a fake. | 270 // so that tests can install a fake. |
137 if (authenticator_allocator_.is_null()) | 271 if (authenticator_allocator_.is_null()) |
138 extended_authenticator_ = chromeos::ExtendedAuthenticator::Create(this); | 272 extended_authenticator_ = chromeos::ExtendedAuthenticator::Create(this); |
139 else | 273 else |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
223 } | 357 } |
224 | 358 |
225 std::unique_ptr<base::ListValue> args = OnActiveModesChanged::Create(modes); | 359 std::unique_ptr<base::ListValue> args = OnActiveModesChanged::Create(modes); |
226 std::unique_ptr<Event> event( | 360 std::unique_ptr<Event> event( |
227 new Event(events::QUICK_UNLOCK_PRIVATE_ON_ACTIVE_MODES_CHANGED, | 361 new Event(events::QUICK_UNLOCK_PRIVATE_ON_ACTIVE_MODES_CHANGED, |
228 OnActiveModesChanged::kEventName, std::move(args))); | 362 OnActiveModesChanged::kEventName, std::move(args))); |
229 EventRouter::Get(browser_context())->BroadcastEvent(std::move(event)); | 363 EventRouter::Get(browser_context())->BroadcastEvent(std::move(event)); |
230 } | 364 } |
231 | 365 |
232 } // namespace extensions | 366 } // namespace extensions |
OLD | NEW |