summaryrefslogtreecommitdiffstats
path: root/remoting/protocol/third_party_authenticator_base.cc
blob: 9d6be72db2eabdb2d0c032bba867e9ea8c556ebc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "remoting/protocol/third_party_authenticator_base.h"

#include "base/base64.h"
#include "base/bind.h"
#include "base/callback.h"
#include "base/logging.h"
#include "remoting/base/constants.h"
#include "remoting/base/rsa_key_pair.h"
#include "remoting/protocol/channel_authenticator.h"
#include "remoting/protocol/v2_authenticator.h"
#include "third_party/libjingle/source/talk/xmllite/xmlelement.h"

namespace remoting {
namespace protocol {

// static
const buzz::StaticQName ThirdPartyAuthenticatorBase::kTokenUrlTag =
    { remoting::kChromotingXmlNamespace, "third-party-token-url" };
const buzz::StaticQName ThirdPartyAuthenticatorBase::kTokenScopeTag =
    { remoting::kChromotingXmlNamespace, "third-party-token-scope" };
const buzz::StaticQName ThirdPartyAuthenticatorBase::kTokenTag =
    { remoting::kChromotingXmlNamespace, "third-party-token" };

ThirdPartyAuthenticatorBase::ThirdPartyAuthenticatorBase(
    Authenticator::State initial_state)
    : token_state_(initial_state),
      rejection_reason_(INVALID_CREDENTIALS) {
}

ThirdPartyAuthenticatorBase::~ThirdPartyAuthenticatorBase() {
}

Authenticator::State ThirdPartyAuthenticatorBase::state() const {
  if (token_state_ == ACCEPTED)
    return underlying_->state();
  return token_state_;
}

Authenticator::RejectionReason
ThirdPartyAuthenticatorBase::rejection_reason() const {
  DCHECK_EQ(state(), REJECTED);

  if (token_state_ == REJECTED)
    return rejection_reason_;
  return underlying_->rejection_reason();
}

void ThirdPartyAuthenticatorBase::ProcessMessage(
    const buzz::XmlElement* message,
    const base::Closure& resume_callback) {
  DCHECK_EQ(state(), WAITING_MESSAGE);

  if (token_state_ == WAITING_MESSAGE) {
    ProcessTokenMessage(message, resume_callback);
  } else {
    DCHECK_EQ(token_state_, ACCEPTED);
    DCHECK(underlying_);
    DCHECK_EQ(underlying_->state(), WAITING_MESSAGE);
    underlying_->ProcessMessage(message, resume_callback);
  }
}

scoped_ptr<buzz::XmlElement> ThirdPartyAuthenticatorBase::GetNextMessage() {
  DCHECK_EQ(state(), MESSAGE_READY);

  scoped_ptr<buzz::XmlElement> message;
  if (underlying_ && underlying_->state() == MESSAGE_READY) {
    message = underlying_->GetNextMessage().Pass();
  } else {
    message = CreateEmptyAuthenticatorMessage();
  }

  if (token_state_ == MESSAGE_READY)
    AddTokenElements(message.get());

  return message.Pass();
}

scoped_ptr<ChannelAuthenticator>
ThirdPartyAuthenticatorBase::CreateChannelAuthenticator() const {
  DCHECK_EQ(state(), ACCEPTED);

  return underlying_->CreateChannelAuthenticator();
}

}  // namespace protocol
}  // namespace remoting