xref: /aosp_15_r20/external/tink/cc/jwt/internal/json_util.cc (revision e7b1675dde1b92d52ec075b0a92829627f2c52a5)
1 // Copyright 2021 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 //
15 ///////////////////////////////////////////////////////////////////////////////
16 
17 #include "tink/jwt/internal/json_util.h"
18 
19 #include <string>
20 
21 #include <google/protobuf/util/json_util.h>
22 #include "absl/status/status.h"
23 #include "absl/strings/substitute.h"
24 
25 namespace crypto {
26 namespace tink {
27 namespace jwt_internal {
28 
29 namespace {
30 
ConvertProtoStatus(const google::protobuf::util::Status & status)31 util::Status ConvertProtoStatus(const google::protobuf::util::Status& status) {
32   return util::Status(static_cast<absl::StatusCode>(status.code()),
33                                     std::string(status.message().data(), status.message().length()));
34 }
35 
36 }  // namespace
37 
JsonStringToProtoStruct(absl::string_view json_string)38 util::StatusOr<google::protobuf::Struct> JsonStringToProtoStruct(
39     absl::string_view json_string) {
40   google::protobuf::Struct proto;
41   google::protobuf::util::JsonParseOptions json_parse_options;
42   auto status = google::protobuf::util::JsonStringToMessage(google::protobuf::StringPiece(json_string.data(), json_string.length()), &proto,
43                                                   json_parse_options);
44   if (!status.ok()) {
45     return util::Status(absl::StatusCode::kInvalidArgument, "invalid JSON");
46   }
47   return proto;
48 }
49 
JsonStringToProtoList(absl::string_view json_string)50 util::StatusOr<google::protobuf::ListValue> JsonStringToProtoList(
51     absl::string_view json_string) {
52   google::protobuf::ListValue proto;
53   google::protobuf::util::JsonParseOptions json_parse_options;
54   auto status = google::protobuf::util::JsonStringToMessage(google::protobuf::StringPiece(json_string.data(), json_string.length()), &proto,
55                                                   json_parse_options);
56   if (!status.ok()) {
57     return util::Status(absl::StatusCode::kInvalidArgument, "invalid JSON");
58   }
59   return proto;
60 }
61 
ProtoStructToJsonString(const google::protobuf::Struct & proto)62 util::StatusOr<std::string> ProtoStructToJsonString(
63     const google::protobuf::Struct& proto) {
64   std::string output;
65   auto status = google::protobuf::util::MessageToJsonString(proto, &output);
66   if (!status.ok()) {
67     return ConvertProtoStatus(status);
68   }
69   return output;
70 }
71 
ProtoListToJsonString(const google::protobuf::ListValue & proto)72 util::StatusOr<std::string> ProtoListToJsonString(
73     const google::protobuf::ListValue& proto) {
74   std::string output;
75   auto status = google::protobuf::util::MessageToJsonString(proto, &output);
76   if (!status.ok()) {
77     return ConvertProtoStatus(status);
78   }
79   return output;
80 }
81 
82 }  // namespace jwt_internal
83 }  // namespace tink
84 }  // namespace crypto
85