xref: /aosp_15_r20/external/tink/cc/jwt/internal/jwt_public_key_sign_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_PUBLIC_KEY_SIGN_IMPL_H_
18 #define TINK_JWT_INTERNAL_JWT_PUBLIC_KEY_SIGN_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_public_key_sign_internal.h"
26 #include "tink/jwt/raw_jwt.h"
27 #include "tink/public_key_sign.h"
28 #include "tink/util/status.h"
29 #include "tink/util/statusor.h"
30 
31 namespace crypto {
32 namespace tink {
33 namespace jwt_internal {
34 
35 class JwtPublicKeySignImpl : public JwtPublicKeySignInternal {
36  public:
JwtPublicKeySignImpl(std::unique_ptr<crypto::tink::PublicKeySign> sign,absl::string_view algorithm,absl::optional<absl::string_view> custom_kid)37   explicit JwtPublicKeySignImpl(
38       std::unique_ptr<crypto::tink::PublicKeySign> sign,
39       absl::string_view algorithm,
40       absl::optional<absl::string_view> custom_kid) {
41     sign_ = std::move(sign);
42     algorithm_ = std::string(algorithm);
43     if (custom_kid.has_value()) {
44       custom_kid_ = std::string(*custom_kid);
45     }
46   }
47   crypto::tink::util::StatusOr<std::string> SignAndEncodeWithKid(
48       const crypto::tink::RawJwt& token,
49       absl::optional<absl::string_view> kid) const override;
50 
51  private:
52   std::unique_ptr<crypto::tink::PublicKeySign> sign_;
53   std::string algorithm_;
54   // custom_kid may be set when a key is converted from another format, for
55   // example JWK. It does not have any relation to the key id. It can only be
56   // set for keys with output prefix RAW.
57   absl::optional<std::string> custom_kid_;
58 };
59 
60 }  // namespace jwt_internal
61 }  // namespace tink
62 }  // namespace crypto
63 
64 #endif  // TINK_JWT_INTERNAL_JWT_PUBLIC_KEY_SIGN_IMPL_H_
65