xref: /aosp_15_r20/external/tink/cc/jwt/internal/jwt_mac_internal.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_INTERNAL_H_
18 #define TINK_JWT_INTERNAL_JWT_MAC_INTERNAL_H_
19 
20 #include <string>
21 
22 #include "absl/strings/string_view.h"
23 #include "tink/jwt/jwt_validator.h"
24 #include "tink/jwt/raw_jwt.h"
25 #include "tink/jwt/verified_jwt.h"
26 #include "tink/util/status.h"
27 #include "tink/util/statusor.h"
28 
29 namespace crypto {
30 namespace tink {
31 namespace jwt_internal {
32 
33 ///////////////////////////////////////////////////////////////////////////////
34 // Internal Interface for authenticating and verifying JWT with JWS MAC.
35 //
36 // Sees RFC 7519 and RFC 7515. Security guarantees: similar to MAC.
37 class JwtMacInternal {
38  public:
39   // Computes a MAC and encodes the raw JWT token and the MAC in the JWS compact
40   // serialization format.
41   //
42   // When the `kid` parameter has a value, the token will have a kid header.
43   virtual crypto::tink::util::StatusOr<std::string> ComputeMacAndEncodeWithKid(
44       const RawJwt& token, absl::optional<absl::string_view> kid) const = 0;
45 
46   // Verifies and decodes a JWT token in the JWS compact serialization format.
47   //
48   // The JWT is validated against the rules in `validator`. That is, every claim
49   // in validator must also be present in the JWT. For example, if validator
50   // contains an issuer (iss) claim, the JWT must contain an identical claim.
51   // The JWT can contain claims that are NOT in the validator. However, if the
52   // JWT contains a list of audiences, the validator must also contain an
53   // audience in the list.
54   //
55   // If the JWT contains timestamp claims such as expiration (exp), issued_at
56   // (iat) or not_before (nbf), they will also be validated. validator allows to
57   // set a clock skew, to deal with small clock differences among different
58   // machines.
59   //
60   // When the `kid` parameter has a value, then only token with the correct kid
61   // header are valid. When the `kid` parameter does not have a value the kid
62   // header in the token is ignored. The `kid` parameter is set by the primitive
63   // wrapper based on the output prefix type and the key id.
64   virtual crypto::tink::util::StatusOr<VerifiedJwt> VerifyMacAndDecodeWithKid(
65       absl::string_view compact, const JwtValidator& validator,
66       absl::optional<absl::string_view> kid) const = 0;
67 
68   virtual ~JwtMacInternal() = default;
69 };
70 
71 }  // namespace jwt_internal
72 }  // namespace tink
73 }  // namespace crypto
74 
75 #endif  // TINK_JWT_INTERNAL_JWT_MAC_INTERNAL_H_
76