Create a JWT Claims Set containing the desired claims. Note that
whitespace is explicitly allowed in the representation and no
canonicalization need be performed before encoding.
JWS represents digitally signed or MACed content using JSON data
structures and base64url encoding. These JSON data structures MAY
contain whitespace and/or line breaks before or after any JSON values
or structural characters, in accordance with Section 2 of RFC 7159
[RFC7159].
However, when generating a token with whitespace or line breaks in the JSON, the token does not pass the verification. For example, Example 1 below fails verification, whereas Example 2 passes verification.
It looks like the verify method takes the decoded header and payload arrays, JSON encodes them, then base64 encodes them. It then uses these values to generate the input that will be checked against the signature. However, since it decodes the token, then re-encodes it, the formatting is lost and the resulting signature is different.
Maybe it should take the original header and payload and use that to check against the signature, so that any formatting changes don't impact the verification step.
In RFC 7519 Section 7.1 it mentions
Additionally, in RFC 7515 Section 3 it mentions
However, when generating a token with whitespace or line breaks in the JSON, the token does not pass the verification. For example, Example 1 below fails verification, whereas Example 2 passes verification.
Example 1
Example 2
It looks like the verify method takes the decoded header and payload arrays, JSON encodes them, then base64 encodes them. It then uses these values to generate the input that will be checked against the signature. However, since it decodes the token, then re-encodes it, the formatting is lost and the resulting signature is different.
Maybe it should take the original header and payload and use that to check against the signature, so that any formatting changes don't impact the verification step.