| OLD | NEW | 
|    1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |    1 // Copyright (c) 2013 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 #ifndef CHROME_BROWSER_CHROMEOS_POLICY_POLICY_OAUTH2_TOKEN_FETCHER_H_ |    5 #ifndef CHROME_BROWSER_CHROMEOS_POLICY_POLICY_OAUTH2_TOKEN_FETCHER_H_ | 
|    6 #define CHROME_BROWSER_CHROMEOS_POLICY_POLICY_OAUTH2_TOKEN_FETCHER_H_ |    6 #define CHROME_BROWSER_CHROMEOS_POLICY_POLICY_OAUTH2_TOKEN_FETCHER_H_ | 
|    7  |    7  | 
|    8 #include <memory> |    8 #include <memory> | 
|    9 #include <string> |    9 #include <string> | 
|   10  |   10  | 
|   11 #include "base/callback.h" |   11 #include "base/callback.h" | 
|   12 #include "base/compiler_specific.h" |  | 
|   13 #include "base/macros.h" |   12 #include "base/macros.h" | 
|   14 #include "base/memory/ref_counted.h" |  | 
|   15 #include "base/memory/weak_ptr.h" |  | 
|   16 #include "google_apis/gaia/gaia_auth_consumer.h" |   13 #include "google_apis/gaia/gaia_auth_consumer.h" | 
|   17 #include "google_apis/gaia/oauth2_access_token_consumer.h" |  | 
|   18  |   14  | 
|   19 class GaiaAuthFetcher; |   15 class GaiaAuthFetcher; | 
|   20 class OAuth2AccessTokenFetcher; |   16 class OAuth2AccessTokenFetcher; | 
|   21  |   17  | 
|   22 namespace net { |   18 namespace net { | 
|   23 class URLRequestContextGetter; |   19 class URLRequestContextGetter; | 
|   24 } |   20 } | 
|   25  |   21  | 
|   26 namespace policy { |   22 namespace policy { | 
|   27  |   23  | 
|   28 // Fetches the OAuth2 token for the device management service. Since Profile |   24 // Fetches the OAuth2 token for the device management service. Since Profile | 
|   29 // creation might be blocking on a user policy fetch, this fetcher must always |   25 // creation might be blocking on a user policy fetch, this fetcher must always | 
|   30 // send a (possibly empty) token to the callback, which will then let the policy |   26 // send a (possibly empty) token to the callback, which will then let the policy | 
|   31 // subsystem proceed and resume Profile creation. Sending the token even when no |   27 // subsystem proceed and resume Profile creation. Sending the token even when no | 
|   32 // Profile is pending is also OK. |   28 // Profile is pending is also OK. | 
|   33 class PolicyOAuth2TokenFetcher |   29 class PolicyOAuth2TokenFetcher { | 
|   34     : public base::SupportsWeakPtr<PolicyOAuth2TokenFetcher>, |  | 
|   35       public GaiaAuthConsumer, |  | 
|   36       public OAuth2AccessTokenConsumer { |  | 
|   37  public: |   30  public: | 
|   38   typedef base::Callback<void(const std::string&, |   31   // Allocates a PolicyOAuth2TokenFetcher instance. | 
|   39                               const GoogleServiceAuthError&)> TokenCallback; |   32   static PolicyOAuth2TokenFetcher* CreateInstance(); | 
 |   33  | 
 |   34   // Makes CreateInstance() return a fake token fetcher that does not make | 
 |   35   // network calls so tests can avoid a dependency on GAIA. | 
 |   36   static void UseFakeTokensForTesting(); | 
 |   37  | 
 |   38   using TokenCallback = | 
 |   39       base::Callback<void(const std::string&, const GoogleServiceAuthError&)>; | 
|   40  |   40  | 
|   41   PolicyOAuth2TokenFetcher(); |   41   PolicyOAuth2TokenFetcher(); | 
|   42   ~PolicyOAuth2TokenFetcher() override; |   42   virtual ~PolicyOAuth2TokenFetcher(); | 
|   43  |   43  | 
|   44   // Fetches the device management service's oauth2 token. This may be fetched |   44   // Fetches the device management service's oauth2 token. This may be fetched | 
|   45   // via signin context, auth code, or oauth2 refresh token. |   45   // via signin context, auth code, or oauth2 refresh token. | 
|   46   void StartWithSigninContext( |   46   virtual void StartWithSigninContext( | 
|   47       net::URLRequestContextGetter* auth_context_getter, |   47       net::URLRequestContextGetter* auth_context_getter, | 
|   48       net::URLRequestContextGetter* system_context_getter, |   48       net::URLRequestContextGetter* system_context_getter, | 
|   49       const TokenCallback& callback); |   49       const TokenCallback& callback) = 0; | 
|   50   void StartWithAuthCode(const std::string& auth_code, |   50   virtual void StartWithAuthCode( | 
|   51                          net::URLRequestContextGetter* system_context_getter, |   51       const std::string& auth_code, | 
|   52                          const TokenCallback& callback); |   52       net::URLRequestContextGetter* system_context_getter, | 
|   53   void StartWithRefreshToken( |   53       const TokenCallback& callback) = 0; | 
 |   54   virtual void StartWithRefreshToken( | 
|   54       const std::string& oauth2_refresh_token, |   55       const std::string& oauth2_refresh_token, | 
|   55       net::URLRequestContextGetter* system_context_getter, |   56       net::URLRequestContextGetter* system_context_getter, | 
|   56       const TokenCallback& callback); |   57       const TokenCallback& callback) = 0; | 
|   57  |   58  | 
|   58   // Returns true if we have previously attempted to fetch tokens with this |   59   // Returns true if we have previously attempted to fetch tokens with this | 
|   59   // class and failed. |   60   // class and failed. | 
|   60   bool failed() const { |   61   virtual bool Failed() const = 0; | 
|   61     return failed_; |   62   virtual const std::string& OAuth2RefreshToken() const = 0; | 
|   62   } |   63   virtual const std::string& OAuth2AccessToken() const = 0; | 
|   63  |  | 
|   64   const std::string& oauth2_refresh_token() const { |  | 
|   65     return oauth2_refresh_token_; |  | 
|   66   } |  | 
|   67   const std::string& oauth2_access_token() const { |  | 
|   68     return oauth2_access_token_; |  | 
|   69   } |  | 
|   70  |   64  | 
|   71  private: |   65  private: | 
|   72   // GaiaAuthConsumer overrides. |  | 
|   73   void OnClientOAuthSuccess( |  | 
|   74       const GaiaAuthConsumer::ClientOAuthResult& oauth_tokens) override; |  | 
|   75   void OnClientOAuthFailure(const GoogleServiceAuthError& error) override; |  | 
|   76  |  | 
|   77   // OAuth2AccessTokenConsumer overrides. |  | 
|   78   void OnGetTokenSuccess(const std::string& access_token, |  | 
|   79                          const base::Time& expiration_time) override; |  | 
|   80   void OnGetTokenFailure(const GoogleServiceAuthError& error) override; |  | 
|   81  |  | 
|   82   // Starts fetching OAuth2 refresh token. |  | 
|   83   void StartFetchingRefreshToken(); |  | 
|   84  |  | 
|   85   // Starts fetching OAuth2 access token for the device management service. |  | 
|   86   void StartFetchingAccessToken(); |  | 
|   87  |  | 
|   88   // Decides how to proceed on GAIA |error|. If the error looks temporary, |  | 
|   89   // retries |task| until max retry count is reached. |  | 
|   90   // If retry count runs out, or error condition is unrecoverable, it calls |  | 
|   91   // Delegate::OnOAuth2TokenFetchFailed(). |  | 
|   92   void RetryOnError(const GoogleServiceAuthError& error, |  | 
|   93                     const base::Closure& task); |  | 
|   94  |  | 
|   95   // Passes |token| and |error| to the |callback_|. |  | 
|   96   void ForwardPolicyToken(const std::string& token, |  | 
|   97                           const GoogleServiceAuthError& error); |  | 
|   98  |  | 
|   99   // Auth code which is used to retreive a refresh token. |  | 
|  100   std::string auth_code_; |  | 
|  101  |  | 
|  102   scoped_refptr<net::URLRequestContextGetter> auth_context_getter_; |  | 
|  103   scoped_refptr<net::URLRequestContextGetter> system_context_getter_; |  | 
|  104   std::unique_ptr<GaiaAuthFetcher> refresh_token_fetcher_; |  | 
|  105   std::unique_ptr<OAuth2AccessTokenFetcher> access_token_fetcher_; |  | 
|  106  |  | 
|  107   // OAuth2 refresh token. Could come either from the outside or through |  | 
|  108   // refresh token fetching flow within this class. |  | 
|  109   std::string oauth2_refresh_token_; |  | 
|  110  |  | 
|  111   // OAuth2 access token. |  | 
|  112   std::string oauth2_access_token_; |  | 
|  113  |  | 
|  114   // The retry counter. Increment this only when failure happened. |  | 
|  115   int retry_count_ = 0; |  | 
|  116  |  | 
|  117   // True if we have already failed to fetch the policy. |  | 
|  118   bool failed_ = false; |  | 
|  119  |  | 
|  120   // The callback to invoke when done. |  | 
|  121   TokenCallback callback_; |  | 
|  122  |  | 
|  123   DISALLOW_COPY_AND_ASSIGN(PolicyOAuth2TokenFetcher); |   66   DISALLOW_COPY_AND_ASSIGN(PolicyOAuth2TokenFetcher); | 
|  124 }; |   67 }; | 
|  125  |   68  | 
|  126 }  // namespace policy |   69 }  // namespace policy | 
|  127  |   70  | 
|  128 #endif  // CHROME_BROWSER_CHROMEOS_POLICY_POLICY_OAUTH2_TOKEN_FETCHER_H_ |   71 #endif  // CHROME_BROWSER_CHROMEOS_POLICY_POLICY_OAUTH2_TOKEN_FETCHER_H_ | 
| OLD | NEW |