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.
5 #ifndef GOOGLE_APIS_GAIA_UBERTOKEN_FETCHER_H_
6 #define GOOGLE_APIS_GAIA_UBERTOKEN_FETCHER_H_
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"
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.
17 // This class should be used on a single thread, but it can be whichever thread
20 // This class can handle one request at a time.
22 class GaiaAuthFetcher
;
23 class GoogleServiceAuthError
;
26 class URLRequestContextGetter
;
29 typedef base::Callback
<GaiaAuthFetcher
*(GaiaAuthConsumer
*,
31 net::URLRequestContextGetter
*)>
32 GaiaAuthFetcherFactory
;
34 // Callback for the |UbertokenFetcher| class.
35 class UbertokenConsumer
{
37 UbertokenConsumer() {}
38 virtual ~UbertokenConsumer() {}
39 virtual void OnUbertokenSuccess(const std::string
& token
) {}
40 virtual void OnUbertokenFailure(const GoogleServiceAuthError
& error
) {}
43 // Allows to retrieve an uber-auth token.
44 class UbertokenFetcher
: public GaiaAuthConsumer
,
45 public OAuth2TokenService::Consumer
{
47 // Maximum number of retries to get the uber-auth token before giving up.
48 static const int kMaxRetries
;
50 UbertokenFetcher(OAuth2TokenService
* token_service
,
51 UbertokenConsumer
* consumer
,
52 const std::string
& source
,
53 net::URLRequestContextGetter
* request_context
);
54 UbertokenFetcher(OAuth2TokenService
* token_service
,
55 UbertokenConsumer
* consumer
,
56 const std::string
& source
,
57 net::URLRequestContextGetter
* request_context
,
58 GaiaAuthFetcherFactory factory
);
59 ~UbertokenFetcher() override
;
61 // Start fetching the token for |account_id|.
62 virtual void StartFetchingToken(const std::string
& account_id
);
63 virtual void StartFetchingTokenWithAccessToken(const std::string
& account_id
,
64 const std::string
& access_token
);
66 // Overriden from GaiaAuthConsumer
67 void OnUberAuthTokenSuccess(const std::string
& token
) override
;
68 void OnUberAuthTokenFailure(const GoogleServiceAuthError
& error
) override
;
70 // Overriden from OAuth2TokenService::Consumer:
71 void OnGetTokenSuccess(const OAuth2TokenService::Request
* request
,
72 const std::string
& access_token
,
73 const base::Time
& expiration_time
) override
;
74 void OnGetTokenFailure(const OAuth2TokenService::Request
* request
,
75 const GoogleServiceAuthError
& error
) override
;
78 // Request a login-scoped access token from the token service.
79 void RequestAccessToken();
81 // Exchanges an oauth2 access token for an uber-auth token.
82 void ExchangeTokens();
84 OAuth2TokenService
* token_service_
;
85 UbertokenConsumer
* consumer_
;
87 net::URLRequestContextGetter
* request_context_
;
88 GaiaAuthFetcherFactory gaia_auth_fetcher_factory_
;
89 scoped_ptr
<GaiaAuthFetcher
> gaia_auth_fetcher_
;
90 scoped_ptr
<OAuth2TokenService::Request
> access_token_request_
;
91 std::string account_id_
;
92 std::string access_token_
;
94 base::OneShotTimer
<UbertokenFetcher
> retry_timer_
;
95 bool second_access_token_request_
;
97 DISALLOW_COPY_AND_ASSIGN(UbertokenFetcher
);
100 #endif // GOOGLE_APIS_GAIA_UBERTOKEN_FETCHER_H_