Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to safely store & process secret key for JWT

After reading this: JWT: What's a good secret key, and how to store it in an Node.js/Express app?, on how to store "secret key" to assign JWT tokens. I had security questions. My data (messages, username, etc...) are going to be encrypted (in database) and only authorised users can decrypt it (based on their private key). Since JWT tokens are generated using 1 "secret key" which is stored on the server, in case an attacker gets the "secret key" and get's hold of the database - tokens can be forged and therefore data can be decrypted bypassing "password", which makes encryption pointless. To protect the "secret key", I could use these methods

Method 1

Store the "secret key" on a separate server (like HSM) which will be received during login and then used to set the token

Method 2

Encrypt some kind of salt for each user and use it as the "secret key"


I'd like to hear your thoughts and ideas. How does facebook or twitter do it? Do I really need HSM to store private keys for encryption or there's some kind of alternative (eg: safe file system) ?

like image 259
Rainbow Avatar asked Jun 22 '16 15:06

Rainbow


People also ask

Why is it important to store things properly?

Proper food storage helps to preserve the quality and nutritional value of the foods you purchase, and also helps make the most of your food dollar by preventing spoilage. Additionally, proper food storage can help prevent foodborne illnesses caused by harmful bacteria.


1 Answers

Depends on your risk appetite. The fact that you are using JWTs indicates that your system is not a high security system (JWTs cannot be revoked server-side very easily so are unsuitable for highly secure applications).

HSM is a good option, although you'll either need to cache it in memory to validate every subsequent page request unless you are using the RSA algorithm.

The file system may be "secure enough" given that an external attacker cannot arbitrarily access files stored on your server.

Having a per user key somewhat defeats the objective of having a client-side session state mechanism as you will have to lookup this key on every request in your database.

See also Are JWTs a secure option for user authentication?

And also this question.

like image 109
SilverlightFox Avatar answered Sep 20 '22 20:09

SilverlightFox