JWT Framework
Search…
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 instal 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.
1
use Jose\Component\NestedToken\NestedTokenLoader;
2
3
$nestedTokenLoader = new NestedTokenLoader($jweLoader, $jwsLoader);
Copied!
Its use is very straightforward, you just have to call the method load using the token, the encryption and signature key sets.
The last argument ($signature in the following example) will represents the signature index of the verified signature. This is only useful when multiple signature support is used.
1
$jws = $nestedTokenLoader->load($token, $encryptionKeySet, $signatureKeySet, $signature);
Copied!

Nested Token Building

To instantiate the NestedTokenBuilder, you will need the following components:
  • a JWSBuilder,
  • a JWEBuilder,
  • a JWESerializerManager,
  • a JWSSerializerManager
1
use Jose\Component\NestedToken\NestedTokenBuilder;
2
3
$nestedTokenBuilder = new NestedTokenBuilder($jweLoader, $jweSerializerManager, $jwsLoader, $jwsSerializerManager);
Copied!
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.
1
$token = $builder->create(
2
$payload, // The payload to protect
3
[[ // A list of signatures. 'key' is mandatory and at least one of 'protected_header'/'header' has to be set.
4
'key' => $signature_key, // The key used to sign. Mandatory.
5
'protected_header' => ['alg' => 'PS256'], // The protected header. Optional.
6
'header' => ['foo' => 'bar'], // The unprotected header. Optional.
7
]],
8
'jws_json_flattened', // The serialization mode for the JWS
9
['alg' => 'RSA-OAEP', 'enc' => 'A128GCM'], // The shared protected header. Optional.
10
['foo' => 'bar'], // The shared unprotected header. Optional.
11
[[ // A list of recipients. 'key' is mandatory.
12
'key' => $encryption_key, // The recipient key.
13
'header' => ['bar' => 'foo'], // The recipient unprotected header.
14
]],
15
'jwe_json_flattened' // The serialization mode for the JWE.
16
'1, 2, 3, 4' // Additional Authenticated Data (AAD). Optional.
17
);
Copied!
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:
1
jose:
2
nested_token:
3
loaders:
4
loader_1:
5
signature_algorithms: ['PS256']
6
key_encryption_algorithms: ['RSA-OAEP']
7
content_encryption_algorithms: ['A128GCM']
8
jws_serializers: ['jws_compact']
9
jws_header_checkers: [...]
10
jwe_serializers: ['jwe_compact']
11
jwe_header_checkers: [...]
12
is_public: true
13
builders:
14
builder_1:
15
signature_algorithms: ['PS256']
16
key_encryption_algorithms: ['RSA-OAEP']
17
content_encryption_algorithms: ['A128GCM']
18
jws_serializers: ['jws_compact']
19
jwe_serializers: ['jwe_compact']
20
is_public: true
Copied!
This configuration will create two public services:
  • jose.nested_token_loader.loader_1
  • jose.nested_token_builder.builder_1
These services can be called from the container (unless private) or injected in your services.

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.
1
class AcmeExtension extends Extension implements PrependExtensionInterface
2
{
3
...
4
5
public function prepend(ContainerBuilder $container)
6
{
7
ConfigurationHelper::addNestedTokenLoader($container, 'loader_1', ['jwe_compact'], ['RSA-OAEP'], ['A128GCM'], ['DEF'], [], ['jws_compact'], ['PS256'], [], true, []);
8
ConfigurationHelper::addNestedTokenBuilder($container, 'builder_1', ['jwe_compact'], ['RSA-OAEP'], ['A128GCM'], ['DEF'], ['jws_compact'], ['PS256'], true, []);
9
}
10
}
Copied!
Last modified 11mo ago