Upstreaming browser/ui/uikit_ui_util from iOS.
[chromium-blink-merge.git] / remoting / protocol / pairing_client_authenticator.cc
blob0d3037aa4ea12f710eb503b3ecbf474cacfa2eac
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.
5 #include "remoting/protocol/pairing_client_authenticator.h"
7 #include "base/bind.h"
8 #include "base/logging.h"
9 #include "remoting/base/constants.h"
10 #include "remoting/base/rsa_key_pair.h"
11 #include "remoting/protocol/authentication_method.h"
12 #include "remoting/protocol/channel_authenticator.h"
13 #include "remoting/protocol/v2_authenticator.h"
14 #include "third_party/webrtc/libjingle/xmllite/xmlelement.h"
16 namespace remoting {
17 namespace protocol {
19 PairingClientAuthenticator::PairingClientAuthenticator(
20 const std::string& client_id,
21 const std::string& paired_secret,
22 const FetchSecretCallback& fetch_pin_callback,
23 const std::string& authentication_tag)
24 : sent_client_id_(false),
25 client_id_(client_id),
26 paired_secret_(paired_secret),
27 fetch_pin_callback_(fetch_pin_callback),
28 authentication_tag_(authentication_tag),
29 weak_factory_(this) {
30 v2_authenticator_ = V2Authenticator::CreateForClient(
31 paired_secret_, MESSAGE_READY);
32 using_paired_secret_ = true;
35 PairingClientAuthenticator::~PairingClientAuthenticator() {
38 void PairingClientAuthenticator::CreateV2AuthenticatorWithPIN(
39 State initial_state,
40 const SetAuthenticatorCallback& set_authenticator_callback) {
41 SecretFetchedCallback callback = base::Bind(
42 &PairingClientAuthenticator::OnPinFetched,
43 weak_factory_.GetWeakPtr(), initial_state, set_authenticator_callback);
44 fetch_pin_callback_.Run(true, callback);
47 void PairingClientAuthenticator::AddPairingElements(buzz::XmlElement* message) {
48 // If the client id and secret have not yet been sent, do so now. Note that
49 // in this case the V2Authenticator is being used optimistically to send the
50 // first message of the SPAKE exchange since we don't yet know whether or not
51 // the host will accept the client id or request that we fall back to the PIN.
52 if (!sent_client_id_) {
53 buzz::XmlElement* pairing_tag = new buzz::XmlElement(kPairingInfoTag);
54 pairing_tag->AddAttr(kClientIdAttribute, client_id_);
55 message->AddElement(pairing_tag);
56 sent_client_id_ = true;
60 void PairingClientAuthenticator::OnPinFetched(
61 State initial_state,
62 const SetAuthenticatorCallback& callback,
63 const std::string& pin) {
64 callback.Run(V2Authenticator::CreateForClient(
65 AuthenticationMethod::ApplyHashFunction(
66 AuthenticationMethod::HMAC_SHA256,
67 authentication_tag_, pin),
68 initial_state));
71 } // namespace protocol
72 } // namespace remoting