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