52

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?

Maen
  • 10,603
  • 3
  • 45
  • 71
vincentsty
  • 2,963
  • 7
  • 34
  • 51

5 Answers5

63

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?

Community
  • 1
  • 1
Spomky-Labs
  • 15,473
  • 5
  • 40
  • 64
  • 2
    Third bullet is incorrect: it's recommended to sign-then-encrypt, but encrypt-then-sign is also possible. See https://tools.ietf.org/html/rfc7519#section-11.2 – Steven Liekens Oct 12 '18 at 12:55
  • 3
    You are right: even if it is not recommended, this order is not forbidden. I updated the answer accordingly. – Spomky-Labs Oct 12 '18 at 13:03
20

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

Community
  • 1
  • 1
Eugenio Pace
  • 14,094
  • 1
  • 34
  • 43
  • 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 '15 at 08: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. – Eugenio Pace Dec 12 '15 at 19:20
  • 10
    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) – Sinaesthetic Jun 04 '19 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.

Pang
  • 9,564
  • 146
  • 81
  • 122
A Programmer
  • 625
  • 8
  • 30
1

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.

0

JSON Web Token Best Current Practices, RFC 8725 highlights how to properly architect a JWT implementation from scratch, but there is no recommendation from an authoritative body such as IEEE, W3C, or IETF regarding whether or not one should encrypt the JWT payload.

Note: If you follow Google and Bing's web recommendations then you are responding over a secure transport, e.g., HTTPS.

In terms of nailing stateful information such as an access token to the stateless HTTP protocol, see cookies: Restrict access to cookies.

Provided you have implemented JWT properly, your signed JWT shall be considered ironclad, i.e., it is practically impossible as a matter of physics to forge the payload returned to the server.

Ronnie Royston
  • 16,778
  • 6
  • 77
  • 91