1 // Copyright 2013 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_HOST_SETUP_OAUTH_CLIENT 6 #define REMOTING_HOST_SETUP_OAUTH_CLIENT 7 8 #include <queue> 9 #include <string> 10 11 #include "base/callback.h" 12 #include "google_apis/gaia/gaia_oauth_client.h" 13 #include "remoting/base/url_request_context.h" 14 15 namespace remoting { 16 17 // A wrapper around GaiaOAuthClient that provides a more convenient interface, 18 // with queueing of requests and a callback rather than a delegate. 19 class OAuthClient : public gaia::GaiaOAuthClient::Delegate { 20 public: 21 // Called when GetCredentialsFromAuthCode is completed, with the |user_email| 22 // and |refresh_token| that correspond to the given |auth_code|, or with empty 23 // strings on error. 24 typedef base::Callback<void( 25 const std::string& user_email, 26 const std::string& refresh_token)> CompletionCallback; 27 28 OAuthClient( 29 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter); 30 31 virtual ~OAuthClient(); 32 33 // Redeems |auth_code| using |oauth_client_info| to obtain |refresh_token| and 34 // |access_token|, then uses the userinfo endpoint to obtain |user_email|. 35 // Calls CompletionCallback with |user_email| and |refresh_token| when done, 36 // or with empty strings on error. 37 // If a request is received while another one is being processed, it is 38 // enqueued and processed after the first one is finished. 39 void GetCredentialsFromAuthCode( 40 const gaia::OAuthClientInfo& oauth_client_info, 41 const std::string& auth_code, 42 CompletionCallback on_done); 43 44 // gaia::GaiaOAuthClient::Delegate 45 virtual void OnGetTokensResponse(const std::string& refresh_token, 46 const std::string& access_token, 47 int expires_in_seconds) OVERRIDE; 48 virtual void OnRefreshTokenResponse(const std::string& access_token, 49 int expires_in_seconds) OVERRIDE; 50 virtual void OnGetUserEmailResponse(const std::string& user_email) OVERRIDE; 51 52 virtual void OnOAuthError() OVERRIDE; 53 virtual void OnNetworkError(int response_code) OVERRIDE; 54 55 private: 56 struct Request { 57 Request(const gaia::OAuthClientInfo& oauth_client_info, 58 const std::string& auth_code, 59 CompletionCallback on_done); 60 virtual ~Request(); 61 gaia::OAuthClientInfo oauth_client_info; 62 std::string auth_code; 63 CompletionCallback on_done; 64 }; 65 66 void SendResponse(const std::string& user_email, 67 const std::string& refresh_token); 68 69 std::queue<Request> pending_requests_; 70 gaia::GaiaOAuthClient gaia_oauth_client_; 71 std::string refresh_token_; 72 CompletionCallback on_done_; 73 74 DISALLOW_COPY_AND_ASSIGN(OAuthClient); 75 }; 76 77 } // namespace remoting 78 79 #endif // REMOTING_HOST_SETUP_OAUTH_CLIENT 80