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

Side by Side Diff: components/user_manager/known_user.cc

Issue 2529103002: Add account_type into AccountId (Closed)
Patch Set: more tests Created 4 years 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 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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 "components/user_manager/known_user.h" 5 #include "components/user_manager/known_user.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include <memory> 9 #include <memory>
10 #include <utility> 10 #include <utility>
(...skipping 14 matching lines...) Expand all
25 const char kKnownUsers[] = "KnownUsers"; 25 const char kKnownUsers[] = "KnownUsers";
26 26
27 // Known user preferences keys (stored in Local State). 27 // Known user preferences keys (stored in Local State).
28 28
29 // Key of canonical e-mail value. 29 // Key of canonical e-mail value.
30 const char kCanonicalEmail[] = "email"; 30 const char kCanonicalEmail[] = "email";
31 31
32 // Key of obfuscated GAIA id value. 32 // Key of obfuscated GAIA id value.
33 const char kGAIAIdKey[] = "gaia_id"; 33 const char kGAIAIdKey[] = "gaia_id";
34 34
35 // Key of obfuscated object guid value for Active Directory accounts.
36 const char kObjGuidKey[] = "obj_guid";
37
38 // Key of account type.
39 const char kAccountTypeKey[] = "account_type";
40
35 // Key of whether this user ID refers to a SAML user. 41 // Key of whether this user ID refers to a SAML user.
36 const char kUsingSAMLKey[] = "using_saml"; 42 const char kUsingSAMLKey[] = "using_saml";
37 43
38 // Key of Device Id. 44 // Key of Device Id.
39 const char kDeviceId[] = "device_id"; 45 const char kDeviceId[] = "device_id";
40 46
41 // Key of GAPS cookie. 47 // Key of GAPS cookie.
42 const char kGAPSCookie[] = "gaps_cookie"; 48 const char kGAPSCookie[] = "gaps_cookie";
43 49
44 // Key of the reason for re-auth. 50 // Key of the reason for re-auth.
45 const char kReauthReasonKey[] = "reauth_reason"; 51 const char kReauthReasonKey[] = "reauth_reason";
46 52
47 // Key for the GaiaId migration status. 53 // Key for the GaiaId migration status.
48 const char kGaiaIdMigration[] = "gaia_id_migration"; 54 const char kGaiaIdMigration[] = "gaia_id_migration";
49 55
50 PrefService* GetLocalState() { 56 PrefService* GetLocalState() {
51 if (!UserManager::IsInitialized()) 57 if (!UserManager::IsInitialized())
52 return nullptr; 58 return nullptr;
53 59
54 return UserManager::Get()->GetLocalState(); 60 return UserManager::Get()->GetLocalState();
55 } 61 }
56 62
57 // Checks if values in |dict| correspond with |account_id| identity. 63 // Checks if values in |dict| correspond with |account_id| identity.
58 bool UserMatches(const AccountId& account_id, 64 bool UserMatches(const AccountId& account_id,
59 const base::DictionaryValue& dict) { 65 const base::DictionaryValue& dict) {
60 std::string value; 66 std::string value;
67 if (account_id.GetAccountType() != AccountType::UNKNOWN &&
68 dict.GetString(kAccountTypeKey, &value) &&
69 account_id.GetAccountType() != AccountId::StringToAccountType(value)) {
70 return false;
71 }
61 72
62 // TODO(alemate): update code once user id is really a struct. 73 // TODO(alemate): update code once user id is really a struct.
63 bool has_gaia_id = dict.GetString(kGAIAIdKey, &value); 74 switch (account_id.GetAccountType()) {
64 if (has_gaia_id && account_id.GetGaiaId() == value) 75 case AccountType::GOOGLE: {
65 return true; 76 bool has_gaia_id = dict.GetString(kGAIAIdKey, &value);
77 if (has_gaia_id && account_id.GetGaiaId() == value)
78 return true;
79 break;
80 }
81 case AccountType::ACTIVE_DIRECTORY: {
82 bool has_obj_guid = dict.GetString(kObjGuidKey, &value);
83 if (has_obj_guid && account_id.GetObjGuid() == value)
84 return true;
85 }
86 case AccountType::UNKNOWN: {
87 }
88 }
66 89
67 bool has_email = dict.GetString(kCanonicalEmail, &value); 90 bool has_email = dict.GetString(kCanonicalEmail, &value);
68 if (has_email && account_id.GetUserEmail() == value) 91 if (has_email && account_id.GetUserEmail() == value)
69 return true; 92 return true;
70 93
71 return false; 94 return false;
72 } 95 }
73 96
74 // Fills relevant |dict| values based on |account_id|. 97 // Fills relevant |dict| values based on |account_id|.
75 void UpdateIdentity(const AccountId& account_id, base::DictionaryValue& dict) { 98 void UpdateIdentity(const AccountId& account_id, base::DictionaryValue& dict) {
76 if (!account_id.GetUserEmail().empty()) 99 if (!account_id.GetUserEmail().empty())
77 dict.SetString(kCanonicalEmail, account_id.GetUserEmail()); 100 dict.SetString(kCanonicalEmail, account_id.GetUserEmail());
78 101
79 if (!account_id.GetGaiaId().empty()) 102 switch (account_id.GetAccountType()) {
80 dict.SetString(kGAIAIdKey, account_id.GetGaiaId()); 103 case AccountType::GOOGLE:
104 if (!account_id.GetGaiaId().empty())
105 dict.SetString(kGAIAIdKey, account_id.GetGaiaId());
106 break;
107 case AccountType::ACTIVE_DIRECTORY:
108 if (!account_id.GetObjGuid().empty())
109 dict.SetString(kObjGuidKey, account_id.GetObjGuid());
110 break;
111 case AccountType::UNKNOWN:
112 return;
113 }
114 dict.SetString(kAccountTypeKey,
115 AccountId::AccountTypeToString(account_id.GetAccountType()));
81 } 116 }
82 117
83 } // namespace 118 } // namespace
84 119
85 bool FindPrefs(const AccountId& account_id, 120 bool FindPrefs(const AccountId& account_id,
86 const base::DictionaryValue** out_value) { 121 const base::DictionaryValue** out_value) {
87 PrefService* local_state = GetLocalState(); 122 PrefService* local_state = GetLocalState();
88 123
89 // Local State may not be initialized in tests. 124 // Local State may not be initialized in tests.
90 if (!local_state) 125 if (!local_state)
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after
209 if (!local_state) 244 if (!local_state)
210 return; 245 return;
211 246
212 ListPrefUpdate update(local_state, kKnownUsers); 247 ListPrefUpdate update(local_state, kKnownUsers);
213 base::DictionaryValue dict; 248 base::DictionaryValue dict;
214 dict.SetInteger(path, in_value); 249 dict.SetInteger(path, in_value);
215 UpdatePrefs(account_id, dict, false); 250 UpdatePrefs(account_id, dict, false);
216 } 251 }
217 252
218 AccountId GetAccountId(const std::string& user_email, 253 AccountId GetAccountId(const std::string& user_email,
219 const std::string& gaia_id) { 254 const std::string& id,
255 const AccountType& account_type) {
256 DCHECK((id.empty() && account_type == AccountType::UNKNOWN) ||
257 (!id.empty() && account_type != AccountType::UNKNOWN));
220 // In tests empty accounts are possible. 258 // In tests empty accounts are possible.
221 if (user_email.empty() && gaia_id.empty()) 259 if (user_email.empty() && id.empty() &&
260 account_type == AccountType::UNKNOWN) {
222 return EmptyAccountId(); 261 return EmptyAccountId();
262 }
223 263
224 AccountId result(EmptyAccountId()); 264 AccountId result(EmptyAccountId());
225 // UserManager is usually NULL in unit tests. 265 // UserManager is usually NULL in unit tests.
226 if (UserManager::IsInitialized() && 266 if (account_type == AccountType::UNKNOWN && UserManager::IsInitialized() &&
227 UserManager::Get()->GetPlatformKnownUserId(user_email, gaia_id, 267 UserManager::Get()->GetPlatformKnownUserId(user_email, id, &result)) {
228 &result)) {
229 return result; 268 return result;
230 } 269 }
231 270
232 // We can have several users with the same gaia_id but different e-mails.
233 // The opposite case is not possible.
234 std::string stored_gaia_id; 271 std::string stored_gaia_id;
272 std::string stored_obj_guid;
235 const std::string sanitized_email = 273 const std::string sanitized_email =
236 user_email.empty() 274 user_email.empty()
237 ? std::string() 275 ? std::string()
238 : gaia::CanonicalizeEmail(gaia::SanitizeEmail(user_email)); 276 : gaia::CanonicalizeEmail(gaia::SanitizeEmail(user_email));
239 277
240 if (!sanitized_email.empty() && 278 if (!sanitized_email.empty()) {
241 GetStringPref(AccountId::FromUserEmail(sanitized_email), kGAIAIdKey, 279 if (GetStringPref(AccountId::FromUserEmail(sanitized_email), kGAIAIdKey,
242 &stored_gaia_id)) { 280 &stored_gaia_id)) {
243 if (!gaia_id.empty() && gaia_id != stored_gaia_id) 281 if (!id.empty()) {
244 LOG(ERROR) << "User gaia id has changed. Sync will not work."; 282 DCHECK(account_type == AccountType::GOOGLE);
283 if (id != stored_gaia_id)
284 LOG(ERROR) << "User gaia id has changed. Sync will not work.";
285 }
245 286
246 // gaia_id is associated with cryptohome. 287 // gaia_id is associated with cryptohome.
247 return AccountId::FromUserEmailGaiaId(sanitized_email, stored_gaia_id); 288 return AccountId::FromUserEmailGaiaId(sanitized_email, stored_gaia_id);
289 }
290
291 if (GetStringPref(AccountId::FromUserEmail(sanitized_email), kObjGuidKey,
292 &stored_obj_guid)) {
293 if (!id.empty()) {
294 DCHECK(account_type == AccountType::ACTIVE_DIRECTORY);
295 if (id != stored_obj_guid)
296 LOG(ERROR) << "User object guid has changed. Sync will not work.";
297 }
298
299 // obj_guid is associated with cryptohome.
300 return AccountId::AdFromUserEmailObjGuid(sanitized_email,
301 stored_obj_guid);
302 }
248 } 303 }
249 304
250 std::string stored_email; 305 std::string stored_email;
251 // GetStringPref() returns the first user record that matches 306 switch (account_type) {
252 // given ID. So we will get the first one if there are multiples. 307 case AccountType::GOOGLE:
253 if (!gaia_id.empty() && GetStringPref(AccountId::FromGaiaId(gaia_id), 308 if (GetStringPref(AccountId::FromGaiaId(id), kCanonicalEmail,
254 kCanonicalEmail, &stored_email)) { 309 &stored_email)) {
255 return AccountId::FromUserEmailGaiaId(stored_email, gaia_id); 310 return AccountId::FromUserEmailGaiaId(stored_email, id);
311 }
312 return AccountId::FromUserEmailGaiaId(sanitized_email, id);
313 case AccountType::ACTIVE_DIRECTORY:
314 if (GetStringPref(AccountId::AdFromObjGuid(id), kCanonicalEmail,
315 &stored_email)) {
316 return AccountId::AdFromUserEmailObjGuid(stored_email, id);
317 }
318 return AccountId::AdFromUserEmailObjGuid(sanitized_email, id);
319 case AccountType::UNKNOWN:
320 return AccountId::FromUserEmail(sanitized_email);
256 } 321 }
257 322 NOTREACHED();
258 return (gaia_id.empty() 323 return EmptyAccountId();
259 ? AccountId::FromUserEmail(user_email)
260 : AccountId::FromUserEmailGaiaId(user_email, gaia_id));
261 } 324 }
262 325
263 std::vector<AccountId> GetKnownAccountIds() { 326 std::vector<AccountId> GetKnownAccountIds() {
264 std::vector<AccountId> result; 327 std::vector<AccountId> result;
265 PrefService* local_state = GetLocalState(); 328 PrefService* local_state = GetLocalState();
266 329
267 // Local State may not be initialized in tests. 330 // Local State may not be initialized in tests.
268 if (!local_state) 331 if (!local_state)
269 return result; 332 return result;
270 333
271 const base::ListValue* known_users = local_state->GetList(kKnownUsers); 334 const base::ListValue* known_users = local_state->GetList(kKnownUsers);
272 for (size_t i = 0; i < known_users->GetSize(); ++i) { 335 for (size_t i = 0; i < known_users->GetSize(); ++i) {
273 const base::DictionaryValue* element = nullptr; 336 const base::DictionaryValue* element = nullptr;
274 if (known_users->GetDictionary(i, &element)) { 337 if (known_users->GetDictionary(i, &element)) {
275 std::string email; 338 std::string email;
276 std::string gaia_id; 339 std::string gaia_id;
340 std::string obj_guid;
277 const bool has_email = element->GetString(kCanonicalEmail, &email); 341 const bool has_email = element->GetString(kCanonicalEmail, &email);
278 const bool has_gaia_id = element->GetString(kGAIAIdKey, &gaia_id); 342 const bool has_gaia_id = element->GetString(kGAIAIdKey, &gaia_id);
279 if (has_email || has_gaia_id) 343 const bool has_obj_guid = element->GetString(kObjGuidKey, &obj_guid);
280 result.push_back(AccountId::FromUserEmailGaiaId(email, gaia_id)); 344 AccountType account_type = AccountType::GOOGLE;
345 std::string account_type_string;
346 if (element->GetString(kAccountTypeKey, &account_type_string)) {
347 account_type = AccountId::StringToAccountType(account_type_string);
348 }
349 switch (account_type) {
350 case AccountType::GOOGLE:
351 if (has_email || has_gaia_id) {
352 result.push_back(AccountId::FromUserEmailGaiaId(email, gaia_id));
353 }
354 break;
355 case AccountType::ACTIVE_DIRECTORY:
356 if (has_email && has_obj_guid) {
357 result.push_back(
358 AccountId::AdFromUserEmailObjGuid(email, obj_guid));
359 }
360 break;
361 default:
362 NOTREACHED() << "Unknown account type";
363 }
281 } 364 }
282 } 365 }
283 return result; 366 return result;
284 } 367 }
285 368
286 bool GetGaiaIdMigrationStatus(const AccountId& account_id, 369 bool GetGaiaIdMigrationStatus(const AccountId& account_id,
287 const std::string& subsystem) { 370 const std::string& subsystem) {
288 bool migrated = false; 371 bool migrated = false;
289 372
290 if (GetBooleanPref(account_id, 373 if (GetBooleanPref(account_id,
291 std::string(kGaiaIdMigration) + "." + subsystem, 374 std::string(kGaiaIdMigration) + "." + subsystem,
292 &migrated)) { 375 &migrated)) {
293 return migrated; 376 return migrated;
294 } 377 }
295 378
296 return false; 379 return false;
297 } 380 }
298 381
299 void SetGaiaIdMigrationStatusDone(const AccountId& account_id, 382 void SetGaiaIdMigrationStatusDone(const AccountId& account_id,
300 const std::string& subsystem) { 383 const std::string& subsystem) {
301 SetBooleanPref(account_id, std::string(kGaiaIdMigration) + "." + subsystem, 384 SetBooleanPref(account_id, std::string(kGaiaIdMigration) + "." + subsystem,
302 true); 385 true);
303 } 386 }
304 387
305 void UpdateGaiaID(const AccountId& account_id, const std::string& gaia_id) { 388 void UpdateGaiaID(const AccountId& account_id, const std::string& gaia_id) {
306 SetStringPref(account_id, kGAIAIdKey, gaia_id); 389 SetStringPref(account_id, kGAIAIdKey, gaia_id);
390 SetStringPref(account_id, kAccountTypeKey,
391 AccountId::AccountTypeToString(account_id.GetAccountType()));
392 }
393
394 void UpdateId(const AccountId& account_id) {
395 switch (account_id.GetAccountType()) {
396 case AccountType::GOOGLE:
397 SetStringPref(account_id, kGAIAIdKey, account_id.GetGaiaId());
398 break;
399 case AccountType::ACTIVE_DIRECTORY:
400 SetStringPref(account_id, kObjGuidKey, account_id.GetObjGuid());
401 break;
402 case AccountType::UNKNOWN:
403 return;
404 }
405 SetStringPref(account_id, kAccountTypeKey,
406 AccountId::AccountTypeToString(account_id.GetAccountType()));
307 } 407 }
308 408
309 bool FindGaiaID(const AccountId& account_id, std::string* out_value) { 409 bool FindGaiaID(const AccountId& account_id, std::string* out_value) {
310 return GetStringPref(account_id, kGAIAIdKey, out_value); 410 return GetStringPref(account_id, kGAIAIdKey, out_value);
311 } 411 }
312 412
313 void SetDeviceId(const AccountId& account_id, const std::string& device_id) { 413 void SetDeviceId(const AccountId& account_id, const std::string& device_id) {
314 const std::string known_device_id = GetDeviceId(account_id); 414 const std::string known_device_id = GetDeviceId(account_id);
315 if (!known_device_id.empty() && device_id != known_device_id) { 415 if (!known_device_id.empty() && device_id != known_device_id) {
316 NOTREACHED() << "Trying to change device ID for known user."; 416 NOTREACHED() << "Trying to change device ID for known user.";
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
376 } 476 }
377 } 477 }
378 } 478 }
379 479
380 void RegisterPrefs(PrefRegistrySimple* registry) { 480 void RegisterPrefs(PrefRegistrySimple* registry) {
381 registry->RegisterListPref(kKnownUsers); 481 registry->RegisterListPref(kKnownUsers);
382 } 482 }
383 483
384 } // namespace known_user 484 } // namespace known_user
385 } // namespace user_manager 485 } // namespace user_manager
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698