policy_oauth2_token_fetcher.h revision 558790d6acca3451cf3a6b497803a5f07d0bec58
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
19class GaiaAuthFetcher;
20class OAuth2AccessTokenFetcher;
21
22namespace net {
23class URLRequestContextGetter;
24}
25
26namespace 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.
33class 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 using
42  // |oauth2_tokens.refresh_token|.
43  PolicyOAuth2TokenFetcher(net::URLRequestContextGetter* system_context_getter,
44                           const std::string& oauth2_refresh_token,
45                           const TokenCallback& callback);
46
47  // Fetches the device management service's oauth2 token, after also retrieving
48  // the OAuth2 refresh tokens.
49  PolicyOAuth2TokenFetcher(net::URLRequestContextGetter* auth_context_getter,
50                           net::URLRequestContextGetter* system_context_getter,
51                           const TokenCallback& callback);
52
53  virtual ~PolicyOAuth2TokenFetcher();
54
55  // Starts process of minting device management service OAuth2 access token.
56  void Start();
57
58  // Returns OAuth2 tokens fetched through an authenticated cookie jar.
59  const GaiaAuthConsumer::ClientOAuthResult& oauth2_tokens() const {
60    return oauth2_tokens_;
61  }
62
63  // True if we have OAuth2 tokens that were fetched through an authenticated
64  // cookie jar.
65  bool has_oauth2_tokens() const {
66    return !oauth2_tokens_.refresh_token.empty();
67  }
68
69  // Returns true if we have previously attempted to fetch tokens with this
70  // class and failed.
71  bool failed() const {
72    return failed_;
73  }
74
75  const std::string& oauth2_refresh_token() const {
76    return oauth2_refresh_token_;
77  }
78  const std::string& oauth2_access_token() const {
79    return oauth2_access_token_;
80  }
81
82 private:
83  // GaiaAuthConsumer overrides.
84  virtual void OnClientOAuthSuccess(
85      const GaiaAuthConsumer::ClientOAuthResult& oauth_tokens) OVERRIDE;
86  virtual void OnClientOAuthFailure(
87      const GoogleServiceAuthError& error) OVERRIDE;
88
89  // OAuth2AccessTokenConsumer overrides.
90  virtual void OnGetTokenSuccess(const std::string& access_token,
91                                 const base::Time& expiration_time) OVERRIDE;
92  virtual void OnGetTokenFailure(const GoogleServiceAuthError& error) OVERRIDE;
93
94  // Starts fetching OAuth2 refresh token.
95  void StartFetchingRefreshToken();
96
97  // Starts fetching OAuth2 access token for the device management service.
98  void StartFetchingAccessToken();
99
100  // Decides how to proceed on GAIA |error|. If the error looks temporary,
101  // retries |task| until max retry count is reached.
102  // If retry count runs out, or error condition is unrecoverable, it calls
103  // Delegate::OnOAuth2TokenFetchFailed().
104  void RetryOnError(const GoogleServiceAuthError& error,
105                    const base::Closure& task);
106
107  // Passes |token| and |error| to the |callback_|.
108  void ForwardPolicyToken(const std::string& token,
109                          const GoogleServiceAuthError& error);
110
111  scoped_refptr<net::URLRequestContextGetter> auth_context_getter_;
112  scoped_refptr<net::URLRequestContextGetter> system_context_getter_;
113  scoped_ptr<GaiaAuthFetcher> refresh_token_fetcher_;
114  scoped_ptr<OAuth2AccessTokenFetcher> access_token_fetcher_;
115  GaiaAuthConsumer::ClientOAuthResult oauth2_tokens_;
116
117  // OAuth2 refresh token. Could come either from the outside or through
118  // refresh token fetching flow within this class.
119  std::string oauth2_refresh_token_;
120
121  // OAuth2 access token.
122  std::string oauth2_access_token_;
123
124  // The retry counter. Increment this only when failure happened.
125  int retry_count_;
126
127  // True if we have already failed to fetch the policy.
128  bool failed_;
129
130  // The callback to invoke when done.
131  TokenCallback callback_;
132
133  DISALLOW_COPY_AND_ASSIGN(PolicyOAuth2TokenFetcher);
134};
135
136}  // namespace policy
137
138#endif  // CHROME_BROWSER_CHROMEOS_POLICY_POLICY_OAUTH2_TOKEN_FETCHER_H_
139