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