To use the encrypted tokens (JWE), you have to install the web-token/jwt-encryption
component.
This component provides lot of encryption algorithms and classes to load and create encrypted tokens.
Please refer to this encryption algorithm table to know what algorithms are available.
Then, you will find an example to create an encrypted token here and another example to load and decrypt incoming tokens.
The computation of a JWE is done by the JWEBuilder
object. This object requires the following services:
an algorithm manager with key encryption algorithms
an algorithm manager with content encryption algorithms
a compression method manager. No compression method is needed if you do not intent to compress the payload
and a JSON converter.
Now let's create our first JWE object.
Great! If everything is fine you will get a JWE object with one recipient. We want to send it to the audience. Before that, it must be serialized.
We will use the compact serialization mode. This is the most common mode as it is URL safe and very compact. Perfect for a use in a web context!
All good! The variable $token
now contains a string that should be something like that: eyJhbGciOiJBMjU2S1ciLCJlbmMiOiJBMjU2Q0JDLUhTNTEyIiwiemlwIjoiREVGIn0.9RLpf3Gauf05QPNCMzPcH4XNBLmH0s3e-YWwOe57MTG844gnc-g2ywfXt_R0Q9qsR6WhkmQEhdLk2CBvfqr4ob4jFlvJK0yW.CCvfoTKO9tQlzCvbAuFAJg.PxrDlsbSRcxC5SuEJ84i9E9_R3tCyDQsEPTIllSCVxVcHiPOC2EdDlvUwYvznirYP6KMTdKMgLqxB4BwI3CWtys0fceSNxrEIu_uv1WhzJg.4DnyeLEAfB4I8Eq0UobnP8ymlX1UIfSSADaJCXr3RlU
.
This framework comes with several encryption algorithms. These algorithms are in the following namespaces:
Jose\Component\Encryption\Algorithm\KeyEncryption
: key encryption algorithms
Jose\Component\Encryption\Algorithm\ContentEncryption
: content encryption algorithms
From v1.2, the algorithms have their own sub-packages. To avoid BC breaks, these packages are automatically installed for all v1.x of the framework. Starting at v2.0, you will have to explicitly install the algorithm packages you need.
Key Encryption
Package web-token/jwt-encryption-algorithm-aeskw
A128KW
A192KW
A256KW
Package web-token/jwt-encryption-algorithm-aesgcmkw
A128GCMKW
A192GCMKW
A256GCMKW
Package web-token/jwt-encryption-algorithm-dir
dir
(class Dir
)
Package web-token/jwt-encryption-algorithm-ecdh-es
ECDH-ES
(class ECDHES
) READ THE NOTE BELOW
ECDH-ES+A128KW
(class ECDHESA128KW
) READ THE NOTE BELOW
ECDH-ES+A192KW
(class ECDHESA192KW
) READ THE NOTE BELOW
ECDH-ES+A256KW
(class ECDHESA256KW
) READ THE NOTE BELOW
Package web-token/jwt-encryption-algorithm-pbes2
PBES2-HS256+A128KW
(class PBES2HS256A128KW
)
PBES2-HS384+A192KW
(class PBES2HS384A192KW
)
PBES2-HS512+A259KW
(class PBES2HS512A1256KW
)
Package web-token/jwt-encryption-algorithm-rsa
RSA1_5
(class RSA15
) READ THE NOTE BELOW
RSA-OAEP
(class RSAOAEP
)
RSA-OAEP-256
(class RSAOAEP256
)
Content Encryption
Package web-token/jwt-encryption-algorithm-aesgcm
A128GCM
A192GCM
A256GCM
Package web-token/jwt-encryption-algorithm-aescbc
A128CBC-HS256
(class A128CBCHS256
)
A192CBC-HS384
(class A192CBCHS384
)
A256CBC-HS512
(class A256CBCHS512
)
IMPORTANT NOTE:
The algorithm RSA1_5
is deprecated due to known security vulnerability.
The algorithms ECDH-ES*
are not recommended unless used with the OKP
key type.
The following signature algorithms are experimental and must not be used in production unless you know what you are doing. They are proposed for testing purpose only.
They are all part of the package web-token/jwt-encryption-algorithm-experimental
Key Encryption
A128CTR
, A192CTR
and A256CTR
: AES CTR based encryption.
Chacha20+Poly1305
: Please note that this algorithm requires OpenSSL 1.1
RSA-OAEP-384
and RSA-OAEP-512
: Same algorithm as RSA-OAEP-256 but with SHA-384 and SHA-512 hashing functions.
Content Encryption
AxxxCCM-16-128
, AxxxCCM-16-64
, AxxxCCM-64-128
, AxxxCCM-64-64
: AES-CCM based aalgorithms. xxx can be 128 or 256.
These algorithms have to be used with the Algorithm Manager. They do not need any arguments.
Example:
By default, PBES2*
algorithms use the following parameter values:
Salt size: 64 bytes (512 bits)
Count: 4096
You may need to use other values. This can be done during the instantiation of the algorithm:
Example with 16 bytes (128 bits) salt and 1024 counts:
Encrypted tokens are loaded by a serializer or the serializer manager and decrypted by the JWEDecrypter
object. This JWEDecrypter object requires several services for the process:
an algorithm manager with key encryption algorithms
an algorithm manager with content encryption algorithms
a compression method manager. No compression method is needed if you do not intent to compress the payload.
In the following example, we will use the same assumptions as the ones used during the JWE Creation process.
Now we can try to deserialize and decrypt the input we receive. We will continue with the result we got during the JWE creation section.
Note: we do not check header parameters here, but it is very important to do it. This step is described in the Header Checker section.
OK so if not exception is thrown, then your token is loaded and the payload correctly decrypted.
To avoid duplication of code lines, you can create a JWELoader
object. This object contains a serializer, a decrypter and an optional header checker (highly recommended).
In the following example, the JWELoader
object will try to unserialize the token $token
, check the header parameters and decrypt with the key $key
.
If the decryption succeeded, the variable $recipient
will be set with the recipient index and should be in case of multiple recipients. The method returns the JWE object.
In case you use a key set, you can use the method loadAndDecryptWithKeySet
.
This feature was introduced in version 1.1.
The JWELoaderFactory
object is able to create JWELoader
objects on demand. It requires the following factories:
JWESerializerManagerFactory
JWEDecrypterFactory
HeaderCheckerManagerFactory
(optional)