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