xref: /aosp_15_r20/external/tink/cc/jwt/internal/jwt_mac_impl.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_MAC_IMPL_H_
18 #define TINK_JWT_INTERNAL_JWT_MAC_IMPL_H_
19 
20 #include <memory>
21 #include <string>
22 #include <utility>
23 
24 #include "absl/strings/string_view.h"
25 #include "tink/jwt/internal/jwt_mac_internal.h"
26 #include "tink/jwt/jwt_mac.h"
27 #include "tink/jwt/jwt_validator.h"
28 #include "tink/jwt/raw_jwt.h"
29 #include "tink/jwt/verified_jwt.h"
30 #include "tink/mac.h"
31 #include "tink/util/status.h"
32 #include "tink/util/statusor.h"
33 
34 namespace crypto {
35 namespace tink {
36 namespace jwt_internal {
37 
38 class JwtMacImpl : public JwtMacInternal {
39  public:
JwtMacImpl(std::unique_ptr<crypto::tink::Mac> mac,absl::string_view algorithm,absl::optional<absl::string_view> custom_kid)40   explicit JwtMacImpl(std::unique_ptr<crypto::tink::Mac> mac,
41                       absl::string_view algorithm,
42                       absl::optional<absl::string_view> custom_kid) {
43     mac_ = std::move(mac);
44     algorithm_ = std::string(algorithm);
45     if (custom_kid.has_value()) {
46       custom_kid_ = std::string(*custom_kid);
47     }
48   }
49 
50   crypto::tink::util::StatusOr<std::string> ComputeMacAndEncodeWithKid(
51       const crypto::tink::RawJwt& token,
52       absl::optional<absl::string_view> kid) const override;
53 
54   crypto::tink::util::StatusOr<crypto::tink::VerifiedJwt>
55   VerifyMacAndDecodeWithKid(
56       absl::string_view compact, const crypto::tink::JwtValidator& validator,
57       absl::optional<absl::string_view> kid) const override;
58 
59  private:
60   std::unique_ptr<crypto::tink::Mac> mac_;
61   std::string algorithm_;
62   absl::optional<std::string> custom_kid_;
63 };
64 
65 }  // namespace jwt_internal
66 }  // namespace tink
67 }  // namespace crypto
68 
69 #endif  // TINK_JWT_INTERNAL_JWT_MAC_IMPL_H_
70