47

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?

2
  • If not encrypted (just signed), wouldn't it be a security vulnerability? With enough such tokens, wouldn't the the hashing be eventually compromised by doing a lot of try-fails? May 23, 2021 at 10:07
  • Vincent, what's the article you were reading? Jun 22, 2021 at 8:38

4 Answers 4

56

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

JWT can be:

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?

2
  • 2
    Third bullet is incorrect: it's recommended to sign-then-encrypt, but encrypt-then-sign is also possible. See tools.ietf.org/html/rfc7519#section-11.2 Oct 12, 2018 at 12:55
  • 3
    You are right: even if it is not recommended, this order is not forbidden. I updated the answer accordingly. Oct 12, 2018 at 13:03
18

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

3
  • 1
    To put it simple, it is wrongful to encrypt the entire JWT so that the consumer do now know the content at all? What i mean here is to Encrypt the entire jwt token again with own algorithm instead of optionally encrypt the content.
    – vincentsty
    Dec 12, 2015 at 8:03
  • It is not wrong. It might not always be necessary. Often, it is required if the information is sensitive. But that not always is the case. I would stick with standards. Dec 12, 2015 at 19:20
  • 8
    I think the callout here, to be very clear for other visitors reading this, is that the JWT is protected from tampering by way of its signature, but the payload is still visible to anyone that gets hold of it. If you have sensitive data in that payload, then encrypting it might be a good idea. The challenge is that you need be sure that whichever system is receiving that token can decrypt that token if it needs to read the payload (e.g. claims) Jun 4, 2019 at 16:24
6

A token contains user data and acts like a temp storage. It is not good to store sensitive data in a token.

At the first level, you should store the user name and maybe role or something like that. You should not include passwords, so it does not need to be encrypted. Nevertheless, you can encrypt it if you want.

0

Yes, the best practice is use the JSON Web Encryption (JWE) RFC, the claims in the JWT after decode it are in plain text, so if the user lost the token, the sensitive information as email, username, access permissions can be visible and can be used as a the initial information of any attack.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.