OLD | NEW |
(Empty) | |
| 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 "chrome/browser/chromeos/login/saml/saml_offline_signin_limiter.h" |
| 6 |
| 7 #include <string> |
| 8 |
| 9 #include "base/bind.h" |
| 10 #include "base/bind_helpers.h" |
| 11 #include "base/location.h" |
| 12 #include "base/logging.h" |
| 13 #include "base/prefs/pref_service.h" |
| 14 #include "base/time/clock.h" |
| 15 #include "base/time/time.h" |
| 16 #include "chrome/browser/chromeos/login/user_manager.h" |
| 17 #include "chrome/browser/profiles/profile.h" |
| 18 #include "chrome/common/pref_names.h" |
| 19 #include "components/user_prefs/pref_registry_syncable.h" |
| 20 |
| 21 namespace chromeos { |
| 22 |
| 23 namespace { |
| 24 |
| 25 const int kDefaultSAMLOfflineSigninTimeLimit = 14 * 24 * 60 * 60; // 14 days. |
| 26 |
| 27 } // namespace |
| 28 |
| 29 // static |
| 30 void SAMLOfflineSigninLimiter::RegisterProfilePrefs( |
| 31 user_prefs::PrefRegistrySyncable* registry) { |
| 32 registry->RegisterIntegerPref( |
| 33 prefs::kSAMLOfflineSigninTimeLimit, |
| 34 kDefaultSAMLOfflineSigninTimeLimit, |
| 35 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); |
| 36 registry->RegisterInt64Pref( |
| 37 prefs::kSAMLLastGAIASignInTime, |
| 38 0, |
| 39 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); |
| 40 } |
| 41 |
| 42 void SAMLOfflineSigninLimiter::SignedIn(UserContext::AuthFlow auth_flow) { |
| 43 PrefService* prefs = profile_->GetPrefs(); |
| 44 const User* user = UserManager::Get()->GetUserByProfile(profile_); |
| 45 if (!user) { |
| 46 NOTREACHED(); |
| 47 return; |
| 48 } |
| 49 const std::string& user_id = user->email(); |
| 50 |
| 51 if (auth_flow == UserContext::AUTH_FLOW_GAIA_WITHOUT_SAML) { |
| 52 // The user went through online authentication and GAIA did not redirect to |
| 53 // a SAML IdP. No limit applies in this case. Clear the time of last login |
| 54 // with SAML and the flag enforcing online login, then return. |
| 55 prefs->ClearPref(prefs::kSAMLLastGAIASignInTime); |
| 56 UserManager::Get()->SaveForceOnlineSignin(user_id, false); |
| 57 return; |
| 58 } |
| 59 |
| 60 if (auth_flow == UserContext::AUTH_FLOW_GAIA_WITH_SAML) { |
| 61 // The user went through online authentication and GAIA did redirect to a |
| 62 // SAML IdP. Update the time of last login with SAML and clear the flag |
| 63 // enforcing online login. The flag will be set again when the limit |
| 64 // expires. If the limit already expired (e.g. because it was set to zero), |
| 65 // the flag will be set again immediately. |
| 66 UserManager::Get()->SaveForceOnlineSignin(user_id, false); |
| 67 prefs->SetInt64(prefs::kSAMLLastGAIASignInTime, |
| 68 clock_->Now().ToInternalValue()); |
| 69 } |
| 70 |
| 71 // Start listening for pref changes. |
| 72 pref_change_registrar_.Init(prefs); |
| 73 pref_change_registrar_.Add(prefs::kSAMLOfflineSigninTimeLimit, |
| 74 base::Bind(&SAMLOfflineSigninLimiter::UpdateLimit, |
| 75 base::Unretained(this))); |
| 76 |
| 77 // Arm the |offline_signin_limit_timer_| if a limit is in force. |
| 78 UpdateLimit(); |
| 79 } |
| 80 |
| 81 void SAMLOfflineSigninLimiter::Shutdown() { |
| 82 pref_change_registrar_.RemoveAll(); |
| 83 offline_signin_limit_timer_.reset(); |
| 84 } |
| 85 |
| 86 SAMLOfflineSigninLimiter::SAMLOfflineSigninLimiter(Profile* profile, |
| 87 base::Clock* clock) |
| 88 : profile_(profile), |
| 89 clock_(clock ? clock : &default_clock_) { |
| 90 } |
| 91 |
| 92 SAMLOfflineSigninLimiter::~SAMLOfflineSigninLimiter() { |
| 93 } |
| 94 |
| 95 void SAMLOfflineSigninLimiter::UpdateLimit() { |
| 96 // Stop the |offline_signin_limit_timer_|. |
| 97 offline_signin_limit_timer_.reset(); |
| 98 |
| 99 PrefService* prefs = pref_change_registrar_.prefs(); |
| 100 const base::TimeDelta offline_signin_time_limit = |
| 101 base::TimeDelta::FromSeconds( |
| 102 prefs->GetInteger(prefs::kSAMLOfflineSigninTimeLimit)); |
| 103 base::Time last_gaia_signin_time = base::Time::FromInternalValue( |
| 104 prefs->GetInt64(prefs::kSAMLLastGAIASignInTime)); |
| 105 if (offline_signin_time_limit < base::TimeDelta() || |
| 106 last_gaia_signin_time.is_null()) { |
| 107 // If no limit is in force, return. |
| 108 return; |
| 109 } |
| 110 |
| 111 const base::Time now = clock_->Now(); |
| 112 if (last_gaia_signin_time > now) { |
| 113 // If the time of last login with SAML lies in the future, set it to the |
| 114 // current time. |
| 115 NOTREACHED(); |
| 116 last_gaia_signin_time = now; |
| 117 prefs->SetInt64(prefs::kSAMLLastGAIASignInTime, now.ToInternalValue()); |
| 118 } |
| 119 |
| 120 const base::TimeDelta time_since_last_gaia_signin = |
| 121 now - last_gaia_signin_time; |
| 122 if (time_since_last_gaia_signin >= offline_signin_time_limit) { |
| 123 // If the limit already expired, set the flag enforcing online login |
| 124 // immediately and return. |
| 125 ForceOnlineLogin(); |
| 126 return; |
| 127 } |
| 128 |
| 129 // Arm |offline_signin_limit_timer_| so that it sets the flag enforcing online |
| 130 // login when the limit expires. |
| 131 offline_signin_limit_timer_.reset( |
| 132 new base::OneShotTimer<SAMLOfflineSigninLimiter>); |
| 133 offline_signin_limit_timer_->Start( |
| 134 FROM_HERE, |
| 135 offline_signin_time_limit - time_since_last_gaia_signin, |
| 136 this, |
| 137 &SAMLOfflineSigninLimiter::ForceOnlineLogin); |
| 138 } |
| 139 |
| 140 void SAMLOfflineSigninLimiter::ForceOnlineLogin() { |
| 141 User* user = UserManager::Get()->GetUserByProfile(profile_); |
| 142 if (!user) { |
| 143 NOTREACHED(); |
| 144 return; |
| 145 } |
| 146 |
| 147 UserManager::Get()->SaveForceOnlineSignin(user->email(), true); |
| 148 offline_signin_limit_timer_.reset(); |
| 149 } |
| 150 |
| 151 } // namespace chromeos |
OLD | NEW |