Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2015 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 REMOTING_TEST_ACCESS_TOKEN_FETCHER_H_ | |
| 6 #define REMOTING_TEST_ACCESS_TOKEN_FETCHER_H_ | |
| 7 | |
| 8 #include "base/callback.h" | |
| 9 #include "base/memory/ref_counted.h" | |
| 10 #include "base/memory/scoped_ptr.h" | |
| 11 #include "google_apis/gaia/gaia_oauth_client.h" | |
| 12 | |
| 13 namespace remoting { | |
| 14 namespace test { | |
| 15 | |
| 16 typedef base::Callback<void( | |
| 17 const std::string& access_token, | |
| 18 const std::string& refresh_token)> AccessTokenCallback; | |
| 19 | |
| 20 // This class presents the same interface as the GaiaOAuthClient and wraps an | |
| 21 // instance of that class internally. The reason for using an adapter instead | |
| 22 // of creating the client object directly is that the GaiaOAuthClient class | |
| 23 // has non-virtual methods so it is difficult to abstract. | |
| 24 // Using this adapter allows us to easily create a mock or fake class and test | |
| 25 // the AccessTokenFetcher more efficiently. | |
|
Wez
2015/02/13 03:01:51
Why do we need this class? Why not just make the G
joedow
2015/02/14 02:31:25
Done.
| |
| 26 class GaiaOAuthClientAdapter { | |
|
Wez
2015/02/13 03:01:50
nit: Move this class into it's own .h & .cc files?
joedow
2015/02/14 02:31:26
Done.
| |
| 27 public: | |
| 28 explicit GaiaOAuthClientAdapter(net::URLRequestContextGetter* context_getter); | |
|
Wez
2015/02/13 03:01:51
const scoped_refptr<>&
joedow
2015/02/14 02:31:26
Done.
| |
| 29 virtual ~GaiaOAuthClientAdapter(); | |
| 30 | |
| 31 // Uses the |auth_code| passed in to retrieve an access token. | |
|
Wez
2015/02/13 03:01:51
Since these three methods just mirror the GaiaOAut
joedow
2015/02/14 02:31:26
Done.
| |
| 32 virtual void GetTokensFromAuthCode( | |
| 33 const gaia::OAuthClientInfo& oauth_client_info, | |
| 34 const std::string& auth_code, | |
| 35 int max_retries, | |
| 36 gaia::GaiaOAuthClient::Delegate* delegate); | |
| 37 | |
| 38 // Uses the |refresh_token| passed in to retrieve an access token. | |
| 39 virtual void RefreshToken( | |
|
Wez
2015/02/13 03:01:51
This seems a daft name for the API; why is it not
joedow
2015/02/14 02:31:25
Done.
| |
| 40 const gaia::OAuthClientInfo& oauth_client_info, | |
| 41 const std::string& refresh_token, | |
| 42 const std::vector<std::string>& scopes, | |
| 43 int max_retries, | |
| 44 gaia::GaiaOAuthClient::Delegate* delegate); | |
| 45 | |
| 46 // Calls the Gaia service to validate the |oauth_access_token| passed in. | |
| 47 virtual void GetTokenInfo( | |
| 48 const std::string& oauth_access_token, | |
| 49 int max_retries, | |
| 50 gaia::GaiaOAuthClient::Delegate* delegate); | |
| 51 | |
| 52 private: | |
| 53 // Used to make token requests to Gaia service. Uses a request_context_getter | |
| 54 // to make those requests on the current thread's task runner. | |
|
Wez
2015/02/13 03:01:51
"Uses a ..." doesn't seem relevant here - the rele
joedow
2015/02/14 02:31:25
Done.
| |
| 55 scoped_ptr<gaia::GaiaOAuthClient> auth_client_; | |
| 56 | |
| 57 DISALLOW_COPY_AND_ASSIGN(GaiaOAuthClientAdapter); | |
| 58 }; | |
| 59 | |
| 60 // The AccessTokenFetcher class is used to retrieve an access token from either | |
|
Wez
2015/02/13 03:01:51
You don't really need "The AccessTokenFetcher clas
joedow
2015/02/14 02:31:25
Done.
| |
| 61 // an authorization code or a refresh token. If an auth code is provided, | |
| 62 // a refresh token will be retrieved as well. A new callback is supplied by the | |
|
Wez
2015/02/13 03:01:51
What does the fetching of the refresh token actual
joedow
2015/02/14 02:31:25
Done. Removed the comment since the tokens return
| |
| 63 // client for each network call which will return a valid token on success or | |
|
Wez
2015/02/13 03:01:51
What network calls?
joedow
2015/02/14 02:31:25
Done.
| |
| 64 // an empty token on failure. This callback will not be called back once the | |
| 65 // AccessTokenFetcher instance has been destructed and it is intended to be safe | |
| 66 // to destruct the AccessTokenFetcher instance within the callback if needed. | |
|
Wez
2015/02/13 03:01:51
This could be expressed more succinctly, e.g. "Des
joedow
2015/02/14 02:31:26
Done.
| |
| 67 // This class requires a valid IO MessageLoop on the thread it runs on as it | |
| 68 // uses the current thread's task runner to make its network requests. | |
|
Wez
2015/02/13 03:01:51
Again, this could be shorter, e.g. "Must be used f
joedow
2015/02/14 02:31:25
Done.
| |
| 69 class AccessTokenFetcher : public gaia::GaiaOAuthClient::Delegate { | |
| 70 public: | |
| 71 AccessTokenFetcher(); | |
| 72 ~AccessTokenFetcher() override; | |
| 73 | |
| 74 // Retrieve an access token from a one time use authorization code. | |
| 75 // Method is marked virtual to allow for mocking. | |
|
Wez
2015/02/13 03:01:51
The comments re marking virtual feel like they'd m
joedow
2015/02/14 02:31:25
Done.
| |
| 76 virtual void GetAccessTokenFromAuthCode(const std::string& auth_code, | |
| 77 const AccessTokenCallback& callback); | |
| 78 | |
| 79 // Retrieve an access token from a refresh token. | |
| 80 // Method is marked virtual to allow for mocking. | |
| 81 virtual void GetAccessTokenFromRefreshToken(const std::string& refresh_token, | |
| 82 const AccessTokenCallback& callback); | |
| 83 | |
| 84 protected: | |
| 85 // Updates |auth_client_| with a new GaiaOAuthClient instance. | |
| 86 virtual void RefreshGaiaOAuthClientInstance(); | |
|
Wez
2015/02/13 03:01:51
Why is this also virtual? For mocking?
"Refresh"
joedow
2015/02/14 02:31:25
Done.
| |
| 87 | |
| 88 // Used to make token requests to GAIA. Uses a request_context_getter | |
| 89 // to make those requests on the current thread's task runner. | |
|
Wez
2015/02/13 03:01:51
As above.
joedow
2015/02/14 02:31:25
Done.
| |
| 90 scoped_ptr<GaiaOAuthClientAdapter> auth_client_; | |
| 91 | |
| 92 private: | |
| 93 // gaia::GaiaOAuthClient::Delegate Interface. | |
| 94 void OnGetTokensResponse( | |
| 95 const std::string& refresh_token, | |
| 96 const std::string& access_token, | |
| 97 int expires_in_seconds) override; | |
| 98 void OnRefreshTokenResponse( | |
| 99 const std::string& access_token, | |
| 100 int expires_in_seconds) override; | |
| 101 void OnGetUserEmailResponse(const std::string& user_email) override; | |
| 102 void OnGetUserIdResponse(const std::string& user_id) override; | |
| 103 void OnGetUserInfoResponse( | |
| 104 scoped_ptr<base::DictionaryValue> user_info) override; | |
| 105 void OnGetTokenInfoResponse( | |
| 106 scoped_ptr<base::DictionaryValue> token_info) override; | |
| 107 void OnOAuthError() override; | |
| 108 void OnNetworkError(int response_code) override; | |
| 109 | |
| 110 // Validates the current access token and either returns it via the success | |
| 111 // callback or clears the token and calls the failure callback. | |
| 112 void ValidateAccessToken(); | |
|
Wez
2015/02/13 03:01:51
ValidateAccessTokenAndNotifyCallback, or simply No
joedow
2015/02/14 02:31:26
Cleaned up the comment since the function doesn't
| |
| 113 | |
| 114 // Caller-supplied callback used to return valid tokens on success or an | |
| 115 // empty access token if a failure occurred. The refresh token may or may not | |
| 116 // be empty on failure. | |
|
Wez
2015/02/13 03:01:51
Why would the refresh token not be empty on failur
joedow
2015/02/14 02:31:26
Comment was out of date, it is empty now.
| |
| 117 AccessTokenCallback access_token_callback_; | |
| 118 | |
| 119 // The access token retrieved based on the |refresh_token_|. | |
| 120 std::string access_token_; | |
|
Wez
2015/02/13 03:01:51
Why do you cache the access token in this class if
joedow
2015/02/14 02:31:26
I make a validate token call after I receive the a
Wez
2015/02/19 22:00:22
OK; it wasn't clear why you needed to store it rat
| |
| 121 | |
| 122 // Refresh token supplied by the caller or retrieved from a caller-supplied | |
|
Wez
2015/02/13 03:01:50
nit: No need for "Refresh token"
joedow
2015/02/14 02:31:26
Done.
| |
| 123 // auth token. | |
| 124 std::string refresh_token_; | |
| 125 | |
| 126 // Object which holds the client id, secret, and redirect url used to make | |
|
Wez
2015/02/13 03:01:50
nit: No need for "Object which"
joedow
2015/02/14 02:31:25
Done.
| |
| 127 // the Gaia service request. | |
| 128 gaia::OAuthClientInfo oauth_client_info_; | |
| 129 | |
| 130 DISALLOW_COPY_AND_ASSIGN(AccessTokenFetcher); | |
| 131 }; | |
| 132 | |
| 133 } // namespace test | |
| 134 } // namespace remoting | |
| 135 | |
| 136 #endif // REMOTING_TEST_ACCESS_TOKEN_FETCHER_H_ | |
| OLD | NEW |