pairing_registry.cc revision b2df76ea8fec9e32f6f3718986dba0d95315b29c
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/pairing_registry.h" 6 7#include "base/base64.h" 8#include "base/bind.h" 9#include "base/guid.h" 10#include "crypto/random.h" 11 12namespace remoting { 13namespace protocol { 14 15// How many bytes of random data to use for the client id and shared secret. 16const int kKeySize = 16; 17 18PairingRegistry::PairingRegistry(scoped_ptr<Delegate> delegate, 19 const PairedClients& paired_clients) 20 : delegate_(delegate.Pass()) { 21 DCHECK(delegate_); 22 paired_clients_ = paired_clients; 23} 24 25PairingRegistry::~PairingRegistry() { 26} 27 28const PairingRegistry::Pairing& PairingRegistry::CreatePairing( 29 const std::string& client_name) { 30 DCHECK(CalledOnValidThread()); 31 32 Pairing result; 33 result.client_name = client_name; 34 result.client_id = base::GenerateGUID(); 35 36 // Create a random shared secret to authenticate this client. 37 char buffer[kKeySize]; 38 crypto::RandBytes(buffer, arraysize(buffer)); 39 result.shared_secret = std::string(buffer, buffer+arraysize(buffer)); 40 41 // Save the result via the Delegate and return it to the caller. 42 paired_clients_[result.client_id] = result; 43 delegate_->Save(paired_clients_); 44 45 return paired_clients_[result.client_id]; 46} 47 48std::string PairingRegistry::GetSecret(const std::string& client_id) const { 49 DCHECK(CalledOnValidThread()); 50 51 std::string result; 52 PairedClients::const_iterator i = paired_clients_.find(client_id); 53 if (i != paired_clients_.end()) { 54 result = i->second.shared_secret; 55 } 56 return result; 57} 58 59} // namespace protocol 60} // namespace remoting 61