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

#include <JWKSSignatureValidator.h>

Detailed Description

A tenduke::jwt::JWTSignatureValidator, which determines the validation key and algorithm based on the JWT header and looking up the key from memory cache.

The service maintains a (usually shared) key set, which is essentially a map. The service uses "kid"-claim from the header to lookup key from the key set. If the set does not contain the key, the token does not validate. If the header has no "kid"-claim, the key set must contain exactly one key usable for signatures, otherwise the token does not validate. If that key does not verify the signature, the key may have been rotated: subclasses can then provide a replacement key, see findRotatedJWK(). The key must be usable for verifying signatures with the validation algorithm, see tenduke::jwk::JWK::isUsableForSignature().

The service honors the "alg"-claim of the header and uses the algorithm to validate the signature (but note the validationAlgorithm constructor parameter). If the alg-claim is missing, is none or is not a supported algorithm, the token does not validate.

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

Public Member Functions

 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.

Protected Member Functions

virtual std::unique_ptr< tenduke::jwk::JWK > findJWK (const std::string &keyId) const
 Locates the key from cache by id.
virtual std::unique_ptr< tenduke::jwk::JWK > findRotatedJWK (const tenduke::jwk::JWK &rejectedKey) const
 Locates a replacement for the key, which did not verify the signature of a JWT without "kid"-claim.
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

◆ JWKSSignatureValidator()

tenduke::jwk::JWKSSignatureValidator::JWKSSignatureValidator ( const std::shared_ptr< tenduke::jwk::JWKS > & keySet,
::tenduke::crypto::HashAlgorithm validationAlgorithm = ::tenduke::crypto::HashAlgorithm::NONE )
explicit

Constructs new instance.

Parameters
keySet-
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.

Member Function Documentation

◆ findJWK()

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

Locates the key from cache by id.

Parameters
keyIdthe key id. If empty, the only key usable for signatures is returned.
Returns
the key, or nullptr if key not found (or keyId is empty and there is not exactly one key usable for signatures)

Reimplemented in tenduke::jwk::DynamicJWKSSignatureValidator.

◆ findRotatedJWK()

std::unique_ptr< xdjwk::JWK > tenduke::jwk::JWKSSignatureValidator::findRotatedJWK ( const tenduke::jwk::JWK & rejectedKey) const
protectedvirtual

Locates a replacement for the key, which did not verify the signature of a JWT without "kid"-claim.

A JWT without "kid" is verified with the only key usable for signatures (see findJWK()). If the key has been rotated, the JWT cannot select the new key by "kid". This hook allows e.g. refreshing the keys. This implementation does not replace keys and returns nullptr.

Parameters
rejectedKeythe key, which did not verify the signature
Returns
the key to try next, or nullptr if there is no replacement

Reimplemented in tenduke::jwk::DynamicJWKSSignatureValidator.

◆ getVerificationKeys()

std::vector< std::shared_ptr< const xdcrypto::PublicKey > > tenduke::jwk::JWKSSignatureValidator::getVerificationKeys ( ) const
overridevirtual

Returns the keys in the key set, which are usable for verifying signatures with the validation algorithm.

The keys come from the key set in memory: Nothing is loaded over the network.

Returns
-

Reimplemented from tenduke::jwt::JWTSignatureValidator.

◆ validate()

bool tenduke::jwk::JWKSSignatureValidator::validate ( const tenduke::json::JSONObject * jwtHeader,
const char * payload,
std::size_t payloadLength,
const unsigned char * signatureBytes,
std::size_t signatureLengthB ) const
overridevirtual

Validates JWT signature.

Parameters
jwtHeaderJWT-header, can be e.g. used to determine the key and digestAlgorithm used
payloadJWT-payload to verify
payloadLengthlength of the payload
signatureBytesthe signature
signatureLengthBthe signature length
Returns
true if the signature is valid, false if not
Exceptions
tenduke::crypto::CryptoExceptionif there are are cryptographic issues

Implements tenduke::jwt::JWTSignatureValidator.


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