| 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 |
| 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 break; |
| 86 } |
| 87 case AccountType::UNKNOWN: { |
| 88 } |
| 89 } |
| 66 | 90 |
| 67 bool has_email = dict.GetString(kCanonicalEmail, &value); | 91 bool has_email = dict.GetString(kCanonicalEmail, &value); |
| 68 if (has_email && account_id.GetUserEmail() == value) | 92 if (has_email && account_id.GetUserEmail() == value) |
| 69 return true; | 93 return true; |
| 70 | 94 |
| 71 return false; | 95 return false; |
| 72 } | 96 } |
| 73 | 97 |
| 74 // Fills relevant |dict| values based on |account_id|. | 98 // Fills relevant |dict| values based on |account_id|. |
| 75 void UpdateIdentity(const AccountId& account_id, base::DictionaryValue& dict) { | 99 void UpdateIdentity(const AccountId& account_id, base::DictionaryValue& dict) { |
| 76 if (!account_id.GetUserEmail().empty()) | 100 if (!account_id.GetUserEmail().empty()) |
| 77 dict.SetString(kCanonicalEmail, account_id.GetUserEmail()); | 101 dict.SetString(kCanonicalEmail, account_id.GetUserEmail()); |
| 78 | 102 |
| 79 if (!account_id.GetGaiaId().empty()) | 103 switch (account_id.GetAccountType()) { |
| 80 dict.SetString(kGAIAIdKey, account_id.GetGaiaId()); | 104 case AccountType::GOOGLE: |
| 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())); |
| 81 } | 117 } |
| 82 | 118 |
| 83 } // namespace | 119 } // namespace |
| 84 | 120 |
| 85 bool FindPrefs(const AccountId& account_id, | 121 bool FindPrefs(const AccountId& account_id, |
| 86 const base::DictionaryValue** out_value) { | 122 const base::DictionaryValue** out_value) { |
| 87 PrefService* local_state = GetLocalState(); | 123 PrefService* local_state = GetLocalState(); |
| 88 | 124 |
| 89 // Local State may not be initialized in tests. | 125 // Local State may not be initialized in tests. |
| 90 if (!local_state) | 126 if (!local_state) |
| (...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 209 if (!local_state) | 245 if (!local_state) |
| 210 return; | 246 return; |
| 211 | 247 |
| 212 ListPrefUpdate update(local_state, kKnownUsers); | 248 ListPrefUpdate update(local_state, kKnownUsers); |
| 213 base::DictionaryValue dict; | 249 base::DictionaryValue dict; |
| 214 dict.SetInteger(path, in_value); | 250 dict.SetInteger(path, in_value); |
| 215 UpdatePrefs(account_id, dict, false); | 251 UpdatePrefs(account_id, dict, false); |
| 216 } | 252 } |
| 217 | 253 |
| 218 AccountId GetAccountId(const std::string& user_email, | 254 AccountId GetAccountId(const std::string& user_email, |
| 219 const std::string& gaia_id) { | 255 const std::string& id, |
| 256 const AccountType& account_type) { |
| 257 DCHECK((id.empty() && account_type == AccountType::UNKNOWN) || |
| 258 (!id.empty() && account_type != AccountType::UNKNOWN)); |
| 220 // In tests empty accounts are possible. | 259 // In tests empty accounts are possible. |
| 221 if (user_email.empty() && gaia_id.empty()) | 260 if (user_email.empty() && id.empty() && |
| 261 account_type == AccountType::UNKNOWN) { |
| 222 return EmptyAccountId(); | 262 return EmptyAccountId(); |
| 263 } |
| 223 | 264 |
| 224 AccountId result(EmptyAccountId()); | 265 AccountId result(EmptyAccountId()); |
| 225 // UserManager is usually NULL in unit tests. | 266 // UserManager is usually NULL in unit tests. |
| 226 if (UserManager::IsInitialized() && | 267 if (account_type == AccountType::UNKNOWN && UserManager::IsInitialized() && |
| 227 UserManager::Get()->GetPlatformKnownUserId(user_email, gaia_id, | 268 UserManager::Get()->GetPlatformKnownUserId(user_email, id, &result)) { |
| 228 &result)) { | |
| 229 return result; | 269 return result; |
| 230 } | 270 } |
| 231 | 271 |
| 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; | 272 std::string stored_gaia_id; |
| 273 std::string stored_obj_guid; |
| 235 const std::string sanitized_email = | 274 const std::string sanitized_email = |
| 236 user_email.empty() | 275 user_email.empty() |
| 237 ? std::string() | 276 ? std::string() |
| 238 : gaia::CanonicalizeEmail(gaia::SanitizeEmail(user_email)); | 277 : gaia::CanonicalizeEmail(gaia::SanitizeEmail(user_email)); |
| 239 | 278 |
| 240 if (!sanitized_email.empty() && | 279 if (!sanitized_email.empty()) { |
| 241 GetStringPref(AccountId::FromUserEmail(sanitized_email), kGAIAIdKey, | 280 if (GetStringPref(AccountId::FromUserEmail(sanitized_email), kGAIAIdKey, |
| 242 &stored_gaia_id)) { | 281 &stored_gaia_id)) { |
| 243 if (!gaia_id.empty() && gaia_id != stored_gaia_id) | 282 if (!id.empty()) { |
| 244 LOG(ERROR) << "User gaia id has changed. Sync will not work."; | 283 DCHECK(account_type == AccountType::GOOGLE); |
| 284 if (id != stored_gaia_id) |
| 285 LOG(ERROR) << "User gaia id has changed. Sync will not work."; |
| 286 } |
| 245 | 287 |
| 246 // gaia_id is associated with cryptohome. | 288 // gaia_id is associated with cryptohome. |
| 247 return AccountId::FromUserEmailGaiaId(sanitized_email, stored_gaia_id); | 289 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 } |
| 248 } | 304 } |
| 249 | 305 |
| 250 std::string stored_email; | 306 std::string stored_email; |
| 251 // GetStringPref() returns the first user record that matches | 307 switch (account_type) { |
| 252 // given ID. So we will get the first one if there are multiples. | 308 case AccountType::GOOGLE: |
| 253 if (!gaia_id.empty() && GetStringPref(AccountId::FromGaiaId(gaia_id), | 309 if (GetStringPref(AccountId::FromGaiaId(id), kCanonicalEmail, |
| 254 kCanonicalEmail, &stored_email)) { | 310 &stored_email)) { |
| 255 return AccountId::FromUserEmailGaiaId(stored_email, gaia_id); | 311 return AccountId::FromUserEmailGaiaId(stored_email, 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); |
| 256 } | 322 } |
| 257 | 323 NOTREACHED(); |
| 258 return (gaia_id.empty() | 324 return EmptyAccountId(); |
| 259 ? AccountId::FromUserEmail(user_email) | |
| 260 : AccountId::FromUserEmailGaiaId(user_email, gaia_id)); | |
| 261 } | 325 } |
| 262 | 326 |
| 263 std::vector<AccountId> GetKnownAccountIds() { | 327 std::vector<AccountId> GetKnownAccountIds() { |
| 264 std::vector<AccountId> result; | 328 std::vector<AccountId> result; |
| 265 PrefService* local_state = GetLocalState(); | 329 PrefService* local_state = GetLocalState(); |
| 266 | 330 |
| 267 // Local State may not be initialized in tests. | 331 // Local State may not be initialized in tests. |
| 268 if (!local_state) | 332 if (!local_state) |
| 269 return result; | 333 return result; |
| 270 | 334 |
| 271 const base::ListValue* known_users = local_state->GetList(kKnownUsers); | 335 const base::ListValue* known_users = local_state->GetList(kKnownUsers); |
| 272 for (size_t i = 0; i < known_users->GetSize(); ++i) { | 336 for (size_t i = 0; i < known_users->GetSize(); ++i) { |
| 273 const base::DictionaryValue* element = nullptr; | 337 const base::DictionaryValue* element = nullptr; |
| 274 if (known_users->GetDictionary(i, &element)) { | 338 if (known_users->GetDictionary(i, &element)) { |
| 275 std::string email; | 339 std::string email; |
| 276 std::string gaia_id; | 340 std::string gaia_id; |
| 341 std::string obj_guid; |
| 277 const bool has_email = element->GetString(kCanonicalEmail, &email); | 342 const bool has_email = element->GetString(kCanonicalEmail, &email); |
| 278 const bool has_gaia_id = element->GetString(kGAIAIdKey, &gaia_id); | 343 const bool has_gaia_id = element->GetString(kGAIAIdKey, &gaia_id); |
| 279 if (has_email || has_gaia_id) | 344 const bool has_obj_guid = element->GetString(kObjGuidKey, &obj_guid); |
| 280 result.push_back(AccountId::FromUserEmailGaiaId(email, gaia_id)); | 345 AccountType account_type = AccountType::GOOGLE; |
| 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 } |
| 281 } | 365 } |
| 282 } | 366 } |
| 283 return result; | 367 return result; |
| 284 } | 368 } |
| 285 | 369 |
| 286 bool GetGaiaIdMigrationStatus(const AccountId& account_id, | 370 bool GetGaiaIdMigrationStatus(const AccountId& account_id, |
| 287 const std::string& subsystem) { | 371 const std::string& subsystem) { |
| 288 bool migrated = false; | 372 bool migrated = false; |
| 289 | 373 |
| 290 if (GetBooleanPref(account_id, | 374 if (GetBooleanPref(account_id, |
| 291 std::string(kGaiaIdMigration) + "." + subsystem, | 375 std::string(kGaiaIdMigration) + "." + subsystem, |
| 292 &migrated)) { | 376 &migrated)) { |
| 293 return migrated; | 377 return migrated; |
| 294 } | 378 } |
| 295 | 379 |
| 296 return false; | 380 return false; |
| 297 } | 381 } |
| 298 | 382 |
| 299 void SetGaiaIdMigrationStatusDone(const AccountId& account_id, | 383 void SetGaiaIdMigrationStatusDone(const AccountId& account_id, |
| 300 const std::string& subsystem) { | 384 const std::string& subsystem) { |
| 301 SetBooleanPref(account_id, std::string(kGaiaIdMigration) + "." + subsystem, | 385 SetBooleanPref(account_id, std::string(kGaiaIdMigration) + "." + subsystem, |
| 302 true); | 386 true); |
| 303 } | 387 } |
| 304 | 388 |
| 305 void UpdateGaiaID(const AccountId& account_id, const std::string& gaia_id) { | 389 void UpdateGaiaID(const AccountId& account_id, const std::string& gaia_id) { |
| 306 SetStringPref(account_id, kGAIAIdKey, gaia_id); | 390 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())); |
| 307 } | 408 } |
| 308 | 409 |
| 309 bool FindGaiaID(const AccountId& account_id, std::string* out_value) { | 410 bool FindGaiaID(const AccountId& account_id, std::string* out_value) { |
| 310 return GetStringPref(account_id, kGAIAIdKey, out_value); | 411 return GetStringPref(account_id, kGAIAIdKey, out_value); |
| 311 } | 412 } |
| 312 | 413 |
| 313 void SetDeviceId(const AccountId& account_id, const std::string& device_id) { | 414 void SetDeviceId(const AccountId& account_id, const std::string& device_id) { |
| 314 const std::string known_device_id = GetDeviceId(account_id); | 415 const std::string known_device_id = GetDeviceId(account_id); |
| 315 if (!known_device_id.empty() && device_id != known_device_id) { | 416 if (!known_device_id.empty() && device_id != known_device_id) { |
| 316 NOTREACHED() << "Trying to change device ID for known user."; | 417 NOTREACHED() << "Trying to change device ID for known user."; |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 376 } | 477 } |
| 377 } | 478 } |
| 378 } | 479 } |
| 379 | 480 |
| 380 void RegisterPrefs(PrefRegistrySimple* registry) { | 481 void RegisterPrefs(PrefRegistrySimple* registry) { |
| 381 registry->RegisterListPref(kKnownUsers); | 482 registry->RegisterListPref(kKnownUsers); |
| 382 } | 483 } |
| 383 | 484 |
| 384 } // namespace known_user | 485 } // namespace known_user |
| 385 } // namespace user_manager | 486 } // namespace user_manager |
| OLD | NEW |