Strider
Strider

Reputation: 3749

How to handle JWT refresh token in a mobile app environment

I am implementing JWT inside a client mobile app with a separate back-end server, and I am looking for an optimum way to use refresh tokens without too much server calls and while keeping a good user experience.

I am new to implementing such a mechanism, and I am confused about many things, so what I am really looking for is a solid conceptual solution to secure the user access to the app, and keep him at the same time logged in indefinitely.

Any correction or suggestion would be most welcome:

Upvotes: 12

Views: 9737

Answers (2)

Chirag Soni
Chirag Soni

Reputation: 3

These are some very great questions you have asked, I have tried to answer them with my knowledge as I have also had the same questions when I studied about refresh tokens.

Regardless of how the attacker gets hold of the tokens from the user environment, would he be able to use them indefinitely as long as the user is still inactive and isn't logged in again with his credentials to create new tokens?

Answer:- Here, the solution is not binary, It's about how hard we can make it for the attacker to get the resource, this solution which you have told us prevents a lot of cases because the probability of users using their refresh token before the attacker accesses their resources is random and it gives us flexibility to invalidate that attacker's token if we found anything suspicious with our account, so it's always better from a case where attacker can use your access token to access your resources for lifetime.

Having such a service to avoid the asynchronous refresh token problem means more round trips to the server, which might prove costly. Is there a better solution?

Answer:- Correct me if I am wrong, but what I understood from your question is that exchanging a refresh token for new access and a refresh token is costly, so we can always adjust this exchange rate by defining a proper expiry for your access tokens depending on your use case, here we are trading cost and speed for security so, if we need a cheap solution we can always set the expiration of access token to a large interval and of cause it will be less secure.

Upvotes: 0

eyetags
eyetags

Reputation: 157

There are some steps to login / revoke access to an api:

  • When you do log in, send 2 tokens (Access token, Refresh token) in response to the client.
  • The access token will have less expiry time and Refresh will have long expiry time.
  • The client (Front end) will store refresh token in his local storage and access token in cookies.
  • The client will use an access token for calling APIs. But when it expires, pick the refresh token from local storage and call auth server API to get the new token.
  • Your auth server will have an API exposed which will accept refresh token and checks for its validity and return a new access token.
  • Once the refresh token is expired, the User will be logged out.

JSON Web Tokens are a good way of securely transmitting information between parties. Because JWTs can be signed—for example, using public/private key pairs—you can be sure the senders are who they say they are. Additionally, as the signature is calculated using the header and the payload, you can also verify that the content hasn't been tampered with.

What about when the tokens are refreshed asynchronously?

that supposed be done with a single request to an endpoint, so there is a single accessToken

Having such a service to avoid the asynchronous refresh token problem means more round trips to the server, which might prove costly. Is there a better solution?

i think that's the best & secure solution for mobile and serverless apps, token are like ssh keys must be kept secure all the time :)

for more information check [question]: JWT refresh token flow

Here's the official introduction to JWT

Upvotes: 4

Related Questions