I am little confuse of Refresh Token in OAuth2. Like it says access token limit the time window of 1 hour that hacker can use the user credentials and refresh token is long live token which can be use to recreate the access token.
I am confused if someone stole the access token from cookie he can also stole the refresh token and can use the refresh token to create new access token as I have ajax request in JQuery (Client Side)
NOTE: I have created ajax request to send refresh token on server side I append the Client ID and Secret there with grant type refresh token.
I have saved both access token and refresh token in cookie and use following the ajax request to get new access token
jQuery(document).ajaxError(function(event, jqXHR, ajaxSettings, thrownError) {
//console.log('event');console.log(event);
//console.log('jqXHR');console.log(jqXHR);
//console.log('ajaxSettings');console.log(ajaxSettings);
//console.log('thrownError');console.log(thrownError);
if(jqXHR.status == 403)
{
console.log('User is not Loged in Redictet to Login Page');
}
if(jqXHR.status == 401)
{
var refresh_token = Cookies.get('refresh_token');
if(refresh_token != undefined)
{
$.ajax({
url: CONNECT_API_URL+'/refresh-token',
type: "POST",
data:{ refresh_token: refresh_token },
success: function(response, status, jqXHR){
if(response.access_token != undefined)
{
var expires_in = new Date(new Date().getTime() + response.expires_in * 1000);
var access_token = response.token_type+' '+response.access_token;
Cookies.set('access_token', access_token, { expires: expires_in });
Cookies.set('refresh_token', response.refresh_token, { expires: 14 });
$.ajax(ajaxSettings); // Re send same ajax request with access token in cookie has been set
}
else
{
console.log('Redirect to login page.');
}
},
});
}
}
});
How should I used refresh token to enhance the security?
Here on this blog post with the title Refresh Tokens: When to Use Them and How They Interact with JWTs the topic from your question is nicely discussed.
A quote from that post:
In the RFC6819 spec they write the following about storing access tokens on the client:
And about the issuance of refresh tokens:
This means you should probably think carefully on where you want to store your refresh tokens. This post Where to Store your JWTs – Cookies vs HTML5 Web Storage deals with exactly this topic.
As also mentioned in this answer on StackOverflow only the
refresh_token
is not enough to get a newaccess_token
.This can also be found in that same RFC6819 spec:
Refresh tokens should be used only once. When the
refresh_token
is used it will return a newaccess_token
and a newrefresh_token
. It renders the oldrefresh_token
useless meaning it can no longer be used.It also allows the authentication server to recognize that a
refresh_token
was compromised, since it should only be used once. If a new renew request with the samerefresh_token
comes in the authentication server knows there is something fishy going on. Not sure what is the proper way for the server to deal with such a scenario though... (maybe someone else can shine some light on this)This is also in the RFC6819 spec:
Your access and refresh tokens can be compromised on the client but the tokens could also be intercepted somewhere in between your client and the server. The
refresh_token
is sent only once to a client while theaccess_token
is being sent to the server with every request, that is why the chances of a man-in-the-middle getting his hands on youraccess_token
are much bigger then the chance that yourrefresh_token
is compromised.In general it is good to fully understand the OAuth2 protocol so that you can properly implement it. About the security I would say in short:
I hope this gives you some insight on this topic. If someone wants to add or correct my post please feel free to edit/update/compliment the answer or leave a comment.