1
#pragma once
2

            
3
// Copyright 2018 Google LLC
4
// Copyright Envoy Project Authors
5
// SPDX-License-Identifier: Apache-2.0
6

            
7
#pragma once
8

            
9
#include <string>
10
#include <vector>
11

            
12
#include "source/common/jwt/status.h"
13
#include "source/common/protobuf/protobuf.h"
14

            
15
namespace Envoy {
16
namespace JwtVerify {
17

            
18
// Clock skew defaults to one minute.
19
constexpr uint64_t kClockSkewInSecond = 60;
20

            
21
/**
22
 * struct to hold a JWT data.
23
 */
24
struct Jwt {
25
  // entire jwt
26
  std::string jwt_;
27

            
28
  // header string
29
  std::string header_str_;
30
  // header base64_url encoded
31
  std::string header_str_base64url_;
32
  // header in Struct protobuf
33
  Protobuf::Struct header_pb_;
34

            
35
  // payload string
36
  std::string payload_str_;
37
  // payload base64_url encoded
38
  std::string payload_str_base64url_;
39
  // payload in Struct protobuf
40
  Protobuf::Struct payload_pb_;
41
  // signature string
42
  std::string signature_;
43
  // alg
44
  std::string alg_;
45
  // kid
46
  std::string kid_;
47
  // iss
48
  std::string iss_;
49
  // audiences
50
  std::vector<std::string> audiences_;
51
  // sub
52
  std::string sub_;
53
  // issued at
54
  uint64_t iat_ = 0;
55
  // not before
56
  uint64_t nbf_ = 0;
57
  // expiration
58
  uint64_t exp_ = 0;
59
  // JWT ID
60
  std::string jti_;
61

            
62
  /**
63
   * Standard constructor.
64
   */
65
467
  Jwt() {}
66
  /**
67
   * Copy constructor. The copy constructor is marked as explicit as the caller
68
   * should understand the copy operation is non-trivial as a complete
69
   * re-deserialization occurs.
70
   * @param rhs the instance to copy.
71
   */
72
  explicit Jwt(const Jwt& instance);
73

            
74
  /**
75
   * Copy Jwt instance.
76
   * @param rhs the instance to copy.
77
   * @return this
78
   */
79
  Jwt& operator=(const Jwt& rhs);
80

            
81
  /**
82
   * Parse Jwt from string text
83
   * @return the status.
84
   */
85
  Status parseFromString(const std::string& jwt);
86

            
87
  /*
88
   * Verify Jwt time constraint if specified
89
   * esp: expiration time, nbf: not before time.
90
   * @param now: is the current time in seconds since the unix epoch
91
   * @param clock_skew: the the clock skew in second.
92
   * @return the verification status.
93
   */
94
  Status verifyTimeConstraint(uint64_t now, uint64_t clock_skew = kClockSkewInSecond) const;
95
};
96

            
97
} // namespace JwtVerify
98
} // namespace Envoy