Sourcemeta Core 0.0.0
Loading...
Searching...
No Matches
jose_jwt.h
1#ifndef SOURCEMETA_CORE_JOSE_JWT_H_
2#define SOURCEMETA_CORE_JOSE_JWT_H_
3
4#ifndef SOURCEMETA_CORE_JOSE_EXPORT
5#include <sourcemeta/core/jose_export.h>
6#endif
7
8// NOLINTBEGIN(misc-include-cleaner)
9#include <sourcemeta/core/jose_algorithm.h>
10#include <sourcemeta/core/jose_error.h>
11// NOLINTEND(misc-include-cleaner)
12
13#include <sourcemeta/core/json.h>
14
15#include <chrono> // std::chrono::system_clock::time_point
16#include <optional> // std::optional
17#include <string> // std::string
18#include <string_view> // std::string_view
19
20namespace sourcemeta::core {
21
38class SOURCEMETA_CORE_JOSE_EXPORT JWT {
39public:
42 explicit JWT(const std::string_view input);
43
46 [[nodiscard]] static auto from(const std::string_view input)
47 -> std::optional<JWT>;
48
49 // Header (RFC 7515 Section 4)
50
52 [[nodiscard]] auto algorithm() const noexcept -> std::optional<JWSAlgorithm> {
53 return this->algorithm_;
54 }
55
57 [[nodiscard]] auto key_id() const noexcept -> std::optional<std::string_view>;
58
60 [[nodiscard]] auto type() const noexcept -> std::optional<std::string_view>;
61
65 [[nodiscard]] auto has_type(const std::string_view media_type) const -> bool;
66
68 [[nodiscard]] auto header() const noexcept -> const JSON & {
69 return this->header_;
70 }
71
72 // Registered claims (RFC 7519 Section 4.1)
73
75 [[nodiscard]] auto issuer() const noexcept -> std::optional<std::string_view>;
76
78 [[nodiscard]] auto subject() const noexcept
79 -> std::optional<std::string_view>;
80
82 [[nodiscard]] auto
83 has_audience(const std::string_view audience) const noexcept -> bool;
84
86 [[nodiscard]] auto expires_at() const
87 -> std::optional<std::chrono::system_clock::time_point>;
88
90 [[nodiscard]] auto not_before() const
91 -> std::optional<std::chrono::system_clock::time_point>;
92
94 [[nodiscard]] auto issued_at() const
95 -> std::optional<std::chrono::system_clock::time_point>;
96
98 [[nodiscard]] auto token_id() const noexcept
99 -> std::optional<std::string_view>;
100
102 [[nodiscard]] auto payload() const noexcept -> const JSON & {
103 return this->payload_;
104 }
105
106 // The exact wire bytes the signature is computed over, never re-serialized
107 // (RFC 7515 Section 5.1)
109 [[nodiscard]] auto signing_input() const noexcept -> std::string_view {
110 return this->signing_input_;
111 }
112
114 [[nodiscard]] auto signature() const noexcept -> std::string_view {
115 return this->signature_;
116 }
117
118private:
119 JWT() = default;
120 static auto parse(const std::string_view input, JWT &result) -> bool;
121
122#if defined(_MSC_VER)
123#pragma warning(disable : 4251)
124#endif
125 std::string_view signing_input_;
126 std::string signature_;
127 JSON header_{nullptr};
128 JSON payload_{nullptr};
129 std::optional<JWSAlgorithm> algorithm_;
130#if defined(_MSC_VER)
131#pragma warning(default : 4251)
132#endif
133};
134
135} // namespace sourcemeta::core
136
137#endif
auto signing_input() const noexcept -> std::string_view
The exact wire bytes the signature is computed over.
Definition jose_jwt.h:109
auto has_audience(const std::string_view audience) const noexcept -> bool
Whether the token is intended for the given audience.
static auto from(const std::string_view input) -> std::optional< JWT >
auto key_id() const noexcept -> std::optional< std::string_view >
The key identifier from the token header, if present.
auto issuer() const noexcept -> std::optional< std::string_view >
The issuer that created the token, if present.
auto subject() const noexcept -> std::optional< std::string_view >
The subject the token is about, if present.
auto token_id() const noexcept -> std::optional< std::string_view >
The unique identifier of the token, if present.
auto type() const noexcept -> std::optional< std::string_view >
The token type declared in the header, if present.
auto algorithm() const noexcept -> std::optional< JWSAlgorithm >
The signing algorithm declared in the token header, if present.
Definition jose_jwt.h:52
auto has_type(const std::string_view media_type) const -> bool
auto not_before() const -> std::optional< std::chrono::system_clock::time_point >
The time before which the token is not yet valid, if present.
auto payload() const noexcept -> const JSON &
The decoded token payload.
Definition jose_jwt.h:102
auto signature() const noexcept -> std::string_view
The raw token signature.
Definition jose_jwt.h:114
auto issued_at() const -> std::optional< std::chrono::system_clock::time_point >
The time at which the token was issued, if present.
auto header() const noexcept -> const JSON &
The decoded token header.
Definition jose_jwt.h:68
JWT(const std::string_view input)
auto expires_at() const -> std::optional< std::chrono::system_clock::time_point >
The time after which the token is no longer valid, if present.
Definition jose_jwt.h:38
JWSAlgorithm
Definition jose_algorithm.h:21
Definition json_value.h:39