中文 / English
铁叔

天地不仁 以万物为刍狗


  • 首页

  • 归档

  • 关于我

  • 公益404

  • 搜索

Make JWT more safe

时间: 2023-10-21   |   分类: JWT   | 字数: 419 字 | 阅读: 2分钟 | 阅读次数:

What is JWT

JSON Web Token (JWT) is an open standard (RFC 7519) that defines a compact and self-contained way for securely transmitting information between parties as a JSON object. This information can be verified and trusted because it is digitally signed. JWTs can be signed using a secret (with the HMAC algorithm) or a public/private key pair using RSA or ECDSA.

Make JWT more safe

secret for every Account

In some best practice of JWT, they just say we need an universe secret for every account signature, which mostly from an envirment varible. But the secret is so fundemental, is it enough safe that a global secret for all account? Nobody care it, nobody discuss it, nobody mention it.

We use different salt to hash account password for different account, why we use same secret for all account? The JWT is JUST as important as user’s password!

In my opinion, we should use different secret for every account, just like password salt.

If we use different secret for every account, we should save the secret in server side, DB or redis. Of course, this will slow down the JWT validate flow, but we could save the secret in server’s memory cache after server start up.

save & validate signature in server side

JWT is mostly about how to verify the JWT token is valid, by the signature. In most scene, this is great. But in some case, for example, we want kick off a special user, JWT can do nothing.

So, we could save the JWT signature in the server, then we could verify the JWT twice:

  • first, verify the signature;
  • second, check the signature in server DB or redis, if exists, the JWT is valid; or else invalid

By this method, we can kick off user or force user re-login.

Also, there are some tricks, we don’t need to save all JWT payload in the server, for example, we can just save the userId and JWT signature in the server side.

JWT for anonymous or visitors

Sometime we want give better user expirence for the anonymous or visitors, we can use JWT to record visitor’s information. For example, the user’s information:

    name
    IP address
    first visit time
    location(optional)

use these information, we could track visitors and provide good expirence.

Question

If we use JWT this way, why we use JWT instead of session? One of the most benefit of JWT is the client can get many information from JWT, even they have not connect to the server.

#JWT# #safety#

声明:Make JWT more safe

链接:https://guotie.github.io/post/make-jwt-more-safe/

作者:铁叔

声明: 本博客文章除特别声明外,均采用 CC BY-NC-SA 3.0许可协议,转载请注明出处!

创作实属不易,如有帮助,那就打赏博主些许茶钱吧 ^_^
WeChat Pay

微信打赏

Alipay

支付宝打赏

Centralized exchanges and wallets should take the lead in using Account abstraction
关于稳定币的一个小小的想法(1)
铁叔

铁叔

千里之行 始于足下

25 日志
14 分类
56 标签
GitHub twitter telegram email medium
标签云
  • Solidity
  • Defi
  • Aave
  • Compound
  • Abi
  • Dapp
  • Ethereum
  • Evm
  • Lend protocol
  • Lending
© 2010 - 2024 铁叔
Powered by - Hugo v0.119.0 / Theme by - NexT
/
0%