[email protected] | 9a6361d0 | 2013-03-23 16:27:52 | [diff] [blame] | 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 | |
sergeyu | 89d088b | 2015-12-24 00:22:44 | [diff] [blame] | 5 | #include <utility> |
| 6 | |
[email protected] | 9a6361d0 | 2013-03-23 16:27:52 | [diff] [blame] | 7 | #include "base/bind.h" |
sergeyu | d9bdcb6 | 2015-04-25 00:18:08 | [diff] [blame] | 8 | #include "base/callback_helpers.h" |
avi | 5a080f01 | 2015-12-22 23:15:43 | [diff] [blame] | 9 | #include "base/macros.h" |
dcheng | 0765c49 | 2016-04-06 22:41:53 | [diff] [blame] | 10 | #include "base/memory/ptr_util.h" |
fdoray | 2ad58be | 2016-06-22 20:36:16 | [diff] [blame] | 11 | #include "base/run_loop.h" |
[email protected] | 9a6361d0 | 2013-03-23 16:27:52 | [diff] [blame] | 12 | #include "net/base/net_errors.h" |
| 13 | #include "remoting/base/rsa_key_pair.h" |
| 14 | #include "remoting/protocol/authenticator_test_base.h" |
| 15 | #include "remoting/protocol/channel_authenticator.h" |
| 16 | #include "remoting/protocol/connection_tester.h" |
| 17 | #include "remoting/protocol/fake_authenticator.h" |
| 18 | #include "remoting/protocol/third_party_authenticator_base.h" |
| 19 | #include "remoting/protocol/third_party_client_authenticator.h" |
| 20 | #include "remoting/protocol/third_party_host_authenticator.h" |
[email protected] | d95ee26 | 2014-02-26 06:30:31 | [diff] [blame] | 21 | #include "remoting/protocol/token_validator.h" |
sergeyu | 12e320a | 2016-03-08 18:10:28 | [diff] [blame] | 22 | #include "remoting/protocol/v2_authenticator.h" |
[email protected] | 9a6361d0 | 2013-03-23 16:27:52 | [diff] [blame] | 23 | #include "testing/gmock/include/gmock/gmock.h" |
| 24 | #include "testing/gtest/include/gtest/gtest.h" |
kjellander | f0e410b | 2017-01-04 14:45:01 | [diff] [blame] | 25 | #include "third_party/libjingle_xmpp/xmllite/xmlelement.h" |
[email protected] | 9a6361d0 | 2013-03-23 16:27:52 | [diff] [blame] | 26 | |
| 27 | using testing::_; |
| 28 | using testing::DeleteArg; |
| 29 | using testing::SaveArg; |
| 30 | |
| 31 | namespace { |
| 32 | |
| 33 | const int kMessageSize = 100; |
| 34 | const int kMessages = 1; |
| 35 | |
| 36 | const char kTokenUrl[] = "https://example.com/Issue"; |
| 37 | const char kTokenScope[] = "host:[email protected]/1 client:[email protected]/2"; |
| 38 | const char kToken[] = "abc123456xyz789"; |
| 39 | const char kSharedSecret[] = "1234-1234-5678"; |
| 40 | const char kSharedSecretBad[] = "0000-0000-0001"; |
| 41 | |
| 42 | } // namespace |
| 43 | |
| 44 | namespace remoting { |
| 45 | namespace protocol { |
| 46 | |
| 47 | class ThirdPartyAuthenticatorTest : public AuthenticatorTestBase { |
sergeyu | 1acf67ba | 2016-03-10 02:59:14 | [diff] [blame] | 48 | class FakeTokenFetcher { |
[email protected] | 9a6361d0 | 2013-03-23 16:27:52 | [diff] [blame] | 49 | public: |
dcheng | 562aba5 | 2014-10-21 12:30:14 | [diff] [blame] | 50 | void FetchThirdPartyToken( |
sergeyu | 1acf67ba | 2016-03-10 02:59:14 | [diff] [blame] | 51 | const std::string& token_url, |
[email protected] | 9a6361d0 | 2013-03-23 16:27:52 | [diff] [blame] | 52 | const std::string& scope, |
sergeyu | 1acf67ba | 2016-03-10 02:59:14 | [diff] [blame] | 53 | const ThirdPartyTokenFetchedCallback& token_fetched_callback) { |
| 54 | ASSERT_EQ(token_url, kTokenUrl); |
| 55 | ASSERT_EQ(scope, kTokenScope); |
| 56 | ASSERT_FALSE(token_fetched_callback.is_null()); |
| 57 | on_token_fetched_ = token_fetched_callback; |
[email protected] | 9a6361d0 | 2013-03-23 16:27:52 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | void OnTokenFetched(const std::string& token, |
| 61 | const std::string& shared_secret) { |
| 62 | ASSERT_FALSE(on_token_fetched_.is_null()); |
sergeyu | d9bdcb6 | 2015-04-25 00:18:08 | [diff] [blame] | 63 | base::ResetAndReturn(&on_token_fetched_).Run(token, shared_secret); |
[email protected] | 9a6361d0 | 2013-03-23 16:27:52 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | private: |
sergeyu | 1acf67ba | 2016-03-10 02:59:14 | [diff] [blame] | 67 | ThirdPartyTokenFetchedCallback on_token_fetched_; |
[email protected] | 9a6361d0 | 2013-03-23 16:27:52 | [diff] [blame] | 68 | }; |
| 69 | |
[email protected] | d95ee26 | 2014-02-26 06:30:31 | [diff] [blame] | 70 | class FakeTokenValidator : public TokenValidator { |
[email protected] | 9a6361d0 | 2013-03-23 16:27:52 | [diff] [blame] | 71 | public: |
| 72 | FakeTokenValidator() |
| 73 | : token_url_(kTokenUrl), |
| 74 | token_scope_(kTokenScope) {} |
| 75 | |
Chris Watkins | 6fe52aa | 2017-11-28 03:24:05 | [diff] [blame] | 76 | ~FakeTokenValidator() override = default; |
[email protected] | 9a6361d0 | 2013-03-23 16:27:52 | [diff] [blame] | 77 | |
dcheng | 562aba5 | 2014-10-21 12:30:14 | [diff] [blame] | 78 | void ValidateThirdPartyToken( |
[email protected] | 9a6361d0 | 2013-03-23 16:27:52 | [diff] [blame] | 79 | const std::string& token, |
mostynb | 4faccee | 2014-10-09 09:33:25 | [diff] [blame] | 80 | const TokenValidatedCallback& token_validated_callback) override { |
[email protected] | 9a6361d0 | 2013-03-23 16:27:52 | [diff] [blame] | 81 | ASSERT_FALSE(token_validated_callback.is_null()); |
| 82 | on_token_validated_ = token_validated_callback; |
| 83 | } |
| 84 | |
| 85 | void OnTokenValidated(const std::string& shared_secret) { |
| 86 | ASSERT_FALSE(on_token_validated_.is_null()); |
sergeyu | d9bdcb6 | 2015-04-25 00:18:08 | [diff] [blame] | 87 | base::ResetAndReturn(&on_token_validated_).Run(shared_secret); |
[email protected] | 9a6361d0 | 2013-03-23 16:27:52 | [diff] [blame] | 88 | } |
| 89 | |
dcheng | 562aba5 | 2014-10-21 12:30:14 | [diff] [blame] | 90 | const GURL& token_url() const override { return token_url_; } |
[email protected] | 9a6361d0 | 2013-03-23 16:27:52 | [diff] [blame] | 91 | |
dcheng | 562aba5 | 2014-10-21 12:30:14 | [diff] [blame] | 92 | const std::string& token_scope() const override { return token_scope_; } |
[email protected] | 9a6361d0 | 2013-03-23 16:27:52 | [diff] [blame] | 93 | |
| 94 | private: |
| 95 | GURL token_url_; |
| 96 | std::string token_scope_; |
| 97 | base::Callback<void(const std::string& shared_secret)> on_token_validated_; |
| 98 | }; |
| 99 | |
| 100 | public: |
Chris Watkins | 6fe52aa | 2017-11-28 03:24:05 | [diff] [blame] | 101 | ThirdPartyAuthenticatorTest() = default; |
| 102 | ~ThirdPartyAuthenticatorTest() override = default; |
[email protected] | 9a6361d0 | 2013-03-23 16:27:52 | [diff] [blame] | 103 | |
| 104 | protected: |
| 105 | void InitAuthenticators() { |
sergeyu | 12e320a | 2016-03-08 18:10:28 | [diff] [blame] | 106 | token_validator_ = new FakeTokenValidator(); |
[email protected] | 9a6361d0 | 2013-03-23 16:27:52 | [diff] [blame] | 107 | host_.reset(new ThirdPartyHostAuthenticator( |
sergeyu | 12e320a | 2016-03-08 18:10:28 | [diff] [blame] | 108 | base::Bind(&V2Authenticator::CreateForHost, host_cert_, key_pair_), |
dcheng | 0765c49 | 2016-04-06 22:41:53 | [diff] [blame] | 109 | base::WrapUnique(token_validator_))); |
sergeyu | 12e320a | 2016-03-08 18:10:28 | [diff] [blame] | 110 | client_.reset(new ThirdPartyClientAuthenticator( |
| 111 | base::Bind(&V2Authenticator::CreateForClient), |
sergeyu | 1acf67ba | 2016-03-10 02:59:14 | [diff] [blame] | 112 | base::Bind(&FakeTokenFetcher::FetchThirdPartyToken, |
| 113 | base::Unretained(&token_fetcher_)))); |
[email protected] | 9a6361d0 | 2013-03-23 16:27:52 | [diff] [blame] | 114 | } |
| 115 | |
sergeyu | 1acf67ba | 2016-03-10 02:59:14 | [diff] [blame] | 116 | FakeTokenFetcher token_fetcher_; |
[email protected] | 9a6361d0 | 2013-03-23 16:27:52 | [diff] [blame] | 117 | FakeTokenValidator* token_validator_; |
| 118 | |
| 119 | private: |
| 120 | DISALLOW_COPY_AND_ASSIGN(ThirdPartyAuthenticatorTest); |
| 121 | }; |
| 122 | |
[email protected] | 591cffcd | 2014-08-18 20:02:30 | [diff] [blame] | 123 | TEST_F(ThirdPartyAuthenticatorTest, SuccessfulAuth) { |
[email protected] | 9a6361d0 | 2013-03-23 16:27:52 | [diff] [blame] | 124 | ASSERT_NO_FATAL_FAILURE(InitAuthenticators()); |
| 125 | ASSERT_NO_FATAL_FAILURE(RunHostInitiatedAuthExchange()); |
| 126 | ASSERT_EQ(Authenticator::PROCESSING_MESSAGE, client_->state()); |
sergeyu | 1acf67ba | 2016-03-10 02:59:14 | [diff] [blame] | 127 | ASSERT_NO_FATAL_FAILURE(token_fetcher_.OnTokenFetched(kToken, kSharedSecret)); |
[email protected] | 9a6361d0 | 2013-03-23 16:27:52 | [diff] [blame] | 128 | ASSERT_EQ(Authenticator::PROCESSING_MESSAGE, host_->state()); |
sergeyu | 1acf67ba | 2016-03-10 02:59:14 | [diff] [blame] | 129 | ASSERT_NO_FATAL_FAILURE(token_validator_->OnTokenValidated(kSharedSecret)); |
[email protected] | 9a6361d0 | 2013-03-23 16:27:52 | [diff] [blame] | 130 | |
| 131 | // Both sides have finished. |
| 132 | ASSERT_EQ(Authenticator::ACCEPTED, host_->state()); |
| 133 | ASSERT_EQ(Authenticator::ACCEPTED, client_->state()); |
| 134 | |
| 135 | // An authenticated channel can be created after the authentication. |
| 136 | client_auth_ = client_->CreateChannelAuthenticator(); |
| 137 | host_auth_ = host_->CreateChannelAuthenticator(); |
| 138 | RunChannelAuth(false); |
| 139 | |
| 140 | StreamConnectionTester tester(host_socket_.get(), client_socket_.get(), |
| 141 | kMessageSize, kMessages); |
| 142 | |
| 143 | tester.Start(); |
fdoray | 2ad58be | 2016-06-22 20:36:16 | [diff] [blame] | 144 | base::RunLoop().Run(); |
[email protected] | 9a6361d0 | 2013-03-23 16:27:52 | [diff] [blame] | 145 | tester.CheckResults(); |
| 146 | } |
| 147 | |
[email protected] | 591cffcd | 2014-08-18 20:02:30 | [diff] [blame] | 148 | TEST_F(ThirdPartyAuthenticatorTest, ClientNoSecret) { |
[email protected] | 9a6361d0 | 2013-03-23 16:27:52 | [diff] [blame] | 149 | ASSERT_NO_FATAL_FAILURE(InitAuthenticators()); |
| 150 | ASSERT_NO_FATAL_FAILURE(RunHostInitiatedAuthExchange()); |
| 151 | ASSERT_EQ(Authenticator::PROCESSING_MESSAGE, client_->state()); |
sergeyu | 1acf67ba | 2016-03-10 02:59:14 | [diff] [blame] | 152 | ASSERT_NO_FATAL_FAILURE(token_fetcher_.OnTokenFetched(kToken, std::string())); |
[email protected] | 9a6361d0 | 2013-03-23 16:27:52 | [diff] [blame] | 153 | |
| 154 | // The end result is that the client rejected the connection, since it |
| 155 | // couldn't fetch the secret. |
| 156 | ASSERT_EQ(Authenticator::REJECTED, client_->state()); |
| 157 | } |
| 158 | |
[email protected] | 591cffcd | 2014-08-18 20:02:30 | [diff] [blame] | 159 | TEST_F(ThirdPartyAuthenticatorTest, InvalidToken) { |
[email protected] | 9a6361d0 | 2013-03-23 16:27:52 | [diff] [blame] | 160 | ASSERT_NO_FATAL_FAILURE(InitAuthenticators()); |
| 161 | ASSERT_NO_FATAL_FAILURE(RunHostInitiatedAuthExchange()); |
| 162 | ASSERT_EQ(Authenticator::PROCESSING_MESSAGE, client_->state()); |
sergeyu | 1acf67ba | 2016-03-10 02:59:14 | [diff] [blame] | 163 | ASSERT_NO_FATAL_FAILURE(token_fetcher_.OnTokenFetched( |
[email protected] | 9a6361d0 | 2013-03-23 16:27:52 | [diff] [blame] | 164 | kToken, kSharedSecret)); |
| 165 | ASSERT_EQ(Authenticator::PROCESSING_MESSAGE, host_->state()); |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 166 | ASSERT_NO_FATAL_FAILURE(token_validator_->OnTokenValidated(std::string())); |
[email protected] | 9a6361d0 | 2013-03-23 16:27:52 | [diff] [blame] | 167 | |
| 168 | // The end result is that the host rejected the token. |
| 169 | ASSERT_EQ(Authenticator::REJECTED, host_->state()); |
| 170 | } |
| 171 | |
[email protected] | 591cffcd | 2014-08-18 20:02:30 | [diff] [blame] | 172 | TEST_F(ThirdPartyAuthenticatorTest, CannotFetchToken) { |
[email protected] | 9a6361d0 | 2013-03-23 16:27:52 | [diff] [blame] | 173 | ASSERT_NO_FATAL_FAILURE(InitAuthenticators()); |
| 174 | ASSERT_NO_FATAL_FAILURE(RunHostInitiatedAuthExchange()); |
| 175 | ASSERT_EQ(Authenticator::PROCESSING_MESSAGE, client_->state()); |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 176 | ASSERT_NO_FATAL_FAILURE( |
sergeyu | 1acf67ba | 2016-03-10 02:59:14 | [diff] [blame] | 177 | token_fetcher_.OnTokenFetched(std::string(), std::string())); |
[email protected] | 9a6361d0 | 2013-03-23 16:27:52 | [diff] [blame] | 178 | |
| 179 | // The end result is that the client rejected the connection, since it |
| 180 | // couldn't fetch the token. |
| 181 | ASSERT_EQ(Authenticator::REJECTED, client_->state()); |
| 182 | } |
| 183 | |
| 184 | // Test that negotiation stops when the fake authentication is rejected. |
[email protected] | 591cffcd | 2014-08-18 20:02:30 | [diff] [blame] | 185 | TEST_F(ThirdPartyAuthenticatorTest, HostBadSecret) { |
[email protected] | 9a6361d0 | 2013-03-23 16:27:52 | [diff] [blame] | 186 | ASSERT_NO_FATAL_FAILURE(InitAuthenticators()); |
| 187 | ASSERT_NO_FATAL_FAILURE(RunHostInitiatedAuthExchange()); |
| 188 | ASSERT_EQ(Authenticator::PROCESSING_MESSAGE, client_->state()); |
sergeyu | 1acf67ba | 2016-03-10 02:59:14 | [diff] [blame] | 189 | ASSERT_NO_FATAL_FAILURE(token_fetcher_.OnTokenFetched(kToken, kSharedSecret)); |
[email protected] | 9a6361d0 | 2013-03-23 16:27:52 | [diff] [blame] | 190 | ASSERT_EQ(Authenticator::PROCESSING_MESSAGE, host_->state()); |
| 191 | ASSERT_NO_FATAL_FAILURE( |
| 192 | token_validator_->OnTokenValidated(kSharedSecretBad)); |
| 193 | |
| 194 | // The end result is that the host rejected the fake authentication. |
| 195 | ASSERT_EQ(Authenticator::REJECTED, client_->state()); |
| 196 | } |
| 197 | |
[email protected] | 591cffcd | 2014-08-18 20:02:30 | [diff] [blame] | 198 | TEST_F(ThirdPartyAuthenticatorTest, ClientBadSecret) { |
[email protected] | 9a6361d0 | 2013-03-23 16:27:52 | [diff] [blame] | 199 | ASSERT_NO_FATAL_FAILURE(InitAuthenticators()); |
| 200 | ASSERT_NO_FATAL_FAILURE(RunHostInitiatedAuthExchange()); |
| 201 | ASSERT_EQ(Authenticator::PROCESSING_MESSAGE, client_->state()); |
| 202 | ASSERT_NO_FATAL_FAILURE( |
sergeyu | 1acf67ba | 2016-03-10 02:59:14 | [diff] [blame] | 203 | token_fetcher_.OnTokenFetched(kToken, kSharedSecretBad)); |
[email protected] | 9a6361d0 | 2013-03-23 16:27:52 | [diff] [blame] | 204 | ASSERT_EQ(Authenticator::PROCESSING_MESSAGE, host_->state()); |
| 205 | ASSERT_NO_FATAL_FAILURE( |
| 206 | token_validator_->OnTokenValidated(kSharedSecret)); |
| 207 | |
| 208 | // The end result is that the host rejected the fake authentication. |
| 209 | ASSERT_EQ(Authenticator::REJECTED, client_->state()); |
| 210 | } |
| 211 | |
| 212 | } // namespace protocol |
| 213 | } // namespace remoting |