Nested tokens¶
JWT can be signed or encrypted and both. A nested token is a signed token enclosed in an encrypted one. This order is very important: signed then encrypted.
The NestedTokenLoader and NestedTokenBuilder classes will help you to create nested tokens with ease. Just install the package web-token/jwt-nested-token. It contains all the classes and dependencies will be directly managed by composer. You can install it if needed.
Nested token loading¶
To instantiate the NestedTokenLoader, you need a JWSLoader and a JWELoader.
use Jose\Component\NestedToken\NestedTokenLoader;
$nestedTokenLoader = new NestedTokenLoader($jweLoader, $jwsLoader);
Its use is very straightforward, you just have to call the method loadAndVerify using the token, the encryption and signature key sets.
$result = $nestedTokenLoader->loadAndVerify($token, $encryptionKeySet, $signatureKeySet);
$jws = $result->getJws();
$signature = $result->getSignatureIndex(); // Useful when multiple signatures are used
Warning
load() wrote the signature index into a variable of the caller. Passing that $signature argument is deprecated since 4.3 and the argument is removed in 5.0: use loadAndVerify(), which carries the index in its result. Calling load($token, $encryptionKeySet, $signatureKeySet) without it stays valid.
The cty header parameter¶
A JWE carries a nested token when its cty header parameter is JWT. The NestedTokenBuilder writes it in the protected header, the only header covered by the authentication tag. In the JSON serializations, the loader also finds it in the shared unprotected header or in the header of the recipient, where it can be added to a token without any key.
Since 4.3, loading a nested token whose cty is outside the protected header is deprecated. A third argument of the constructor sets what the loader does with such a token:
| Third argument | Token whose cty is outside the protected header |
|---|---|
absent or null |
loaded, with a deprecation notice |
true |
loaded |
false |
refused before the inner JWS is verified: The "cty" header parameter must be in the protected header. |
5.0 refuses such a token by default. Have the issuer put cty in the protected header, and pass false to refuse the other tokens now; pass true to keep loading the tokens of an issuer you cannot change. A token whose cty is in the protected header, every compact token among them, loads whatever the value.
NestedTokenLoaderFactory::create() takes the same value as a seventh argument, after the JWS header checkers.
Nested token building¶
To instantiate the NestedTokenBuilder, you will need the following components:
- a
JWSBuilder, - a
JWEBuilder, - a
JWESerializerManager, - a
JWSSerializerManager
use Jose\Component\NestedToken\NestedTokenBuilder;
$nestedTokenBuilder = new NestedTokenBuilder($jweBuilder, $jweSerializerManager, $jwsBuilder, $jwsSerializerManager);
Its use is a bit more complicated than the loading as the nested token may be designed for several recipients or may have several signatures.
$token = $nestedTokenBuilder->create(
$payload, // The payload to protect
[[ // A list of signatures. 'key' is mandatory and at least one of 'protected_header'/'header' has to be set.
'key' => $signature_key, // The key used to sign. Mandatory.
'protected_header' => ['alg' => 'PS256'], // The protected header. Optional.
'header' => ['foo' => 'bar'], // The unprotected header. Optional.
]],
'jws_json_flattened', // The serialization mode for the JWS
['alg' => 'RSA-OAEP', 'enc' => 'A128GCM'], // The shared protected header. Optional.
['foo' => 'bar'], // The shared unprotected header. Optional.
[[ // A list of recipients. 'key' is mandatory.
'key' => $encryption_key, // The recipient key.
'header' => ['bar' => 'foo'], // The recipient unprotected header.
]],
'jwe_json_flattened', // The serialization mode for the JWE.
'1, 2, 3, 4' // Additional Authenticated Data (AAD). Optional.
);
As a reminder, if one of the following parameter is set, the compact serialization mode cannot be used:
- signature unprotected header,
- JWE shared unprotected header,
- recipient unprotected header,
- Additional Authenticated Data.
Symfony bundle¶
Configuration¶
Hereafter an example of a Symfony application configuration:
jose:
nested_token:
loaders:
loader_1:
signature_algorithms: ['PS256']
encryption_algorithms: ['RSA-OAEP', 'A128GCM']
jws_serializers: ['jws_compact']
jws_header_checkers: [...]
jwe_serializers: ['jwe_compact']
jwe_header_checkers: [...]
is_public: true
builders:
builder_1:
signature_algorithms: ['PS256']
encryption_algorithms: ['RSA-OAEP', 'A128GCM']
jws_serializers: ['jws_compact']
jwe_serializers: ['jwe_compact']
is_public: true
This configuration will create two public services:
jose.nested_token_loader.loader_1jose.nested_token_builder.builder_1
These services can be called from the container (unless private) or injected in your services.
Since 4.3, the allow_unprotected_content_type option of a loader sets what it does with a token whose cty header parameter is outside the protected header: true loads it, false refuses it. When the option is not set, such a token loads with a deprecation notice.
Configuration helper¶
As any other services, you can create a nested token loader or builder from another bundle extension. The following bundle extension class will create the same configuration and services as above.
class AcmeExtension extends Extension implements PrependExtensionInterface
{
...
public function prepend(ContainerBuilder $container)
{
ConfigurationHelper::addNestedTokenLoader($container, 'loader_1', ['jwe_compact'], ['RSA-OAEP', 'A128GCM'], [], ['jws_compact'], ['PS256'], [], true, []);
ConfigurationHelper::addNestedTokenBuilder($container, 'builder_1', ['jwe_compact'], ['RSA-OAEP', 'A128GCM'], ['jws_compact'], ['PS256'], true, []);
}
}