Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use RS256 for JWT?

So, right now I'm building an API for third parties uses and I was reading about RS256 and HS256. What I understood was that diff between is that in the first one you use a public key to verify and a private key to sign, and the other one, use just one key.. So if you use RS256 if because you want to keep your secret key secure and want the client to verify the token, but what I don't understand why you would like to verify the token in the client? Because you do a post request to the server, then it sends you back a token and whenever you want to make an authorized request you just use that token and the server verifies it and let you continue if its ok. So, why you would like to verify the token in the client? I thought it was a backend's duty.

I think maybe I'm wrong in something, hope you help clear this. Thanks.

EDIT:

So, my question is, I know the differences between RS256 and HS256 but what I don't understand it's the flow of how is use it. Right now I'm developing a third party api, and I just need to return a token when the client ask for it and then in the request that needs it, just verify from the server if it's a valid token. From what I understand, RS256 it's used when you want to verify your token from the client, if that's right, someone can give me an example of when or why would you want to verify the token in the client?

like image 747
Leonardo Emilio Dominguez Avatar asked Mar 29 '18 18:03

Leonardo Emilio Dominguez


People also ask

Is RS256 same as SHA256?

There are several algorithm options, but the most common are RS256 (RSA Signature with SHA-256) and HS256 (HMAC with SHA-256). The key difference between these two algorithms is that RS256 is asymmetric, and HS256 is symmetric.

What algorithm should I use for JWT?

JWTs are most commonly signed using one of two algorithms: HS256 (HMAC using SHA256), and RS256 (RSA using SHA256).

How do you validate a JWT?

For obtaining claims from JWT, use the verify() method to validate the claims and the signature. Avoid using the decode() method to validate a token, especially if it's coming from a public client.


1 Answers

Use RS256 when:

  • tokens are signed by a third party, usually an Identity Provider(e.g. oauth2/oidc), and you need to verify that the token has been issued by a trusted entity

  • tokens are signed by clients, usually to get access to an API, where clients have previously registered the public key

  • tokens are signed by a centralized authentication server in a SingleSignOn system and they are used to get access to several federated servers

  • tokens are used to transfer data between two parties, not neccesarily for authentication purposes, and the signature is used to ensure the identity of the signatory

Use HS256 when:

  • tokens are signed and validated by the same server
like image 68
pedrofb Avatar answered Oct 03 '22 11:10

pedrofb