[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 | |
| 5 | #include "remoting/protocol/third_party_authenticator_base.h" |
| 6 | |
| 7 | #include "base/base64.h" |
| 8 | #include "base/bind.h" |
| 9 | #include "base/callback.h" |
| 10 | #include "base/logging.h" |
| 11 | #include "remoting/base/constants.h" |
| 12 | #include "remoting/base/rsa_key_pair.h" |
| 13 | #include "remoting/protocol/channel_authenticator.h" |
kjellander | f0e410b | 2017-01-04 14:45:01 | [diff] [blame] | 14 | #include "third_party/libjingle_xmpp/xmllite/xmlelement.h" |
[email protected] | 9a6361d0 | 2013-03-23 16:27:52 | [diff] [blame] | 15 | |
| 16 | namespace remoting { |
| 17 | namespace protocol { |
| 18 | |
| 19 | // static |
| 20 | const buzz::StaticQName ThirdPartyAuthenticatorBase::kTokenUrlTag = |
| 21 | { remoting::kChromotingXmlNamespace, "third-party-token-url" }; |
| 22 | const buzz::StaticQName ThirdPartyAuthenticatorBase::kTokenScopeTag = |
| 23 | { remoting::kChromotingXmlNamespace, "third-party-token-scope" }; |
| 24 | const buzz::StaticQName ThirdPartyAuthenticatorBase::kTokenTag = |
| 25 | { remoting::kChromotingXmlNamespace, "third-party-token" }; |
| 26 | |
| 27 | ThirdPartyAuthenticatorBase::ThirdPartyAuthenticatorBase( |
| 28 | Authenticator::State initial_state) |
| 29 | : token_state_(initial_state), |
[email protected] | 064128c | 2014-04-07 22:33:28 | [diff] [blame] | 30 | started_(false), |
[email protected] | 9a6361d0 | 2013-03-23 16:27:52 | [diff] [blame] | 31 | rejection_reason_(INVALID_CREDENTIALS) { |
| 32 | } |
| 33 | |
Chris Watkins | 6fe52aa | 2017-11-28 03:24:05 | [diff] [blame^] | 34 | ThirdPartyAuthenticatorBase::~ThirdPartyAuthenticatorBase() = default; |
[email protected] | 9a6361d0 | 2013-03-23 16:27:52 | [diff] [blame] | 35 | |
[email protected] | 064128c | 2014-04-07 22:33:28 | [diff] [blame] | 36 | bool ThirdPartyAuthenticatorBase::started() const { |
| 37 | return started_; |
| 38 | } |
| 39 | |
[email protected] | 9a6361d0 | 2013-03-23 16:27:52 | [diff] [blame] | 40 | Authenticator::State ThirdPartyAuthenticatorBase::state() const { |
| 41 | if (token_state_ == ACCEPTED) |
| 42 | return underlying_->state(); |
| 43 | return token_state_; |
| 44 | } |
| 45 | |
| 46 | Authenticator::RejectionReason |
| 47 | ThirdPartyAuthenticatorBase::rejection_reason() const { |
| 48 | DCHECK_EQ(state(), REJECTED); |
| 49 | |
| 50 | if (token_state_ == REJECTED) |
| 51 | return rejection_reason_; |
| 52 | return underlying_->rejection_reason(); |
| 53 | } |
| 54 | |
| 55 | void ThirdPartyAuthenticatorBase::ProcessMessage( |
| 56 | const buzz::XmlElement* message, |
| 57 | const base::Closure& resume_callback) { |
| 58 | DCHECK_EQ(state(), WAITING_MESSAGE); |
| 59 | |
| 60 | if (token_state_ == WAITING_MESSAGE) { |
| 61 | ProcessTokenMessage(message, resume_callback); |
| 62 | } else { |
| 63 | DCHECK_EQ(token_state_, ACCEPTED); |
| 64 | DCHECK(underlying_); |
| 65 | DCHECK_EQ(underlying_->state(), WAITING_MESSAGE); |
| 66 | underlying_->ProcessMessage(message, resume_callback); |
| 67 | } |
| 68 | } |
| 69 | |
dcheng | 0765c49 | 2016-04-06 22:41:53 | [diff] [blame] | 70 | std::unique_ptr<buzz::XmlElement> |
| 71 | ThirdPartyAuthenticatorBase::GetNextMessage() { |
[email protected] | 9a6361d0 | 2013-03-23 16:27:52 | [diff] [blame] | 72 | DCHECK_EQ(state(), MESSAGE_READY); |
| 73 | |
dcheng | 0765c49 | 2016-04-06 22:41:53 | [diff] [blame] | 74 | std::unique_ptr<buzz::XmlElement> message; |
[email protected] | 9a6361d0 | 2013-03-23 16:27:52 | [diff] [blame] | 75 | if (underlying_ && underlying_->state() == MESSAGE_READY) { |
sergeyu | aa6fa234 | 2015-12-22 23:26:48 | [diff] [blame] | 76 | message = underlying_->GetNextMessage(); |
[email protected] | 9a6361d0 | 2013-03-23 16:27:52 | [diff] [blame] | 77 | } else { |
| 78 | message = CreateEmptyAuthenticatorMessage(); |
| 79 | } |
| 80 | |
[email protected] | 064128c | 2014-04-07 22:33:28 | [diff] [blame] | 81 | if (token_state_ == MESSAGE_READY) { |
[email protected] | 9a6361d0 | 2013-03-23 16:27:52 | [diff] [blame] | 82 | AddTokenElements(message.get()); |
[email protected] | 064128c | 2014-04-07 22:33:28 | [diff] [blame] | 83 | started_ = true; |
| 84 | } |
sergeyu | aa6fa234 | 2015-12-22 23:26:48 | [diff] [blame] | 85 | return message; |
[email protected] | 9a6361d0 | 2013-03-23 16:27:52 | [diff] [blame] | 86 | } |
| 87 | |
sergeyu | 2a64040 | 2015-08-14 19:52:18 | [diff] [blame] | 88 | const std::string& ThirdPartyAuthenticatorBase::GetAuthKey() const { |
| 89 | DCHECK_EQ(state(), ACCEPTED); |
| 90 | |
| 91 | return underlying_->GetAuthKey(); |
| 92 | } |
| 93 | |
dcheng | 0765c49 | 2016-04-06 22:41:53 | [diff] [blame] | 94 | std::unique_ptr<ChannelAuthenticator> |
[email protected] | 9a6361d0 | 2013-03-23 16:27:52 | [diff] [blame] | 95 | ThirdPartyAuthenticatorBase::CreateChannelAuthenticator() const { |
| 96 | DCHECK_EQ(state(), ACCEPTED); |
| 97 | |
| 98 | return underlying_->CreateChannelAuthenticator(); |
| 99 | } |
| 100 | |
| 101 | } // namespace protocol |
| 102 | } // namespace remoting |