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