Chromium Code Reviews| 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 account type. | |
| 36 const char kAccountTypeKey[] = "account_type"; | |
| 37 | |
| 35 // Key of whether this user ID refers to a SAML user. | 38 // Key of whether this user ID refers to a SAML user. |
| 36 const char kUsingSAMLKey[] = "using_saml"; | 39 const char kUsingSAMLKey[] = "using_saml"; |
| 37 | 40 |
| 38 // Key of Device Id. | 41 // Key of Device Id. |
| 39 const char kDeviceId[] = "device_id"; | 42 const char kDeviceId[] = "device_id"; |
| 40 | 43 |
| 41 // Key of GAPS cookie. | 44 // Key of GAPS cookie. |
| 42 const char kGAPSCookie[] = "gaps_cookie"; | 45 const char kGAPSCookie[] = "gaps_cookie"; |
| 43 | 46 |
| 44 // Key of the reason for re-auth. | 47 // Key of the reason for re-auth. |
| 45 const char kReauthReasonKey[] = "reauth_reason"; | 48 const char kReauthReasonKey[] = "reauth_reason"; |
| 46 | 49 |
| 47 // Key for the GaiaId migration status. | 50 // Key for the GaiaId migration status. |
| 48 const char kGaiaIdMigration[] = "gaia_id_migration"; | 51 const char kGaiaIdMigration[] = "gaia_id_migration"; |
| 49 | 52 |
| 50 PrefService* GetLocalState() { | 53 PrefService* GetLocalState() { |
| 51 if (!UserManager::IsInitialized()) | 54 if (!UserManager::IsInitialized()) |
| 52 return nullptr; | 55 return nullptr; |
| 53 | 56 |
| 54 return UserManager::Get()->GetLocalState(); | 57 return UserManager::Get()->GetLocalState(); |
| 55 } | 58 } |
| 56 | 59 |
| 57 // Checks if values in |dict| correspond with |account_id| identity. | 60 // Checks if values in |dict| correspond with |account_id| identity. |
| 58 bool UserMatches(const AccountId& account_id, | 61 bool UserMatches(const AccountId& account_id, |
| 59 const base::DictionaryValue& dict) { | 62 const base::DictionaryValue& dict) { |
| 60 std::string value; | 63 std::string value; |
| 64 std::string account_type; | |
| 65 if (!dict.GetString(kAccountTypeKey, &account_type)) | |
| 66 account_type = AccountId::kGoogle; | |
| 61 | 67 |
| 62 // TODO(alemate): update code once user id is really a struct. | 68 // TODO(alemate): update code once user id is really a struct. |
| 63 bool has_gaia_id = dict.GetString(kGAIAIdKey, &value); | 69 bool has_gaia_id = dict.GetString(kGAIAIdKey, &value); |
| 64 if (has_gaia_id && account_id.GetGaiaId() == value) | 70 if (has_gaia_id && account_id.GetGaiaId() == value) { |
| 71 DCHECK(account_id.GetAccountType() == account_type); | |
| 65 return true; | 72 return true; |
| 73 } | |
| 66 | 74 |
| 67 bool has_email = dict.GetString(kCanonicalEmail, &value); | 75 bool has_email = dict.GetString(kCanonicalEmail, &value); |
| 68 if (has_email && account_id.GetUserEmail() == value) | 76 if (has_email && account_id.GetUserEmail() == value) |
| 69 return true; | 77 return true; |
| 70 | 78 |
| 71 return false; | 79 return false; |
| 72 } | 80 } |
| 73 | 81 |
| 74 // Fills relevant |dict| values based on |account_id|. | 82 // Fills relevant |dict| values based on |account_id|. |
| 75 void UpdateIdentity(const AccountId& account_id, base::DictionaryValue& dict) { | 83 void UpdateIdentity(const AccountId& account_id, base::DictionaryValue& dict) { |
| 76 if (!account_id.GetUserEmail().empty()) | 84 if (!account_id.GetUserEmail().empty()) |
| 77 dict.SetString(kCanonicalEmail, account_id.GetUserEmail()); | 85 dict.SetString(kCanonicalEmail, account_id.GetUserEmail()); |
| 78 | 86 |
| 79 if (!account_id.GetGaiaId().empty()) | 87 if (!account_id.GetGaiaId().empty()) |
| 80 dict.SetString(kGAIAIdKey, account_id.GetGaiaId()); | 88 dict.SetString(kGAIAIdKey, account_id.GetGaiaId()); |
| 89 | |
| 90 if (!account_id.GetAccountType().empty()) | |
| 91 dict.SetString(kAccountTypeKey, account_id.GetAccountType()); | |
| 81 } | 92 } |
| 82 | 93 |
| 83 } // namespace | 94 } // namespace |
| 84 | 95 |
| 85 bool FindPrefs(const AccountId& account_id, | 96 bool FindPrefs(const AccountId& account_id, |
| 86 const base::DictionaryValue** out_value) { | 97 const base::DictionaryValue** out_value) { |
| 87 PrefService* local_state = GetLocalState(); | 98 PrefService* local_state = GetLocalState(); |
| 88 | 99 |
| 89 // Local State may not be initialized in tests. | 100 // Local State may not be initialized in tests. |
| 90 if (!local_state) | 101 if (!local_state) |
| (...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 209 if (!local_state) | 220 if (!local_state) |
| 210 return; | 221 return; |
| 211 | 222 |
| 212 ListPrefUpdate update(local_state, kKnownUsers); | 223 ListPrefUpdate update(local_state, kKnownUsers); |
| 213 base::DictionaryValue dict; | 224 base::DictionaryValue dict; |
| 214 dict.SetInteger(path, in_value); | 225 dict.SetInteger(path, in_value); |
| 215 UpdatePrefs(account_id, dict, false); | 226 UpdatePrefs(account_id, dict, false); |
| 216 } | 227 } |
| 217 | 228 |
| 218 AccountId GetAccountId(const std::string& user_email, | 229 AccountId GetAccountId(const std::string& user_email, |
| 219 const std::string& gaia_id) { | 230 const std::string& gaia_id, |
| 231 const std::string& account_type) { | |
| 232 // Account type shouldn't be passed w/o gaia_id. | |
| 233 DCHECK(account_type.empty() || !gaia_id.empty()); | |
| 220 // In tests empty accounts are possible. | 234 // In tests empty accounts are possible. |
| 221 if (user_email.empty() && gaia_id.empty()) | 235 if (user_email.empty() && gaia_id.empty()) |
| 222 return EmptyAccountId(); | 236 return EmptyAccountId(); |
| 223 | 237 |
| 224 AccountId result(EmptyAccountId()); | 238 AccountId result(EmptyAccountId()); |
| 225 // UserManager is usually NULL in unit tests. | 239 // UserManager is usually NULL in unit tests. |
| 226 if (UserManager::IsInitialized() && | 240 if (UserManager::IsInitialized() && |
| 227 UserManager::Get()->GetPlatformKnownUserId(user_email, gaia_id, | 241 UserManager::Get()->GetPlatformKnownUserId(user_email, gaia_id, |
| 228 &result)) { | 242 &result)) { |
| 229 return result; | 243 return result; |
| 230 } | 244 } |
| 231 | 245 |
| 232 // We can have several users with the same gaia_id but different e-mails. | 246 // We can have several users with the same gaia_id but different e-mails. |
| 233 // The opposite case is not possible. | 247 // The opposite case is not possible. |
| 234 std::string stored_gaia_id; | 248 std::string stored_gaia_id; |
| 235 const std::string sanitized_email = | 249 const std::string sanitized_email = |
| 236 user_email.empty() | 250 user_email.empty() |
| 237 ? std::string() | 251 ? std::string() |
| 238 : gaia::CanonicalizeEmail(gaia::SanitizeEmail(user_email)); | 252 : gaia::CanonicalizeEmail(gaia::SanitizeEmail(user_email)); |
| 253 std::string stored_account_type; | |
| 254 const bool has_stored_account_type = | |
| 255 GetStringPref(AccountId::FromUserEmail(sanitized_email), kAccountTypeKey, | |
| 256 &stored_account_type); | |
| 257 if (has_stored_account_type && !account_type.empty() && | |
| 258 account_type != stored_account_type) { | |
| 259 LOG(FATAL) << "Account type has changed"; | |
| 260 } | |
| 239 | 261 |
| 240 if (!sanitized_email.empty() && | 262 if (!sanitized_email.empty() && |
| 241 GetStringPref(AccountId::FromUserEmail(sanitized_email), kGAIAIdKey, | 263 GetStringPref(AccountId::FromUserEmail(sanitized_email), kGAIAIdKey, |
| 242 &stored_gaia_id)) { | 264 &stored_gaia_id)) { |
| 243 if (!gaia_id.empty() && gaia_id != stored_gaia_id) | 265 if (!gaia_id.empty() && gaia_id != stored_gaia_id) |
| 244 LOG(ERROR) << "User gaia id has changed. Sync will not work."; | 266 LOG(ERROR) << "User gaia id has changed. Sync will not work."; |
| 245 | 267 |
| 246 // gaia_id is associated with cryptohome. | 268 return has_stored_account_type |
| 247 return AccountId::FromUserEmailGaiaId(sanitized_email, stored_gaia_id); | 269 ? AccountId::FromUserEmailGaiaIdAccountType( |
| 270 sanitized_email, stored_gaia_id, stored_account_type) | |
| 271 : AccountId::FromUserEmailGaiaId(sanitized_email, | |
| 272 stored_gaia_id); | |
| 248 } | 273 } |
| 274 DCHECK(!has_stored_account_type); | |
| 249 | 275 |
| 250 std::string stored_email; | 276 std::string stored_email; |
| 251 // GetStringPref() returns the first user record that matches | 277 // GetStringPref() returns the first user record that matches |
| 252 // given ID. So we will get the first one if there are multiples. | 278 // given ID. So we will get the first one if there are multiples. |
| 253 if (!gaia_id.empty() && GetStringPref(AccountId::FromGaiaId(gaia_id), | 279 if (!gaia_id.empty()) { |
| 254 kCanonicalEmail, &stored_email)) { | 280 const AccountId account_id( |
| 255 return AccountId::FromUserEmailGaiaId(stored_email, gaia_id); | 281 account_type.empty() |
| 282 ? AccountId::FromGaiaId(gaia_id) | |
| 283 : AccountId::FromGaiaIdAccountType(gaia_id, account_type)); | |
| 284 if (GetStringPref(account_id, kCanonicalEmail, &stored_email)) { | |
| 285 return AccountId::FromUserEmailGaiaIdAccountType( | |
| 286 stored_email, gaia_id, account_id.GetAccountType()); | |
| 287 } | |
| 256 } | 288 } |
| 257 | 289 |
| 258 return (gaia_id.empty() | 290 if (!account_type.empty()) { |
| 259 ? AccountId::FromUserEmail(user_email) | 291 return AccountId::FromUserEmailGaiaIdAccountType(user_email, gaia_id, |
| 260 : AccountId::FromUserEmailGaiaId(user_email, gaia_id)); | 292 account_type); |
| 293 } else { | |
| 294 return (gaia_id.empty() | |
|
Alexander Alekseev
2016/11/27 08:41:54
nit: no else
Roman Sorokin (ftl)
2016/12/02 11:13:11
Done.
| |
| 295 ? AccountId::FromUserEmail(user_email) | |
| 296 : AccountId::FromUserEmailGaiaId(user_email, gaia_id)); | |
| 297 } | |
| 261 } | 298 } |
| 262 | 299 |
| 263 std::vector<AccountId> GetKnownAccountIds() { | 300 std::vector<AccountId> GetKnownAccountIds() { |
| 264 std::vector<AccountId> result; | 301 std::vector<AccountId> result; |
| 265 PrefService* local_state = GetLocalState(); | 302 PrefService* local_state = GetLocalState(); |
| 266 | 303 |
| 267 // Local State may not be initialized in tests. | 304 // Local State may not be initialized in tests. |
| 268 if (!local_state) | 305 if (!local_state) |
| 269 return result; | 306 return result; |
| 270 | 307 |
| 271 const base::ListValue* known_users = local_state->GetList(kKnownUsers); | 308 const base::ListValue* known_users = local_state->GetList(kKnownUsers); |
| 272 for (size_t i = 0; i < known_users->GetSize(); ++i) { | 309 for (size_t i = 0; i < known_users->GetSize(); ++i) { |
| 273 const base::DictionaryValue* element = nullptr; | 310 const base::DictionaryValue* element = nullptr; |
| 274 if (known_users->GetDictionary(i, &element)) { | 311 if (known_users->GetDictionary(i, &element)) { |
| 275 std::string email; | 312 std::string email; |
| 276 std::string gaia_id; | 313 std::string gaia_id; |
| 314 std::string account_type = AccountId::kGoogle; | |
| 277 const bool has_email = element->GetString(kCanonicalEmail, &email); | 315 const bool has_email = element->GetString(kCanonicalEmail, &email); |
| 278 const bool has_gaia_id = element->GetString(kGAIAIdKey, &gaia_id); | 316 const bool has_gaia_id = element->GetString(kGAIAIdKey, &gaia_id); |
| 279 if (has_email || has_gaia_id) | 317 element->GetString(kAccountTypeKey, &account_type); |
| 280 result.push_back(AccountId::FromUserEmailGaiaId(email, gaia_id)); | 318 if (has_email || has_gaia_id) { |
| 319 result.push_back(AccountId::FromUserEmailGaiaIdAccountType( | |
| 320 email, gaia_id, account_type)); | |
| 321 } | |
| 281 } | 322 } |
| 282 } | 323 } |
| 283 return result; | 324 return result; |
| 284 } | 325 } |
| 285 | 326 |
| 286 bool GetGaiaIdMigrationStatus(const AccountId& account_id, | 327 bool GetGaiaIdMigrationStatus(const AccountId& account_id, |
| 287 const std::string& subsystem) { | 328 const std::string& subsystem) { |
| 288 bool migrated = false; | 329 bool migrated = false; |
| 289 | 330 |
| 290 if (GetBooleanPref(account_id, | 331 if (GetBooleanPref(account_id, |
| 291 std::string(kGaiaIdMigration) + "." + subsystem, | 332 std::string(kGaiaIdMigration) + "." + subsystem, |
| 292 &migrated)) { | 333 &migrated)) { |
| 293 return migrated; | 334 return migrated; |
| 294 } | 335 } |
| 295 | 336 |
| 296 return false; | 337 return false; |
| 297 } | 338 } |
| 298 | 339 |
| 299 void SetGaiaIdMigrationStatusDone(const AccountId& account_id, | 340 void SetGaiaIdMigrationStatusDone(const AccountId& account_id, |
| 300 const std::string& subsystem) { | 341 const std::string& subsystem) { |
| 301 SetBooleanPref(account_id, std::string(kGaiaIdMigration) + "." + subsystem, | 342 SetBooleanPref(account_id, std::string(kGaiaIdMigration) + "." + subsystem, |
| 302 true); | 343 true); |
| 303 } | 344 } |
| 304 | 345 |
| 305 void UpdateGaiaID(const AccountId& account_id, const std::string& gaia_id) { | 346 void UpdateGaiaID(const AccountId& account_id, const std::string& gaia_id) { |
| 306 SetStringPref(account_id, kGAIAIdKey, gaia_id); | 347 SetStringPref(account_id, kGAIAIdKey, gaia_id); |
| 307 } | 348 } |
| 308 | 349 |
| 350 void UpdateAccountType(const AccountId& account_id) { | |
| 351 SetStringPref(account_id, kAccountTypeKey, account_id.GetAccountType()); | |
| 352 } | |
| 353 | |
| 309 bool FindGaiaID(const AccountId& account_id, std::string* out_value) { | 354 bool FindGaiaID(const AccountId& account_id, std::string* out_value) { |
| 310 return GetStringPref(account_id, kGAIAIdKey, out_value); | 355 return GetStringPref(account_id, kGAIAIdKey, out_value); |
| 311 } | 356 } |
| 312 | 357 |
| 313 void SetDeviceId(const AccountId& account_id, const std::string& device_id) { | 358 void SetDeviceId(const AccountId& account_id, const std::string& device_id) { |
| 314 const std::string known_device_id = GetDeviceId(account_id); | 359 const std::string known_device_id = GetDeviceId(account_id); |
| 315 if (!known_device_id.empty() && device_id != known_device_id) { | 360 if (!known_device_id.empty() && device_id != known_device_id) { |
| 316 NOTREACHED() << "Trying to change device ID for known user."; | 361 NOTREACHED() << "Trying to change device ID for known user."; |
| 317 } | 362 } |
| 318 SetStringPref(account_id, kDeviceId, device_id); | 363 SetStringPref(account_id, kDeviceId, device_id); |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 376 } | 421 } |
| 377 } | 422 } |
| 378 } | 423 } |
| 379 | 424 |
| 380 void RegisterPrefs(PrefRegistrySimple* registry) { | 425 void RegisterPrefs(PrefRegistrySimple* registry) { |
| 381 registry->RegisterListPref(kKnownUsers); | 426 registry->RegisterListPref(kKnownUsers); |
| 382 } | 427 } |
| 383 | 428 |
| 384 } // namespace known_user | 429 } // namespace known_user |
| 385 } // namespace user_manager | 430 } // namespace user_manager |
| OLD | NEW |