oauth2_access_token_fetcher_impl.h revision a1401311d1ab56c4ed0a474bd38c108f75cb0cd9
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.
4
5#ifndef GOOGLE_APIS_GAIA_OAUTH2_ACCESS_TOKEN_FETCHER_IMPL_H_
6#define GOOGLE_APIS_GAIA_OAUTH2_ACCESS_TOKEN_FETCHER_IMPL_H_
7
8#include <string>
9#include <vector>
10
11#include "base/gtest_prod_util.h"
12#include "base/memory/scoped_ptr.h"
13#include "google_apis/gaia/oauth2_access_token_consumer.h"
14#include "google_apis/gaia/oauth2_access_token_fetcher.h"
15#include "net/url_request/url_fetcher_delegate.h"
16#include "url/gurl.h"
17
18class OAuth2AccessTokenFetcherImplTest;
19
20namespace base {
21class Time;
22}
23
24namespace net {
25class URLFetcher;
26class URLRequestContextGetter;
27class URLRequestStatus;
28}
29
30// Abstracts the details to get OAuth2 access token token from
31// OAuth2 refresh token.
32// See "Using the Refresh Token" section in:
33// http://code.google.com/apis/accounts/docs/OAuth2WebServer.html
34//
35// This class should be used on a single thread, but it can be whichever thread
36// that you like.
37// Also, do not reuse the same instance. Once Start() is called, the instance
38// should not be reused.
39//
40// Usage:
41// * Create an instance with a consumer.
42// * Call Start()
43// * The consumer passed in the constructor will be called on the same
44//   thread Start was called with the results.
45//
46// This class can handle one request at a time. To parallelize requests,
47// create multiple instances.
48class OAuth2AccessTokenFetcherImpl : public OAuth2AccessTokenFetcher,
49                                     public net::URLFetcherDelegate {
50 public:
51  OAuth2AccessTokenFetcherImpl(OAuth2AccessTokenConsumer* consumer,
52                               net::URLRequestContextGetter* getter,
53                               const std::string& refresh_token);
54  virtual ~OAuth2AccessTokenFetcherImpl();
55
56  // Implementation of OAuth2AccessTokenFetcher
57  virtual void Start(const std::string& client_id,
58                     const std::string& client_secret,
59                     const std::vector<std::string>& scopes) OVERRIDE;
60
61  virtual void CancelRequest() OVERRIDE;
62
63  // Implementation of net::URLFetcherDelegate
64  virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE;
65
66 private:
67  enum State {
68    INITIAL,
69    GET_ACCESS_TOKEN_STARTED,
70    GET_ACCESS_TOKEN_DONE,
71    ERROR_STATE,
72  };
73
74  // Helper methods for the flow.
75  void StartGetAccessToken();
76  void EndGetAccessToken(const net::URLFetcher* source);
77
78  // Helper mehtods for reporting back results.
79  void OnGetTokenSuccess(const std::string& access_token,
80                         const base::Time& expiration_time);
81  void OnGetTokenFailure(const GoogleServiceAuthError& error);
82
83  // Other helpers.
84  static GURL MakeGetAccessTokenUrl();
85  static std::string MakeGetAccessTokenBody(
86      const std::string& client_id,
87      const std::string& client_secret,
88      const std::string& refresh_token,
89      const std::vector<std::string>& scopes);
90
91  static bool ParseGetAccessTokenSuccessResponse(const net::URLFetcher* source,
92                                                 std::string* access_token,
93                                                 int* expires_in);
94
95  static bool ParseGetAccessTokenFailureResponse(const net::URLFetcher* source,
96                                                 std::string* error);
97
98  // State that is set during construction.
99  net::URLRequestContextGetter* const getter_;
100  std::string refresh_token_;
101  State state_;
102
103  // While a fetch is in progress.
104  scoped_ptr<net::URLFetcher> fetcher_;
105  std::string client_id_;
106  std::string client_secret_;
107  std::vector<std::string> scopes_;
108
109  friend class OAuth2AccessTokenFetcherImplTest;
110  FRIEND_TEST_ALL_PREFIXES(OAuth2AccessTokenFetcherImplTest,
111                           ParseGetAccessTokenResponse);
112  FRIEND_TEST_ALL_PREFIXES(OAuth2AccessTokenFetcherImplTest,
113                           MakeGetAccessTokenBody);
114
115  DISALLOW_COPY_AND_ASSIGN(OAuth2AccessTokenFetcherImpl);
116};
117
118#endif  // GOOGLE_APIS_GAIA_OAUTH2_ACCESS_TOKEN_FETCHER_IMPL_H_
119