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."; |
30 | 36 |
(...skipping 18 matching lines...) Expand all Loading... | |
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 bool CheckPinInvalid(const std::string& pin, | |
jdufault
2016/09/30 01:05:50
I think it would be easier to read if this was sta
sammiequon
2016/09/30 18:38:35
Done.
| |
66 Profile* profile, | |
67 CredentialRequirement* result) { | |
jdufault
2016/09/30 01:05:50
Document that |result| is optional.
sammiequon
2016/09/30 18:38:35
Done.
| |
68 bool is_invalid = false; | |
69 PrefService* prefservice = profile->GetPrefs(); | |
jdufault
2016/09/30 01:05:50
nit: pref_service or just prefs
sammiequon
2016/09/30 18:38:35
Done.
| |
70 int minimum_length = prefservice->GetInteger(prefs::kPinUnlockMinimumLength); | |
71 int maximum_length = prefservice->GetInteger(prefs::kPinUnlockMaximumLength); | |
72 if (result) { | |
73 result->min_length.reset(new int(minimum_length)); | |
jdufault
2016/09/30 01:05:50
base::MakeUnique(minimum_length)
sammiequon
2016/09/30 18:38:35
Done.
| |
74 result->max_length.reset(new int(maximum_length)); | |
75 } | |
76 | |
77 // Check if the pin is shorter than the minimum specified length. | |
78 if (int{pin.size()} < minimum_length) { | |
79 is_invalid = true; | |
80 if (result) { | |
81 result->failures.push_back(CredentialRequirementFailure:: | |
82 CREDENTIAL_REQUIREMENT_FAILURE_TOO_SHORT); | |
jdufault
2016/09/30 01:05:50
Did you run git cl format? This looks strange.
sammiequon
2016/09/30 18:38:35
Yes I did.
| |
83 } | |
84 } | |
85 | |
86 // If the maximum specified length is shorter or equal to the minimum | |
87 // specified length, there is no maximum length. Otherwise check if the pin is | |
88 // longer than the maximum specified length. | |
89 if (maximum_length > minimum_length && int{pin.size()} > maximum_length) { | |
90 is_invalid = true; | |
91 if (result) { | |
92 result->failures.push_back(CredentialRequirementFailure:: | |
93 CREDENTIAL_REQUIREMENT_FAILURE_TOO_LONG); | |
94 } | |
95 } | |
96 return is_invalid; | |
jdufault
2016/09/30 01:05:50
nit: newline above
sammiequon
2016/09/30 18:38:35
Done.
| |
97 } | |
98 | |
59 } // namespace | 99 } // namespace |
60 | 100 |
61 // quickUnlockPrivate.getAvailableModes | 101 // quickUnlockPrivate.getAvailableModes |
62 | 102 |
63 QuickUnlockPrivateGetAvailableModesFunction:: | 103 QuickUnlockPrivateGetAvailableModesFunction:: |
64 QuickUnlockPrivateGetAvailableModesFunction() | 104 QuickUnlockPrivateGetAvailableModesFunction() |
65 : chrome_details_(this) {} | 105 : chrome_details_(this) {} |
66 | 106 |
67 QuickUnlockPrivateGetAvailableModesFunction:: | 107 QuickUnlockPrivateGetAvailableModesFunction:: |
68 ~QuickUnlockPrivateGetAvailableModesFunction() {} | 108 ~QuickUnlockPrivateGetAvailableModesFunction() {} |
(...skipping 17 matching lines...) Expand all Loading... | |
86 QuickUnlockPrivateGetActiveModesFunction:: | 126 QuickUnlockPrivateGetActiveModesFunction:: |
87 ~QuickUnlockPrivateGetActiveModesFunction() {} | 127 ~QuickUnlockPrivateGetActiveModesFunction() {} |
88 | 128 |
89 ExtensionFunction::ResponseAction | 129 ExtensionFunction::ResponseAction |
90 QuickUnlockPrivateGetActiveModesFunction::Run() { | 130 QuickUnlockPrivateGetActiveModesFunction::Run() { |
91 const QuickUnlockModeList modes = | 131 const QuickUnlockModeList modes = |
92 ComputeActiveModes(chrome_details_.GetProfile()); | 132 ComputeActiveModes(chrome_details_.GetProfile()); |
93 return RespondNow(ArgumentList(GetActiveModes::Results::Create(modes))); | 133 return RespondNow(ArgumentList(GetActiveModes::Results::Create(modes))); |
94 } | 134 } |
95 | 135 |
136 // quickUnlockPrivate.isCredentialUsable | |
137 | |
138 QuickUnlockPrivateIsCredentialUsableFunction:: | |
139 QuickUnlockPrivateIsCredentialUsableFunction() | |
140 : chrome_details_(this) {} | |
141 | |
142 QuickUnlockPrivateIsCredentialUsableFunction:: | |
143 ~QuickUnlockPrivateIsCredentialUsableFunction() {} | |
144 | |
145 ExtensionFunction::ResponseAction | |
146 QuickUnlockPrivateIsCredentialUsableFunction::Run() { | |
147 params_ = IsCredentialUsable::Params::Create(*args_); | |
148 EXTENSION_FUNCTION_VALIDATE(params_.get()); | |
149 | |
150 std::unique_ptr<CredentialRequirement> result = | |
jdufault
2016/09/30 01:05:50
auto*
sammiequon
2016/09/30 18:38:35
auto* does not work but auto does.
| |
151 base::MakeUnique<CredentialRequirement>(); | |
152 | |
153 QuickUnlockMode mode = params_->mode; | |
jdufault
2016/09/30 01:05:50
I don't think you need this local if you're only u
sammiequon
2016/09/30 18:38:35
Done.
| |
154 // Only handles pins for now. | |
155 if (mode != QuickUnlockMode::QUICK_UNLOCK_MODE_PIN) { | |
156 return RespondNow( | |
157 ArgumentList(IsCredentialUsable::Results::Create(*result))); | |
158 } | |
159 | |
160 std::string tried_password = params_->attempted_password; | |
161 | |
162 Profile* profile = chrome_details_.GetProfile(); | |
163 CheckPinInvalid(tried_password, profile, result.get()); | |
164 | |
165 // If the pin length is two or less, there is no need to check for same | |
166 // character and increasing pin. | |
167 const int pin_check_easy_threshold = 2; | |
jdufault
2016/09/30 01:05:50
Is this an optimization? If so, I don't think it i
sammiequon
2016/09/30 18:38:35
The const int? It's just to show what the 2 stands
| |
168 if (int{tried_password.size()} > pin_check_easy_threshold) { | |
169 // Check for same digits, increasing pin simutaneously. | |
170 bool is_same = true, is_increasing = true; | |
jdufault
2016/09/30 01:05:50
Separate these into different lines.
sammiequon
2016/09/30 18:38:35
Done.
| |
171 char last_char; | |
172 for (int j = 0; j < int{tried_password.size()}; j++) { | |
jdufault
2016/09/30 01:05:50
Iterate over chars directly
for (char c : tried
sammiequon
2016/09/30 18:38:35
Done.
| |
173 if (j == 0) { | |
174 last_char = tried_password[j]; | |
175 continue; | |
176 } | |
177 is_same = is_same && (tried_password[j] == last_char); | |
178 is_increasing = is_increasing && (tried_password[j] == last_char + 1); | |
jdufault
2016/09/30 01:05:50
This algorithm looks different from the JS one. Fo
sammiequon
2016/09/30 18:38:35
Yes the google doc only says increasing.
jdufault
2016/10/04 22:57:27
I think we should also consider 4321 weak.
sammiequon
2016/10/05 19:48:10
Done.
| |
179 last_char = tried_password[j]; | |
180 } | |
181 | |
182 // Pin is considered weak if either of these is met. | |
183 if (is_same || is_increasing) { | |
184 result->failures.push_back(CredentialRequirementFailure:: | |
185 CREDENTIAL_REQUIREMENT_FAILURE_TOO_WEAK); | |
186 } | |
187 } | |
188 | |
189 return RespondNow(ArgumentList(IsCredentialUsable::Results::Create(*result))); | |
190 } | |
191 | |
96 // quickUnlockPrivate.setModes | 192 // quickUnlockPrivate.setModes |
97 | 193 |
98 QuickUnlockPrivateSetModesFunction::QuickUnlockPrivateSetModesFunction() | 194 QuickUnlockPrivateSetModesFunction::QuickUnlockPrivateSetModesFunction() |
99 : chrome_details_(this) {} | 195 : chrome_details_(this) {} |
100 | 196 |
101 QuickUnlockPrivateSetModesFunction::~QuickUnlockPrivateSetModesFunction() {} | 197 QuickUnlockPrivateSetModesFunction::~QuickUnlockPrivateSetModesFunction() {} |
102 | 198 |
103 void QuickUnlockPrivateSetModesFunction::SetAuthenticatorAllocatorForTesting( | 199 void QuickUnlockPrivateSetModesFunction::SetAuthenticatorAllocatorForTesting( |
104 const QuickUnlockPrivateSetModesFunction::AuthenticatorAllocator& | 200 const QuickUnlockPrivateSetModesFunction::AuthenticatorAllocator& |
105 allocator) { | 201 allocator) { |
(...skipping 14 matching lines...) Expand all Loading... | |
120 | 216 |
121 if (params_->modes.size() > 1) | 217 if (params_->modes.size() > 1) |
122 return RespondNow(Error(kMultipleModesNotSupported)); | 218 return RespondNow(Error(kMultipleModesNotSupported)); |
123 | 219 |
124 // Verify every credential is numeric. | 220 // Verify every credential is numeric. |
125 for (const std::string& credential : params_->credentials) { | 221 for (const std::string& credential : params_->credentials) { |
126 if (!std::all_of(credential.begin(), credential.end(), ::isdigit)) | 222 if (!std::all_of(credential.begin(), credential.end(), ::isdigit)) |
127 return RespondNow(ArgumentList(SetModes::Results::Create(false))); | 223 return RespondNow(ArgumentList(SetModes::Results::Create(false))); |
128 } | 224 } |
129 | 225 |
226 // Verify every credential is valid based on policies. | |
227 for (const std::string& credential : params_->credentials) { | |
228 if (CheckPinInvalid(credential, chrome_details_.GetProfile(), nullptr)) | |
jdufault
2016/09/30 01:05:50
This assumes that every credential is a PIN, which
sammiequon
2016/09/30 18:38:35
Done.
| |
229 return RespondNow(ArgumentList(SetModes::Results::Create(false))); | |
230 } | |
231 | |
130 user_manager::User* user = chromeos::ProfileHelper::Get()->GetUserByProfile( | 232 user_manager::User* user = chromeos::ProfileHelper::Get()->GetUserByProfile( |
131 chrome_details_.GetProfile()); | 233 chrome_details_.GetProfile()); |
132 chromeos::UserContext user_context(user->GetAccountId()); | 234 chromeos::UserContext user_context(user->GetAccountId()); |
133 user_context.SetKey(chromeos::Key(params_->account_password)); | 235 user_context.SetKey(chromeos::Key(params_->account_password)); |
134 | 236 |
135 // Lazily allocate the authenticator. We do this here, instead of in the ctor, | 237 // Lazily allocate the authenticator. We do this here, instead of in the ctor, |
136 // so that tests can install a fake. | 238 // so that tests can install a fake. |
137 if (authenticator_allocator_.is_null()) | 239 if (authenticator_allocator_.is_null()) |
138 extended_authenticator_ = chromeos::ExtendedAuthenticator::Create(this); | 240 extended_authenticator_ = chromeos::ExtendedAuthenticator::Create(this); |
139 else | 241 else |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
223 } | 325 } |
224 | 326 |
225 std::unique_ptr<base::ListValue> args = OnActiveModesChanged::Create(modes); | 327 std::unique_ptr<base::ListValue> args = OnActiveModesChanged::Create(modes); |
226 std::unique_ptr<Event> event( | 328 std::unique_ptr<Event> event( |
227 new Event(events::QUICK_UNLOCK_PRIVATE_ON_ACTIVE_MODES_CHANGED, | 329 new Event(events::QUICK_UNLOCK_PRIVATE_ON_ACTIVE_MODES_CHANGED, |
228 OnActiveModesChanged::kEventName, std::move(args))); | 330 OnActiveModesChanged::kEventName, std::move(args))); |
229 EventRouter::Get(browser_context())->BroadcastEvent(std::move(event)); | 331 EventRouter::Get(browser_context())->BroadcastEvent(std::move(event)); |
230 } | 332 } |
231 | 333 |
232 } // namespace extensions | 334 } // namespace extensions |
OLD | NEW |