15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 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)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef GOOGLE_APIS_GAIA_OAUTH2_ACCESS_TOKEN_FETCHER_H_
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define GOOGLE_APIS_GAIA_OAUTH2_ACCESS_TOKEN_FETCHER_H_
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <string>
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <vector>
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "google_apis/gaia/oauth2_access_token_consumer.h"
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/url_request/url_fetcher_delegate.h"
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
14a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)class OAuth2AccessTokenConsumer;
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
16a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// Interface of a OAuth2 access token fetcher.
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Usage:
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// * Create an instance with a consumer.
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// * Call Start()
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// * The consumer passed in the constructor will be called on the same
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   thread Start was called with the results.
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// This class can handle one request at a time. To parallelize requests,
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// create multiple instances.
26a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)class OAuth2AccessTokenFetcher {
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
28a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  explicit OAuth2AccessTokenFetcher(OAuth2AccessTokenConsumer* consumer);
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual ~OAuth2AccessTokenFetcher();
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Starts the flow with the given parameters.
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // |scopes| can be empty. If it is empty then the access token will have the
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // same scope as the refresh token. If not empty, then access token will have
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // the scopes specified. In this case, the access token will successfully be
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // generated only if refresh token has login scope of a list of scopes that is
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // a super-set of the specified scopes.
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void Start(const std::string& client_id,
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                     const std::string& client_secret,
39a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                     const std::vector<std::string>& scopes) = 0;
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
41a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // Cancels the current request and informs the consumer.
42a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  virtual void CancelRequest() = 0;
43f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
44a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) protected:
45a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // Fires |OnGetTokenSuccess| on |consumer_|.
46a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  void FireOnGetTokenSuccess(const std::string& access_token,
47a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                             const base::Time& expiration_time);
48f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
49a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // Fires |OnGetTokenFailure| on |consumer_|.
50a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  void FireOnGetTokenFailure(const GoogleServiceAuthError& error);
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
52a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) private:
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  OAuth2AccessTokenConsumer* const consumer_;
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(OAuth2AccessTokenFetcher);
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif  // GOOGLE_APIS_GAIA_OAUTH2_ACCESS_TOKEN_FETCHER_H_
59