Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / google_apis / gaia / gaia_auth_util.cc
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.
4
5 #include "google_apis/gaia/gaia_auth_util.h"
6
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"
14
15 namespace gaia {
16
17 namespace {
18 const char kGmailDomain[] = "gmail.com";
19 }
20
21 std::string CanonicalizeEmail(const std::string& email_address) {
22   std::vector<std::string> parts;
23   char at = '@';
24   base::SplitString(email_address, at, &parts);
25   if (parts.size() != 2U) {
26     NOTREACHED() << "expecting exactly one @, but got " << parts.size()-1 <<
27         " : " << email_address;
28   } else if (parts[1] == kGmailDomain) {  // only strip '.' for gmail accounts.
29     base::RemoveChars(parts[0], ".", &parts[0]);
30   }
31   std::string new_email = StringToLowerASCII(JoinString(parts, at));
32   VLOG(1) << "Canonicalized " << email_address << " to " << new_email;
33   return new_email;
34 }
35
36 std::string CanonicalizeDomain(const std::string& domain) {
37   // Canonicalization of domain names means lower-casing them. Make sure to
38   // update this function in sync with Canonicalize if this ever changes.
39   return StringToLowerASCII(domain);
40 }
41
42 std::string SanitizeEmail(const std::string& email_address) {
43   std::string sanitized(email_address);
44
45   // Apply a default domain if necessary.
46   if (sanitized.find('@') == std::string::npos) {
47     sanitized += '@';
48     sanitized += kGmailDomain;
49   }
50
51   return sanitized;
52 }
53
54 bool AreEmailsSame(const std::string& email1, const std::string& email2) {
55   return gaia::CanonicalizeEmail(gaia::SanitizeEmail(email1)) ==
56       gaia::CanonicalizeEmail(gaia::SanitizeEmail(email2));
57 }
58
59 std::string ExtractDomainName(const std::string& email_address) {
60   // First canonicalize which will also verify we have proper domain part.
61   std::string email = CanonicalizeEmail(email_address);
62   size_t separator_pos = email.find('@');
63   if (separator_pos != email.npos && separator_pos < email.length() - 1)
64     return email.substr(separator_pos + 1);
65   else
66     NOTREACHED() << "Not a proper email address: " << email;
67   return std::string();
68 }
69
70 bool IsGaiaSignonRealm(const GURL& url) {
71   if (!url.SchemeIsSecure())
72     return false;
73
74   return url == GaiaUrls::GetInstance()->gaia_url();
75 }
76
77
78 bool ParseListAccountsData(
79     const std::string& data,
80     std::vector<std::pair<std::string, bool> >* accounts) {
81   accounts->clear();
82
83   // Parse returned data and make sure we have data.
84   scoped_ptr<base::Value> value(base::JSONReader::Read(data));
85   if (!value)
86     return false;
87
88   base::ListValue* list;
89   if (!value->GetAsList(&list) || list->GetSize() < 2)
90     return false;
91
92   // Get list of account info.
93   base::ListValue* account_list;
94   if (!list->GetList(1, &account_list) || accounts == NULL)
95     return false;
96
97   // Build a vector of accounts from the cookie.  Order is important: the first
98   // account in the list is the primary account.
99   for (size_t i = 0; i < account_list->GetSize(); ++i) {
100     base::ListValue* account;
101     if (account_list->GetList(i, &account) && account != NULL) {
102       std::string email;
103       // Canonicalize the email since ListAccounts returns "display email".
104       if (account->GetString(3, &email) && !email.empty()) {
105         // New version if ListAccounts indicates whether the email's session
106         // is still valid or not.  If this value is present and false, assume
107         // its invalid.  Otherwise assume it's valid to remain compatible with
108         // old version.
109         int is_email_valid = 1;
110         if (!account->GetInteger(9, &is_email_valid))
111           is_email_valid = 1;
112
113         accounts->push_back(
114             std::make_pair(CanonicalizeEmail(email), is_email_valid != 0));
115       }
116     }
117   }
118
119   return true;
120 }
121
122 }  // namespace gaia