Sfisioza
Sfisioza

Reputation: 3940

JSON web token auth logic with refresh tokens

Angular 4 application running in browser (website backend), displaying from the server data owned by particular user. Server: PHP+MySQL, Zend Framework 3 + Doctrine ORM

Naming:

Main point of using refresh_tokens is to be logged in longer than access_token short lifetime (possibly forever if refresh_token expiration time is updated each time user authorization happens), user needs to provide credentials only in case of inactivity longer than refresh_token lifetime. Refresh tokens are stored in db, so can be easily revoked.

1. Browser tries to authenticate

REQUEST:

RESPONSE:

Username and password is validated and checked against database

If valid:

If invalid:

ACTION AFTER

IF valid:

Looks like it's not a good idea to store refresh_token in local storage - but this allows for "keep me signed in". If stored only per browser session in private member variable, user would need to log in every time opens the browser. Any ideas?

If invalid:

2. Browser requests protected data from server

REQUEST:

RESPONSE:

ACTION AFTER RESPONSE:

3. Retry to autenticate using refresh_token (after HTTP 401 Unauthorized)

REQUEST:

RESPONSE:

If valid:

Generate new refresh_token, save to database, update ttl (or should be the same token, only updated ttl?) Generate new access_token HTTP 200 OK

If invalid:

HTTP 401 Unauthorized

ACTIONS AFTER RESPONSE:

If valid:

If invalid:

Questions

The key point I don't like is that access_token and refresh_token are stored in the same place and sent the same way. Maybe there is another way to do it?

Upvotes: 5

Views: 2648

Answers (1)

L RodMrez
L RodMrez

Reputation: 138

Regarding where to store tokens and the security flaws (taken from Github user brettpostin https://github.com/IdentityServer/IdentityServer3/issues/2039#issuecomment-288135399) keep in mind that:

  • Storing tokens in localStorage is susceptible to xss.
  • Storing tokens in cookies is susceptible to csrf.

Best option is to protect against both as described here: http://www.redotheweb.com/2015/11/09/api-security.html

Store your tokens in http-only cookies and use a suitable targeted csrf defense as suggested here: https://www.owasp.org/index.php/Cross-Site_Request_Forgery_(CSRF)_Prevention_Cheat_Sheet

when should I update refresh_token lifetime?

If you are asking when to update the refresh token it depends on the implementation of your protocol, for example Google Auth server issued Refresh tokens never expire, they are revoked when the user revokes access to the app. But may I suggest a few weeks, so you don't lose control over it.

any open source projects to see similar authentication in action?

Well, you can download any project that uses OAuth2, for example this project on github: https://github.com/authlib/example-oauth2-server

any other suggestions?

Have a look at OpenID Connect and Identity Providers (IDPs) such as Keycloack.

Hope this helps you in any way.

Upvotes: 4

Related Questions