1// Copyright 2014 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 GOOGLE_APIS_GAIA_UBERTOKEN_FETCHER_H_
6#define GOOGLE_APIS_GAIA_UBERTOKEN_FETCHER_H_
7
8#include "base/memory/scoped_ptr.h"
9#include "base/timer/timer.h"
10#include "google_apis/gaia/gaia_auth_consumer.h"
11#include "google_apis/gaia/oauth2_token_service.h"
12
13// Allow to retrieves an uber-auth token for the user. This class uses the
14// |OAuth2TokenService| and considers that the user is already logged in. It
15// will use the OAuth2 access token to generate the uber-auth token.
16//
17// This class should be used on a single thread, but it can be whichever thread
18// that you like.
19//
20// This class can handle one request at a time.
21
22class GaiaAuthFetcher;
23class GoogleServiceAuthError;
24
25namespace net {
26class URLRequestContextGetter;
27}
28
29// Callback for the |UbertokenFetcher| class.
30class UbertokenConsumer {
31 public:
32  UbertokenConsumer() {}
33  virtual ~UbertokenConsumer() {}
34  virtual void OnUbertokenSuccess(const std::string& token) {}
35  virtual void OnUbertokenFailure(const GoogleServiceAuthError& error) {}
36};
37
38// Allows to retrieve an uber-auth token.
39class UbertokenFetcher : public GaiaAuthConsumer,
40                         public OAuth2TokenService::Consumer {
41 public:
42  // Maximum number of retries to get the uber-auth token before giving up.
43  static const int kMaxRetries;
44
45  UbertokenFetcher(OAuth2TokenService* token_service,
46                   UbertokenConsumer* consumer,
47                   net::URLRequestContextGetter* request_context);
48  virtual ~UbertokenFetcher();
49
50  // Start fetching the token for |account_id|.
51  virtual void StartFetchingToken(const std::string& account_id);
52
53  // Overriden from GaiaAuthConsumer
54  virtual void OnUberAuthTokenSuccess(const std::string& token) OVERRIDE;
55  virtual void OnUberAuthTokenFailure(
56      const GoogleServiceAuthError& error) OVERRIDE;
57
58  // Overriden from OAuth2TokenService::Consumer:
59  virtual void OnGetTokenSuccess(const OAuth2TokenService::Request* request,
60                                 const std::string& access_token,
61                                 const base::Time& expiration_time) OVERRIDE;
62  virtual void OnGetTokenFailure(const OAuth2TokenService::Request* request,
63                                 const GoogleServiceAuthError& error) OVERRIDE;
64
65 private:
66  // Request a login-scoped access token from the token service.
67  void RequestAccessToken();
68
69  // Exchanges an oauth2 access token for an uber-auth token.
70  void ExchangeTokens();
71
72  OAuth2TokenService* token_service_;
73  UbertokenConsumer* consumer_;
74  net::URLRequestContextGetter* request_context_;
75  scoped_ptr<GaiaAuthFetcher> gaia_auth_fetcher_;
76  scoped_ptr<OAuth2TokenService::Request> access_token_request_;
77  std::string account_id_;
78  std::string access_token_;
79  int retry_number_;
80  base::OneShotTimer<UbertokenFetcher> retry_timer_;
81  bool second_access_token_request_;
82
83  DISALLOW_COPY_AND_ASSIGN(UbertokenFetcher);
84};
85
86#endif  // GOOGLE_APIS_GAIA_UBERTOKEN_FETCHER_H_
87