Infobar material design refresh: bg color
[chromium-blink-merge.git] / google_apis / gaia / gaia_auth_util.cc
blobcaf802f9954e535b9c774df961be2e32018e16e9
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "google_apis/gaia/gaia_auth_util.h"
7 #include "base/json/json_reader.h"
8 #include "base/logging.h"
9 #include "base/strings/string_split.h"
10 #include "base/strings/string_util.h"
11 #include "base/values.h"
12 #include "google_apis/gaia/gaia_urls.h"
13 #include "url/gurl.h"
15 namespace gaia {
17 namespace {
19 const char kGmailDomain[] = "gmail.com";
20 const char kGooglemailDomain[] = "googlemail.com";
22 std::string CanonicalizeEmailImpl(const std::string& email_address,
23 bool change_googlemail_to_gmail) {
24 std::vector<std::string> parts = base::SplitString(
25 email_address, "@", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
26 if (parts.size() != 2U) {
27 NOTREACHED() << "expecting exactly one @, but got "
28 << (parts.empty() ? 0 : parts.size() - 1)
29 << " : " << email_address;
30 } else {
31 if (change_googlemail_to_gmail && parts[1] == kGooglemailDomain)
32 parts[1] = kGmailDomain;
34 if (parts[1] == kGmailDomain) // only strip '.' for gmail accounts.
35 base::RemoveChars(parts[0], ".", &parts[0]);
38 std::string new_email = base::ToLowerASCII(base::JoinString(parts, "@"));
39 VLOG(1) << "Canonicalized " << email_address << " to " << new_email;
40 return new_email;
43 } // namespace
46 ListedAccount::ListedAccount() {}
48 ListedAccount::~ListedAccount() {}
50 bool ListedAccount::operator==(const ListedAccount& other) const {
51 // Only use ids for comparison if they've been computed by some caller, since
52 // this class does not assign the id.
53 if (!id.empty() && !other.id.empty()) {
54 return id == other.id;
55 } else {
56 return email == other.email &&
57 gaia_id == other.gaia_id &&
58 valid == other.valid &&
59 raw_email == other.raw_email;
63 std::string CanonicalizeEmail(const std::string& email_address) {
64 // CanonicalizeEmail() is called to process email strings that are eventually
65 // shown to the user, and may also be used in persisting email strings. To
66 // avoid breaking this existing behavior, this function will not try to
67 // change googlemail to gmail.
68 return CanonicalizeEmailImpl(email_address, false);
71 std::string CanonicalizeDomain(const std::string& domain) {
72 // Canonicalization of domain names means lower-casing them. Make sure to
73 // update this function in sync with Canonicalize if this ever changes.
74 return base::ToLowerASCII(domain);
77 std::string SanitizeEmail(const std::string& email_address) {
78 std::string sanitized(email_address);
80 // Apply a default domain if necessary.
81 if (sanitized.find('@') == std::string::npos) {
82 sanitized += '@';
83 sanitized += kGmailDomain;
86 return sanitized;
89 bool AreEmailsSame(const std::string& email1, const std::string& email2) {
90 return CanonicalizeEmailImpl(gaia::SanitizeEmail(email1), true) ==
91 CanonicalizeEmailImpl(gaia::SanitizeEmail(email2), true);
94 std::string ExtractDomainName(const std::string& email_address) {
95 // First canonicalize which will also verify we have proper domain part.
96 std::string email = CanonicalizeEmail(email_address);
97 size_t separator_pos = email.find('@');
98 if (separator_pos != email.npos && separator_pos < email.length() - 1)
99 return email.substr(separator_pos + 1);
100 else
101 NOTREACHED() << "Not a proper email address: " << email;
102 return std::string();
105 bool IsGaiaSignonRealm(const GURL& url) {
106 if (!url.SchemeIsCryptographic())
107 return false;
109 return url == GaiaUrls::GetInstance()->gaia_url();
113 bool ParseListAccountsData(
114 const std::string& data, std::vector<ListedAccount>* accounts) {
115 accounts->clear();
117 // Parse returned data and make sure we have data.
118 scoped_ptr<base::Value> value = base::JSONReader::Read(data);
119 if (!value)
120 return false;
122 base::ListValue* list;
123 if (!value->GetAsList(&list) || list->GetSize() < 2)
124 return false;
126 // Get list of account info.
127 base::ListValue* account_list;
128 if (!list->GetList(1, &account_list) || accounts == NULL)
129 return false;
131 // Build a vector of accounts from the cookie. Order is important: the first
132 // account in the list is the primary account.
133 for (size_t i = 0; i < account_list->GetSize(); ++i) {
134 base::ListValue* account;
135 if (account_list->GetList(i, &account) && account != NULL) {
136 std::string email;
137 // Canonicalize the email since ListAccounts returns "display email".
138 if (account->GetString(3, &email) && !email.empty()) {
139 // New version if ListAccounts indicates whether the email's session
140 // is still valid or not. If this value is present and false, assume
141 // its invalid. Otherwise assume it's valid to remain compatible with
142 // old version.
143 int is_email_valid = 1;
144 if (!account->GetInteger(9, &is_email_valid))
145 is_email_valid = 1;
147 std::string gaia_id;
148 // ListAccounts must also return the Gaia Id.
149 if (account->GetString(10, &gaia_id) && !gaia_id.empty()) {
150 ListedAccount listed_account;
151 listed_account.email = CanonicalizeEmail(email);
152 listed_account.gaia_id = gaia_id;
153 listed_account.valid = is_email_valid != 0;
154 listed_account.raw_email = email;
155 accounts->push_back(listed_account);
161 return true;
164 } // namespace gaia