xref: /aosp_15_r20/external/tink/cc/jwt/jwt_validator.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_JWT_VALIDATOR_H_
18 #define TINK_JWT_JWT_VALIDATOR_H_
19 
20 #include <string>
21 
22 #include "absl/strings/string_view.h"
23 #include "absl/time/clock.h"
24 #include "absl/time/time.h"
25 #include "tink/jwt/raw_jwt.h"
26 #include "tink/util/status.h"
27 #include "tink/util/statusor.h"
28 
29 namespace crypto {
30 namespace tink {
31 
32 ///////////////////////////////////////////////////////////////////////////////
33 // A JwtValidator defines how JSON Web Tokens (JWTs) should be validated.
34 //
35 
36 class JwtValidatorBuilder;
37 
38 class JwtValidator {
39  public:
40   // JwtValidator objects are copiable and movable.
41   JwtValidator(const JwtValidator&) = default;
42   JwtValidator& operator=(const JwtValidator&) = default;
43   JwtValidator(JwtValidator&& other) = default;
44   JwtValidator& operator=(JwtValidator&& other) = default;
45 
46   util::Status Validate(crypto::tink::RawJwt const& raw_jwt) const;
47 
48  private:
49   util::Status ValidateTimestamps(crypto::tink::RawJwt const& raw_jwt) const;
50   util::Status ValidateTypeHeader(crypto::tink::RawJwt const& raw_jwt) const;
51   util::Status ValidateIssuer(crypto::tink::RawJwt const& raw_jwt) const;
52   util::Status ValidateAudiences(crypto::tink::RawJwt const& raw_jwt) const;
53   explicit JwtValidator(const JwtValidatorBuilder& builder);
54   friend class JwtValidatorBuilder;
55   absl::optional<std::string> expected_type_header_;
56   absl::optional<std::string> expected_issuer_;
57   absl::optional<std::string> expected_audience_;
58   bool ignore_type_header_;
59   bool ignore_issuer_;
60   bool ignore_audiences_;
61   bool allow_missing_expiration_;
62   bool expect_issued_in_the_past_;
63   absl::Duration clock_skew_;
64   absl::optional<absl::Time> fixed_now_;
65 };
66 
67 class JwtValidatorBuilder {
68  public:
69   JwtValidatorBuilder();
70 
71   // JwtValidatorBuilder objects are copiable and movable.
72   JwtValidatorBuilder(const JwtValidatorBuilder&) = default;
73   JwtValidatorBuilder& operator=(const JwtValidatorBuilder&) = default;
74   JwtValidatorBuilder(JwtValidatorBuilder&& other) = default;
75   JwtValidatorBuilder& operator=(JwtValidatorBuilder&& other) = default;
76 
77   JwtValidatorBuilder& ExpectTypeHeader(absl::string_view expected_type_header);
78   JwtValidatorBuilder& ExpectIssuer(absl::string_view expected_issuer);
79   JwtValidatorBuilder& ExpectAudience(absl::string_view expected_audience);
80 
81   JwtValidatorBuilder& IgnoreTypeHeader();
82   JwtValidatorBuilder& IgnoreIssuer();
83   JwtValidatorBuilder& IgnoreAudiences();
84 
85   JwtValidatorBuilder& AllowMissingExpiration();
86   JwtValidatorBuilder& ExpectIssuedInThePast();
87 
88   JwtValidatorBuilder& SetClockSkew(absl::Duration clock_skew);
89   JwtValidatorBuilder& SetFixedNow(absl::Time fixed_now);
90 
91   util::StatusOr<JwtValidator> Build();
92 
93  private:
94   friend class JwtValidator;
95   absl::optional<std::string> expected_type_header_;
96   absl::optional<std::string> expected_issuer_;
97   absl::optional<std::string> expected_audience_;
98   bool ignore_type_header_;
99   bool ignore_issuer_;
100   bool ignore_audiences_;
101   bool allow_missing_expiration_;
102   bool expect_issued_in_the_past_;
103   absl::Duration clock_skew_;
104   absl::optional<absl::Time> fixed_now_;
105 };
106 
107 }  // namespace tink
108 }  // namespace crypto
109 
110 #endif  // TINK_JWT_JWT_VALIDATOR_H_
111