10Duke Enterprise C++ SDK
Loading...
Searching...
No Matches
tenduke::jwk::DynamicJWKSSignatureValidator Class Reference

#include <DynamicJWKSSignatureValidator.h>

Detailed Description

Extension of tenduke::jwk::JWKSSignatureValidator, which first looks up key in cache, and if not found, tries to refresh the cache.

The cache is also refreshed, if the JWT has no "kid"-claim and the only cached key does not verify the signature: The IdP may have rotated the key (e.g. older backends, which do not set "kid").

The refresh replaces the cached keys with the downloaded key set, so keys removed by the IdP are no longer trusted. The refreshes are rate limited: after a refresh, the cache is not refreshed again for minRefreshIntervalS seconds, and the key is not found. Concurrent lookups share one refresh.

NOTE: Downloading the keyset may fail for various reasons (no network connection, invalid URL, temporary backend errors etc.), these cause tenduke::jwt::JWTValidationException, with the original exception nested.

Inheritance diagram for tenduke::jwk::DynamicJWKSSignatureValidator:
tenduke::jwk::JWKSSignatureValidator tenduke::jwt::JWTSignatureValidator

Public Member Functions

 DynamicJWKSSignatureValidator (const std::shared_ptr< tenduke::jwk::JWKS > &keySet, const std::shared_ptr< const tenduke::jwk::JWKSDownloader > &jwksDownloader, std::string jwksUrl, const std::shared_ptr< const tenduke::time::Clock > &clock, ::tenduke::crypto::HashAlgorithm validationAlgorithm=::tenduke::crypto::HashAlgorithm::NONE, std::int64_t minRefreshIntervalS=DEFAULT_MIN_REFRESH_INTERVAL_S)
 Constructs new instance.
Public Member Functions inherited from tenduke::jwk::JWKSSignatureValidator
 JWKSSignatureValidator (const std::shared_ptr< tenduke::jwk::JWKS > &keySet, ::tenduke::crypto::HashAlgorithm validationAlgorithm=::tenduke::crypto::HashAlgorithm::NONE)
 Constructs new instance.
bool validate (const tenduke::json::JSONObject *jwtHeader, const char *payload, std::size_t payloadLength, const unsigned char *signatureBytes, std::size_t signatureLengthB) const override
 Validates JWT signature.
std::vector< std::shared_ptr< const tenduke::crypto::PublicKey > > getVerificationKeys () const override
 Returns the keys in the key set, which are usable for verifying signatures with the validation algorithm.

Static Public Attributes

static const std::int64_t DEFAULT_MIN_REFRESH_INTERVAL_S = 300
 Default minimum interval in seconds between key set refreshes.

Protected Member Functions

std::unique_ptr< tenduke::jwk::JWK > findJWK (const std::string &keyId) const override
 Locates the key from cache by id.
std::unique_ptr< tenduke::jwk::JWK > findRotatedJWK (const tenduke::jwk::JWK &rejectedKey) const override
 Refreshes the cache (unless refreshed within minRefreshIntervalS) and returns the only cached key usable for signatures, if it is not the rejected key.
Protected Member Functions inherited from tenduke::jwk::JWKSSignatureValidator
virtual void mergeKeys (const tenduke::jwk::JWKS &newKeySet) const
 Merges keys from newKeySet to the contained keySet.
virtual void replaceKeys (const tenduke::jwk::JWKS &newKeySet) const
 Replaces keys of the contained keySet with the keys from newKeySet.

Constructor & Destructor Documentation

◆ DynamicJWKSSignatureValidator()

tenduke::jwk::DynamicJWKSSignatureValidator::DynamicJWKSSignatureValidator ( const std::shared_ptr< tenduke::jwk::JWKS > & keySet,
const std::shared_ptr< const tenduke::jwk::JWKSDownloader > & jwksDownloader,
std::string jwksUrl,
const std::shared_ptr< const tenduke::time::Clock > & clock,
::tenduke::crypto::HashAlgorithm validationAlgorithm = ::tenduke::crypto::HashAlgorithm::NONE,
std::int64_t minRefreshIntervalS = DEFAULT_MIN_REFRESH_INTERVAL_S )

Constructs new instance.

Parameters
keySetinitial keys
jwksDownloader-
jwksUrlURL to download the key set from
clockclock for rate limiting the refreshes
validationAlgorithmPre-set validation algorithm to use. If set to anything else than NONE, this algorithm is ALWAYS used to validate the token. This prevents tampering the token. If NONE, the algorithm from the JWT header is used: the JWT-header must then have alg-claim which maps to supported algorithm (excluding NONE), otherwise the token does not validate.
minRefreshIntervalSminimum interval in seconds between key set refreshes

Member Function Documentation

◆ findJWK()

std::unique_ptr< xdjwk::JWK > tenduke::jwk::DynamicJWKSSignatureValidator::findJWK ( const std::string & keyId) const
overrideprotectedvirtual

Locates the key from cache by id.

If key is not found from cache, this extension downloads the keyset from jwksURL and replaces the cache with the downloaded keyset (unless refreshed within minRefreshIntervalS). Then it attempts to find the key from cache again.

Parameters
keyId-
Returns
the key, or nullptr if key not found
Exceptions
tenduke::jwt::JWTValidationExceptionif downloading the keyset fails

Reimplemented from tenduke::jwk::JWKSSignatureValidator.

◆ findRotatedJWK()

std::unique_ptr< xdjwk::JWK > tenduke::jwk::DynamicJWKSSignatureValidator::findRotatedJWK ( const tenduke::jwk::JWK & rejectedKey) const
overrideprotectedvirtual

Refreshes the cache (unless refreshed within minRefreshIntervalS) and returns the only cached key usable for signatures, if it is not the rejected key.

Parameters
rejectedKey-
Returns
the key, or nullptr if not found
Exceptions
tenduke::jwt::JWTValidationExceptionif downloading the keyset fails

Reimplemented from tenduke::jwk::JWKSSignatureValidator.


The documentation for this class was generated from the following files: