Skip to content
Snippets Groups Projects
Commit c61f5aac authored by cmasone@chromium.org's avatar cmasone@chromium.org
Browse files

[Chrome OS] Refactor Canonicalize() method into base class to prepare for new...

[Chrome OS] Refactor Canonicalize() method into base class to prepare for new Authenticator subclass

BUG=chromium-os:4929
TEST=unit tests

Review URL: http://codereview.chromium.org/3436031

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@60752 0039d316-1c4b-4281-b951-d872f2087c98
parent 3e50e07a
No related merge requests found
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/chromeos/login/authenticator.h"
#include <string>
#include <vector>
#include "base/logging.h"
#include "base/string_split.h"
#include "base/string_util.h"
namespace chromeos {
class LoginStatusConsumer;
Authenticator::Authenticator(LoginStatusConsumer* consumer)
: consumer_(consumer) {
}
Authenticator::~Authenticator() {}
// static
std::string Authenticator::Canonicalize(const std::string& email_address) {
std::vector<std::string> parts;
char at = '@';
SplitString(email_address, at, &parts);
DCHECK_EQ(parts.size(), 2U) << "email_address should have only one @";
RemoveChars(parts[0], ".", &parts[0]);
if (parts[0].find('+') != std::string::npos)
parts[0].erase(parts[0].find('+'));
std::string new_email = StringToLowerASCII(JoinString(parts, at));
LOG(INFO) << "Canonicalized " << email_address << " to " << new_email;
return new_email;
}
} // namespace chromeos
......@@ -8,7 +8,6 @@
#include "base/basictypes.h"
#include "base/ref_counted.h"
#include "chrome/browser/chrome_thread.h"
#include "chrome/browser/chromeos/login/login_status_consumer.h"
#include "chrome/common/net/gaia/gaia_auth_consumer.h"
......@@ -24,10 +23,8 @@ namespace chromeos {
// consumer_->OnPasswordChangeDetected() on the UI thread.
class Authenticator : public base::RefCountedThreadSafe<Authenticator> {
public:
explicit Authenticator(LoginStatusConsumer* consumer)
: consumer_(consumer) {
}
virtual ~Authenticator() {}
explicit Authenticator(LoginStatusConsumer* consumer);
virtual ~Authenticator();
// Given a |username| and |password|, this method attempts to authenticate
// to login.
......@@ -74,6 +71,13 @@ class Authenticator : public base::RefCountedThreadSafe<Authenticator> {
virtual void ResyncEncryptedData(
const GaiaAuthConsumer::ClientLoginResult& credentials) = 0;
// Perform basic canonicalization of |email_address|, taking into account
// that gmail does not consider '.' or caps inside a username to matter.
// It also ignores everything after a '+'.
// For example, c.masone+abc@gmail.com == cMaSone@gmail.com, per
// http://mail.google.com/support/bin/answer.py?hl=en&ctx=mail&answer=10313#
static std::string Canonicalize(const std::string& email_address);
protected:
LoginStatusConsumer* consumer_;
......
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/chromeos/login/authenticator.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace chromeos {
TEST(AuthenticatorTest, EmailAddressNoOp) {
const char lower_case[] = "user@what.com";
EXPECT_EQ(lower_case, Authenticator::Canonicalize(lower_case));
}
TEST(AuthenticatorTest, EmailAddressIgnoreCaps) {
EXPECT_EQ(Authenticator::Canonicalize("user@what.com"),
Authenticator::Canonicalize("UsEr@what.com"));
}
TEST(AuthenticatorTest, EmailAddressIgnoreDomainCaps) {
EXPECT_EQ(Authenticator::Canonicalize("user@what.com"),
Authenticator::Canonicalize("UsEr@what.COM"));
}
TEST(AuthenticatorTest, EmailAddressIgnoreOneUsernameDot) {
EXPECT_EQ(Authenticator::Canonicalize("us.er@what.com"),
Authenticator::Canonicalize("UsEr@what.com"));
}
TEST(AuthenticatorTest, EmailAddressIgnoreManyUsernameDots) {
EXPECT_EQ(Authenticator::Canonicalize("u.ser@what.com"),
Authenticator::Canonicalize("Us.E.r@what.com"));
}
TEST(AuthenticatorTest, EmailAddressIgnoreConsecutiveUsernameDots) {
EXPECT_EQ(Authenticator::Canonicalize("use.r@what.com"),
Authenticator::Canonicalize("Us....E.r@what.com"));
}
TEST(AuthenticatorTest, EmailAddressDifferentOnesRejected) {
EXPECT_NE(Authenticator::Canonicalize("who@what.com"),
Authenticator::Canonicalize("Us....E.r@what.com"));
}
TEST(AuthenticatorTest, EmailAddressIgnorePlusSuffix) {
EXPECT_EQ(Authenticator::Canonicalize("user+cc@what.com"),
Authenticator::Canonicalize("user@what.com"));
}
TEST(AuthenticatorTest, EmailAddressIgnoreMultiPlusSuffix) {
EXPECT_EQ(Authenticator::Canonicalize("user+cc+bcc@what.com"),
Authenticator::Canonicalize("user@what.com"));
}
} // namespace chromeos
......@@ -13,7 +13,6 @@
#include "base/logging.h"
#include "base/path_service.h"
#include "base/sha2.h"
#include "base/string_split.h"
#include "base/string_util.h"
#include "base/third_party/nss/blapi.h"
#include "base/third_party/nss/sha256.h"
......@@ -435,19 +434,4 @@ bool GoogleAuthenticator::BinaryToHex(const std::vector<unsigned char>& binary,
return true;
}
// static
std::string GoogleAuthenticator::Canonicalize(
const std::string& email_address) {
std::vector<std::string> parts;
char at = '@';
SplitString(email_address, at, &parts);
DCHECK_EQ(parts.size(), 2U) << "email_address should have only one @";
RemoveChars(parts[0], ".", &parts[0]);
if (parts[0].find('+') != std::string::npos)
parts[0].erase(parts[0].find('+'));
std::string new_email = StringToLowerASCII(JoinString(parts, at));
LOG(INFO) << "Canonicalized " << email_address << " to " << new_email;
return new_email;
}
} // namespace chromeos
......@@ -85,12 +85,6 @@ class GoogleAuthenticator : public Authenticator, public GaiaAuthConsumer {
void ResyncEncryptedData(
const GaiaAuthConsumer::ClientLoginResult& credentials);
// Perform basic canonicalization of |email_address|, taking into account
// that gmail does not consider '.' or caps inside a username to matter.
// For example, c.masone@gmail.com == cMaSone@gmail.com, per
// http://mail.google.com/support/bin/answer.py?hl=en&ctx=mail&answer=10313#
static std::string Canonicalize(const std::string& email_address);
// Callbacks from GaiaAuthenticator2
virtual void OnClientLoginFailure(
const GoogleServiceAuthError& error);
......
......@@ -2,6 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/chromeos/login/google_authenticator.h"
#include <string>
#include <vector>
......@@ -16,7 +18,6 @@
#include "chrome/browser/chromeos/cros/mock_cryptohome_library.h"
#include "chrome/browser/chromeos/cros/mock_library_loader.h"
#include "chrome/browser/chromeos/login/client_login_response_handler.h"
#include "chrome/browser/chromeos/login/google_authenticator.h"
#include "chrome/browser/chromeos/login/issue_response_handler.h"
#include "chrome/browser/chromeos/login/mock_auth_response_handler.h"
#include "chrome/browser/chromeos/login/mock_url_fetchers.h"
......@@ -171,51 +172,6 @@ TEST_F(GoogleAuthenticatorTest, SaltToAscii) {
EXPECT_EQ("0a010000000000a0", auth->SaltAsAscii());
}
TEST_F(GoogleAuthenticatorTest, EmailAddressNoOp) {
const char lower_case[] = "user@what.com";
EXPECT_EQ(lower_case, GoogleAuthenticator::Canonicalize(lower_case));
}
TEST_F(GoogleAuthenticatorTest, EmailAddressIgnoreCaps) {
EXPECT_EQ(GoogleAuthenticator::Canonicalize("user@what.com"),
GoogleAuthenticator::Canonicalize("UsEr@what.com"));
}
TEST_F(GoogleAuthenticatorTest, EmailAddressIgnoreDomainCaps) {
EXPECT_EQ(GoogleAuthenticator::Canonicalize("user@what.com"),
GoogleAuthenticator::Canonicalize("UsEr@what.COM"));
}
TEST_F(GoogleAuthenticatorTest, EmailAddressIgnoreOneUsernameDot) {
EXPECT_EQ(GoogleAuthenticator::Canonicalize("us.er@what.com"),
GoogleAuthenticator::Canonicalize("UsEr@what.com"));
}
TEST_F(GoogleAuthenticatorTest, EmailAddressIgnoreManyUsernameDots) {
EXPECT_EQ(GoogleAuthenticator::Canonicalize("u.ser@what.com"),
GoogleAuthenticator::Canonicalize("Us.E.r@what.com"));
}
TEST_F(GoogleAuthenticatorTest, EmailAddressIgnoreConsecutiveUsernameDots) {
EXPECT_EQ(GoogleAuthenticator::Canonicalize("use.r@what.com"),
GoogleAuthenticator::Canonicalize("Us....E.r@what.com"));
}
TEST_F(GoogleAuthenticatorTest, EmailAddressDifferentOnesRejected) {
EXPECT_NE(GoogleAuthenticator::Canonicalize("who@what.com"),
GoogleAuthenticator::Canonicalize("Us....E.r@what.com"));
}
TEST_F(GoogleAuthenticatorTest, EmailAddressIgnorePlusSuffix) {
EXPECT_EQ(GoogleAuthenticator::Canonicalize("user+cc@what.com"),
GoogleAuthenticator::Canonicalize("user@what.com"));
}
TEST_F(GoogleAuthenticatorTest, EmailAddressIgnoreMultiPlusSuffix) {
EXPECT_EQ(GoogleAuthenticator::Canonicalize("user+cc+bcc@what.com"),
GoogleAuthenticator::Canonicalize("user@what.com"));
}
TEST_F(GoogleAuthenticatorTest, ReadLocalaccount) {
FilePath tmp_file_path = FakeLocalaccountFile(bytes_as_ascii_);
......
......@@ -8,6 +8,7 @@
#include <string>
#include "chrome/browser/chrome_thread.h"
#include "chrome/browser/chromeos/login/authenticator.h"
#include "chrome/browser/chromeos/login/login_utils.h"
#include "chrome/common/net/gaia/google_service_auth_error.h"
......
......@@ -18,6 +18,7 @@
#include "chrome/browser/browser.h"
#include "chrome/browser/browser_list.h"
#include "chrome/browser/browser_window.h"
#include "chrome/browser/chrome_thread.h"
#include "chrome/browser/chromeos/cros/input_method_library.h"
#include "chrome/browser/chromeos/cros/keyboard_library.h"
#include "chrome/browser/chromeos/cros/login_library.h"
......
......@@ -12,7 +12,7 @@
#include "base/values.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/chrome_thread.h"
#include "chrome/browser/chromeos/login/google_authenticator.h"
#include "chrome/browser/chromeos/login/authenticator.h"
#include "chrome/browser/chromeos/login/image_downloader.h"
#include "chrome/browser/chromeos/login/user_manager.h"
#include "chrome/browser/profile_manager.h"
......@@ -158,7 +158,7 @@ bool UserImageDownloader::IsUserEntry(ListValue* email_list) const {
if (!email_dictionary->GetStringASCII("address", &email))
continue;
if (GoogleAuthenticator::Canonicalize(email) == username_)
if (Authenticator::Canonicalize(email) == username_)
return true;
}
return false;
......
......@@ -489,7 +489,8 @@
'browser/chromeos/login/account_creation_view.h',
'browser/chromeos/login/account_screen.cc',
'browser/chromeos/login/account_screen.h',
'browser/chromeos/login/authentication_notification_details.h'
'browser/chromeos/login/authentication_notification_details.h',
'browser/chromeos/login/authenticator.cc',
'browser/chromeos/login/authenticator.h',
'browser/chromeos/login/auth_attempt_state.cc',
'browser/chromeos/login/auth_attempt_state.h',
......
......@@ -865,6 +865,7 @@
'browser/chromeos/gview_request_interceptor_unittest.cc',
'browser/chromeos/input_method/input_method_util_unittest.cc',
'browser/chromeos/language_preferences_unittest.cc',
'browser/chromeos/login/authenticator_unittest.cc',
'browser/chromeos/login/cookie_fetcher_unittest.cc',
'browser/chromeos/login/cryptohome_op_unittest.cc',
'browser/chromeos/login/google_authenticator_unittest.cc',
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment