| 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/cros_user_policy_identity_strategy.h" |
| 6 | 6 |
| 7 #include "base/file_util.h" |
| 7 #include "chrome/browser/browser_signin.h" | 8 #include "chrome/browser/browser_signin.h" |
| 9 #include "chrome/browser/chromeos/login/user_manager.h" |
| 8 #include "chrome/browser/net/gaia/token_service.h" | 10 #include "chrome/browser/net/gaia/token_service.h" |
| 9 #include "chrome/browser/policy/proto/device_management_backend.pb.h" | 11 #include "chrome/browser/policy/proto/device_management_backend.pb.h" |
| 10 #include "chrome/browser/policy/proto/device_management_constants.h" | 12 #include "chrome/browser/policy/proto/device_management_constants.h" |
| 11 #include "chrome/browser/profiles/profile.h" | 13 #include "chrome/browser/profiles/profile.h" |
| 12 #include "chrome/common/guid.h" | 14 #include "chrome/common/guid.h" |
| 13 #include "chrome/common/net/gaia/gaia_constants.h" | 15 #include "chrome/common/net/gaia/gaia_constants.h" |
| 14 #include "content/browser/browser_thread.h" | 16 #include "content/browser/browser_thread.h" |
| 15 #include "content/common/notification_details.h" | 17 #include "content/common/notification_details.h" |
| 16 #include "content/common/notification_service.h" | 18 #include "content/common/notification_service.h" |
| 17 #include "content/common/notification_source.h" | 19 #include "content/common/notification_source.h" |
| 18 | 20 |
| 19 #if defined(OS_CHROMEOS) | |
| 20 #include "chrome/browser/chromeos/login/user_manager.h" | |
| 21 #endif | |
| 22 | |
| 23 namespace policy { | 21 namespace policy { |
| 24 | 22 |
| 25 namespace em = enterprise_management; | 23 namespace em = enterprise_management; |
| 26 | 24 |
| 27 UserPolicyIdentityStrategy::UserPolicyIdentityStrategy( | 25 CrosUserPolicyIdentityStrategy::CrosUserPolicyIdentityStrategy(Profile* profile) |
| 28 Profile* profile, | 26 : should_register_(false), |
| 29 const FilePath& cache_file) | 27 profile_(profile) { |
| 30 : profile_(profile), | |
| 31 ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) { | |
| 32 cache_ = new UserPolicyTokenCache(weak_ptr_factory_.GetWeakPtr(), cache_file); | |
| 33 registrar_.Add(this, | 28 registrar_.Add(this, |
| 34 NotificationType::TOKEN_AVAILABLE, | 29 NotificationType::TOKEN_AVAILABLE, |
| 35 Source<TokenService>(profile->GetTokenService())); | 30 Source<TokenService>(profile->GetTokenService())); |
| 36 | 31 |
| 37 // Register for the event of user login. The device management token won't | 32 // Register for the event of user login. The device management token won't |
| 38 // be fetched until we know the domain of the currently logged in user. | 33 // be fetched until we know the domain of the currently logged in user. |
| 39 #if defined(OS_CHROMEOS) | 34 registrar_.Add(this, |
| 40 registrar_.Add(this, | |
| 41 NotificationType::LOGIN_USER_CHANGED, | 35 NotificationType::LOGIN_USER_CHANGED, |
| 42 NotificationService::AllSources()); | 36 NotificationService::AllSources()); |
| 43 #else | |
| 44 registrar_.Add(this, | |
| 45 NotificationType::GOOGLE_SIGNIN_SUCCESSFUL, | |
| 46 Source<Profile>(profile_)); | |
| 47 #endif | |
| 48 } | 37 } |
| 49 | 38 |
| 50 UserPolicyIdentityStrategy::~UserPolicyIdentityStrategy() {} | 39 CrosUserPolicyIdentityStrategy::~CrosUserPolicyIdentityStrategy() {} |
| 51 | 40 |
| 52 void UserPolicyIdentityStrategy::LoadTokenCache() { | 41 void CrosUserPolicyIdentityStrategy::SetDeviceCredentials( |
| 53 cache_->Load(); | 42 const std::string& device_id, |
| 43 const std::string& token) { |
| 44 should_register_ = true; |
| 45 device_id_ = device_id; |
| 46 device_token_ = token; |
| 47 NotifyDeviceTokenChanged(); |
| 54 } | 48 } |
| 55 | 49 |
| 56 std::string UserPolicyIdentityStrategy::GetDeviceToken() { | 50 void CrosUserPolicyIdentityStrategy::EnableRegistration() { |
| 51 should_register_ = true; |
| 52 CheckAndTriggerFetch(); |
| 53 } |
| 54 |
| 55 std::string CrosUserPolicyIdentityStrategy::GetDeviceToken() { |
| 57 return device_token_; | 56 return device_token_; |
| 58 } | 57 } |
| 59 | 58 |
| 60 std::string UserPolicyIdentityStrategy::GetDeviceID() { | 59 std::string CrosUserPolicyIdentityStrategy::GetDeviceID() { |
| 61 return device_id_; | 60 return device_id_; |
| 62 } | 61 } |
| 63 | 62 |
| 64 std::string UserPolicyIdentityStrategy::GetMachineID() { | 63 std::string CrosUserPolicyIdentityStrategy::GetMachineID() { |
| 65 return std::string(); | 64 return std::string(); |
| 66 } | 65 } |
| 67 | 66 |
| 68 std::string UserPolicyIdentityStrategy::GetMachineModel() { | 67 std::string CrosUserPolicyIdentityStrategy::GetMachineModel() { |
| 69 return std::string(); | 68 return std::string(); |
| 70 } | 69 } |
| 71 | 70 |
| 72 em::DeviceRegisterRequest_Type | 71 em::DeviceRegisterRequest_Type |
| 73 UserPolicyIdentityStrategy::GetPolicyRegisterType() { | 72 CrosUserPolicyIdentityStrategy::GetPolicyRegisterType() { |
| 74 return em::DeviceRegisterRequest::USER; | 73 return em::DeviceRegisterRequest::USER; |
| 75 } | 74 } |
| 76 | 75 |
| 77 std::string UserPolicyIdentityStrategy::GetPolicyType() { | 76 std::string CrosUserPolicyIdentityStrategy::GetPolicyType() { |
| 78 return kChromeUserPolicyType; | 77 return kChromeUserPolicyType; |
| 79 } | 78 } |
| 80 | 79 |
| 81 bool UserPolicyIdentityStrategy::GetCredentials(std::string* username, | 80 bool CrosUserPolicyIdentityStrategy::GetCredentials(std::string* username, |
| 82 std::string* auth_token) { | 81 std::string* auth_token) { |
| 82 if (!should_register_) |
| 83 return false; |
| 84 |
| 83 *username = GetCurrentUser(); | 85 *username = GetCurrentUser(); |
| 84 *auth_token = profile_->GetTokenService()->GetTokenForService( | 86 *auth_token = profile_->GetTokenService()->GetTokenForService( |
| 85 GaiaConstants::kDeviceManagementService); | 87 GaiaConstants::kDeviceManagementService); |
| 86 | 88 |
| 87 return !username->empty() && !auth_token->empty() && !device_id_.empty(); | 89 return !username->empty() && !auth_token->empty() && !device_id_.empty(); |
| 88 } | 90 } |
| 89 | 91 |
| 90 void UserPolicyIdentityStrategy::OnDeviceTokenAvailable( | 92 void CrosUserPolicyIdentityStrategy::OnDeviceTokenAvailable( |
| 91 const std::string& token) { | 93 const std::string& token) { |
| 92 DCHECK(!device_id_.empty()); | 94 DCHECK(!device_id_.empty()); |
| 93 device_token_ = token; | 95 device_token_ = token; |
| 94 cache_->Store(device_token_, device_id_); | |
| 95 NotifyDeviceTokenChanged(); | 96 NotifyDeviceTokenChanged(); |
| 96 } | 97 } |
| 97 | 98 |
| 98 std::string UserPolicyIdentityStrategy::GetCurrentUser() { | 99 std::string CrosUserPolicyIdentityStrategy::GetCurrentUser() { |
| 99 #if defined(OS_CHROMEOS) | |
| 100 // TODO(mnissler) On CrOS it seems impossible to figure out what user belongs | |
| 101 // to a profile. Revisit after multi-profile support landed. | |
| 102 return chromeos::UserManager::Get()->logged_in_user().email(); | 100 return chromeos::UserManager::Get()->logged_in_user().email(); |
| 103 #else | |
| 104 return profile_->GetBrowserSignin()->GetSignedInUsername(); | |
| 105 #endif | |
| 106 } | 101 } |
| 107 | 102 |
| 108 void UserPolicyIdentityStrategy::CheckAndTriggerFetch() { | 103 void CrosUserPolicyIdentityStrategy::CheckAndTriggerFetch() { |
| 109 if (!GetCurrentUser().empty() && | 104 if (should_register_ && |
| 105 !GetCurrentUser().empty() && |
| 110 profile_->GetTokenService()->HasTokenForService( | 106 profile_->GetTokenService()->HasTokenForService( |
| 111 GaiaConstants::kDeviceManagementService)) { | 107 GaiaConstants::kDeviceManagementService)) { |
| 112 // For user tokens, there is no actual identifier. We generate a random | 108 // For user tokens, there is no actual identifier. We generate a random |
| 113 // identifier instead each time we ask for the token. | 109 // identifier instead each time we ask for the token. |
| 114 device_id_ = guid::GenerateGUID(); | 110 device_id_ = guid::GenerateGUID(); |
| 115 NotifyAuthChanged(); | 111 NotifyAuthChanged(); |
| 116 } | 112 } |
| 117 } | 113 } |
| 118 | 114 |
| 119 void UserPolicyIdentityStrategy::OnTokenCacheLoaded( | 115 void CrosUserPolicyIdentityStrategy::Observe( |
| 120 const std::string& token, | 116 NotificationType type, |
| 121 const std::string& device_id) { | 117 const NotificationSource& source, |
| 122 if (!token.empty() && !device_id.empty()) { | 118 const NotificationDetails& details) { |
| 123 device_token_ = token; | |
| 124 device_id_ = device_id; | |
| 125 NotifyDeviceTokenChanged(); | |
| 126 } else { | |
| 127 CheckAndTriggerFetch(); | |
| 128 } | |
| 129 } | |
| 130 | |
| 131 void UserPolicyIdentityStrategy::Observe(NotificationType type, | |
| 132 const NotificationSource& source, | |
| 133 const NotificationDetails& details) { | |
| 134 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 119 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 135 if (type == NotificationType::TOKEN_AVAILABLE) { | 120 if (type == NotificationType::TOKEN_AVAILABLE) { |
| 136 if (Source<TokenService>(source).ptr() == profile_->GetTokenService()) { | 121 if (Source<TokenService>(source).ptr() == profile_->GetTokenService()) { |
| 137 const TokenService::TokenAvailableDetails* token_details = | 122 const TokenService::TokenAvailableDetails* token_details = |
| 138 Details<const TokenService::TokenAvailableDetails>(details).ptr(); | 123 Details<const TokenService::TokenAvailableDetails>(details).ptr(); |
| 139 if (token_details->service() == GaiaConstants::kDeviceManagementService) | 124 if (token_details->service() == GaiaConstants::kDeviceManagementService) { |
| 140 if (device_token_.empty()) { | 125 if (device_token_.empty()) { |
| 141 // Request a new device management server token, but only in case we | 126 // Request a new device management server token, but only in case we |
| 142 // don't already have it. | 127 // don't already have it. |
| 143 CheckAndTriggerFetch(); | 128 CheckAndTriggerFetch(); |
| 144 } | 129 } |
| 130 } |
| 145 } | 131 } |
| 146 #if defined(OS_CHROMEOS) | |
| 147 } else if (type == NotificationType::LOGIN_USER_CHANGED) { | 132 } else if (type == NotificationType::LOGIN_USER_CHANGED) { |
| 148 CheckAndTriggerFetch(); | 133 CheckAndTriggerFetch(); |
| 149 #else | |
| 150 } else if (type == NotificationType::GOOGLE_SIGNIN_SUCCESSFUL) { | |
| 151 if (profile_ == Source<Profile>(source).ptr()) | |
| 152 CheckAndTriggerFetch(); | |
| 153 #endif | |
| 154 } else { | 134 } else { |
| 155 NOTREACHED(); | 135 NOTREACHED(); |
| 156 } | 136 } |
| 157 } | 137 } |
| 158 | 138 |
| 159 } // namespace policy | 139 } // namespace policy |
| OLD | NEW |