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 // Callback for the |UbertokenFetcher| class.
30 class UbertokenConsumer
{
32 UbertokenConsumer() {}
33 virtual ~UbertokenConsumer() {}
34 virtual void OnUbertokenSuccess(const std::string
& token
) {}
35 virtual void OnUbertokenFailure(const GoogleServiceAuthError
& error
) {}
38 // Allows to retrieve an uber-auth token.
39 class UbertokenFetcher
: public GaiaAuthConsumer
,
40 public OAuth2TokenService::Consumer
{
42 // Maximum number of retries to get the uber-auth token before giving up.
43 static const int kMaxRetries
;
45 UbertokenFetcher(OAuth2TokenService
* token_service
,
46 UbertokenConsumer
* consumer
,
47 const std::string
& source
,
48 net::URLRequestContextGetter
* request_context
);
49 ~UbertokenFetcher() override
;
51 // Start fetching the token for |account_id|.
52 virtual void StartFetchingToken(const std::string
& account_id
);
54 // Overriden from GaiaAuthConsumer
55 void OnUberAuthTokenSuccess(const std::string
& token
) override
;
56 void OnUberAuthTokenFailure(const GoogleServiceAuthError
& error
) override
;
58 // Overriden from OAuth2TokenService::Consumer:
59 void OnGetTokenSuccess(const OAuth2TokenService::Request
* request
,
60 const std::string
& access_token
,
61 const base::Time
& expiration_time
) override
;
62 void OnGetTokenFailure(const OAuth2TokenService::Request
* request
,
63 const GoogleServiceAuthError
& error
) override
;
66 // Request a login-scoped access token from the token service.
67 void RequestAccessToken();
69 // Exchanges an oauth2 access token for an uber-auth token.
70 void ExchangeTokens();
72 OAuth2TokenService
* token_service_
;
73 UbertokenConsumer
* consumer_
;
75 net::URLRequestContextGetter
* request_context_
;
76 scoped_ptr
<GaiaAuthFetcher
> gaia_auth_fetcher_
;
77 scoped_ptr
<OAuth2TokenService::Request
> access_token_request_
;
78 std::string account_id_
;
79 std::string access_token_
;
81 base::OneShotTimer
<UbertokenFetcher
> retry_timer_
;
82 bool second_access_token_request_
;
84 DISALLOW_COPY_AND_ASSIGN(UbertokenFetcher
);
87 #endif // GOOGLE_APIS_GAIA_UBERTOKEN_FETCHER_H_