最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to keep the shared secret secret when using JWT, for example? - Stack Overflow

programmeradmin1浏览0评论

There is something very basic I do not understand. In order for JWT to be secure both the client and the server must share a secret.

However, the client is typically a JavaScript application running in a browser on some remote pletely unknown client machine.

Suppose I am the author of both the server and the client code, how am I supposed to ensure the safety of the shared secret on the client side?

There is something very basic I do not understand. In order for JWT to be secure both the client and the server must share a secret.

However, the client is typically a JavaScript application running in a browser on some remote pletely unknown client machine.

Suppose I am the author of both the server and the client code, how am I supposed to ensure the safety of the shared secret on the client side?

Share Improve this question asked Aug 18, 2016 at 20:15 markmark 63k96 gold badges347 silver badges671 bronze badges 2
  • 1 Are you talking about the secret key used to build the JWT? Because that's not sent to the client... – Blakethepatton Commented Aug 18, 2016 at 20:19
  • This is probably one of my favorite articles (with picto) cryto/~joepie91/blog/2016/06/19/… – Derek Pollard Commented Aug 18, 2016 at 20:20
Add a ment  | 

2 Answers 2

Reset to default 4

You assume the secret is shared. It doesn't have to be. (And it only ever should be shared between systems trusting each other. You usually cannot trust the client that executes your JavaScript.)

A typical use for JWT is for the Server to produce signed data using the secret and sending the signed data (without the secret) somewhere (e.g. a client) without persisting it. When it gets the data back, it can verify (using the secret rather than a persisted copy of the data) that the data hasn't been tampered with since is has been signed.

What application does that use pattern have? You can e.g. implement token-based permissions that way and thus have authentication without identification:

Let's assume you provide a cloud storage service. A user can upload a file, to which you assign some identifier, let's say 5. You generate a shareable URL that has the JWT-signed data "may access file #5" as one of its parameters and display that URL to the user. The user and everyone they share this link with can then access that file through that URL. You just have to verify that the signature is a valid signature created by you and that the signed data indicates the correct file. Of course, if someone with whom the user has shared the URL distributes it further, other people may get access that way, too. But without knowledge of the URL, the file isn't accessible.

It is the same as sending cookie to client on auth and then relying to it for other actions.

Yep, you can't ensure safety of client's cookies, they can be stolen. Same as jwt token can be stolen.

Good part about jwt is that token itself is not being the part of minication as cookie, so you can use it even in http munications - even if somebody gets the payload or header of user's request he wont be able to create new request with other data, which is possible in case of cookies usage.

发布评论

评论列表(0)

  1. 暂无评论