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(); 27 else if (parts[1] == kGmailDomain) // only strip '.' for gmail accounts. 28 base::RemoveChars(parts[0], ".", &parts[0]); 29 std::string new_email = StringToLowerASCII(JoinString(parts, at)); 30 VLOG(1) << "Canonicalized " << email_address << " to " << new_email; 31 return new_email; 32 } 33 34 std::string CanonicalizeDomain(const std::string& domain) { 35 // Canonicalization of domain names means lower-casing them. Make sure to 36 // update this function in sync with Canonicalize if this ever changes. 37 return StringToLowerASCII(domain); 38 } 39 40 std::string SanitizeEmail(const std::string& email_address) { 41 std::string sanitized(email_address); 42 43 // Apply a default domain if necessary. 44 if (sanitized.find('@') == std::string::npos) { 45 sanitized += '@'; 46 sanitized += kGmailDomain; 47 } 48 49 return sanitized; 50 } 51 52 bool AreEmailsSame(const std::string& email1, const std::string& email2) { 53 return gaia::CanonicalizeEmail(gaia::SanitizeEmail(email1)) == 54 gaia::CanonicalizeEmail(gaia::SanitizeEmail(email2)); 55 } 56 57 std::string ExtractDomainName(const std::string& email_address) { 58 // First canonicalize which will also verify we have proper domain part. 59 std::string email = CanonicalizeEmail(email_address); 60 size_t separator_pos = email.find('@'); 61 if (separator_pos != email.npos && separator_pos < email.length() - 1) 62 return email.substr(separator_pos + 1); 63 else 64 NOTREACHED() << "Not a proper email address: " << email; 65 return std::string(); 66 } 67 68 bool IsGaiaSignonRealm(const GURL& url) { 69 if (!url.SchemeIsSecure()) 70 return false; 71 72 return url == GaiaUrls::GetInstance()->gaia_url(); 73 } 74 75 76 std::vector<std::string> ParseListAccountsData(const std::string& data) { 77 std::vector<std::string> account_ids; 78 79 // Parse returned data and make sure we have data. 80 scoped_ptr<base::Value> value(base::JSONReader::Read(data)); 81 if (!value) 82 return account_ids; 83 84 base::ListValue* list; 85 if (!value->GetAsList(&list) || list->GetSize() < 2) 86 return account_ids; 87 88 // Get list of account info. 89 base::ListValue* accounts; 90 if (!list->GetList(1, &accounts) || accounts == NULL) 91 return account_ids; 92 93 // Build a vector of accounts from the cookie. Order is important: the first 94 // account in the list is the primary account. 95 for (size_t i = 0; i < accounts->GetSize(); ++i) { 96 base::ListValue* account; 97 if (accounts->GetList(i, &account) && account != NULL) { 98 std::string email; 99 if (account->GetString(3, &email) && !email.empty()) 100 account_ids.push_back(email); 101 } 102 } 103 104 return account_ids; 105 } 106 107 } // namespace gaia 108