Coverage Report

Created: 2023-06-07 07:04

/proc/self/cwd/jwt_verify_lib/jwt.h
Line
Count
Source
1
// Copyright 2018 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
//    https://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.#pragma once
14
15
#pragma once
16
17
#include <string>
18
#include <vector>
19
20
#include "google/protobuf/struct.pb.h"
21
#include "jwt_verify_lib/status.h"
22
23
namespace google {
24
namespace jwt_verify {
25
26
// Clock skew defaults to one minute.
27
constexpr uint64_t kClockSkewInSecond = 60;
28
29
/**
30
 * struct to hold a JWT data.
31
 */
32
struct Jwt {
33
  // entire jwt
34
  std::string jwt_;
35
36
  // header string
37
  std::string header_str_;
38
  // header base64_url encoded
39
  std::string header_str_base64url_;
40
  // header in Struct protobuf
41
  ::google::protobuf::Struct header_pb_;
42
43
  // payload string
44
  std::string payload_str_;
45
  // payload base64_url encoded
46
  std::string payload_str_base64url_;
47
  // payload in Struct protobuf
48
  ::google::protobuf::Struct payload_pb_;
49
  // signature string
50
  std::string signature_;
51
  // alg
52
  std::string alg_;
53
  // kid
54
  std::string kid_;
55
  // iss
56
  std::string iss_;
57
  // audiences
58
  std::vector<std::string> audiences_;
59
  // sub
60
  std::string sub_;
61
  // issued at
62
  uint64_t iat_ = 0;
63
  // not before
64
  uint64_t nbf_ = 0;
65
  // expiration
66
  uint64_t exp_ = 0;
67
  // JWT ID
68
  std::string jti_;
69
70
  /**
71
   * Standard constructor.
72
   */
73
10.4k
  Jwt() {}
74
  /**
75
   * Copy constructor. The copy constructor is marked as explicit as the caller
76
   * should understand the copy operation is non-trivial as a complete
77
   * re-deserialization occurs.
78
   * @param rhs the instance to copy.
79
   */
80
  explicit Jwt(const Jwt& instance);
81
82
  /**
83
   * Copy Jwt instance.
84
   * @param rhs the instance to copy.
85
   * @return this
86
   */
87
  Jwt& operator=(const Jwt& rhs);
88
89
  /**
90
   * Parse Jwt from string text
91
   * @return the status.
92
   */
93
  Status parseFromString(const std::string& jwt);
94
95
  /*
96
   * Verify Jwt time constraint if specified
97
   * esp: expiration time, nbf: not before time.
98
   * @param now: is the current time in seconds since the unix epoch
99
   * @param clock_skew: the the clock skew in second.
100
   * @return the verification status.
101
   */
102
  Status verifyTimeConstraint(uint64_t now,
103
                              uint64_t clock_skew = kClockSkewInSecond) const;
104
};
105
106
}  // namespace jwt_verify
107
}  // namespace google