summaryrefslogtreecommitdiffstats
path: root/google_apis/gaia/ubertoken_fetcher.h
blob: 5ff73bd41a0ae787a9a8ce3f09a867381ae2c915 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef GOOGLE_APIS_GAIA_UBERTOKEN_FETCHER_H_
#define GOOGLE_APIS_GAIA_UBERTOKEN_FETCHER_H_

#include "base/macros.h"
#include "base/memory/scoped_ptr.h"
#include "base/timer/timer.h"
#include "google_apis/gaia/gaia_auth_consumer.h"
#include "google_apis/gaia/oauth2_token_service.h"

// Allow to retrieves an uber-auth token for the user. This class uses the
// |OAuth2TokenService| and considers that the user is already logged in. It
// will use the OAuth2 access token to generate the uber-auth token.
//
// This class should be used on a single thread, but it can be whichever thread
// that you like.
//
// This class can handle one request at a time.

class GaiaAuthFetcher;
class GoogleServiceAuthError;

namespace net {
class URLRequestContextGetter;
}

typedef base::Callback<GaiaAuthFetcher*(GaiaAuthConsumer*,
                                        const std::string&,
                                        net::URLRequestContextGetter*)>
    GaiaAuthFetcherFactory;

// Callback for the |UbertokenFetcher| class.
class UbertokenConsumer {
 public:
  UbertokenConsumer() {}
  virtual ~UbertokenConsumer() {}
  virtual void OnUbertokenSuccess(const std::string& token) {}
  virtual void OnUbertokenFailure(const GoogleServiceAuthError& error) {}
};

// Allows to retrieve an uber-auth token.
class UbertokenFetcher : public GaiaAuthConsumer,
                         public OAuth2TokenService::Consumer {
 public:
  // Maximum number of retries to get the uber-auth token before giving up.
  static const int kMaxRetries;

  UbertokenFetcher(OAuth2TokenService* token_service,
                   UbertokenConsumer* consumer,
                   const std::string& source,
                   net::URLRequestContextGetter* request_context);
  UbertokenFetcher(OAuth2TokenService* token_service,
                   UbertokenConsumer* consumer,
                   const std::string& source,
                   net::URLRequestContextGetter* request_context,
                   GaiaAuthFetcherFactory factory);
  ~UbertokenFetcher() override;

  // Start fetching the token for |account_id|.
  virtual void StartFetchingToken(const std::string& account_id);
  virtual void StartFetchingTokenWithAccessToken(const std::string& account_id,
      const std::string& access_token);

  // Overriden from GaiaAuthConsumer
  void OnUberAuthTokenSuccess(const std::string& token) override;
  void OnUberAuthTokenFailure(const GoogleServiceAuthError& error) override;

  // Overriden from OAuth2TokenService::Consumer:
  void OnGetTokenSuccess(const OAuth2TokenService::Request* request,
                         const std::string& access_token,
                         const base::Time& expiration_time) override;
  void OnGetTokenFailure(const OAuth2TokenService::Request* request,
                         const GoogleServiceAuthError& error) override;

 private:
  // Request a login-scoped access token from the token service.
  void RequestAccessToken();

  // Exchanges an oauth2 access token for an uber-auth token.
  void ExchangeTokens();

  OAuth2TokenService* token_service_;
  UbertokenConsumer* consumer_;
  std::string source_;
  net::URLRequestContextGetter* request_context_;
  GaiaAuthFetcherFactory gaia_auth_fetcher_factory_;
  scoped_ptr<GaiaAuthFetcher> gaia_auth_fetcher_;
  scoped_ptr<OAuth2TokenService::Request> access_token_request_;
  std::string account_id_;
  std::string access_token_;
  int retry_number_;
  base::OneShotTimer retry_timer_;
  bool second_access_token_request_;

  DISALLOW_COPY_AND_ASSIGN(UbertokenFetcher);
};

#endif  // GOOGLE_APIS_GAIA_UBERTOKEN_FETCHER_H_