Home | History | Annotate | Download | only in policy
      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
      3 // found in the LICENSE file.
      4 
      5 #ifndef CHROME_BROWSER_CHROMEOS_POLICY_POLICY_OAUTH2_TOKEN_FETCHER_H_
      6 #define CHROME_BROWSER_CHROMEOS_POLICY_POLICY_OAUTH2_TOKEN_FETCHER_H_
      7 
      8 #include <string>
      9 
     10 #include "base/basictypes.h"
     11 #include "base/callback.h"
     12 #include "base/compiler_specific.h"
     13 #include "base/memory/ref_counted.h"
     14 #include "base/memory/scoped_ptr.h"
     15 #include "base/memory/weak_ptr.h"
     16 #include "google_apis/gaia/gaia_auth_consumer.h"
     17 #include "google_apis/gaia/oauth2_access_token_consumer.h"
     18 
     19 class GaiaAuthFetcher;
     20 class OAuth2AccessTokenFetcher;
     21 
     22 namespace net {
     23 class URLRequestContextGetter;
     24 }
     25 
     26 namespace policy {
     27 
     28 // 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
     30 // 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
     32 // Profile is pending is also OK.
     33 class PolicyOAuth2TokenFetcher
     34     : public base::SupportsWeakPtr<PolicyOAuth2TokenFetcher>,
     35       public GaiaAuthConsumer,
     36       public OAuth2AccessTokenConsumer {
     37  public:
     38   typedef base::Callback<void(const std::string&,
     39                               const GoogleServiceAuthError&)> TokenCallback;
     40 
     41   // Fetches the device management service's oauth2 token, after also retrieving
     42   // the OAuth2 refresh tokens.
     43   PolicyOAuth2TokenFetcher(net::URLRequestContextGetter* auth_context_getter,
     44                            net::URLRequestContextGetter* system_context_getter,
     45                            const TokenCallback& callback);
     46 
     47   virtual ~PolicyOAuth2TokenFetcher();
     48 
     49   // Starts process of minting device management service OAuth2 access token.
     50   void Start();
     51 
     52   // Returns true if we have previously attempted to fetch tokens with this
     53   // class and failed.
     54   bool failed() const {
     55     return failed_;
     56   }
     57 
     58   const std::string& oauth2_refresh_token() const {
     59     return oauth2_refresh_token_;
     60   }
     61   const std::string& oauth2_access_token() const {
     62     return oauth2_access_token_;
     63   }
     64 
     65  private:
     66   // GaiaAuthConsumer overrides.
     67   virtual void OnClientOAuthSuccess(
     68       const GaiaAuthConsumer::ClientOAuthResult& oauth_tokens) OVERRIDE;
     69   virtual void OnClientOAuthFailure(
     70       const GoogleServiceAuthError& error) OVERRIDE;
     71 
     72   // OAuth2AccessTokenConsumer overrides.
     73   virtual void OnGetTokenSuccess(const std::string& access_token,
     74                                  const base::Time& expiration_time) OVERRIDE;
     75   virtual void OnGetTokenFailure(const GoogleServiceAuthError& error) OVERRIDE;
     76 
     77   // Starts fetching OAuth2 refresh token.
     78   void StartFetchingRefreshToken();
     79 
     80   // Starts fetching OAuth2 access token for the device management service.
     81   void StartFetchingAccessToken();
     82 
     83   // Decides how to proceed on GAIA |error|. If the error looks temporary,
     84   // retries |task| until max retry count is reached.
     85   // If retry count runs out, or error condition is unrecoverable, it calls
     86   // Delegate::OnOAuth2TokenFetchFailed().
     87   void RetryOnError(const GoogleServiceAuthError& error,
     88                     const base::Closure& task);
     89 
     90   // Passes |token| and |error| to the |callback_|.
     91   void ForwardPolicyToken(const std::string& token,
     92                           const GoogleServiceAuthError& error);
     93 
     94   scoped_refptr<net::URLRequestContextGetter> auth_context_getter_;
     95   scoped_refptr<net::URLRequestContextGetter> system_context_getter_;
     96   scoped_ptr<GaiaAuthFetcher> refresh_token_fetcher_;
     97   scoped_ptr<OAuth2AccessTokenFetcher> access_token_fetcher_;
     98 
     99   // OAuth2 refresh token. Could come either from the outside or through
    100   // refresh token fetching flow within this class.
    101   std::string oauth2_refresh_token_;
    102 
    103   // OAuth2 access token.
    104   std::string oauth2_access_token_;
    105 
    106   // The retry counter. Increment this only when failure happened.
    107   int retry_count_;
    108 
    109   // True if we have already failed to fetch the policy.
    110   bool failed_;
    111 
    112   // The callback to invoke when done.
    113   TokenCallback callback_;
    114 
    115   DISALLOW_COPY_AND_ASSIGN(PolicyOAuth2TokenFetcher);
    116 };
    117 
    118 }  // namespace policy
    119 
    120 #endif  // CHROME_BROWSER_CHROMEOS_POLICY_POLICY_OAUTH2_TOKEN_FETCHER_H_
    121