Now that you have an algorithm manager and a key, it is time to create your first signed token.
The computation is done by the JWSBuilder
object. This object requires the algorithm manager and a JSON converter. In the following example, we will use the standard converter provided by the framework.
Now let's create our first JWS object.
Great! If everything is fine you will get a JWS object with one signature. 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 this:
Other serialization modes exist. We will see them in the Advanced Topics section.
This framework comes with several signature algorithms. These algorithms are in the following namespace: Jose\Component\Signature\Algorithm
.
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.
HMAC with SHA-2 Functions. Package web-token/jwt-signature-algorithm-hmac
HS256
HS384
HS512
Elliptic Curve Digital Signature Algorithm (ECDSA). Package web-token/jwt-signature-algorithm-ecdsa
ES256
ES384
ES512
RSASSA-PKCS1 v1_5. Package web-token/jwt-signature-algorithm-rsa
RS256
RS384
RS512
RSASSA-PSS. Package web-token/jwt-signature-algorithm-rsa
PS256
PS384
PS512
Edwards-curve Digital Signature Algorithm (EdDSA) Package web-token/jwt-signature-algorithm-eddsa
EdDSA
(only with the Ed25519
curve)
Unsecured algorithm Package web-token/jwt-signature-algorithm-none
none
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-signature-algorithm-experimental
RS1
: RSASSA-PKCS1 v1_5 with SHA-1 hashing function.
HS1
: HMAC with SHA-1 hashing function.
These algorithms have to be used with the Algorithm Manager. They do not need any arguments.
Example:
Signed tokens are loaded by a serializer or the serializer manager and verified by the JWSVerifier
object. This JWSVerifier object just requires an algorithm manager.
In the following example, we will try to load a signed token. We will only use the HS256
algorithm.
Now we can deserialize the input we receive and check the signature using our key. We will continue with the data we got in the JWS 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.
The method verifyWithKey
returns a boolean. If true, then your token signature is valid. You can then check the claims (if any) using the claim checker manager.
To avoid duplication of code lines, you can create a JWSLoader
object. This object contains a serializer, a verifier and an optional header checker (highly recommended).
In the following example, the JWSLoader
object will try to unserialize the token $token
, check the header parameters and verify the signature with the key $jwk
. The variable $payload
corresponds to the detached payload (null
by default).
If the verification succeeded, the variable $signature
will be set with the signature index and should be in case of multiple signatures. The method returns the JWS object.
In case you use a key set, you can use the method loadAndVerifyWithKeySet
.
This feature was introduced in version 1.1.
The JWSLoaderFactory
object is able to create JWSLoader
objects on demand. It requires the following factories:
JWSSerializerManagerFactory
JWSVerifierFactory
HeaderCheckerManagerFactory
(optional)
To use the signed tokens (JWS), you have to install the web-token/jwt-signature
component.
This component provides lot of signature algorithms and classes to load and create signed tokens.
Please refer to this signature algorithm table to know what algorithms are available.
Then, you will find an example to create a signed token here and another example to load and verify incoming tokens.