1 // Copyright 2015 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.
5 #ifndef CHROME_BROWSER_CHROMEOS_SETTINGS_DEVICE_OAUTH2_TOKEN_SERVICE_DELEGATE_H_
6 #define CHROME_BROWSER_CHROMEOS_SETTINGS_DEVICE_OAUTH2_TOKEN_SERVICE_DELEGATE_H_
11 #include "base/basictypes.h"
12 #include "base/callback.h"
13 #include "base/gtest_prod_util.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/memory/weak_ptr.h"
16 #include "base/stl_util.h"
17 #include "chrome/browser/chromeos/settings/cros_settings.h"
18 #include "google_apis/gaia/gaia_oauth_client.h"
19 #include "google_apis/gaia/oauth2_token_service_delegate.h"
20 #include "net/url_request/url_request_context_getter.h"
23 class GaiaOAuthClient
;
27 class URLRequestContextGetter
;
34 class DeviceOAuth2TokenServiceDelegate
35 : public OAuth2TokenServiceDelegate
,
36 public gaia::GaiaOAuthClient::Delegate
{
38 DeviceOAuth2TokenServiceDelegate(net::URLRequestContextGetter
* getter
,
39 PrefService
* local_state
);
40 ~DeviceOAuth2TokenServiceDelegate() override
;
42 typedef base::Callback
<void(bool)> StatusCallback
;
44 // Persist the given refresh token on the device. Overwrites any previous
45 // value. Should only be called during initial device setup. Signals
46 // completion via the given callback, passing true if the operation succeeded.
47 void SetAndSaveRefreshToken(const std::string
& refresh_token
,
48 const StatusCallback
& callback
);
50 // Pull the robot account ID from device policy.
51 std::string
GetRobotAccountId() const;
53 // Implementation of OAuth2TokenServiceDelegate.
54 bool RefreshTokenIsAvailable(const std::string
& account_id
) const override
;
56 net::URLRequestContextGetter
* GetRequestContext() const override
;
58 OAuth2AccessTokenFetcher
* CreateAccessTokenFetcher(
59 const std::string
& account_id
,
60 net::URLRequestContextGetter
* getter
,
61 OAuth2AccessTokenConsumer
* consumer
) override
;
63 // gaia::GaiaOAuthClient::Delegate implementation.
64 void OnRefreshTokenResponse(const std::string
& access_token
,
65 int expires_in_seconds
) override
;
66 void OnGetTokenInfoResponse(
67 scoped_ptr
<base::DictionaryValue
> token_info
) override
;
68 void OnOAuthError() override
;
69 void OnNetworkError(int response_code
) override
;
72 friend class DeviceOAuth2TokenService
;
73 friend class DeviceOAuth2TokenServiceTest
;
75 class ValidationStatusDelegate
{
77 virtual void OnValidationCompleted(GoogleServiceAuthError::State error
) {}
80 // Describes the operational state of this object.
82 // Pending system salt / refresh token load.
84 // No token available.
86 // System salt loaded, validation not started yet.
87 STATE_VALIDATION_PENDING
,
88 // Refresh token validation underway.
89 STATE_VALIDATION_STARTED
,
90 // Token validation failed.
92 // Refresh token is valid.
96 // Invoked by CrosSettings when the robot account ID becomes available.
97 void OnServiceAccountIdentityChanged();
99 // Returns the refresh token for account_id.
100 std::string
GetRefreshToken(const std::string
& account_id
) const;
102 // Handles completion of the system salt input.
103 void DidGetSystemSalt(const std::string
& system_salt
);
105 // Checks whether |gaia_robot_id| matches the expected account ID indicated in
107 void CheckRobotAccountId(const std::string
& gaia_robot_id
);
109 // Encrypts and saves the refresh token. Should only be called when the system
110 // salt is available.
111 void EncryptAndSaveToken();
113 // Starts the token validation flow, i.e. token info fetch.
114 void StartValidation();
116 // Flushes |token_save_callbacks_|, indicating the specified result.
117 void FlushTokenSaveCallbacks(bool result
);
119 void RequestValidation();
121 void SetValidationStatusDelegate(ValidationStatusDelegate
* delegate
);
123 void ReportServiceError(GoogleServiceAuthError::State error
);
126 scoped_refptr
<net::URLRequestContextGetter
> url_request_context_getter_
;
127 PrefService
* local_state_
;
129 // Current operational state.
132 // Token save callbacks waiting to be completed.
133 std::vector
<StatusCallback
> token_save_callbacks_
;
135 // The system salt for encrypting and decrypting the refresh token.
136 std::string system_salt_
;
138 int max_refresh_token_validation_retries_
;
140 // Flag to indicate whether there are pending requests.
141 bool validation_requested_
;
143 // Validation status delegate
144 ValidationStatusDelegate
* validation_status_delegate_
;
146 // Cache the decrypted refresh token, so we only decrypt once.
147 std::string refresh_token_
;
149 scoped_ptr
<gaia::GaiaOAuthClient
> gaia_oauth_client_
;
151 scoped_ptr
<CrosSettings::ObserverSubscription
>
152 service_account_identity_subscription_
;
154 base::WeakPtrFactory
<DeviceOAuth2TokenServiceDelegate
> weak_ptr_factory_
;
156 DISALLOW_COPY_AND_ASSIGN(DeviceOAuth2TokenServiceDelegate
);
159 } // namespace chromeos
161 #endif // CHROME_BROWSER_CHROMEOS_SETTINGS_DEVICE_OAUTH2_TOKEN_SERVICE_DELEGATE_H_