Refreshing a JWT

From DreamFactory
Jump to: navigation, search
DreamFactoryTutorialsRefreshing a JWT
 
(12 intermediate revisions by 3 users not shown)
Line 1: Line 1:
DreamFactory 2.0 uses Json Web Token (JWT) to maintain user session on the server side in a stateless manner. One of the  
+
DreamFactory uses JSON Web Tokens (JWT) to maintain user session on the server side in a stateless manner. One of the  
features of JWT is the ability to refresh it without re-authenticating as long as the JWT (token) is within the allowed  
+
features of JWT is the ability to refresh the token without re-authenticating, as long as the JWT (token) is within the allowed  
refresh time-frame since it was created. There are two time-to-live (TTL) values that you can config for JWT in the .env
+
refresh timeframe since it was created.  
file. They are...
+
 
 +
There are two time-to-live (TTL) values that you can configure for JWT in the .env
 +
file.
  
 
* DF_JWT_TTL             
 
* DF_JWT_TTL             
** ''Expiration TTL. This the time (in minutes) the token can be active. After this time token expires.''
+
** ''Expiration TTL. This the time (in minutes) until the token expires. After it expires it can be refreshed until DF_JWT_REFRESH_TTL.
 
* DF_JWT_REFRESH_TTL     
 
* DF_JWT_REFRESH_TTL     
** ''Refresh TTL. This the time (in minutes) in which you can refresh the token as times as you want since it's creation.''
+
** ''Refresh TTL. This the time (in minutes) in which you can refresh the token. Between DF_JWT_TTL and DF_JWT_REFRESH_TTL the token can be refreshed as many times as you want without reauthenticating. After DF_JWT_REFRESH_TTL you must log in again.''
  
For example, let's say your expiration TTL (DF_JWT_TTL) is 60 (1 hour) and your refresh TTL (DF_JWT_REFRESH_TTL) is 360 (6 hours).  
+
For example, let's say your expiration TTL (DF_JWT_TTL) is 60 (1 hour) and your refresh TTL (DF_JWT_REFRESH_TTL) is 360 (6 hours). Your user authenticates at 09:00 and receives a JWT. This token is valid until 10:00. Between 10:00 and 15:00 it can be refreshed as many times as you like. Keep in mind that refreshing a token does not extend the refresh window! See these tutorials for more information about JWT internals:
Your user authenticates at 9:00 AM and receives a JWT. Now you have till 3:00 PM to be able to refresh this JWT as many times as  
+
you need to. If you do to refresh the JWT it will expire at 10:00 AM but you will still have another 5 hours to refresh it.
+
Anytime the token is refreshed it will be active for another hour before it expires again.
+
  
=== API Endpoints ===
+
* [https://jwt.io/introduction/ JWT Introduction]
 +
* [https://scotch.io/tutorials/the-anatomy-of-a-json-web-token The Anatomy of a JSON Web Token]
  
==== For admin users ====
+
For these examples, assume your current session token is abc.123.efg.
  
<pre>PUT https://{url}/api/v2/system/admin/session?session_token={current_jwt}</pre>
+
=== Refreshing JWT as an Admin ===
  
-- OR --
+
The Admin refresh API endpoint is api/v2/system/admin/session
  
<pre>PUT https://{url}/api/v2/system/admin/session</pre>
+
Request URL:
  
Request header
 
<pre>
 
...
 
X-DreamFactory-Session-Token: {JWT}
 
...
 
</pre>
 
 
==== Fon non-admin users ====
 
 
<pre>PUT https://{url}/api/v2/user/session?session_token={current_jwt}</pre>
 
 
-- OR --
 
 
<pre>PUT https://{url}/api/v2/user/session</pre>
 
 
Request header
 
<pre>
 
...
 
X-DreamFactory-Session-Token: {JWT}
 
...
 
</pre>
 
 
=== Example - Refreshing JWT for an admin user ===
 
 
Request URL:
 
 
<pre>PUT https://foo.com/api/v2/system/admin/session?session_token=abc.123.efg</pre>
 
<pre>PUT https://foo.com/api/v2/system/admin/session?session_token=abc.123.efg</pre>
* Session Token: abc.123.efg
 
  
 +
Note: Session token can also be supplied using X-DreamFactory-Session-Token request header.
  
-- OR --
+
=== Refreshing JWT as a User (Non-Admin) ===
 
+
Request header
+
<pre>
+
...
+
X-DreamFactory-Session-Token: abc.123.efg
+
...
+
</pre>
+
* Session Token: abc.123.efg
+
  
 +
The non-admin refresh API endpoint is api/v2/user/session
  
 
Request URL:
 
Request URL:
<pre>PUT https://foo.com/api/v2/system/admin/session</pre>
 
  
=== Example - Refreshing JWT for a non-admin user ===
 
 
Request URL:
 
 
<pre>PUT https://foo.com/api/v2/user/session?session_token=abc.123.efg</pre>
 
<pre>PUT https://foo.com/api/v2/user/session?session_token=abc.123.efg</pre>
  
* Session Token: abc.123.efg
+
Note: Session token can also be supplied using X-DreamFactory-Session-Token request header.
 
+
 
+
-- OR --
+
 
+
Request header
+
<pre>
+
...
+
X-DreamFactory-Session-Token: abc.123.efg
+
...
+
</pre>
+
 
+
* Session Token: abc.123.efg
+
 
+
 
+
Request URL:
+
<pre>PUT https://foo.com/api/v2/user/session</pre>
+

Latest revision as of 14:54, 27 August 2018

DreamFactory uses JSON Web Tokens (JWT) to maintain user session on the server side in a stateless manner. One of the features of JWT is the ability to refresh the token without re-authenticating, as long as the JWT (token) is within the allowed refresh timeframe since it was created.

There are two time-to-live (TTL) values that you can configure for JWT in the .env file.

  • DF_JWT_TTL
    • Expiration TTL. This the time (in minutes) until the token expires. After it expires it can be refreshed until DF_JWT_REFRESH_TTL.
  • DF_JWT_REFRESH_TTL
    • Refresh TTL. This the time (in minutes) in which you can refresh the token. Between DF_JWT_TTL and DF_JWT_REFRESH_TTL the token can be refreshed as many times as you want without reauthenticating. After DF_JWT_REFRESH_TTL you must log in again.

For example, let's say your expiration TTL (DF_JWT_TTL) is 60 (1 hour) and your refresh TTL (DF_JWT_REFRESH_TTL) is 360 (6 hours). Your user authenticates at 09:00 and receives a JWT. This token is valid until 10:00. Between 10:00 and 15:00 it can be refreshed as many times as you like. Keep in mind that refreshing a token does not extend the refresh window! See these tutorials for more information about JWT internals:

For these examples, assume your current session token is abc.123.efg.

Refreshing JWT as an Admin

The Admin refresh API endpoint is api/v2/system/admin/session

Request URL:

PUT https://foo.com/api/v2/system/admin/session?session_token=abc.123.efg

Note: Session token can also be supplied using X-DreamFactory-Session-Token request header.

Refreshing JWT as a User (Non-Admin)

The non-admin refresh API endpoint is api/v2/user/session

Request URL:

PUT https://foo.com/api/v2/user/session?session_token=abc.123.efg

Note: Session token can also be supplied using X-DreamFactory-Session-Token request header.