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"
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
;
26 base::SplitString(email_address
, at
, &parts
);
27 if (parts
.size() != 2U) {
28 NOTREACHED() << "expecting exactly one @, but got "
29 << (parts
.empty() ? 0 : parts
.size() - 1)
30 << " : " << email_address
;
32 if (change_googlemail_to_gmail
&& parts
[1] == kGooglemailDomain
)
33 parts
[1] = kGmailDomain
;
35 if (parts
[1] == kGmailDomain
) // only strip '.' for gmail accounts.
36 base::RemoveChars(parts
[0], ".", &parts
[0]);
39 std::string new_email
= base::StringToLowerASCII(
40 base::JoinString(parts
, "@"));
41 VLOG(1) << "Canonicalized " << email_address
<< " to " << new_email
;
48 ListedAccount::ListedAccount() {}
50 ListedAccount::~ListedAccount() {}
52 bool ListedAccount::operator==(const ListedAccount
& other
) const {
53 // Only use ids for comparison if they've been computed by some caller, since
54 // this class does not assign the id.
55 if (!id
.empty() && !other
.id
.empty()) {
56 return id
== other
.id
;
58 return email
== other
.email
&&
59 gaia_id
== other
.gaia_id
&&
60 valid
== other
.valid
&&
61 raw_email
== other
.raw_email
;
65 std::string
CanonicalizeEmail(const std::string
& email_address
) {
66 // CanonicalizeEmail() is called to process email strings that are eventually
67 // shown to the user, and may also be used in persisting email strings. To
68 // avoid breaking this existing behavior, this function will not try to
69 // change googlemail to gmail.
70 return CanonicalizeEmailImpl(email_address
, false);
73 std::string
CanonicalizeDomain(const std::string
& domain
) {
74 // Canonicalization of domain names means lower-casing them. Make sure to
75 // update this function in sync with Canonicalize if this ever changes.
76 return base::StringToLowerASCII(domain
);
79 std::string
SanitizeEmail(const std::string
& email_address
) {
80 std::string
sanitized(email_address
);
82 // Apply a default domain if necessary.
83 if (sanitized
.find('@') == std::string::npos
) {
85 sanitized
+= kGmailDomain
;
91 bool AreEmailsSame(const std::string
& email1
, const std::string
& email2
) {
92 return CanonicalizeEmailImpl(gaia::SanitizeEmail(email1
), true) ==
93 CanonicalizeEmailImpl(gaia::SanitizeEmail(email2
), true);
96 std::string
ExtractDomainName(const std::string
& email_address
) {
97 // First canonicalize which will also verify we have proper domain part.
98 std::string email
= CanonicalizeEmail(email_address
);
99 size_t separator_pos
= email
.find('@');
100 if (separator_pos
!= email
.npos
&& separator_pos
< email
.length() - 1)
101 return email
.substr(separator_pos
+ 1);
103 NOTREACHED() << "Not a proper email address: " << email
;
104 return std::string();
107 bool IsGaiaSignonRealm(const GURL
& url
) {
108 if (!url
.SchemeIsCryptographic())
111 return url
== GaiaUrls::GetInstance()->gaia_url();
115 bool ParseListAccountsData(
116 const std::string
& data
, std::vector
<ListedAccount
>* accounts
) {
119 // Parse returned data and make sure we have data.
120 scoped_ptr
<base::Value
> value
= base::JSONReader::Read(data
);
124 base::ListValue
* list
;
125 if (!value
->GetAsList(&list
) || list
->GetSize() < 2)
128 // Get list of account info.
129 base::ListValue
* account_list
;
130 if (!list
->GetList(1, &account_list
) || accounts
== NULL
)
133 // Build a vector of accounts from the cookie. Order is important: the first
134 // account in the list is the primary account.
135 for (size_t i
= 0; i
< account_list
->GetSize(); ++i
) {
136 base::ListValue
* account
;
137 if (account_list
->GetList(i
, &account
) && account
!= NULL
) {
139 // Canonicalize the email since ListAccounts returns "display email".
140 if (account
->GetString(3, &email
) && !email
.empty()) {
141 // New version if ListAccounts indicates whether the email's session
142 // is still valid or not. If this value is present and false, assume
143 // its invalid. Otherwise assume it's valid to remain compatible with
145 int is_email_valid
= 1;
146 if (!account
->GetInteger(9, &is_email_valid
))
150 // ListAccounts must also return the Gaia Id.
151 if (account
->GetString(10, &gaia_id
) && !gaia_id
.empty()) {
152 ListedAccount listed_account
;
153 listed_account
.email
= CanonicalizeEmail(email
);
154 listed_account
.gaia_id
= gaia_id
;
155 listed_account
.valid
= is_email_valid
!= 0;
156 listed_account
.raw_email
= email
;
157 accounts
->push_back(listed_account
);