OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef GOOGLE_APIS_GAIA_UBERTOKEN_FETCHER_H_ | 5 #ifndef GOOGLE_APIS_GAIA_UBERTOKEN_FETCHER_H_ |
6 #define GOOGLE_APIS_GAIA_UBERTOKEN_FETCHER_H_ | 6 #define GOOGLE_APIS_GAIA_UBERTOKEN_FETCHER_H_ |
7 | 7 |
8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
| 9 #include "base/timer/timer.h" |
9 #include "google_apis/gaia/gaia_auth_consumer.h" | 10 #include "google_apis/gaia/gaia_auth_consumer.h" |
10 #include "google_apis/gaia/oauth2_token_service.h" | 11 #include "google_apis/gaia/oauth2_token_service.h" |
11 | 12 |
12 // Allow to retrieves an uber-auth token for the user. This class uses the | 13 // Allow to retrieves an uber-auth token for the user. This class uses the |
13 // |OAuth2TokenService| and considers that the user is already logged in. It | 14 // |OAuth2TokenService| and considers that the user is already logged in. It |
14 // will use the OAuth2 access token to generate the uber-auth token. | 15 // will use the OAuth2 access token to generate the uber-auth token. |
15 // | 16 // |
16 // This class should be used on a single thread, but it can be whichever thread | 17 // This class should be used on a single thread, but it can be whichever thread |
17 // that you like. | 18 // that you like. |
18 // | 19 // |
(...skipping 12 matching lines...) Expand all Loading... |
31 UbertokenConsumer() {} | 32 UbertokenConsumer() {} |
32 virtual ~UbertokenConsumer() {} | 33 virtual ~UbertokenConsumer() {} |
33 virtual void OnUbertokenSuccess(const std::string& token) {} | 34 virtual void OnUbertokenSuccess(const std::string& token) {} |
34 virtual void OnUbertokenFailure(const GoogleServiceAuthError& error) {} | 35 virtual void OnUbertokenFailure(const GoogleServiceAuthError& error) {} |
35 }; | 36 }; |
36 | 37 |
37 // Allows to retrieve an uber-auth token. | 38 // Allows to retrieve an uber-auth token. |
38 class UbertokenFetcher : public GaiaAuthConsumer, | 39 class UbertokenFetcher : public GaiaAuthConsumer, |
39 public OAuth2TokenService::Consumer { | 40 public OAuth2TokenService::Consumer { |
40 public: | 41 public: |
| 42 // Maximum number of retries to get the uber-auth token before giving up. |
| 43 static const int kMaxRetries; |
| 44 |
41 UbertokenFetcher(OAuth2TokenService* token_service, | 45 UbertokenFetcher(OAuth2TokenService* token_service, |
42 UbertokenConsumer* consumer, | 46 UbertokenConsumer* consumer, |
43 net::URLRequestContextGetter* request_context); | 47 net::URLRequestContextGetter* request_context); |
44 virtual ~UbertokenFetcher(); | 48 virtual ~UbertokenFetcher(); |
45 | 49 |
46 // Start fetching the token for |account_id|. | 50 // Start fetching the token for |account_id|. |
47 virtual void StartFetchingToken(const std::string& account_id); | 51 virtual void StartFetchingToken(const std::string& account_id); |
48 | 52 |
49 // Overriden from GaiaAuthConsumer | 53 // Overriden from GaiaAuthConsumer |
50 virtual void OnUberAuthTokenSuccess(const std::string& token) OVERRIDE; | 54 virtual void OnUberAuthTokenSuccess(const std::string& token) OVERRIDE; |
51 virtual void OnUberAuthTokenFailure( | 55 virtual void OnUberAuthTokenFailure( |
52 const GoogleServiceAuthError& error) OVERRIDE; | 56 const GoogleServiceAuthError& error) OVERRIDE; |
53 | 57 |
54 // Overriden from OAuth2TokenService::Consumer: | 58 // Overriden from OAuth2TokenService::Consumer: |
55 virtual void OnGetTokenSuccess(const OAuth2TokenService::Request* request, | 59 virtual void OnGetTokenSuccess(const OAuth2TokenService::Request* request, |
56 const std::string& access_token, | 60 const std::string& access_token, |
57 const base::Time& expiration_time) OVERRIDE; | 61 const base::Time& expiration_time) OVERRIDE; |
58 virtual void OnGetTokenFailure(const OAuth2TokenService::Request* request, | 62 virtual void OnGetTokenFailure(const OAuth2TokenService::Request* request, |
59 const GoogleServiceAuthError& error) OVERRIDE; | 63 const GoogleServiceAuthError& error) OVERRIDE; |
60 | 64 |
61 private: | 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 |
62 OAuth2TokenService* token_service_; | 72 OAuth2TokenService* token_service_; |
63 UbertokenConsumer* consumer_; | 73 UbertokenConsumer* consumer_; |
64 net::URLRequestContextGetter* request_context_; | 74 net::URLRequestContextGetter* request_context_; |
65 scoped_ptr<GaiaAuthFetcher> gaia_auth_fetcher_; | 75 scoped_ptr<GaiaAuthFetcher> gaia_auth_fetcher_; |
66 scoped_ptr<OAuth2TokenService::Request> access_token_request_; | 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_; |
67 | 82 |
68 DISALLOW_COPY_AND_ASSIGN(UbertokenFetcher); | 83 DISALLOW_COPY_AND_ASSIGN(UbertokenFetcher); |
69 }; | 84 }; |
70 | 85 |
71 #endif // GOOGLE_APIS_GAIA_UBERTOKEN_FETCHER_H_ | 86 #endif // GOOGLE_APIS_GAIA_UBERTOKEN_FETCHER_H_ |
OLD | NEW |