diff options
author | simonmorris@chromium.org <simonmorris@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-10-18 19:00:04 +0000 |
---|---|---|
committer | simonmorris@chromium.org <simonmorris@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-10-18 19:00:04 +0000 |
commit | 33c431a74102ef54624dc9c9ff603dcb4a7d3894 (patch) | |
tree | 74f24e0a75c5fbafa99a65db516412ba0c75addd /remoting | |
parent | 6664ea4821ed6899174293520d9b2bbf7f685b00 (diff) | |
download | chromium_src-33c431a74102ef54624dc9c9ff603dcb4a7d3894.zip chromium_src-33c431a74102ef54624dc9c9ff603dcb4a7d3894.tar.gz chromium_src-33c431a74102ef54624dc9c9ff603dcb4a7d3894.tar.bz2 |
[Chromoting] Let the Linux start-host app take a redirect-URL parameter.
This lets us change the flow that produces an OAuth authorization code.
BUG=155431
Review URL: https://chromiumcodereview.appspot.com/11185037
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@162752 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'remoting')
-rw-r--r-- | remoting/host/setup/host_starter.cc | 11 | ||||
-rw-r--r-- | remoting/host/setup/host_starter.h | 7 | ||||
-rw-r--r-- | remoting/host/setup/oauth_helper.cc | 47 | ||||
-rw-r--r-- | remoting/host/setup/oauth_helper.h | 11 | ||||
-rw-r--r-- | remoting/host/setup/oauth_helper_unittest.cc | 33 | ||||
-rw-r--r-- | remoting/host/setup/start_host.cc | 10 | ||||
-rw-r--r-- | remoting/host/setup/win/auth_code_getter.cc | 6 | ||||
-rw-r--r-- | remoting/host/setup/win/start_host_window.cc | 2 |
8 files changed, 76 insertions, 51 deletions
diff --git a/remoting/host/setup/host_starter.cc b/remoting/host/setup/host_starter.cc index ff7deb9..f7aa5be 100644 --- a/remoting/host/setup/host_starter.cc +++ b/remoting/host/setup/host_starter.cc @@ -70,11 +70,6 @@ HostStarter::HostStarter( consent_to_data_collection_(false), ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)), weak_ptr_(weak_ptr_factory_.GetWeakPtr()) { - oauth_client_info_.client_id = - google_apis::GetOAuth2ClientID(google_apis::CLIENT_REMOTING); - oauth_client_info_.client_secret = - google_apis::GetOAuth2ClientSecret(google_apis::CLIENT_REMOTING); - oauth_client_info_.redirect_uri = GetOauthRedirectUrl(); main_task_runner_ = base::ThreadTaskRunnerHandle::Get(); } @@ -102,6 +97,7 @@ void HostStarter::StartHost( const std::string& host_pin, bool consent_to_data_collection, const std::string& auth_code, + const std::string& redirect_url, CompletionCallback on_done) { DCHECK(main_task_runner_->BelongsToCurrentThread()); if (in_progress_) { @@ -113,6 +109,11 @@ void HostStarter::StartHost( host_pin_ = host_pin; consent_to_data_collection_ = consent_to_data_collection; on_done_ = on_done; + oauth_client_info_.client_id = + google_apis::GetOAuth2ClientID(google_apis::CLIENT_REMOTING); + oauth_client_info_.client_secret = + google_apis::GetOAuth2ClientSecret(google_apis::CLIENT_REMOTING); + oauth_client_info_.redirect_uri = redirect_url; // Map the authorization code to refresh and access tokens. oauth_client_->GetTokensFromAuthCode(oauth_client_info_, auth_code, kMaxGetTokensRetries, this); diff --git a/remoting/host/setup/host_starter.h b/remoting/host/setup/host_starter.h index 508e13a..11b2df1 100644 --- a/remoting/host/setup/host_starter.h +++ b/remoting/host/setup/host_starter.h @@ -42,8 +42,11 @@ class HostStarter : // Registers a new host with the Chromoting service, and starts it. // |auth_code| must be a valid OAuth2 authorization code, typically acquired // from a browser. This method uses that code to get an OAuth2 refresh token. - void StartHost(const std::string& host_name, const std::string& host_pin, - bool consent_to_data_collection, const std::string& auth_code, + void StartHost(const std::string& host_name, + const std::string& host_pin, + bool consent_to_data_collection, + const std::string& auth_code, + const std::string& redirect_url, CompletionCallback on_done); // gaia::GaiaOAuthClient::Delegate diff --git a/remoting/host/setup/oauth_helper.cc b/remoting/host/setup/oauth_helper.cc index 3228384..6f110f6 100644 --- a/remoting/host/setup/oauth_helper.cc +++ b/remoting/host/setup/oauth_helper.cc @@ -30,39 +30,40 @@ std::string GetOauthScope() { "https://www.googleapis.com/auth/userinfo.email "; } -std::string GetOauthStartUrl() { - return base::StringPrintf( - "https://accounts.google.com/o/oauth2/auth" - "?scope=%s" - "&redirect_uri=%s" - "&response_type=code" - "&client_id=%s" - "&access_type=offline" - "&approval_prompt=force", - net::EscapeUrlEncodedData(GetOauthScope(), true).c_str(), - net::EscapeUrlEncodedData(GetOauthRedirectUrl(), true).c_str(), - net::EscapeUrlEncodedData(google_apis::GetOAuth2ClientID( - google_apis::CLIENT_REMOTING), true).c_str()); +std::string GetDefaultOauthRedirectUrl() { + return + "https://chromoting-oauth.talkgadget.google.com/talkgadget/oauth/" + "chrome-remote-desktop/rel/kgngmbheleoaphbjbaiobfdepmghbfah"; } -std::string GetOauthRedirectUrl() { - return - "https://chromoting-oauth.talkgadget.google.com/talkgadget/oauth/" - "chrome-remote-desktop/rel/kgngmbheleoaphbjbaiobfdepmghbfah"; +std::string GetOauthStartUrl(const std::string& redirect_url) { + return base::StringPrintf( + "https://accounts.google.com/o/oauth2/auth" + "?scope=%s" + "&redirect_uri=%s" + "&response_type=code" + "&client_id=%s" + "&access_type=offline" + "&approval_prompt=force", + net::EscapeUrlEncodedData(GetOauthScope(), true).c_str(), + redirect_url.c_str(), + net::EscapeUrlEncodedData(google_apis::GetOAuth2ClientID( + google_apis::CLIENT_REMOTING), true).c_str()); } -std::string GetOauthCodeInUrl(const std::string& url) { +std::string GetOauthCodeInUrl(const std::string& url, + const std::string& redirect_url) { url_parse::Parsed url_parsed; ParseStandardURL(url.c_str(), url.length(), &url_parsed); - std::string redirect = GetOauthRedirectUrl(); - url_parse::Parsed redirect_parsed; - ParseStandardURL(redirect.c_str(), redirect.length(), &redirect_parsed); + url_parse::Parsed redirect_url_parsed; + ParseStandardURL(redirect_url.c_str(), redirect_url.length(), + &redirect_url_parsed); if (GetComponent(url, url_parsed.scheme) != - GetComponent(redirect, redirect_parsed.scheme)) { + GetComponent(redirect_url, redirect_url_parsed.scheme)) { return ""; } if (GetComponent(url, url_parsed.host) != - GetComponent(redirect, redirect_parsed.host)) { + GetComponent(redirect_url, redirect_url_parsed.host)) { return ""; } url_parse::Component query = url_parsed.query; diff --git a/remoting/host/setup/oauth_helper.h b/remoting/host/setup/oauth_helper.h index a762c23..cdb8445 100644 --- a/remoting/host/setup/oauth_helper.h +++ b/remoting/host/setup/oauth_helper.h @@ -12,11 +12,11 @@ namespace remoting { // Gets the OAuth scope of the host's refresh token. std::string GetOauthScope(); -// Gets a URL at which the OAuth dance starts. -std::string GetOauthStartUrl(); +// Gets the default redirect URL for the OAuth dance. +std::string GetDefaultOauthRedirectUrl(); -// Gets a redirect URL for the OAuth dance. -std::string GetOauthRedirectUrl(); +// Gets a URL at which the OAuth dance starts. +std::string GetOauthStartUrl(const std::string& redirect_url); // Returns the OAuth authorization code embedded in a URL, or the empty string // if there is no such code. @@ -25,7 +25,8 @@ std::string GetOauthRedirectUrl(); // and grant the requested permissions, (iv) monitor the URLs that the browser // shows, passing each one to |GetOauthCodeInUrl()|, until that function returns // a non-empty string. That string is the authorization code. -std::string GetOauthCodeInUrl(const std::string& url); +std::string GetOauthCodeInUrl(const std::string& url, + const std::string& redirect_url); } // namespace remoting diff --git a/remoting/host/setup/oauth_helper_unittest.cc b/remoting/host/setup/oauth_helper_unittest.cc index aff85d1..cddb32a 100644 --- a/remoting/host/setup/oauth_helper_unittest.cc +++ b/remoting/host/setup/oauth_helper_unittest.cc @@ -18,49 +18,58 @@ std::string Replace(const std::string& s, const std::string& old_substr, s.substr(pos + old_substr.length(), std::string::npos); } +std::string GetTestRedirectUrl() { + return std::string("https://google.com/redirect"); +} + } // namespace namespace remoting { TEST(OauthHelperTest, TestNotCode) { - ASSERT_EQ("", GetOauthCodeInUrl("notURL")); + ASSERT_EQ("", GetOauthCodeInUrl("notURL", GetTestRedirectUrl())); } TEST(OauthHelperTest, TestVeryShort) { - ASSERT_EQ("", GetOauthCodeInUrl(GetOauthRedirectUrl())); + ASSERT_EQ("", GetOauthCodeInUrl(GetTestRedirectUrl(), GetTestRedirectUrl())); } TEST(OauthHelperTest, TestEmptyQuery) { - ASSERT_EQ("", GetOauthCodeInUrl(GetOauthRedirectUrl() + "?")); + ASSERT_EQ("", GetOauthCodeInUrl(GetTestRedirectUrl() + "?", + GetTestRedirectUrl())); } TEST(OauthHelperTest, TestNoQueryValue) { - ASSERT_EQ("", GetOauthCodeInUrl(GetOauthRedirectUrl() + "?code")); + ASSERT_EQ("", GetOauthCodeInUrl(GetTestRedirectUrl() + "?code", + GetTestRedirectUrl())); } TEST(OauthHelperTest, TestEmptyCode) { - ASSERT_EQ("", GetOauthCodeInUrl(GetOauthRedirectUrl() + "?code=")); + ASSERT_EQ("", GetOauthCodeInUrl(GetTestRedirectUrl() + "?code=", + GetTestRedirectUrl())); } TEST(OauthHelperTest, TestCode) { - ASSERT_EQ("Dummy", GetOauthCodeInUrl(GetOauthRedirectUrl() + "?code=Dummy")); + ASSERT_EQ("Dummy", GetOauthCodeInUrl(GetTestRedirectUrl() + "?code=Dummy", + GetTestRedirectUrl())); } TEST(OauthHelperTest, TestCodeInLongQuery) { - ASSERT_EQ("Dummy", GetOauthCodeInUrl(GetOauthRedirectUrl() + - "?x=1&code=Dummy&y=2")); + ASSERT_EQ("Dummy", GetOauthCodeInUrl(GetTestRedirectUrl() + + "?x=1&code=Dummy&y=2", + GetTestRedirectUrl())); } TEST(OauthHelperTest, TestBadScheme) { - std::string url = GetOauthRedirectUrl() + "?code=Dummy"; + std::string url = GetTestRedirectUrl() + "?code=Dummy"; url = Replace(url, "https:", "http"); - ASSERT_EQ("", GetOauthCodeInUrl(url)); + ASSERT_EQ("", GetOauthCodeInUrl(url, GetTestRedirectUrl())); } TEST(OauthHelperTest, TestBadHost) { - std::string url = GetOauthRedirectUrl() + "?code=Dummy"; + std::string url = GetTestRedirectUrl() + "?code=Dummy"; url = Replace(url, "google", "goggle"); - ASSERT_EQ("", GetOauthCodeInUrl(url)); + ASSERT_EQ("", GetOauthCodeInUrl(url, GetTestRedirectUrl())); } } // namespace remoting diff --git a/remoting/host/setup/start_host.cc b/remoting/host/setup/start_host.cc index 2c311a7..07db748 100644 --- a/remoting/host/setup/start_host.cc +++ b/remoting/host/setup/start_host.cc @@ -12,6 +12,7 @@ #include "base/threading/thread.h" #include "net/url_request/url_request_context_getter.h" #include "remoting/host/setup/host_starter.h" +#include "remoting/host/setup/oauth_helper.h" #include "remoting/host/setup/pin_validator.h" #include "remoting/host/url_request_context.h" @@ -92,10 +93,12 @@ int main(int argc, char** argv) { std::string host_name = command_line->GetSwitchValueASCII("name"); std::string host_pin = command_line->GetSwitchValueASCII("pin"); std::string auth_code = command_line->GetSwitchValueASCII("code"); + std::string redirect_url = command_line->GetSwitchValueASCII("redirect-url"); if (host_name.empty()) { fprintf(stderr, - "Usage: %s --name=<hostname> [--code=<auth-code>] [--pin=<PIN>]\n", + "Usage: %s --name=<hostname> [--code=<auth-code>] [--pin=<PIN>] " + "[--redirect-url=<redirectURL>]\n", argv[0]); return 1; } @@ -154,7 +157,10 @@ int main(int argc, char** argv) { // Start the host. scoped_ptr<HostStarter> host_starter( HostStarter::Create(url_request_context_getter)); - host_starter->StartHost(host_name, host_pin, true, auth_code, + if (redirect_url.empty()) { + redirect_url = remoting::GetDefaultOauthRedirectUrl(); + } + host_starter->StartHost(host_name, host_pin, true, auth_code, redirect_url, base::Bind(&OnDone)); // Run the message loop until the StartHost completion callback. diff --git a/remoting/host/setup/win/auth_code_getter.cc b/remoting/host/setup/win/auth_code_getter.cc index fd48986..89ce104 100644 --- a/remoting/host/setup/win/auth_code_getter.cc +++ b/remoting/host/setup/win/auth_code_getter.cc @@ -40,7 +40,8 @@ void AuthCodeGetter::GetAuthCode( return; } browser_running_ = true; - base::win::ScopedBstr url(UTF8ToWide(GetOauthStartUrl()).c_str()); + base::win::ScopedBstr url(UTF8ToWide( + GetOauthStartUrl(GetDefaultOauthRedirectUrl())).c_str()); base::win::ScopedVariant empty_variant; hr = browser_->Navigate(url, empty_variant.AsInput(), empty_variant.AsInput(), empty_variant.AsInput(), empty_variant.AsInput()); @@ -77,7 +78,8 @@ bool AuthCodeGetter::TestBrowserUrl(std::string* auth_code) { KillBrowser(); return true; } - *auth_code = GetOauthCodeInUrl(WideToUTF8(static_cast<BSTR>(url))); + *auth_code = GetOauthCodeInUrl(WideToUTF8(static_cast<BSTR>(url)), + GetDefaultOauthRedirectUrl()); if (!auth_code->empty()) { KillBrowser(); return true; diff --git a/remoting/host/setup/win/start_host_window.cc b/remoting/host/setup/win/start_host_window.cc index 4027f32..1609261 100644 --- a/remoting/host/setup/win/start_host_window.cc +++ b/remoting/host/setup/win/start_host_window.cc @@ -11,6 +11,7 @@ #include "base/memory/scoped_ptr.h" #include "base/string16.h" #include "base/utf_string_conversions.h" +#include "remoting/host/setup/oauth_helper.h" #include "remoting/host/setup/pin_validator.h" #include "remoting/host/setup/win/load_string_from_resource.h" @@ -73,6 +74,7 @@ void StartHostWindow::OnOk(UINT code, int id, HWND control) { void StartHostWindow::OnAuthCode(const std::string& auth_code) { host_starter_->StartHost( host_name_, pin_, consent_to_collect_data_, auth_code, + GetDefaultOauthRedirectUrl(), base::Bind(&StartHostWindow::OnHostStarted, weak_ptr_)); } |