mark mark - 2 months ago 18
Javascript Question

How to keep the shared secret secret when using JWT, for example?

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 completely 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?


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.