xref: /aosp_15_r20/external/tink/cc/jwt/internal/jwt_format.h (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 #ifndef TINK_JWT_INTERNAL_JWT_FORMAT_H_
18 #define TINK_JWT_INTERNAL_JWT_FORMAT_H_
19 
20 #include <string>
21 
22 #include "google/protobuf/struct.pb.h"
23 #include "tink/jwt/raw_jwt.h"
24 #include "tink/util/status.h"
25 #include "tink/util/statusor.h"
26 #include "proto/tink.pb.h"
27 
28 namespace crypto {
29 namespace tink {
30 namespace jwt_internal {
31 
32 std::string EncodeHeader(absl::string_view json_header);
33 bool DecodeHeader(absl::string_view header, std::string* json_header);
34 
35 absl::optional<std::string> GetKid(
36     uint32_t key_id,
37     ::google::crypto::tink::OutputPrefixType output_prefix_type);
38 absl::optional<uint32_t> GetKeyId(absl::string_view kid);
39 
40 util::StatusOr<std::string> CreateHeader(absl::string_view algorithm,
41                          absl::optional<absl::string_view> type_header,
42                          absl::optional<absl::string_view> kid);
43 util::Status ValidateHeader(const google::protobuf::Struct& header,
44                             absl::string_view algorithm,
45                             absl::optional<absl::string_view> tink_kid,
46                             absl::optional<absl::string_view> custom_kid);
47 absl::optional<std::string> GetTypeHeader(
48     const google::protobuf::Struct& header);
49 
50 std::string EncodePayload(absl::string_view json_payload);
51 bool DecodePayload(absl::string_view payload, std::string* json_payload);
52 
53 std::string EncodeSignature(absl::string_view signature);
54 bool DecodeSignature(absl::string_view encoded_signature,
55                      std::string* signature);
56 
57 class RawJwtParser {
58  public:
59   static util::StatusOr<RawJwt> FromJson(
60       absl::optional<std::string> type_header, absl::string_view json_payload);
61 };
62 
63 }  // namespace jwt_internal
64 }  // namespace tink
65 }  // namespace crypto
66 
67 #endif  // TINK_JWT_INTERNAL_JWT_FORMAT_H_
68