xref: /aosp_15_r20/external/tink/cc/jwt/internal/jwt_public_key_verify_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_VERIFY_IMPL_H_
18 #define TINK_JWT_INTERNAL_JWT_PUBLIC_KEY_VERIFY_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_verify_internal.h"
26 #include "tink/jwt/jwt_validator.h"
27 #include "tink/jwt/raw_jwt.h"
28 #include "tink/jwt/verified_jwt.h"
29 #include "tink/public_key_verify.h"
30 #include "tink/util/status.h"
31 #include "tink/util/statusor.h"
32 
33 namespace crypto {
34 namespace tink {
35 namespace jwt_internal {
36 
37 class JwtPublicKeyVerifyImpl : public JwtPublicKeyVerifyInternal {
38  public:
JwtPublicKeyVerifyImpl(std::unique_ptr<crypto::tink::PublicKeyVerify> verify,absl::string_view algorithm,absl::optional<absl::string_view> custom_kid)39   explicit JwtPublicKeyVerifyImpl(
40       std::unique_ptr<crypto::tink::PublicKeyVerify> verify,
41       absl::string_view algorithm,
42       absl::optional<absl::string_view> custom_kid) {
43     verify_ = std::move(verify);
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<VerifiedJwt> VerifyAndDecodeWithKid(
51       absl::string_view compact, const JwtValidator& validator,
52       absl::optional<absl::string_view> kid) const override;
53 
54  private:
55   std::unique_ptr<crypto::tink::PublicKeyVerify> verify_;
56   std::string algorithm_;
57   // custom_kid may be set when a key is converted from another format, for
58   // example JWK. It does not have any relation to the key id. It can only be
59   // set for keys with output prefix RAW.
60   absl::optional<std::string> custom_kid_;
61 };
62 
63 }  // namespace jwt_internal
64 }  // namespace tink
65 }  // namespace crypto
66 
67 #endif  // TINK_JWT_INTERNAL_JWT_PUBLIC_KEY_VERIFY_IMPL_H_
68