xref: /aosp_15_r20/external/tink/cc/jwt/verified_jwt.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_VERIFIED_JWT_H_
18 #define TINK_JWT_VERIFIED_JWT_H_
19 
20 #include <string>
21 #include <vector>
22 
23 #include "google/protobuf/struct.pb.h"
24 #include "absl/strings/string_view.h"
25 #include "absl/time/clock.h"
26 #include "absl/time/time.h"
27 #include "tink/jwt/raw_jwt.h"
28 #include "tink/util/status.h"
29 #include "tink/util/statusor.h"
30 
31 namespace crypto {
32 namespace tink {
33 
34 namespace jwt_internal {
35 
36 // For friend declaration
37 class JwtMacImpl;
38 class JwtPublicKeyVerifyImpl;
39 
40 }
41 
42 ///////////////////////////////////////////////////////////////////////////////
43 // A decoded and verified JSON Web Token (JWT).
44 //
45 // A new instance of this class is returned as the result of a sucessfully
46 // verification of a MACed or signed compact JWT.
47 //
48 // It gives read-only access all payload claims and a subset of the headers. It
49 // does not contain any headers that depend on the key, such as "alg" or "kid".
50 // These headers are checked when the signature is verified and should not be
51 // read by the user. This ensures that the key can be changed without any
52 // changes to the user code.
53 class VerifiedJwt {
54  public:
55   // VerifiedJwt objects are copiable and implicitly movable.
56   VerifiedJwt(const VerifiedJwt&) = default;
57   VerifiedJwt& operator=(const VerifiedJwt&) = default;
58 
59   bool HasTypeHeader() const;
60   util::StatusOr<std::string> GetTypeHeader() const;
61   bool HasIssuer() const;
62   util::StatusOr<std::string> GetIssuer() const;
63   bool HasSubject() const;
64   util::StatusOr<std::string> GetSubject() const;
65   bool HasAudiences() const;
66   util::StatusOr<std::vector<std::string>> GetAudiences() const;
67   bool HasJwtId() const;
68   util::StatusOr<std::string> GetJwtId() const;
69   bool HasExpiration() const;
70   util::StatusOr<absl::Time> GetExpiration() const;
71   bool HasNotBefore() const;
72   util::StatusOr<absl::Time> GetNotBefore() const;
73   bool HasIssuedAt() const;
74   util::StatusOr<absl::Time> GetIssuedAt() const;
75 
76   bool IsNullClaim(absl::string_view name) const;
77   bool HasBooleanClaim(absl::string_view name) const;
78   util::StatusOr<bool> GetBooleanClaim(absl::string_view name) const;
79   bool HasStringClaim(absl::string_view name) const;
80   util::StatusOr<std::string> GetStringClaim(absl::string_view name) const;
81   bool HasNumberClaim(absl::string_view name) const;
82   util::StatusOr<double> GetNumberClaim(absl::string_view name) const;
83   bool HasJsonObjectClaim(absl::string_view name) const;
84   util::StatusOr<std::string> GetJsonObjectClaim(absl::string_view name) const;
85   bool HasJsonArrayClaim(absl::string_view name) const;
86   util::StatusOr<std::string> GetJsonArrayClaim(absl::string_view name) const;
87   std::vector<std::string> CustomClaimNames() const;
88 
89   util::StatusOr<std::string> GetJsonPayload();
90 
91  private:
92   VerifiedJwt();
93   explicit VerifiedJwt(const RawJwt& raw_jwt);
94   friend class jwt_internal::JwtMacImpl;
95   friend class jwt_internal::JwtPublicKeyVerifyImpl;
96   RawJwt raw_jwt_;
97 };
98 
99 }  // namespace tink
100 }  // namespace crypto
101 
102 #endif  // TINK_JWT_VERIFIED_JWT_H_
103