| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/policy/user_policy_identity_strategy.h" | 5 #include "chrome/browser/policy/user_policy_identity_strategy.h" |
| 6 | 6 |
| 7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
| 8 #include "chrome/browser/browser_signin.h" | |
| 9 #include "chrome/browser/net/gaia/token_service.h" | |
| 10 #include "chrome/browser/policy/proto/device_management_backend.pb.h" | 8 #include "chrome/browser/policy/proto/device_management_backend.pb.h" |
| 11 #include "chrome/browser/policy/proto/device_management_constants.h" | 9 #include "chrome/browser/policy/proto/device_management_constants.h" |
| 12 #include "chrome/browser/policy/proto/device_management_local.pb.h" | 10 #include "chrome/browser/policy/proto/device_management_local.pb.h" |
| 13 #include "chrome/browser/profiles/profile.h" | |
| 14 #include "chrome/common/guid.h" | 11 #include "chrome/common/guid.h" |
| 15 #include "chrome/common/net/gaia/gaia_constants.h" | 12 #include "chrome/common/net/gaia/gaia_constants.h" |
| 16 #include "content/browser/browser_thread.h" | 13 #include "content/browser/browser_thread.h" |
| 17 #include "content/common/notification_details.h" | |
| 18 #include "content/common/notification_service.h" | |
| 19 #include "content/common/notification_source.h" | |
| 20 | |
| 21 #if defined(OS_CHROMEOS) | |
| 22 #include "chrome/browser/chromeos/login/user_manager.h" | |
| 23 #endif | |
| 24 | 14 |
| 25 namespace policy { | 15 namespace policy { |
| 26 | 16 |
| 27 namespace em = enterprise_management; | 17 namespace em = enterprise_management; |
| 28 | 18 |
| 29 // Responsible for managing the on-disk token cache. | 19 // Responsible for managing the on-disk token cache. |
| 30 class UserPolicyIdentityStrategy::TokenCache | 20 class UserPolicyIdentityStrategy::TokenCache |
| 31 : public base::RefCountedThreadSafe< | 21 : public base::RefCountedThreadSafe< |
| 32 UserPolicyIdentityStrategy::TokenCache> { | 22 UserPolicyIdentityStrategy::TokenCache> { |
| 33 public: | 23 public: |
| 34 TokenCache(const base::WeakPtr<UserPolicyIdentityStrategy>& identity_strategy, | 24 TokenCache(const base::WeakPtr<UserPolicyIdentityStrategy>& identity_strategy, |
| 35 const FilePath& cache_file); | 25 const FilePath& cache_file); |
| 36 | 26 |
| 37 void Load(); | 27 void Load(); |
| 38 void Store(const std::string& token, const std::string& device_id); | 28 void Store(const std::string& token, const std::string& device_id); |
| 39 | 29 |
| 30 bool load_finished() { |
| 31 return load_finished_; |
| 32 } |
| 33 |
| 40 private: | 34 private: |
| 41 friend class base::RefCountedThreadSafe< | 35 friend class base::RefCountedThreadSafe< |
| 42 UserPolicyIdentityStrategy::TokenCache>; | 36 UserPolicyIdentityStrategy::TokenCache>; |
| 43 ~TokenCache() {} | 37 ~TokenCache() {} |
| 44 void LoadOnFileThread(); | 38 void LoadOnFileThread(); |
| 45 void NotifyOnUIThread(const std::string& token, | 39 void NotifyOnUIThread(const std::string& token, |
| 46 const std::string& device_id); | 40 const std::string& device_id); |
| 47 void StoreOnFileThread(const std::string& token, | 41 void StoreOnFileThread(const std::string& token, |
| 48 const std::string& device_id); | 42 const std::string& device_id); |
| 49 | 43 |
| 50 const base::WeakPtr<UserPolicyIdentityStrategy> identity_strategy_; | 44 const base::WeakPtr<UserPolicyIdentityStrategy> identity_strategy_; |
| 51 const FilePath cache_file_; | 45 const FilePath cache_file_; |
| 46 bool load_finished_; |
| 47 bool load_initiated_; |
| 52 | 48 |
| 53 DISALLOW_COPY_AND_ASSIGN(TokenCache); | 49 DISALLOW_COPY_AND_ASSIGN(TokenCache); |
| 54 }; | 50 }; |
| 55 | 51 |
| 56 UserPolicyIdentityStrategy::TokenCache::TokenCache( | 52 UserPolicyIdentityStrategy::TokenCache::TokenCache( |
| 57 const base::WeakPtr<UserPolicyIdentityStrategy>& identity_strategy, | 53 const base::WeakPtr<UserPolicyIdentityStrategy>& identity_strategy, |
| 58 const FilePath& cache_file) | 54 const FilePath& cache_file) |
| 59 : identity_strategy_(identity_strategy), | 55 : identity_strategy_(identity_strategy), |
| 60 cache_file_(cache_file) {} | 56 cache_file_(cache_file), |
| 57 load_finished_(false), |
| 58 load_initiated_(false) {} |
| 61 | 59 |
| 62 void UserPolicyIdentityStrategy::TokenCache::Load() { | 60 void UserPolicyIdentityStrategy::TokenCache::Load() { |
| 63 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 61 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 62 DCHECK(!load_initiated_); |
| 63 load_initiated_ = true; |
| 64 BrowserThread::PostTask( | 64 BrowserThread::PostTask( |
| 65 BrowserThread::FILE, FROM_HERE, | 65 BrowserThread::FILE, FROM_HERE, |
| 66 NewRunnableMethod( | 66 NewRunnableMethod( |
| 67 this, &UserPolicyIdentityStrategy::TokenCache::LoadOnFileThread)); | 67 this, &UserPolicyIdentityStrategy::TokenCache::LoadOnFileThread)); |
| 68 } | 68 } |
| 69 | 69 |
| 70 void UserPolicyIdentityStrategy::TokenCache::Store( | 70 void UserPolicyIdentityStrategy::TokenCache::Store( |
| 71 const std::string& token, | 71 const std::string& token, |
| 72 const std::string& device_id) { | 72 const std::string& device_id) { |
| 73 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 73 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| (...skipping 27 matching lines...) Expand all Loading... |
| 101 this, | 101 this, |
| 102 &UserPolicyIdentityStrategy::TokenCache::NotifyOnUIThread, | 102 &UserPolicyIdentityStrategy::TokenCache::NotifyOnUIThread, |
| 103 device_token, | 103 device_token, |
| 104 device_id)); | 104 device_id)); |
| 105 } | 105 } |
| 106 | 106 |
| 107 void UserPolicyIdentityStrategy::TokenCache::NotifyOnUIThread( | 107 void UserPolicyIdentityStrategy::TokenCache::NotifyOnUIThread( |
| 108 const std::string& token, | 108 const std::string& token, |
| 109 const std::string& device_id) { | 109 const std::string& device_id) { |
| 110 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 110 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 111 DCHECK(!load_finished_); |
| 112 load_finished_ = true; |
| 111 if (identity_strategy_.get()) | 113 if (identity_strategy_.get()) |
| 112 identity_strategy_->OnCacheLoaded(token, device_id); | 114 identity_strategy_->OnCacheLoaded(token, device_id); |
| 113 } | 115 } |
| 114 | 116 |
| 115 void UserPolicyIdentityStrategy::TokenCache::StoreOnFileThread( | 117 void UserPolicyIdentityStrategy::TokenCache::StoreOnFileThread( |
| 116 const std::string& token, | 118 const std::string& token, |
| 117 const std::string& device_id) { | 119 const std::string& device_id) { |
| 118 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 120 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 119 em::DeviceCredentials device_credentials; | 121 em::DeviceCredentials device_credentials; |
| 120 device_credentials.set_device_token(token); | 122 device_credentials.set_device_token(token); |
| 121 device_credentials.set_device_id(device_id); | 123 device_credentials.set_device_id(device_id); |
| 122 std::string data; | 124 std::string data; |
| 123 bool success = device_credentials.SerializeToString(&data); | 125 bool success = device_credentials.SerializeToString(&data); |
| 124 if (!success) { | 126 if (!success) { |
| 125 LOG(WARNING) << "Failed serialize device token data, will not write " | 127 LOG(WARNING) << "Failed serialize device token data, will not write " |
| 126 << cache_file_.value(); | 128 << cache_file_.value(); |
| 127 return; | 129 return; |
| 128 } | 130 } |
| 129 | 131 |
| 130 if (!file_util::CreateDirectory(cache_file_.DirName())) { | 132 if (!file_util::CreateDirectory(cache_file_.DirName())) { |
| 131 LOG(WARNING) << "Failed to create directory " | 133 LOG(WARNING) << "Failed to create directory " |
| 132 << cache_file_.DirName().value(); | 134 << cache_file_.DirName().value(); |
| 133 return; | 135 return; |
| 134 } | 136 } |
| 135 | 137 |
| 136 file_util::WriteFile(cache_file_, data.c_str(), data.length()); | 138 file_util::WriteFile(cache_file_, data.c_str(), data.length()); |
| 137 } | 139 } |
| 138 | 140 |
| 139 UserPolicyIdentityStrategy::UserPolicyIdentityStrategy( | 141 UserPolicyIdentityStrategy::UserPolicyIdentityStrategy( |
| 140 Profile* profile, | 142 const std::string& user_name, |
| 141 const FilePath& cache_file) | 143 const FilePath& cache_file) |
| 142 : profile_(profile), | 144 : user_name_(user_name), |
| 143 ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) { | 145 ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) { |
| 144 cache_ = new TokenCache(weak_ptr_factory_.GetWeakPtr(), cache_file); | 146 cache_ = new TokenCache(weak_ptr_factory_.GetWeakPtr(), cache_file); |
| 145 registrar_.Add(this, | |
| 146 NotificationType::TOKEN_AVAILABLE, | |
| 147 Source<TokenService>(profile->GetTokenService())); | |
| 148 | |
| 149 // Register for the event of user login. The device management token won't | |
| 150 // be fetched until we know the domain of the currently logged in user. | |
| 151 #if defined(OS_CHROMEOS) | |
| 152 registrar_.Add(this, | |
| 153 NotificationType::LOGIN_USER_CHANGED, | |
| 154 NotificationService::AllSources()); | |
| 155 #else | |
| 156 registrar_.Add(this, | |
| 157 NotificationType::GOOGLE_SIGNIN_SUCCESSFUL, | |
| 158 Source<Profile>(profile_)); | |
| 159 #endif | |
| 160 } | 147 } |
| 161 | 148 |
| 162 UserPolicyIdentityStrategy::~UserPolicyIdentityStrategy() {} | 149 UserPolicyIdentityStrategy::~UserPolicyIdentityStrategy() {} |
| 163 | 150 |
| 164 void UserPolicyIdentityStrategy::LoadTokenCache() { | 151 void UserPolicyIdentityStrategy::LoadTokenCache() { |
| 165 cache_->Load(); | 152 cache_->Load(); |
| 166 } | 153 } |
| 167 | 154 |
| 168 std::string UserPolicyIdentityStrategy::GetDeviceToken() { | 155 std::string UserPolicyIdentityStrategy::GetDeviceToken() { |
| 169 return device_token_; | 156 return device_token_; |
| (...skipping 15 matching lines...) Expand all Loading... |
| 185 UserPolicyIdentityStrategy::GetPolicyRegisterType() { | 172 UserPolicyIdentityStrategy::GetPolicyRegisterType() { |
| 186 return em::DeviceRegisterRequest::USER; | 173 return em::DeviceRegisterRequest::USER; |
| 187 } | 174 } |
| 188 | 175 |
| 189 std::string UserPolicyIdentityStrategy::GetPolicyType() { | 176 std::string UserPolicyIdentityStrategy::GetPolicyType() { |
| 190 return kChromeUserPolicyType; | 177 return kChromeUserPolicyType; |
| 191 } | 178 } |
| 192 | 179 |
| 193 bool UserPolicyIdentityStrategy::GetCredentials(std::string* username, | 180 bool UserPolicyIdentityStrategy::GetCredentials(std::string* username, |
| 194 std::string* auth_token) { | 181 std::string* auth_token) { |
| 195 *username = GetCurrentUser(); | 182 *username = user_name_; |
| 196 *auth_token = profile_->GetTokenService()->GetTokenForService( | 183 *auth_token = auth_token_; |
| 197 GaiaConstants::kDeviceManagementService); | |
| 198 | 184 |
| 199 return !username->empty() && !auth_token->empty() && !device_id_.empty(); | 185 return !username->empty() && !auth_token->empty() && !device_id_.empty(); |
| 200 } | 186 } |
| 201 | 187 |
| 202 void UserPolicyIdentityStrategy::OnDeviceTokenAvailable( | 188 void UserPolicyIdentityStrategy::OnDeviceTokenAvailable( |
| 203 const std::string& token) { | 189 const std::string& token) { |
| 204 DCHECK(!device_id_.empty()); | 190 DCHECK(!device_id_.empty()); |
| 205 device_token_ = token; | 191 device_token_ = token; |
| 206 cache_->Store(device_token_, device_id_); | 192 cache_->Store(device_token_, device_id_); |
| 207 NotifyDeviceTokenChanged(); | 193 NotifyDeviceTokenChanged(); |
| 208 } | 194 } |
| 209 | 195 |
| 210 std::string UserPolicyIdentityStrategy::GetCurrentUser() { | 196 void UserPolicyIdentityStrategy::CheckAndTriggerFetch() { |
| 211 #if defined(OS_CHROMEOS) | 197 if (!user_name_.empty() && !auth_token_.empty() && cache_->load_finished()) { |
| 212 // TODO(mnissler) On CrOS it seems impossible to figure out what user belongs | |
| 213 // to a profile. Revisit after multi-profile support landed. | |
| 214 return chromeos::UserManager::Get()->logged_in_user().email(); | |
| 215 #else | |
| 216 return profile_->GetBrowserSignin()->GetSignedInUsername(); | |
| 217 #endif | |
| 218 } | |
| 219 | 198 |
| 220 void UserPolicyIdentityStrategy::CheckAndTriggerFetch() { | |
| 221 if (!GetCurrentUser().empty() && | |
| 222 profile_->GetTokenService()->HasTokenForService( | |
| 223 GaiaConstants::kDeviceManagementService)) { | |
| 224 // For user tokens, there is no actual identifier. We generate a random | 199 // For user tokens, there is no actual identifier. We generate a random |
| 225 // identifier instead each time we ask for the token. | 200 // identifier instead each time we ask for the token. |
| 226 device_id_ = guid::GenerateGUID(); | 201 device_id_ = guid::GenerateGUID(); |
| 227 NotifyAuthChanged(); | 202 NotifyAuthChanged(); |
| 228 } | 203 } |
| 229 } | 204 } |
| 230 | 205 |
| 206 void UserPolicyIdentityStrategy::SetAuthToken(const std::string& auth_token) { |
| 207 auth_token_ = auth_token; |
| 208 |
| 209 // Request a new device management server token, but only in case we |
| 210 // don't already have it. |
| 211 if (device_token_.empty()) |
| 212 CheckAndTriggerFetch(); |
| 213 } |
| 214 |
| 231 void UserPolicyIdentityStrategy::OnCacheLoaded(const std::string& token, | 215 void UserPolicyIdentityStrategy::OnCacheLoaded(const std::string& token, |
| 232 const std::string& device_id) { | 216 const std::string& device_id) { |
| 233 if (!token.empty() && !device_id.empty()) { | 217 if (!token.empty() && !device_id.empty()) { |
| 234 device_token_ = token; | 218 device_token_ = token; |
| 235 device_id_ = device_id; | 219 device_id_ = device_id; |
| 236 NotifyDeviceTokenChanged(); | 220 NotifyDeviceTokenChanged(); |
| 237 } else { | 221 } else { |
| 238 CheckAndTriggerFetch(); | 222 CheckAndTriggerFetch(); |
| 239 } | 223 } |
| 240 } | 224 } |
| 241 | 225 |
| 242 void UserPolicyIdentityStrategy::Observe(NotificationType type, | |
| 243 const NotificationSource& source, | |
| 244 const NotificationDetails& details) { | |
| 245 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 246 if (type == NotificationType::TOKEN_AVAILABLE) { | |
| 247 if (Source<TokenService>(source).ptr() == profile_->GetTokenService()) { | |
| 248 const TokenService::TokenAvailableDetails* token_details = | |
| 249 Details<const TokenService::TokenAvailableDetails>(details).ptr(); | |
| 250 if (token_details->service() == GaiaConstants::kDeviceManagementService) | |
| 251 if (device_token_.empty()) { | |
| 252 // Request a new device management server token, but only in case we | |
| 253 // don't already have it. | |
| 254 CheckAndTriggerFetch(); | |
| 255 } | |
| 256 } | |
| 257 #if defined(OS_CHROMEOS) | |
| 258 } else if (type == NotificationType::LOGIN_USER_CHANGED) { | |
| 259 CheckAndTriggerFetch(); | |
| 260 #else | |
| 261 } else if (type == NotificationType::GOOGLE_SIGNIN_SUCCESSFUL) { | |
| 262 if (profile_ == Source<Profile>(source).ptr()) | |
| 263 CheckAndTriggerFetch(); | |
| 264 #endif | |
| 265 } else { | |
| 266 NOTREACHED(); | |
| 267 } | |
| 268 } | |
| 269 | 226 |
| 270 } // namespace policy | 227 } // namespace policy |
| OLD | NEW |