Class GenericJWKService
- All Implemented Interfaces:
JWKService
Generic JSON Web Key service.
This implementation relies on standard JWK factories to create, read and generate JSON Web keys. Custom JWK factories can also be injected when building the JOSE module.
- Since:
- 1.5
- Author:
- Jeremy Kuhn
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceJWK Service extra JWK factories. -
Constructor Summary
ConstructorsConstructorDescriptionGenericJWKService(JOSEConfiguration configuration, GenericECJWKFactory ecJWKFactory, GenericRSAJWKFactory rsaJWKFactory, GenericOCTJWKFactory octJWKFactory, GenericEdECJWKFactory edecJWKFactory, GenericXECJWKFactory xecJWKFactory, GenericPBES2JWKFactory pbes2JWKFactory, JWKStore jwkStore, JWKURLResolver urlResolver, SwitchableJWKURLResolver switchableUrlResolver, com.fasterxml.jackson.databind.ObjectMapper mapper) Creates a generic JWK service. -
Method Summary
Modifier and TypeMethodDescriptionECJWKFactory<? extends ECJWK,?, ?> ec()Returns the Elliptic Curve JWK factory.EdECJWKFactory<? extends EdECJWK,?, ?> edec()Returns the Edward-Curve JWK factory.org.reactivestreams.Publisher<? extends JWK>Generates a new key using the specified parameters.OCTJWKFactory<? extends OCTJWK,?, ?> oct()Returns the Octet JWK factory.PBES2JWKFactory<? extends PBES2JWK,?, ?> pbes2()Returns the Password-Based JWK factory.org.reactivestreams.Publisher<? extends JWK>read(JOSEHeader header) Tries to resolve the JWK from a JOSE header.org.reactivestreams.Publisher<? extends JWK>Reads the specified JWK or JWK set JSON serialized string and resolves and returns corresponding keys.org.reactivestreams.Publisher<? extends JWK>Reads the JWK or JWK set JSON serialized string at the specified URI.org.reactivestreams.Publisher<? extends JWK>Reads the JWK or JWK set represented in the specified map.RSAJWKFactory<? extends RSAJWK,?, ?> rsa()Returns the RSA JWK factory.voidsetJWKFactories(List<JWKFactory<?, ?, ?>> jwkFactories) Sets custom JWK factories.store()Returns the JWK store.XECJWKFactory<? extends XECJWK,?, ?> xec()Returns the extended Elliptic Curve JWK factory.
-
Constructor Details
-
GenericJWKService
public GenericJWKService(JOSEConfiguration configuration, GenericECJWKFactory ecJWKFactory, GenericRSAJWKFactory rsaJWKFactory, GenericOCTJWKFactory octJWKFactory, GenericEdECJWKFactory edecJWKFactory, GenericXECJWKFactory xecJWKFactory, GenericPBES2JWKFactory pbes2JWKFactory, JWKStore jwkStore, JWKURLResolver urlResolver, SwitchableJWKURLResolver switchableUrlResolver, com.fasterxml.jackson.databind.ObjectMapper mapper) Creates a generic JWK service.
- Parameters:
configuration- the JOSE module configurationecJWKFactory- the Elliptic Curve JWK factoryrsaJWKFactory- the RSA JWK factoryoctJWKFactory- the Octet JWK factoryedecJWKFactory- the Edwards-curve JWK factoryxecJWKFactory- the extended Elliptic Curve JWK factorypbes2JWKFactory- the password-based JWK factoryjwkStore- a JWK storeurlResolver- a JWK URL resolverswitchableUrlResolver- a switchable JWK URL resolvermapper- an object mapper
-
-
Method Details
-
setJWKFactories
Sets custom JWK factories.
Standard JWK factories should be already injected into the constructor, they will be ignored if they are present in the specified list of JWK factories.
Custom JWK factories are prioritized over the standard JWK factories to allow override.
- Parameters:
jwkFactories- a list of custom JWK factories
-
ec
Description copied from interface:JWKServiceReturns the Elliptic Curve JWK factory.
- Specified by:
ecin interfaceJWKService- Returns:
- the Elliptic Curve JWK factory
-
rsa
Description copied from interface:JWKServiceReturns the RSA JWK factory.
- Specified by:
rsain interfaceJWKService- Returns:
- the RSA JWK factory
-
oct
Description copied from interface:JWKServiceReturns the Octet JWK factory.
- Specified by:
octin interfaceJWKService- Returns:
- the Octet JWK factory
-
edec
Description copied from interface:JWKServiceReturns the Edward-Curve JWK factory.
- Specified by:
edecin interfaceJWKService- Returns:
- the Edward-Curve JWK factory
-
xec
Description copied from interface:JWKServiceReturns the extended Elliptic Curve JWK factory.
- Specified by:
xecin interfaceJWKService- Returns:
- the extended Elliptic Curve JWK factory
-
pbes2
Description copied from interface:JWKServiceReturns the Password-Based JWK factory.
- Specified by:
pbes2in interfaceJWKService- Returns:
- the Password-Based JWK factory
-
read
public org.reactivestreams.Publisher<? extends JWK> read(String jwk) throws JWKReadException, JWKBuildException, JWKProcessingException Description copied from interface:JWKServiceReads the specified JWK or JWK set JSON serialized string and resolves and returns corresponding keys.
This method basically iterates over all JWK factories and tries to resolve the key when the factory supports the specified key type and algorithm. The resulting publisher will fail when no key could have been resolved in which case a single
JWKReadExceptionwill be emitted with suppressed errors corresponding to each factories.Note that this method should also fail when a key is missing the key type. If the key type is known it is preferable to use the corresponding
JWKFactoryto avoid unnecessary processing.- Specified by:
readin interfaceJWKService- Parameters:
jwk- a JSON serialized JWK or JWK set- Returns:
- a publisher of keys
- Throws:
JWKReadException- if there was an error reading the JSON string or a particular keyJWKBuildException- if there was an error building a keyJWKProcessingException- if there was a processing error
-
read
public org.reactivestreams.Publisher<? extends JWK> read(Map<String, Object> jwk) throws JWKReadException, JWKBuildException, JWKProcessingExceptionDescription copied from interface:JWKServiceReads the JWK or JWK set represented in the specified map.
This method basically iterates over all JWK factories and tries to resolve the key when the factory supports the specified key type and algorithm. The resulting publisher will fail when no key could have been resolved in which case a single
JWKReadExceptionwill be emitted with suppressed errors corresponding to each factories.Note that this method should also fail when a key is missing the key type. If the key type is known it is preferable to use the corresponding
JWKFactoryto avoid unnecessary processing.- Specified by:
readin interfaceJWKService- Parameters:
jwk- a map representing a JWK or a JWK set- Returns:
- a publisher of keys
- Throws:
JWKReadException- if there was an error reading the JSON string or a particular keyJWKBuildException- if there was an error building a keyJWKProcessingException- if there was a processing error
-
read
public org.reactivestreams.Publisher<? extends JWK> read(URI uri) throws JWKReadException, JWKResolveException, JWKBuildException, JWKProcessingException Description copied from interface:JWKServiceReads the JWK or JWK set JSON serialized string at the specified URI.
This method basically iterates over all JWK factories and tries to resolve the key when the factory supports the specified key type and algorithm. The resulting publisher will fail when no key could have been resolved in which case a single
JWKReadExceptionwill be emitted with suppressed errors corresponding to each factories.Note that this method should also fail when a key is missing the key type. If the key type is known it is preferable to use the corresponding
JWKFactoryto avoid unnecessary processing.Note that this method will also fail if JWK URL resolution is disabled, either in the module's configuration or if no
ResourceServicehas been specified.- Specified by:
readin interfaceJWKService- Parameters:
uri- the URI where to find the JWK or JWK set JSON string- Returns:
- a publisher of keys
- Throws:
JWKReadException- if there was an error reading the JSON string or a particular keyJWKResolveException- if there was an error resolving the resource from the specified URIJWKBuildException- if there was an error building a keyJWKProcessingException- if there was a processing error
-
read
public org.reactivestreams.Publisher<? extends JWK> read(JOSEHeader header) throws JWKReadException, JWKResolveException, JWKBuildException, JWKProcessingException Description copied from interface:JWKServiceTries to resolve the JWK from a JOSE header.
As for other read methods, this method will iterates over all JWK factories and tries to resolve the key that matches the JOSE header when the factory supports the algorithm specified in the header. The resulting publisher will fail when no key could have been resolved in which case a single
JWKReadExceptionwill be emitted with suppressed errors corresponding to each factories.Unlike other read methods, this method does not fail when the key type is missing since a JOSE header does not contain the key type.
- Specified by:
readin interfaceJWKService- Parameters:
header- a JOSE header- Returns:
- a publisher of keys
- Throws:
JWKReadException- if there was an error reading the JOSE headerJWKResolveException- if there was an error resolving the key using aJWKStoreor aJWKURLResolverJWKBuildException- if there was an error building the keyJWKProcessingException- if there was a processing error
-
generate
public org.reactivestreams.Publisher<? extends JWK> generate(String alg, Map<String, Object> parameters) throws JWKGenerateException, JWKProcessingExceptionDescription copied from interface:JWKServiceGenerates a new key using the specified parameters.
This is a convenience method that can be used to generate a key using a custom JWK factory, you should prefer using a
JWKGeneratorobtained from a specificJWKFactoryto avoid unnecessary processing.- Specified by:
generatein interfaceJWKService- Parameters:
alg- a JWA algorithmparameters- a map of key parameters- Returns:
- a publisher of keys
- Throws:
JWKGenerateException- if there was an error generating a keyJWKProcessingException- if there was a processing error
-
store
Description copied from interface:JWKServiceReturns the JWK store.
The JWK store can be used to store frequently used keys so they can be easily resolved when reading a JOSE object.
It is recommended to only store trusted keys inside a JWK store to prevent them from being evicted when resolving a JOSE object key.
- Specified by:
storein interfaceJWKService- Returns:
- the JWK store
-