Home | History | Annotate | Download | only in auth
      1 // Copyright 2014 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 "chromeos/login/auth/mock_authenticator.h"
      6 
      7 #include "base/bind.h"
      8 #include "base/location.h"
      9 #include "base/logging.h"
     10 
     11 namespace chromeos {
     12 
     13 MockAuthenticator::MockAuthenticator(AuthStatusConsumer* consumer,
     14                                      const UserContext& expected_user_context)
     15     : Authenticator(consumer),
     16       expected_user_context_(expected_user_context),
     17       message_loop_(base::MessageLoopProxy::current()) {
     18 }
     19 
     20 void MockAuthenticator::CompleteLogin(Profile* profile,
     21                                       const UserContext& user_context) {
     22   if (expected_user_context_ != user_context)
     23     NOTREACHED();
     24   OnAuthSuccess();
     25 }
     26 
     27 void MockAuthenticator::AuthenticateToLogin(Profile* profile,
     28                                             const UserContext& user_context) {
     29   if (user_context == expected_user_context_) {
     30     message_loop_->PostTask(
     31         FROM_HERE, base::Bind(&MockAuthenticator::OnAuthSuccess, this));
     32     return;
     33   }
     34   GoogleServiceAuthError error(
     35       GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS);
     36   message_loop_->PostTask(
     37       FROM_HERE,
     38       base::Bind(&MockAuthenticator::OnAuthFailure,
     39                  this,
     40                  AuthFailure::FromNetworkAuthFailure(error)));
     41 }
     42 
     43 void MockAuthenticator::AuthenticateToUnlock(const UserContext& user_context) {
     44   AuthenticateToLogin(NULL /* not used */, user_context);
     45 }
     46 
     47 void MockAuthenticator::LoginAsSupervisedUser(const UserContext& user_context) {
     48   UserContext new_user_context = user_context;
     49   new_user_context.SetUserIDHash(user_context.GetUserID());
     50   consumer_->OnAuthSuccess(new_user_context);
     51 }
     52 
     53 void MockAuthenticator::LoginRetailMode() {
     54   UserContext user_context("demo-mode");
     55   user_context.SetUserIDHash("demo-mode");
     56   consumer_->OnRetailModeAuthSuccess(user_context);
     57 }
     58 
     59 void MockAuthenticator::LoginOffTheRecord() {
     60   consumer_->OnOffTheRecordAuthSuccess();
     61 }
     62 
     63 void MockAuthenticator::LoginAsPublicSession(const UserContext& user_context) {
     64   UserContext logged_in_user_context = user_context;
     65   logged_in_user_context.SetUserIDHash(logged_in_user_context.GetUserID());
     66   consumer_->OnAuthSuccess(logged_in_user_context);
     67 }
     68 
     69 void MockAuthenticator::LoginAsKioskAccount(const std::string& app_user_id,
     70                                             bool use_guest_mount) {
     71   UserContext user_context(expected_user_context_.GetUserID());
     72   user_context.SetUserIDHash(expected_user_context_.GetUserID());
     73   consumer_->OnAuthSuccess(user_context);
     74 }
     75 
     76 void MockAuthenticator::OnRetailModeAuthSuccess() {
     77   UserContext user_context(expected_user_context_.GetUserID());
     78   user_context.SetUserIDHash(expected_user_context_.GetUserID());
     79   consumer_->OnRetailModeAuthSuccess(user_context);
     80 }
     81 
     82 void MockAuthenticator::OnAuthSuccess() {
     83   // If we want to be more like the real thing, we could save the user ID
     84   // in AuthenticateToLogin, but there's not much of a point.
     85   UserContext user_context(expected_user_context_);
     86   user_context.SetUserIDHash(expected_user_context_.GetUserID());
     87   consumer_->OnAuthSuccess(user_context);
     88 }
     89 
     90 void MockAuthenticator::OnAuthFailure(const AuthFailure& failure) {
     91   consumer_->OnAuthFailure(failure);
     92 }
     93 
     94 void MockAuthenticator::RecoverEncryptedData(const std::string& old_password) {
     95 }
     96 
     97 void MockAuthenticator::ResyncEncryptedData() {
     98 }
     99 
    100 void MockAuthenticator::SetExpectedCredentials(
    101     const UserContext& user_context) {
    102   expected_user_context_ = user_context;
    103 }
    104 
    105 MockAuthenticator::~MockAuthenticator() {
    106 }
    107 
    108 }  // namespace chromeos
    109