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/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 kAccountTypeKey[] = "account_type"; | |
| 23 | |
| 24 // Serialization values for account type. | |
| 25 const std::string kGoogle = "google"; | |
| 26 const std::string kAd = "ad"; | |
| 25 | 27 |
| 26 // Prefix for GetAccountIdKey(). | 28 // Prefix for GetAccountIdKey(). |
| 27 const char kKeyGaiaIdPrefix[] = "g-"; | 29 const char kKeyGaiaIdPrefix[] = "g-"; |
| 28 | 30 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 | 31 |
| 39 } // anonymous namespace | 32 } // anonymous namespace |
| 40 | 33 |
| 41 struct AccountId::EmptyAccountId { | 34 struct AccountId::EmptyAccountId { |
| 42 EmptyAccountId() : user_id() {} | 35 EmptyAccountId() : user_id() {} |
| 43 const AccountId user_id; | 36 const AccountId user_id; |
| 44 | 37 |
| 45 static EmptyAccountId* GetInstance() { | 38 static EmptyAccountId* GetInstance() { |
| 46 return base::Singleton<EmptyAccountId>::get(); | 39 return base::Singleton<EmptyAccountId>::get(); |
| 47 } | 40 } |
| 48 }; | 41 }; |
| 49 | 42 |
| 50 AccountId::AccountId() {} | 43 AccountId::AccountId() {} |
| 51 | 44 |
| 52 AccountId::AccountId(const std::string& gaia_id, const std::string& user_email) | 45 AccountId::AccountId(const std::string& gaia_id, const std::string& user_email) |
| 53 : gaia_id_(gaia_id), user_email_(user_email) { | 46 : gaia_id_(gaia_id), user_email_(user_email) { |
| 54 // Fail if e-mail looks similar to GaiaIdKey. | 47 // Fail if e-mail looks similar to GaiaIdKey. |
| 55 LOG_ASSERT(!base::StartsWith(user_email, kKeyGaiaIdPrefix, | 48 LOG_ASSERT(!base::StartsWith(user_email, kKeyGaiaIdPrefix, |
| 56 base::CompareCase::SENSITIVE) || | 49 base::CompareCase::SENSITIVE) || |
| 57 user_email.find('@') != std::string::npos) | 50 user_email.find('@') != std::string::npos) |
| 58 << "Bad e-mail: '" << user_email << "' with gaia_id='" << gaia_id << "'"; | 51 << "Bad e-mail: '" << user_email << "' with gaia_id='" << gaia_id << "'"; |
| 59 | 52 |
| 60 // TODO(alemate): DCHECK(!email.empty()); | 53 // TODO(alemate): DCHECK(!email.empty()); |
| 61 // TODO(alemate): check gaia_id is not empty once it is required. | 54 // TODO(alemate): check gaia_id is not empty once it is required. |
| 62 } | 55 } |
| 63 | 56 |
| 57 AccountId::AccountId(const std::string& gaia_id, | |
| 58 const std::string& user_email, | |
| 59 const AccountType& account_type) | |
| 60 : gaia_id_(gaia_id), user_email_(user_email), account_type_(account_type) { | |
| 61 DCHECK(account_type != AccountType::UNKNOWN); | |
| 62 } | |
| 63 | |
| 64 AccountId::AccountId(const AccountId& other) | 64 AccountId::AccountId(const AccountId& other) |
| 65 : gaia_id_(other.gaia_id_), user_email_(other.user_email_) {} | 65 : gaia_id_(other.gaia_id_), |
| 66 user_email_(other.user_email_), | |
| 67 account_type_(other.account_type_) {} | |
| 66 | 68 |
| 67 bool AccountId::operator==(const AccountId& other) const { | 69 bool AccountId::operator==(const AccountId& other) const { |
| 68 return (this == &other) || | 70 return (this == &other) || |
| 69 (gaia_id_ == other.gaia_id_ && user_email_ == other.user_email_) || | 71 (gaia_id_ == other.gaia_id_ && user_email_ == other.user_email_) || |
| 70 (!gaia_id_.empty() && gaia_id_ == other.gaia_id_) || | 72 (!gaia_id_.empty() && gaia_id_ == other.gaia_id_) || |
| 71 (!user_email_.empty() && user_email_ == other.user_email_); | 73 (!user_email_.empty() && user_email_ == other.user_email_); |
| 72 } | 74 } |
| 73 | 75 |
| 74 bool AccountId::operator!=(const AccountId& other) const { | 76 bool AccountId::operator!=(const AccountId& other) const { |
| 75 return !operator==(other); | 77 return !operator==(other); |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 86 | 88 |
| 87 bool AccountId::is_valid() const { | 89 bool AccountId::is_valid() const { |
| 88 return /* !gaia_id_.empty() && */ !user_email_.empty(); | 90 return /* !gaia_id_.empty() && */ !user_email_.empty(); |
| 89 } | 91 } |
| 90 | 92 |
| 91 void AccountId::clear() { | 93 void AccountId::clear() { |
| 92 gaia_id_.clear(); | 94 gaia_id_.clear(); |
| 93 user_email_.clear(); | 95 user_email_.clear(); |
| 94 } | 96 } |
| 95 | 97 |
| 96 const std::string& AccountId::GetAccountType() const { | 98 const AccountType& AccountId::GetAccountType() const { |
| 97 return GoogleStringSingleton::GetInstance()->google; | 99 return account_type_; |
| 98 } | 100 } |
| 99 | 101 |
| 100 const std::string& AccountId::GetGaiaId() const { | 102 const std::string& AccountId::GetGaiaId() const { |
| 101 return gaia_id_; | 103 return gaia_id_; |
| 102 } | 104 } |
| 103 | 105 |
| 104 const std::string& AccountId::GetUserEmail() const { | 106 const std::string& AccountId::GetUserEmail() const { |
| 105 return user_email_; | 107 return user_email_; |
| 106 } | 108 } |
| 107 | 109 |
| 108 const std::string AccountId::GetAccountIdKey() const { | 110 const std::string AccountId::GetAccountIdKey() const { |
| 109 #ifdef NDEBUG | 111 #ifdef NDEBUG |
| 110 if (gaia_id_.empty()) | 112 if (gaia_id_.empty()) |
| 111 LOG(FATAL) << "GetAccountIdKey(): no gaia id for " << Serialize(); | 113 LOG(FATAL) << "GetAccountIdKey(): no gaia id for " << Serialize(); |
| 112 | 114 |
| 113 #else | 115 #else |
| 114 CHECK(!gaia_id_.empty()); | 116 CHECK(!gaia_id_.empty()); |
| 115 #endif | 117 #endif |
| 116 | 118 |
| 117 return std::string(kKeyGaiaIdPrefix) + gaia_id_; | 119 if (GetAccountType() == AccountType::GOOGLE) |
| 120 return std::string(kKeyGaiaIdPrefix) + gaia_id_; | |
| 121 if (GetAccountType() == AccountType::ACTIVE_DIRECTORY) | |
| 122 return std::string(kKeyAdIdPrefix) + gaia_id_; | |
|
Roger Tawa OOO till Jul 10th
2016/12/05 16:34:31
Will there be a later pass to change |gaia_id_| to
Alexander Alekseev
2016/12/06 02:21:14
Uuuups, I missed this. Thank you.
I thought of ha
Roger Tawa OOO till Jul 10th
2016/12/07 22:09:37
Since |gaia_id_| is a private member, seems easier
| |
| 123 NOTREACHED() << "Unknown account type"; | |
| 124 return std::string(); | |
|
Roger Tawa OOO till Jul 10th
2016/12/05 16:34:31
Nit: use a switch() statement. Also line 173.
Roman Sorokin (ftl)
2016/12/13 13:45:59
Done.
| |
| 118 } | 125 } |
| 119 | 126 |
| 120 void AccountId::SetGaiaId(const std::string& gaia_id) { | 127 void AccountId::SetGaiaId(const std::string& gaia_id) { |
| 121 DCHECK(!gaia_id.empty()); | 128 DCHECK(!gaia_id.empty()); |
| 122 gaia_id_ = gaia_id; | 129 gaia_id_ = gaia_id; |
| 123 } | 130 } |
| 124 | 131 |
| 125 void AccountId::SetUserEmail(const std::string& email) { | 132 void AccountId::SetUserEmail(const std::string& email) { |
| 126 DCHECK(!email.empty()); | 133 DCHECK(!email.empty()); |
| 127 user_email_ = email; | 134 user_email_ = email; |
| 128 } | 135 } |
| 129 | 136 |
| 137 void AccountId::SetAccountType(const AccountType& account_type) { | |
| 138 DCHECK(account_type != AccountType::UNKNOWN); | |
| 139 account_type_ = account_type; | |
| 140 } | |
| 141 | |
| 130 // static | 142 // static |
| 131 AccountId AccountId::FromUserEmail(const std::string& email) { | 143 AccountId AccountId::FromUserEmail(const std::string& email) { |
| 132 // TODO(alemate): DCHECK(!email.empty()); | 144 // TODO(alemate): DCHECK(!email.empty()); |
| 133 return AccountId(std::string() /* gaia_id */, email); | 145 return AccountId(std::string() /* gaia_id */, email); |
| 134 } | 146 } |
| 135 | 147 |
| 136 AccountId AccountId::FromGaiaId(const std::string& gaia_id) { | 148 AccountId AccountId::FromGaiaId(const std::string& gaia_id) { |
| 137 DCHECK(!gaia_id.empty()); | 149 DCHECK(!gaia_id.empty()); |
| 138 return AccountId(gaia_id, std::string() /* email */); | 150 return AccountId(gaia_id, std::string() /* email */); |
| 139 } | 151 } |
| 140 | 152 |
| 141 // static | 153 // static |
| 142 AccountId AccountId::FromUserEmailGaiaId(const std::string& email, | 154 AccountId AccountId::FromUserEmailGaiaId(const std::string& email, |
| 143 const std::string& gaia_id) { | 155 const std::string& gaia_id) { |
| 144 DCHECK(!(email.empty() && gaia_id.empty())); | 156 DCHECK(!(email.empty() && gaia_id.empty())); |
| 145 return AccountId(gaia_id, email); | 157 return AccountId(gaia_id, email); |
| 146 } | 158 } |
| 147 | 159 |
| 160 // static | |
| 161 AccountType AccountId::StringToAccountType( | |
| 162 const std::string& account_type_string) { | |
| 163 if (account_type_string == kGoogle) | |
| 164 return AccountType::GOOGLE; | |
| 165 if (account_type_string == kAd) | |
| 166 return AccountType::ACTIVE_DIRECTORY; | |
| 167 NOTREACHED() << "Unknown account type " << account_type_string; | |
| 168 return AccountType::UNKNOWN; | |
| 169 } | |
| 170 | |
| 171 // static | |
| 172 std::string AccountId::AccountTypeToString(const AccountType& account_type) { | |
| 173 if (account_type == AccountType::GOOGLE) | |
| 174 return kGoogle; | |
| 175 if (account_type == AccountType::ACTIVE_DIRECTORY) | |
| 176 return kAd; | |
| 177 NOTREACHED() << "Unknown account type"; | |
| 178 return std::string(); | |
| 179 } | |
| 180 | |
| 181 AccountId AccountId::FromUserEmailGaiaIdAccountType( | |
| 182 const std::string& email, | |
| 183 const std::string& gaia_id, | |
| 184 const AccountType& account_type) { | |
| 185 DCHECK(!(email.empty() && gaia_id.empty())); | |
| 186 return AccountId(gaia_id, email, account_type); | |
| 187 } | |
| 188 | |
| 189 AccountId AccountId::FromGaiaIdAccountType(const std::string& gaia_id, | |
| 190 const AccountType& account_type) { | |
| 191 return FromUserEmailGaiaIdAccountType(std::string() /* email */, gaia_id, | |
| 192 account_type); | |
| 193 } | |
|
Roger Tawa OOO till Jul 10th
2016/12/05 16:34:31
Since these are new methods, maybe not use "Gaia"
Alexander Alekseev
2016/12/06 02:21:14
Or, probably, we should create AccountId::ADFromUs
Roger Tawa OOO till Jul 10th
2016/12/07 22:09:37
Acknowledged.
| |
| 194 | |
|
Roger Tawa OOO till Jul 10th
2016/12/05 16:34:31
These two new methods should have "// static" comm
Roman Sorokin (ftl)
2016/12/13 13:45:59
Done.
| |
| 148 std::string AccountId::Serialize() const { | 195 std::string AccountId::Serialize() const { |
| 149 base::DictionaryValue value; | 196 base::DictionaryValue value; |
| 150 value.SetString(kGaiaIdKey, gaia_id_); | 197 value.SetString(kGaiaIdKey, gaia_id_); |
| 151 value.SetString(kEmailKey, user_email_); | 198 value.SetString(kEmailKey, user_email_); |
| 199 value.SetString(kAccountTypeKey, AccountTypeToString(account_type_)); | |
| 152 | 200 |
| 153 std::string serialized; | 201 std::string serialized; |
| 154 base::JSONWriter::Write(value, &serialized); | 202 base::JSONWriter::Write(value, &serialized); |
| 155 return serialized; | 203 return serialized; |
| 156 } | 204 } |
| 157 | 205 |
| 158 // static | 206 // static |
| 159 bool AccountId::Deserialize(const std::string& serialized, | 207 bool AccountId::Deserialize(const std::string& serialized, |
| 160 AccountId* account_id) { | 208 AccountId* account_id) { |
| 161 base::JSONReader reader; | 209 base::JSONReader reader; |
| 162 std::unique_ptr<const base::Value> value(reader.Read(serialized)); | 210 std::unique_ptr<const base::Value> value(reader.Read(serialized)); |
| 163 const base::DictionaryValue* dictionary_value = NULL; | 211 const base::DictionaryValue* dictionary_value = NULL; |
| 164 | 212 |
| 165 if (!value || !value->GetAsDictionary(&dictionary_value)) | 213 if (!value || !value->GetAsDictionary(&dictionary_value)) |
| 166 return false; | 214 return false; |
| 167 | 215 |
| 168 std::string gaia_id; | 216 std::string gaia_id; |
| 169 std::string user_email; | 217 std::string user_email; |
| 218 AccountType account_type = AccountType::GOOGLE; | |
| 170 | 219 |
| 171 const bool found_gaia_id = dictionary_value->GetString(kGaiaIdKey, &gaia_id); | 220 const bool found_gaia_id = dictionary_value->GetString(kGaiaIdKey, &gaia_id); |
| 172 const bool found_user_email = | 221 const bool found_user_email = |
| 173 dictionary_value->GetString(kEmailKey, &user_email); | 222 dictionary_value->GetString(kEmailKey, &user_email); |
| 174 | 223 |
| 175 if (!found_gaia_id) | 224 if (!found_gaia_id) |
| 176 LOG(ERROR) << "gaia_id is not found in '" << serialized << "'"; | 225 LOG(ERROR) << "gaia_id is not found in '" << serialized << "'"; |
| 177 | 226 |
| 178 if (!found_user_email) | 227 if (!found_user_email) |
| 179 LOG(ERROR) << "user_email is not found in '" << serialized << "'"; | 228 LOG(ERROR) << "user_email is not found in '" << serialized << "'"; |
| 180 | 229 |
| 181 if (!found_gaia_id && !found_user_email) | 230 if (!found_gaia_id && !found_user_email) |
| 182 return false; | 231 return false; |
| 183 | 232 |
| 184 *account_id = FromUserEmailGaiaId(user_email, gaia_id); | 233 std::string account_type_string; |
| 234 if (dictionary_value->GetString(kAccountTypeKey, &account_type_string)) | |
| 235 account_type = StringToAccountType(account_type_string); | |
|
Roger Tawa OOO till Jul 10th
2016/12/05 16:34:31
I think you should do some error checking here sin
Alexander Alekseev
2016/12/06 02:21:14
Totally agree.
Roman Sorokin (ftl)
2016/12/13 13:45:58
done
| |
| 236 | |
| 237 *account_id = | |
| 238 FromUserEmailGaiaIdAccountType(user_email, gaia_id, account_type); | |
| 185 | 239 |
| 186 return true; | 240 return true; |
| 187 } | 241 } |
| 188 | 242 |
| 189 const AccountId& EmptyAccountId() { | 243 const AccountId& EmptyAccountId() { |
| 190 return AccountId::EmptyAccountId::GetInstance()->user_id; | 244 return AccountId::EmptyAccountId::GetInstance()->user_id; |
| 191 } | 245 } |
| 192 | 246 |
| 193 namespace BASE_HASH_NAMESPACE { | 247 namespace BASE_HASH_NAMESPACE { |
| 194 | 248 |
| 195 std::size_t hash<AccountId>::operator()(const AccountId& user_id) const { | 249 std::size_t hash<AccountId>::operator()(const AccountId& user_id) const { |
| 196 return hash<std::string>()(user_id.GetUserEmail()); | 250 return hash<std::string>()(user_id.GetUserEmail()); |
| 197 } | 251 } |
| 198 | 252 |
| 199 } // namespace BASE_HASH_NAMESPACE | 253 } // namespace BASE_HASH_NAMESPACE |
| OLD | NEW |