15d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Copyright 2014 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#ifndef GOOGLE_APIS_GAIA_UBERTOKEN_FETCHER_H_
65d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#define GOOGLE_APIS_GAIA_UBERTOKEN_FETCHER_H_
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/memory/scoped_ptr.h"
91320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "base/timer/timer.h"
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "google_apis/gaia/gaia_auth_consumer.h"
11424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)#include "google_apis/gaia/oauth2_token_service.h"
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Allow to retrieves an uber-auth token for the user. This class uses the
1458e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch// |OAuth2TokenService| and considers that the user is already logged in. It
1558e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch// will use the OAuth2 access token to generate the uber-auth token.
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// This class should be used on a single thread, but it can be whichever thread
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// that you like.
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// This class can handle one request at a time.
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2258e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdochclass GaiaAuthFetcher;
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class GoogleServiceAuthError;
245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)namespace net {
265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)class URLRequestContextGetter;
275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Callback for the |UbertokenFetcher| class.
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class UbertokenConsumer {
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  UbertokenConsumer() {}
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual ~UbertokenConsumer() {}
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void OnUbertokenSuccess(const std::string& token) {}
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void OnUbertokenFailure(const GoogleServiceAuthError& error) {}
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Allows to retrieve an uber-auth token.
3958e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdochclass UbertokenFetcher : public GaiaAuthConsumer,
4058e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch                         public OAuth2TokenService::Consumer {
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
421320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // Maximum number of retries to get the uber-auth token before giving up.
431320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  static const int kMaxRetries;
441320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  UbertokenFetcher(OAuth2TokenService* token_service,
465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                   UbertokenConsumer* consumer,
475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                   net::URLRequestContextGetter* request_context);
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual ~UbertokenFetcher();
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Start fetching the token for |account_id|.
51a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  virtual void StartFetchingToken(const std::string& account_id);
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Overriden from GaiaAuthConsumer
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void OnUberAuthTokenSuccess(const std::string& token) OVERRIDE;
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void OnUberAuthTokenFailure(
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      const GoogleServiceAuthError& error) OVERRIDE;
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5858e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch  // Overriden from OAuth2TokenService::Consumer:
5958e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch  virtual void OnGetTokenSuccess(const OAuth2TokenService::Request* request,
6058e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch                                 const std::string& access_token,
6158e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch                                 const base::Time& expiration_time) OVERRIDE;
6258e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch  virtual void OnGetTokenFailure(const OAuth2TokenService::Request* request,
6358e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch                                 const GoogleServiceAuthError& error) OVERRIDE;
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
661320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // Request a login-scoped access token from the token service.
671320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  void RequestAccessToken();
681320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
691320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // Exchanges an oauth2 access token for an uber-auth token.
701320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  void ExchangeTokens();
711320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
725d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  OAuth2TokenService* token_service_;
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  UbertokenConsumer* consumer_;
745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  net::URLRequestContextGetter* request_context_;
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_ptr<GaiaAuthFetcher> gaia_auth_fetcher_;
7658e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch  scoped_ptr<OAuth2TokenService::Request> access_token_request_;
771320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  std::string account_id_;
781320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  std::string access_token_;
791320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  int retry_number_;
801320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  base::OneShotTimer<UbertokenFetcher> retry_timer_;
811320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  bool second_access_token_request_;
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(UbertokenFetcher);
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
865d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#endif  // GOOGLE_APIS_GAIA_UBERTOKEN_FETCHER_H_
87