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