I am learning about JWTs and the way I have understood about JWTs is as follows (assuming we are using HS256 signing algorithm):
// pseudo code
HEADER = Base64Encoded(headerJSON)
PAYLOAD = Base64Encoded(payloadJSON)
SIGNATURE = HMACSHA256(`${HEADER}.${PAYLOAD}`, secret)
and finally JWT string is generated by joining all of the above:
JWT = `${HEADER}.${PAYLOAD}.${SIGNATURE}`
So, HEADER and PAYLOAD both are base64 encoded, but I am not sure about signature. I think we need SIGNATURE to be base64 encoded as well as it will be safe for HTTP headers to include.
If it is encoded then do we do it manually? or HMACSHA256 algo automatically returns encoded version?
On the other hand if it is not encoded, then why do we not require it?
JWTs are defined by RFC7519 standard and its Section 1 describes:
So a JWT is either represented as the JWS Compact Serialization or JWE Compact Serialization.
For non-encrypted JWT tokens, we use JWS structure which is defined by RFC7515 standard. Hence JWS Compact Serialization is used which is described in its Section-3.1
An encoding example with
HMACSHA256signing algorithm is also provided in the Appendix-A.1So YES, the signature is Base64 encoded as per the specification and we need to manually encode it after running the signing algorithm.