Add 'sessionId' into privet/info

Current implementation is a Unix timestamp plus counter.

BUG=26140026

Change-Id: Idfe0aa81c49e6dab5d638cbedfbeb460b70b5864
Reviewed-on: https://weave-review.googlesource.com/2010
Reviewed-by: Alex Vakulenko <avakulenko@google.com>
diff --git a/src/privet/auth_manager.cc b/src/privet/auth_manager.cc
index 62a640f..51d7592 100644
--- a/src/privet/auth_manager.cc
+++ b/src/privet/auth_manager.cc
@@ -27,6 +27,12 @@
 const size_t kMaxMacaroonSize = 1024;
 const size_t kMaxPendingClaims = 10;
 
+template <class T>
+void AppendToArray(T value, std::vector<uint8_t>* array) {
+  auto begin = reinterpret_cast<const uint8_t*>(&value);
+  array->insert(array->end(), begin, begin + sizeof(value));
+}
+
 // Returns "scope:id:time".
 std::string CreateTokenData(const UserInfo& user_info, const base::Time& time) {
   return base::IntToString(static_cast<int>(user_info.scope())) +
@@ -230,5 +236,12 @@
   return uw_macaroon_verify_(&macaroon, secret_.data(), secret_.size());
 }
 
+std::vector<uint8_t> AuthManager::CreateSessionId() {
+  std::vector<uint8_t> result;
+  AppendToArray(Now().ToTimeT(), &result);
+  AppendToArray(++session_counter_, &result);
+  return result;
+}
+
 }  // namespace privet
 }  // namespace weave
diff --git a/src/privet/auth_manager.h b/src/privet/auth_manager.h
index 44b8bca..a74e43b 100644
--- a/src/privet/auth_manager.h
+++ b/src/privet/auth_manager.h
@@ -55,10 +55,13 @@
   void SetSecret(const std::vector<uint8_t>& secret,
                  RootClientTokenOwner owner);
 
+  std::vector<uint8_t> CreateSessionId();
+
  private:
   Config* config_{nullptr};
   base::DefaultClock default_clock_;
   base::Clock* clock_{&default_clock_};
+  uint32_t session_counter_{0};
 
   std::vector<uint8_t> secret_;
   std::vector<uint8_t> certificate_fingerprint_;
diff --git a/src/privet/mock_delegates.h b/src/privet/mock_delegates.h
index de94fe9..8cede23 100644
--- a/src/privet/mock_delegates.h
+++ b/src/privet/mock_delegates.h
@@ -80,6 +80,7 @@
                     std::string*,
                     ErrorPtr*));
   MOCK_METHOD2(CancelPairing, bool(const std::string&, ErrorPtr*));
+  MOCK_METHOD0(CreateSessionId, std::string());
 
   MockSecurityDelegate() {
     EXPECT_CALL(*this, CreateAccessToken(_))
@@ -114,6 +115,7 @@
         .WillRepeatedly(DoAll(SetArgPointee<2>("testFingerprint"),
                               SetArgPointee<3>("testSignature"), Return(true)));
     EXPECT_CALL(*this, CancelPairing(_, _)).WillRepeatedly(Return(true));
+    EXPECT_CALL(*this, CreateSessionId()).WillRepeatedly(Return("SessionId"));
   }
 };
 
diff --git a/src/privet/privet_handler.cc b/src/privet/privet_handler.cc
index 57c8633..d7e68bb 100644
--- a/src/privet/privet_handler.cc
+++ b/src/privet/privet_handler.cc
@@ -69,8 +69,8 @@
 const char kInfoWifiCapabilitiesKey[] = "capabilities";
 const char kInfoWifiSsidKey[] = "ssid";
 const char kInfoWifiHostedSsidKey[] = "hostedSsid";
-
 const char kInfoTimeKey[] = "time";
+const char kInfoSessionIdKey[] = "sessionId";
 
 const char kPairingKey[] = "pairing";
 const char kPairingSessionIdKey[] = "sessionId";
@@ -607,6 +607,7 @@
   output.Set(kGcdKey, CreateGcdSection(*cloud_).release());
 
   output.SetDouble(kInfoTimeKey, clock_->Now().ToJsTime());
+  output.SetString(kInfoSessionIdKey, security_->CreateSessionId());
 
   callback.Run(http::kOk, output);
 }
diff --git a/src/privet/privet_handler_unittest.cc b/src/privet/privet_handler_unittest.cc
index fcbd0d0..c4180b9 100644
--- a/src/privet/privet_handler_unittest.cc
+++ b/src/privet/privet_handler_unittest.cc
@@ -247,7 +247,8 @@
       'id': '',
       'status': 'disabled'
     },
-    'time': 1410000001000.0
+    'time': 1410000001000.0,
+    'sessionId': 'SessionId'
   })";
   EXPECT_JSON_EQ(kExpected, HandleRequest("/privet/info", "{}"));
 }
@@ -308,7 +309,8 @@
       'id': 'TestCloudId',
       'status': 'online'
     },
-    'time': 1410000001000.0
+    'time': 1410000001000.0,
+    'sessionId': 'SessionId'
   })";
   EXPECT_JSON_EQ(kExpected, HandleRequest("/privet/info", "{}"));
 }
diff --git a/src/privet/security_delegate.h b/src/privet/security_delegate.h
index 54f957d..d8090bd 100644
--- a/src/privet/security_delegate.h
+++ b/src/privet/security_delegate.h
@@ -60,6 +60,8 @@
 
   virtual bool CancelPairing(const std::string& session_id,
                              ErrorPtr* error) = 0;
+
+  virtual std::string CreateSessionId() = 0;
 };
 
 }  // namespace privet
diff --git a/src/privet/security_manager.cc b/src/privet/security_manager.cc
index d2025b1..46212f4 100644
--- a/src/privet/security_manager.cc
+++ b/src/privet/security_manager.cc
@@ -317,6 +317,10 @@
   return false;
 }
 
+std::string SecurityManager::CreateSessionId() {
+  return Base64Encode(auth_manager_->CreateSessionId());
+}
+
 void SecurityManager::RegisterPairingListeners(
     const PairingStartListener& on_start,
     const PairingEndListener& on_end) {
diff --git a/src/privet/security_manager.h b/src/privet/security_manager.h
index 93618d2..2505099 100644
--- a/src/privet/security_manager.h
+++ b/src/privet/security_manager.h
@@ -69,7 +69,6 @@
   bool ConfirmClientAuthToken(const std::string& token,
                               ErrorPtr* error) override;
   bool IsValidPairingCode(const std::string& auth_code) const override;
-
   bool StartPairing(PairingType mode,
                     CryptoType crypto,
                     std::string* session_id,
@@ -82,6 +81,7 @@
                       std::string* signature,
                       ErrorPtr* error) override;
   bool CancelPairing(const std::string& session_id, ErrorPtr* error) override;
+  std::string CreateSessionId() override;
 
   void RegisterPairingListeners(const PairingStartListener& on_start,
                                 const PairingEndListener& on_end);