| 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/signin/core/account_id/account_id.h" | 5 #include "components/signin/core/account_id/account_id.h" |
| 6 | 6 |
| 7 #include <functional> | 7 #include <functional> |
| 8 #include <memory> | 8 #include <memory> |
| 9 | 9 |
| 10 #include "base/json/json_reader.h" | 10 #include "base/json/json_reader.h" |
| 11 #include "base/json/json_writer.h" | 11 #include "base/json/json_writer.h" |
| 12 #include "base/memory/singleton.h" | 12 #include "base/memory/singleton.h" |
| 13 #include "base/strings/string_util.h" | 13 #include "base/strings/string_util.h" |
| 14 #include "base/values.h" | 14 #include "base/values.h" |
| 15 #include "google_apis/gaia/gaia_auth_util.h" | 15 #include "google_apis/gaia/gaia_auth_util.h" |
| 16 | 16 |
| 17 namespace { | 17 namespace { |
| 18 | 18 |
| 19 // Known account types. |
| 20 const char kGoogle[] = "google"; |
| 21 |
| 19 // Serialization keys | 22 // Serialization keys |
| 20 const char kGaiaIdKey[] = "gaia_id"; | 23 const char kGaiaIdKey[] = "gaia_id"; |
| 21 const char kEmailKey[] = "email"; | 24 const char kEmailKey[] = "email"; |
| 22 const char kObjGuid[] = "obj_guid"; | |
| 23 const char kAccountTypeKey[] = "account_type"; | |
| 24 | |
| 25 // Serialization values for account type. | |
| 26 const std::string kGoogle = "google"; | |
| 27 const std::string kAd = "ad"; | |
| 28 | 25 |
| 29 // Prefix for GetAccountIdKey(). | 26 // Prefix for GetAccountIdKey(). |
| 30 const char kKeyGaiaIdPrefix[] = "g-"; | 27 const char kKeyGaiaIdPrefix[] = "g-"; |
| 31 const char kKeyAdIdPrefix[] = "a-"; | 28 |
| 29 struct GoogleStringSingleton { |
| 30 GoogleStringSingleton() : google(kGoogle) {} |
| 31 |
| 32 static GoogleStringSingleton* GetInstance() { |
| 33 return base::Singleton<GoogleStringSingleton>::get(); |
| 34 } |
| 35 |
| 36 const std::string google; |
| 37 }; |
| 32 | 38 |
| 33 } // anonymous namespace | 39 } // anonymous namespace |
| 34 | 40 |
| 35 struct AccountId::EmptyAccountId { | 41 struct AccountId::EmptyAccountId { |
| 36 EmptyAccountId() : user_id() {} | 42 EmptyAccountId() : user_id() {} |
| 37 const AccountId user_id; | 43 const AccountId user_id; |
| 38 | 44 |
| 39 static EmptyAccountId* GetInstance() { | 45 static EmptyAccountId* GetInstance() { |
| 40 return base::Singleton<EmptyAccountId>::get(); | 46 return base::Singleton<EmptyAccountId>::get(); |
| 41 } | 47 } |
| 42 }; | 48 }; |
| 43 | 49 |
| 44 AccountId::AccountId() {} | 50 AccountId::AccountId() {} |
| 45 | 51 |
| 46 AccountId::AccountId(const std::string& id, | 52 AccountId::AccountId(const std::string& gaia_id, const std::string& user_email) |
| 47 const std::string& user_email, | 53 : gaia_id_(gaia_id), user_email_(user_email) { |
| 48 const AccountType& account_type) | |
| 49 : id_(id), user_email_(user_email), account_type_(account_type) { | |
| 50 DCHECK(account_type != AccountType::UNKNOWN || id.empty()); | |
| 51 DCHECK(account_type != AccountType::ACTIVE_DIRECTORY || !id.empty()); | |
| 52 // Fail if e-mail looks similar to GaiaIdKey. | 54 // Fail if e-mail looks similar to GaiaIdKey. |
| 53 LOG_ASSERT(!base::StartsWith(user_email, kKeyGaiaIdPrefix, | 55 LOG_ASSERT(!base::StartsWith(user_email, kKeyGaiaIdPrefix, |
| 54 base::CompareCase::SENSITIVE) || | 56 base::CompareCase::SENSITIVE) || |
| 55 user_email.find('@') != std::string::npos) | 57 user_email.find('@') != std::string::npos) |
| 56 << "Bad e-mail: '" << user_email << "' with gaia_id='" << id << "'"; | 58 << "Bad e-mail: '" << user_email << "' with gaia_id='" << gaia_id << "'"; |
| 57 | 59 |
| 58 // TODO(alemate): DCHECK(!email.empty()); | 60 // TODO(alemate): DCHECK(!email.empty()); |
| 59 // TODO(alemate): check gaia_id is not empty once it is required. | 61 // TODO(alemate): check gaia_id is not empty once it is required. |
| 60 } | 62 } |
| 61 | 63 |
| 62 AccountId::AccountId(const AccountId& other) | 64 AccountId::AccountId(const AccountId& other) |
| 63 : id_(other.id_), | 65 : gaia_id_(other.gaia_id_), user_email_(other.user_email_) {} |
| 64 user_email_(other.user_email_), | |
| 65 account_type_(other.account_type_) {} | |
| 66 | 66 |
| 67 bool AccountId::operator==(const AccountId& other) const { | 67 bool AccountId::operator==(const AccountId& other) const { |
| 68 if (this == &other) | 68 return (this == &other) || |
| 69 return true; | 69 (gaia_id_ == other.gaia_id_ && user_email_ == other.user_email_) || |
| 70 if (account_type_ == AccountType::UNKNOWN || | 70 (!gaia_id_.empty() && gaia_id_ == other.gaia_id_) || |
| 71 other.account_type_ == AccountType::UNKNOWN) | 71 (!user_email_.empty() && user_email_ == other.user_email_); |
| 72 return user_email_ == other.user_email_; | |
| 73 if (account_type_ != other.account_type_) | |
| 74 return false; | |
| 75 switch (account_type_) { | |
| 76 case AccountType::GOOGLE: | |
| 77 return (id_ == other.id_ && user_email_ == other.user_email_) || | |
| 78 (!id_.empty() && id_ == other.id_) || | |
| 79 (!user_email_.empty() && user_email_ == other.user_email_); | |
| 80 case AccountType::ACTIVE_DIRECTORY: | |
| 81 return id_ == other.id_ && user_email_ == other.user_email_; | |
| 82 default: | |
| 83 NOTREACHED() << "Unknown account type"; | |
| 84 } | |
| 85 return false; | |
| 86 } | 72 } |
| 87 | 73 |
| 88 bool AccountId::operator!=(const AccountId& other) const { | 74 bool AccountId::operator!=(const AccountId& other) const { |
| 89 return !operator==(other); | 75 return !operator==(other); |
| 90 } | 76 } |
| 91 | 77 |
| 92 bool AccountId::operator<(const AccountId& right) const { | 78 bool AccountId::operator<(const AccountId& right) const { |
| 93 // TODO(alemate): update this once all AccountId members are filled. | 79 // TODO(alemate): update this once all AccountId members are filled. |
| 94 return user_email_ < right.user_email_; | 80 return user_email_ < right.user_email_; |
| 95 } | 81 } |
| 96 | 82 |
| 97 bool AccountId::empty() const { | 83 bool AccountId::empty() const { |
| 98 return id_.empty() && user_email_.empty() && | 84 return gaia_id_.empty() && user_email_.empty(); |
| 99 account_type_ == AccountType::UNKNOWN; | |
| 100 } | 85 } |
| 101 | 86 |
| 102 bool AccountId::is_valid() const { | 87 bool AccountId::is_valid() const { |
| 103 switch (account_type_) { | 88 return /* !gaia_id_.empty() && */ !user_email_.empty(); |
| 104 case AccountType::GOOGLE: | |
| 105 return /* !id_.empty() && */ !user_email_.empty(); | |
| 106 case AccountType::ACTIVE_DIRECTORY: | |
| 107 return !id_.empty() && !user_email_.empty(); | |
| 108 case AccountType::UNKNOWN: | |
| 109 return id_.empty() && !user_email_.empty(); | |
| 110 } | |
| 111 NOTREACHED(); | |
| 112 return false; | |
| 113 } | 89 } |
| 114 | 90 |
| 115 void AccountId::clear() { | 91 void AccountId::clear() { |
| 116 id_.clear(); | 92 gaia_id_.clear(); |
| 117 user_email_.clear(); | 93 user_email_.clear(); |
| 118 account_type_ = AccountType::UNKNOWN; | |
| 119 } | 94 } |
| 120 | 95 |
| 121 AccountType AccountId::GetAccountType() const { | 96 const std::string& AccountId::GetAccountType() const { |
| 122 return account_type_; | 97 return GoogleStringSingleton::GetInstance()->google; |
| 123 } | 98 } |
| 124 | 99 |
| 125 const std::string& AccountId::GetGaiaId() const { | 100 const std::string& AccountId::GetGaiaId() const { |
| 126 if (account_type_ != AccountType::GOOGLE) | 101 return gaia_id_; |
| 127 NOTIMPLEMENTED() << "Failed to get gaia_id for non-Google account."; | |
| 128 return id_; | |
| 129 } | |
| 130 | |
| 131 const std::string& AccountId::GetObjGuid() const { | |
| 132 if (account_type_ != AccountType::ACTIVE_DIRECTORY) | |
| 133 NOTIMPLEMENTED() | |
| 134 << "Failed to get obj_guid for non-Active Directory account."; | |
| 135 return id_; | |
| 136 } | 102 } |
| 137 | 103 |
| 138 const std::string& AccountId::GetUserEmail() const { | 104 const std::string& AccountId::GetUserEmail() const { |
| 139 return user_email_; | 105 return user_email_; |
| 140 } | 106 } |
| 141 | 107 |
| 142 bool AccountId::HasAccountIdKey() const { | 108 const std::string AccountId::GetAccountIdKey() const { |
| 143 return account_type_ != AccountType::UNKNOWN && !id_.empty(); | 109 #ifdef NDEBUG |
| 110 if (gaia_id_.empty()) |
| 111 LOG(FATAL) << "GetAccountIdKey(): no gaia id for " << Serialize(); |
| 112 |
| 113 #else |
| 114 CHECK(!gaia_id_.empty()); |
| 115 #endif |
| 116 |
| 117 return std::string(kKeyGaiaIdPrefix) + gaia_id_; |
| 144 } | 118 } |
| 145 | 119 |
| 146 const std::string AccountId::GetAccountIdKey() const { | 120 void AccountId::SetGaiaId(const std::string& gaia_id) { |
| 147 #ifdef NDEBUG | 121 DCHECK(!gaia_id.empty()); |
| 148 if (id_.empty()) | 122 gaia_id_ = gaia_id; |
| 149 LOG(FATAL) << "GetAccountIdKey(): no id for " << Serialize(); | |
| 150 #else | |
| 151 CHECK(!id_.empty()); | |
| 152 #endif | |
| 153 switch (GetAccountType()) { | |
| 154 case AccountType::GOOGLE: | |
| 155 return std::string(kKeyGaiaIdPrefix) + id_; | |
| 156 case AccountType::ACTIVE_DIRECTORY: | |
| 157 return std::string(kKeyAdIdPrefix) + id_; | |
| 158 default: | |
| 159 NOTREACHED() << "Unknown account type"; | |
| 160 } | |
| 161 return std::string(); | |
| 162 } | 123 } |
| 163 | 124 |
| 164 void AccountId::SetUserEmail(const std::string& email) { | 125 void AccountId::SetUserEmail(const std::string& email) { |
| 165 DCHECK(!email.empty()); | 126 DCHECK(!email.empty()); |
| 166 user_email_ = email; | 127 user_email_ = email; |
| 167 } | 128 } |
| 168 | 129 |
| 169 // static | 130 // static |
| 170 AccountId AccountId::FromUserEmail(const std::string& email) { | 131 AccountId AccountId::FromUserEmail(const std::string& email) { |
| 171 // TODO(alemate): DCHECK(!email.empty()); | 132 // TODO(alemate): DCHECK(!email.empty()); |
| 172 return AccountId(std::string() /* id */, email, AccountType::UNKNOWN); | 133 return AccountId(std::string() /* gaia_id */, email); |
| 173 } | 134 } |
| 174 | 135 |
| 175 // static | |
| 176 AccountId AccountId::FromGaiaId(const std::string& gaia_id) { | 136 AccountId AccountId::FromGaiaId(const std::string& gaia_id) { |
| 177 DCHECK(!gaia_id.empty()); | 137 DCHECK(!gaia_id.empty()); |
| 178 return AccountId(gaia_id, std::string() /* email */, AccountType::GOOGLE); | 138 return AccountId(gaia_id, std::string() /* email */); |
| 179 } | 139 } |
| 180 | 140 |
| 181 // static | 141 // static |
| 182 AccountId AccountId::FromUserEmailGaiaId(const std::string& email, | 142 AccountId AccountId::FromUserEmailGaiaId(const std::string& email, |
| 183 const std::string& gaia_id) { | 143 const std::string& gaia_id) { |
| 184 DCHECK(!(email.empty() && gaia_id.empty())); | 144 DCHECK(!(email.empty() && gaia_id.empty())); |
| 185 return AccountId(gaia_id, email, AccountType::GOOGLE); | 145 return AccountId(gaia_id, email); |
| 186 } | |
| 187 | |
| 188 // static | |
| 189 AccountId AccountId::AdFromUserEmailObjGuid(const std::string& email, | |
| 190 const std::string& obj_guid) { | |
| 191 DCHECK(!email.empty() && !obj_guid.empty()); | |
| 192 return AccountId(obj_guid, email, AccountType::ACTIVE_DIRECTORY); | |
| 193 } | |
| 194 | |
| 195 // static | |
| 196 AccountId AccountId::AdFromObjGuid(const std::string& obj_guid) { | |
| 197 DCHECK(!obj_guid.empty()); | |
| 198 return AccountId(obj_guid, std::string() /* email */, | |
| 199 AccountType::ACTIVE_DIRECTORY); | |
| 200 } | |
| 201 | |
| 202 // static | |
| 203 AccountType AccountId::StringToAccountType( | |
| 204 const std::string& account_type_string) { | |
| 205 if (account_type_string == kGoogle) | |
| 206 return AccountType::GOOGLE; | |
| 207 if (account_type_string == kAd) | |
| 208 return AccountType::ACTIVE_DIRECTORY; | |
| 209 NOTREACHED() << "Unknown account type " << account_type_string; | |
| 210 return AccountType::UNKNOWN; | |
| 211 } | |
| 212 | |
| 213 // static | |
| 214 std::string AccountId::AccountTypeToString(const AccountType& account_type) { | |
| 215 switch (account_type) { | |
| 216 case AccountType::GOOGLE: | |
| 217 return kGoogle; | |
| 218 case AccountType::ACTIVE_DIRECTORY: | |
| 219 return kAd; | |
| 220 default: | |
| 221 NOTREACHED() << "Unknown account type"; | |
| 222 } | |
| 223 return std::string(); | |
| 224 } | 146 } |
| 225 | 147 |
| 226 std::string AccountId::Serialize() const { | 148 std::string AccountId::Serialize() const { |
| 227 base::DictionaryValue value; | 149 base::DictionaryValue value; |
| 228 switch (GetAccountType()) { | 150 value.SetString(kGaiaIdKey, gaia_id_); |
| 229 case AccountType::UNKNOWN: | |
| 230 case AccountType::GOOGLE: | |
| 231 value.SetString(kGaiaIdKey, id_); | |
| 232 value.SetString(kAccountTypeKey, | |
| 233 AccountTypeToString(AccountType::GOOGLE)); | |
| 234 break; | |
| 235 case AccountType::ACTIVE_DIRECTORY: | |
| 236 value.SetString(kObjGuid, id_); | |
| 237 value.SetString(kAccountTypeKey, AccountTypeToString(GetAccountType())); | |
| 238 break; | |
| 239 } | |
| 240 value.SetString(kEmailKey, user_email_); | 151 value.SetString(kEmailKey, user_email_); |
| 241 | 152 |
| 242 std::string serialized; | 153 std::string serialized; |
| 243 base::JSONWriter::Write(value, &serialized); | 154 base::JSONWriter::Write(value, &serialized); |
| 244 return serialized; | 155 return serialized; |
| 245 } | 156 } |
| 246 | 157 |
| 247 // static | 158 // static |
| 248 bool AccountId::Deserialize(const std::string& serialized, | 159 bool AccountId::Deserialize(const std::string& serialized, |
| 249 AccountId* account_id) { | 160 AccountId* account_id) { |
| 250 base::JSONReader reader; | 161 base::JSONReader reader; |
| 251 std::unique_ptr<const base::Value> value(reader.Read(serialized)); | 162 std::unique_ptr<const base::Value> value(reader.Read(serialized)); |
| 252 const base::DictionaryValue* dictionary_value = NULL; | 163 const base::DictionaryValue* dictionary_value = NULL; |
| 253 | 164 |
| 254 if (!value || !value->GetAsDictionary(&dictionary_value)) | 165 if (!value || !value->GetAsDictionary(&dictionary_value)) |
| 255 return false; | 166 return false; |
| 256 | 167 |
| 257 std::string gaia_id; | 168 std::string gaia_id; |
| 258 std::string user_email; | 169 std::string user_email; |
| 259 std::string obj_guid; | |
| 260 std::string account_type_string; | |
| 261 AccountType account_type = AccountType::GOOGLE; | |
| 262 | 170 |
| 263 const bool found_gaia_id = dictionary_value->GetString(kGaiaIdKey, &gaia_id); | 171 const bool found_gaia_id = dictionary_value->GetString(kGaiaIdKey, &gaia_id); |
| 264 const bool found_user_email = | 172 const bool found_user_email = |
| 265 dictionary_value->GetString(kEmailKey, &user_email); | 173 dictionary_value->GetString(kEmailKey, &user_email); |
| 266 const bool found_obj_guid = dictionary_value->GetString(kObjGuid, &obj_guid); | |
| 267 const bool found_account_type = | |
| 268 dictionary_value->GetString(kAccountTypeKey, &account_type_string); | |
| 269 if (found_account_type) | |
| 270 account_type = StringToAccountType(account_type_string); | |
| 271 | 174 |
| 272 switch (account_type) { | 175 if (!found_gaia_id) |
| 273 case AccountType::GOOGLE: | 176 LOG(ERROR) << "gaia_id is not found in '" << serialized << "'"; |
| 274 if (found_obj_guid) | |
| 275 LOG(ERROR) << "AccountType is 'google' but obj_guid is found in '" | |
| 276 << serialized << "'"; | |
| 277 | 177 |
| 278 if (!found_gaia_id) | 178 if (!found_user_email) |
| 279 LOG(ERROR) << "gaia_id is not found in '" << serialized << "'"; | 179 LOG(ERROR) << "user_email is not found in '" << serialized << "'"; |
| 280 | 180 |
| 281 if (!found_user_email) | 181 if (!found_gaia_id && !found_user_email) |
| 282 LOG(ERROR) << "user_email is not found in '" << serialized << "'"; | 182 return false; |
| 283 | 183 |
| 284 if (!found_gaia_id && !found_user_email) | 184 *account_id = FromUserEmailGaiaId(user_email, gaia_id); |
| 285 return false; | |
| 286 | 185 |
| 287 *account_id = FromUserEmailGaiaId(user_email, gaia_id); | 186 return true; |
| 288 return true; | |
| 289 | |
| 290 case AccountType::ACTIVE_DIRECTORY: | |
| 291 if (found_gaia_id) | |
| 292 LOG(ERROR) | |
| 293 << "AccountType is 'active directory' but gaia_id is found in '" | |
| 294 << serialized << "'"; | |
| 295 | |
| 296 if (!found_obj_guid) { | |
| 297 LOG(ERROR) << "obj_guid is not found in '" << serialized << "'"; | |
| 298 return false; | |
| 299 } | |
| 300 | |
| 301 if (!found_user_email) { | |
| 302 LOG(ERROR) << "user_email is not found in '" << serialized << "'"; | |
| 303 } | |
| 304 | |
| 305 if (!found_obj_guid || !found_user_email) | |
| 306 return false; | |
| 307 | |
| 308 *account_id = AdFromUserEmailObjGuid(user_email, obj_guid); | |
| 309 return true; | |
| 310 | |
| 311 default: | |
| 312 NOTREACHED() << "Unknown account type"; | |
| 313 return false; | |
| 314 } | |
| 315 } | 187 } |
| 316 | 188 |
| 317 const AccountId& EmptyAccountId() { | 189 const AccountId& EmptyAccountId() { |
| 318 return AccountId::EmptyAccountId::GetInstance()->user_id; | 190 return AccountId::EmptyAccountId::GetInstance()->user_id; |
| 319 } | 191 } |
| 320 | 192 |
| 321 namespace BASE_HASH_NAMESPACE { | 193 namespace BASE_HASH_NAMESPACE { |
| 322 | 194 |
| 323 std::size_t hash<AccountId>::operator()(const AccountId& user_id) const { | 195 std::size_t hash<AccountId>::operator()(const AccountId& user_id) const { |
| 324 return hash<std::string>()(user_id.GetUserEmail()); | 196 return hash<std::string>()(user_id.GetUserEmail()); |
| 325 } | 197 } |
| 326 | 198 |
| 327 } // namespace BASE_HASH_NAMESPACE | 199 } // namespace BASE_HASH_NAMESPACE |
| OLD | NEW |