Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(114)

Unified Diff: components/cryptauth/wire_message.cc

Issue 2849493002: [EasyUnlock] Fixing the setup connection finder and message format. (Closed)
Patch Set: date Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « components/cryptauth/wire_message.h ('k') | components/cryptauth/wire_message_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: components/cryptauth/wire_message.cc
diff --git a/components/cryptauth/wire_message.cc b/components/cryptauth/wire_message.cc
index b91b943558934b46b587e7d522b246430ea97838..0e29872e58d8fa8443841dae54a95b54dda60149 100644
--- a/components/cryptauth/wire_message.cc
+++ b/components/cryptauth/wire_message.cc
@@ -20,8 +20,11 @@
// The wire messages have a simple format:
// [ message version ] [ body length ] [ JSON body ]
// 1 byte 2 bytes body length
-// The JSON body contains two fields: an optional permit_id field and a required
-// data field.
+// When sending encrypted messages, the JSON body contains two fields: an
+// optional |permit_id| field and a required |payload| field.
+//
+// For non-encrypted messages, the message itself is the JSON body, and it
+// doesn't have a |payload| field.
namespace cryptauth {
namespace {
@@ -104,8 +107,14 @@ std::unique_ptr<WireMessage> WireMessage::Deserialize(
DCHECK(success);
std::string payload_base64;
- if (!body->GetString(kPayloadKey, &payload_base64) ||
- payload_base64.empty()) {
+ if (!body->GetString(kPayloadKey, &payload_base64)) {
+ // The body is a valid JSON, but it doesn't contain a |payload| field. It
+ // must be a non-encrypted message.
+ return base::WrapUnique(
+ new WireMessage(serialized_message.substr(kHeaderLength)));
+ }
+
+ if (payload_base64.empty()) {
PA_LOG(WARNING) << "Error: Missing payload.";
return nullptr;
}
@@ -127,24 +136,29 @@ std::unique_ptr<WireMessage> WireMessage::Deserialize(
}
std::string WireMessage::Serialize() const {
- if (payload_.empty()) {
- PA_LOG(ERROR) << "Failed to serialize empty wire message.";
- return std::string();
- }
-
- // Create JSON body containing permit id and payload.
- base::DictionaryValue body;
-
- std::string base64_payload;
- base::Base64UrlEncode(payload_, base::Base64UrlEncodePolicy::INCLUDE_PADDING,
- &base64_payload);
- body.SetString(kPayloadKey, base64_payload);
- body.SetString(kFeatureKey, feature_);
-
std::string json_body;
- if (!base::JSONWriter::Write(body, &json_body)) {
- PA_LOG(ERROR) << "Failed to convert WireMessage body to JSON: " << body;
- return std::string();
+ if (body_.empty()) {
+ if (payload_.empty()) {
+ PA_LOG(ERROR) << "Failed to serialize empty wire message.";
+ return std::string();
+ }
+
+ // Create JSON body containing permit id and payload.
+ base::DictionaryValue body;
+
+ std::string base64_payload;
+ base::Base64UrlEncode(payload_,
+ base::Base64UrlEncodePolicy::INCLUDE_PADDING,
+ &base64_payload);
+ body.SetString(kPayloadKey, base64_payload);
+ body.SetString(kFeatureKey, feature_);
+
+ if (!base::JSONWriter::Write(body, &json_body)) {
+ PA_LOG(ERROR) << "Failed to convert WireMessage body to JSON: " << body;
+ return std::string();
+ }
+ } else {
+ json_body = body_;
}
// Create header containing version and payload size.
@@ -170,4 +184,6 @@ std::string WireMessage::Serialize() const {
WireMessage::WireMessage(const std::string& payload, const std::string& feature)
: payload_(payload), feature_(feature) {}
+WireMessage::WireMessage(const std::string& body) : body_(body) {}
+
} // namespace cryptauth
« no previous file with comments | « components/cryptauth/wire_message.h ('k') | components/cryptauth/wire_message_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698