From deefc1673351ea7d81a648f8a79e8f32d8524f9f Mon Sep 17 00:00:00 2001 From: vitalybuka Date: Mon, 26 Jan 2015 13:46:11 -0800 Subject: [PATCH] Fixed Privet v3 auth header. v1 uses "X-Privet-Auth". v3 uses "Authorization". Privet v3 client should use "Authorization: Privet anonymous" before pairing. BUG=449864 Review URL: https://codereview.chromium.org/878463002 Cr-Commit-Position: refs/heads/master@{#313144} --- chrome/browser/local_discovery/privet_url_fetcher.cc | 7 +++---- .../local_discovery/privet_url_fetcher_unittest.cc | 3 ++- chrome/browser/local_discovery/privetv3_session.cc | 20 +++++++++++++++++--- chrome/browser/local_discovery/privetv3_session.h | 2 ++ 4 files changed, 24 insertions(+), 8 deletions(-) diff --git a/chrome/browser/local_discovery/privet_url_fetcher.cc b/chrome/browser/local_discovery/privet_url_fetcher.cc index 627278eabcb4..a5cc2855e4c8 100644 --- a/chrome/browser/local_discovery/privet_url_fetcher.cc +++ b/chrome/browser/local_discovery/privet_url_fetcher.cc @@ -34,7 +34,7 @@ struct TokenMapHolder { }; const char kXPrivetTokenHeaderPrefix[] = "X-Privet-Token: "; -const char kXPrivetAuthTokenHeaderPrefix[] = "X-Privet-Auth: "; +const char kPrivetV3AuthTokenHeaderPrefix[] = "Authorization: "; const char kRangeHeaderFormat[] = "Range: bytes=%d-%d"; const char kXPrivetEmptyToken[] = "\"\""; const char kPrivetAuthTokenUnknown[] = "Unknown"; @@ -151,10 +151,9 @@ void PrivetURLFetcher::Try() { url_fetcher_->SetRequestContext(request_context_.get()); if (v3_mode_) { - std::string auth_token = delegate_->GetAuthToken(); - url_fetcher_->AddExtraRequestHeader( - std::string(kXPrivetAuthTokenHeaderPrefix) + auth_token); + std::string(kPrivetV3AuthTokenHeaderPrefix) + + delegate_->GetAuthToken()); } else { std::string token = GetPrivetAccessToken(); diff --git a/chrome/browser/local_discovery/privet_url_fetcher_unittest.cc b/chrome/browser/local_discovery/privet_url_fetcher_unittest.cc index 5d07b8a4d53f..2b1dacaa65e0 100644 --- a/chrome/browser/local_discovery/privet_url_fetcher_unittest.cc +++ b/chrome/browser/local_discovery/privet_url_fetcher_unittest.cc @@ -311,7 +311,8 @@ TEST_F(PrivetURLFetcherTest, V3Mode) { std::string header_token; ASSERT_FALSE(headers.GetHeader("X-Privet-Token", &header_token)); - ASSERT_TRUE(headers.GetHeader("X-Privet-Auth", &header_token)); + ASSERT_FALSE(headers.GetHeader("X-Privet-Auth", &header_token)); + ASSERT_TRUE(headers.GetHeader("Authorization", &header_token)); ASSERT_EQ("MyAuthToken", header_token); } diff --git a/chrome/browser/local_discovery/privetv3_session.cc b/chrome/browser/local_discovery/privetv3_session.cc index 14d28f1f550c..bd5f4ca678b9 100644 --- a/chrome/browser/local_discovery/privetv3_session.cc +++ b/chrome/browser/local_discovery/privetv3_session.cc @@ -7,6 +7,7 @@ #include "base/json/json_writer.h" #include "base/logging.h" #include "base/message_loop/message_loop.h" +#include "chrome/browser/local_discovery/privet_constants.h" #include "chrome/browser/local_discovery/privet_http.h" #include "chrome/browser/local_discovery/privet_url_fetcher.h" #include "chrome/common/cloud_print/cloud_print_constants.h" @@ -20,6 +21,8 @@ const char kUrlPlaceHolder[] = "http://host/"; const char kStubPrivetCode[] = "1234"; +const char kPrivetV3AuthAnonymous[] = "Privet anonymous"; + GURL CreatePrivetURL(const std::string& path) { GURL url(kUrlPlaceHolder); GURL::Replacements replacements; @@ -32,10 +35,12 @@ GURL CreatePrivetURL(const std::string& path) { class PrivetV3Session::FetcherDelegate : public PrivetURLFetcher::Delegate { public: FetcherDelegate(const base::WeakPtr& session, + const std::string& auth_token, const PrivetV3Session::MessageCallback& callback); ~FetcherDelegate() override; // PrivetURLFetcher::Delegate methods. + std::string GetAuthToken() override; void OnNeedPrivetToken( PrivetURLFetcher* fetcher, const PrivetURLFetcher::TokenCallback& callback) override; @@ -51,22 +56,29 @@ class PrivetV3Session::FetcherDelegate : public PrivetURLFetcher::Delegate { scoped_ptr url_fetcher_; base::WeakPtr session_; + std::string auth_token_; MessageCallback callback_; }; PrivetV3Session::FetcherDelegate::FetcherDelegate( const base::WeakPtr& session, + const std::string& auth_token, const PrivetV3Session::MessageCallback& callback) - : session_(session), callback_(callback) { + : session_(session), auth_token_(auth_token), callback_(callback) { } PrivetV3Session::FetcherDelegate::~FetcherDelegate() { } +std::string PrivetV3Session::FetcherDelegate::GetAuthToken() { + return auth_token_; +} + void PrivetV3Session::FetcherDelegate::OnNeedPrivetToken( PrivetURLFetcher* fetcher, const PrivetURLFetcher::TokenCallback& callback) { NOTREACHED(); + OnError(fetcher, PrivetURLFetcher::URL_FETCH_ERROR); } void PrivetV3Session::FetcherDelegate::OnError( @@ -103,6 +115,8 @@ PrivetV3Session::~PrivetV3Session() { } void PrivetV3Session::Init(const InitCallback& callback) { + privet_auth_token_ = kPrivetV3AuthAnonymous; + // TODO: call /info. base::MessageLoop::current()->PostDelayedTask( FROM_HERE, @@ -140,8 +154,8 @@ void PrivetV3Session::SendMessage(const std::string& api, if (!code_confirmed_) return callback.Run(Result::STATUS_SESSIONERROR, base::DictionaryValue()); - FetcherDelegate* fetcher_delegate( - new FetcherDelegate(weak_ptr_factory_.GetWeakPtr(), callback)); + FetcherDelegate* fetcher_delegate(new FetcherDelegate( + weak_ptr_factory_.GetWeakPtr(), privet_auth_token_, callback)); fetchers_.push_back(fetcher_delegate); scoped_ptr url_fetcher(client_->CreateURLFetcher( diff --git a/chrome/browser/local_discovery/privetv3_session.h b/chrome/browser/local_discovery/privetv3_session.h index f9257b96bb25..648eef7a7715 100644 --- a/chrome/browser/local_discovery/privetv3_session.h +++ b/chrome/browser/local_discovery/privetv3_session.h @@ -60,6 +60,8 @@ class PrivetV3Session { scoped_ptr client_; bool code_confirmed_; ScopedVector fetchers_; + std::string privet_auth_token_; + base::WeakPtrFactory weak_ptr_factory_; DISALLOW_COPY_AND_ASSIGN(PrivetV3Session); }; -- 2.11.4.GIT