Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should jwt web token be encrypted?

I was reading article on JWT web token as an access token that is being response to the user. Some of it mention that the web token should be able to be decoded by the user.

Does it means that it is not a good practice to decrypt the entire web token? For example, I suppose to return following JWT web token to user where this piece of information can be decoded.

eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9.TJVA95OrM7E2cBab30RMHrHDcEfxjoYZgeFONFh7HgQ 

However, I feel that I do not want to let user able to decode his/her access token, so I use another encryption algorithm to encrypt everything into another form as follow and pass back to user.

So, I would decrypt this new text when I'll get this access token in the server and decode it.

Is it recommended to do it this way if I do not wish to expose some of the value available in claim (such as user id) to the user? If not, what are the alternatives?

like image 200
vincentsty Avatar asked Dec 12 '15 03:12

vincentsty


People also ask

When should I encrypt JWT?

Signing and encryption order JSON Web Tokens (JWT) can be signed then encrypted to provide confidentiality of the claims. While it's technically possible to perform the operations in any order to create a nested JWT, senders should first sign the JWT, then encrypt the resulting message.

Is JWT signed or encrypted?

JWTs can be signed using a secret (with the HMAC algorithm) or a public/private key pair using RSA or ECDSA. Although JWTs can be encrypted to also provide secrecy between parties, we will focus on signed tokens.

How do I protect my JWT tokens?

There are two critical steps in using JWT securely in a web application: 1) send them over an encrypted channel, and 2) verify the signature immediately upon receiving it. The asymmetric nature of public key cryptography makes JWT signature verification possible.

Which part of JWT is encrypted?

The generated CEK, which fits the RSA size limitation, is then encrypted with RSA according to the JWE "alg" header parameter, and gets included as part of the JWT.


2 Answers

JWT (RFC7519) is just a compact way to safely transmit claims from an issuer to the audience over HTTP.

JWT can be:

  • signed (JWS - RFC7515)
  • encrypted (JWE - RFC7516)
  • signed then encrypted (this order is highly recommended). The whole JWS is the payload of the JWE
  • encrypted then signed.

It makes sense to encrypt a JWS if you want to keep sensitive information hidden from the bearer (client) or third parties.

The real questions are: does the audience support JWE? If yes, which algorithms are supported?

like image 66
Spomky-Labs Avatar answered Sep 18 '22 06:09

Spomky-Labs


JWT are "signed" and therefore its contents are protected from tampering: you cannot change its contents without invalidating them.

You can optionally "encrypt" the contents and therefore turn them visible only to issuer (the entity creating the token) and the consumer (the entity that is destined to use its contents after verification).

There's a standard for that: JWE

like image 37
Eugenio Pace Avatar answered Sep 21 '22 06:09

Eugenio Pace