Sourcemeta Core 0.0.0
Loading...
Searching...
No Matches
jose_verify.h
1#ifndef SOURCEMETA_CORE_JOSE_VERIFY_H_
2#define SOURCEMETA_CORE_JOSE_VERIFY_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_jwk.h>
11#include <sourcemeta/core/jose_jwks.h>
12#include <sourcemeta/core/jose_jwt.h>
13// NOLINTEND(misc-include-cleaner)
14
15#include <chrono> // std::chrono::seconds, std::chrono::system_clock
16#include <cstdint> // std::uint8_t
17#include <optional> // std::optional
18#include <span> // std::span
19#include <string_view> // std::string_view
20
21namespace sourcemeta::core {
22
40
59 JWTClockSkew() noexcept = default;
60
61 // The conversion is deliberately implicit so that a caller holding a single
62 // uniform tolerance can keep passing plain seconds
64 JWTClockSkew(const std::chrono::seconds uniform) noexcept
65 : expiration{uniform}, not_before{uniform}, issued_at{uniform} {}
66
68 JWTClockSkew(const std::chrono::seconds expiration_tolerance,
69 const std::chrono::seconds not_before_tolerance,
70 const std::chrono::seconds issued_at_tolerance) noexcept
71 : expiration{expiration_tolerance}, not_before{not_before_tolerance},
72 issued_at{issued_at_tolerance} {}
73
75 std::chrono::seconds expiration{0};
77 std::chrono::seconds not_before{0};
79 std::chrono::seconds issued_at{0};
80};
81
98inline auto jwt_bounded_clock_skew(const std::chrono::seconds skew) noexcept
99 -> std::chrono::seconds {
100 // A mean Gregorian year, the widest grace period any deployment plausibly
101 // needs, so an extreme value cannot widen the acceptance window without bound
102 constexpr std::chrono::seconds maximum{31556952};
103 return skew < std::chrono::seconds::zero() ? std::chrono::seconds::zero()
104 : skew > maximum ? maximum
105 : skew;
106}
107
131SOURCEMETA_CORE_JOSE_EXPORT
132auto jwt_check_claims(const JWT &token, const std::string_view expected_issuer,
133 const std::string_view expected_audience,
134 const std::chrono::system_clock::time_point now,
135 const JWTClockSkew clock_skew = {},
136 const std::optional<std::string_view> expected_subject =
137 std::nullopt) -> std::optional<JWTClaimError>;
138
159SOURCEMETA_CORE_JOSE_EXPORT
160auto jws_verify_signature(const std::optional<JWSAlgorithm> algorithm,
161 const std::string_view signing_input,
162 const std::string_view signature, const JWK &key)
163 -> bool;
164
188SOURCEMETA_CORE_JOSE_EXPORT
189auto jwt_verify_signature(const JWT &token, const JWK &key) -> bool;
190
216
248SOURCEMETA_CORE_JOSE_EXPORT
249auto jwt_verify(const JWT &token, const JWKS &keys,
250 const std::span<const JWSAlgorithm> allowed_algorithms,
251 const std::string_view expected_issuer,
252 const std::string_view expected_audience,
253 const std::chrono::system_clock::time_point now,
254 const JWTClockSkew clock_skew,
255 const std::optional<std::string_view> expected_subject,
256 const std::optional<std::string_view> expected_type)
257 -> std::optional<JWTVerificationError>;
258
259} // namespace sourcemeta::core
260
261#endif
std::chrono::seconds issued_at
The tolerance applied to the issued-at time claim.
Definition jose_verify.h:79
JWTClockSkew(const std::chrono::seconds expiration_tolerance, const std::chrono::seconds not_before_tolerance, const std::chrono::seconds issued_at_tolerance) noexcept
Apply a distinct tolerance to each time-based claim.
Definition jose_verify.h:68
std::chrono::seconds expiration
The tolerance applied to the expiration time claim.
Definition jose_verify.h:75
std::chrono::seconds not_before
The tolerance applied to the not-before time claim.
Definition jose_verify.h:77
JWTClockSkew() noexcept=default
Apply no tolerance to any time-based claim.
Definition jose_jwk.h:40
Definition jose_jwks.h:40
Definition jose_jwt.h:38
SOURCEMETA_CORE_JOSE_EXPORT auto jwt_verify(const JWT &token, const JWKS &keys, const std::span< const JWSAlgorithm > allowed_algorithms, const std::string_view expected_issuer, const std::string_view expected_audience, const std::chrono::system_clock::time_point now, const JWTClockSkew clock_skew, const std::optional< std::string_view > expected_subject, const std::optional< std::string_view > expected_type) -> std::optional< JWTVerificationError >
auto jwt_bounded_clock_skew(const std::chrono::seconds skew) noexcept -> std::chrono::seconds
Definition jose_verify.h:98
JWTClaimError
Definition jose_verify.h:26
JWTVerificationError
Definition jose_verify.h:194
SOURCEMETA_CORE_JOSE_EXPORT auto jws_verify_signature(const std::optional< JWSAlgorithm > algorithm, const std::string_view signing_input, const std::string_view signature, const JWK &key) -> bool
SOURCEMETA_CORE_JOSE_EXPORT auto jwt_verify_signature(const JWT &token, const JWK &key) -> bool
SOURCEMETA_CORE_JOSE_EXPORT auto jwt_check_claims(const JWT &token, const std::string_view expected_issuer, const std::string_view expected_audience, const std::chrono::system_clock::time_point now, const JWTClockSkew clock_skew={}, const std::optional< std::string_view > expected_subject=std::nullopt) -> std::optional< JWTClaimError >
@ Issuer
The issuer claim is missing or does not match the expected value.
Definition jose_verify.h:28
@ IssuedAt
The issued-at time claim is malformed or lies in the future.
Definition jose_verify.h:38
@ Subject
The subject claim is missing or does not match the expected value.
Definition jose_verify.h:30
@ Expiration
The expiration time claim is missing or the token has expired.
Definition jose_verify.h:34
@ NotBefore
The not-before time claim is malformed or lies in the future.
Definition jose_verify.h:36
@ Audience
The audience claim is missing or does not contain the expected value.
Definition jose_verify.h:32
@ AlgorithmNotAllowed
The token's algorithm is missing or absent from the allow-list.
Definition jose_verify.h:196
@ Signature
The named key was found but its signature did not verify.
Definition jose_verify.h:200
@ UnknownKey
No key in the set could be selected or verified the signature.
Definition jose_verify.h:198
@ Type
The token type does not match the expected media type.
Definition jose_verify.h:202
Definition jose_verify.h:57