Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get an oauth access token from refresh token in passportjs

To determine when to use a refresh token to ask for a new access token, I'm aware of two approaches (below). Are either of these approaches possible without modifying the passport-google-oauth library?

1) The "pre-emptive" method

  • Save the access token's expiry time when its granted
  • Check the expiry time against the current time whenever using an access token to access the API
  • If the access token is not expired, use it to access the API
  • If the access token is expired (or close to being expired), supply the refresh token to get a new access token

2) The "handle failure" method

  • Always supply access token
  • If the access token fails to authenticate, supply the refresh token, get a new access token

Thanks. Also welcome any alternatives.

like image 766
max Avatar asked Oct 25 '13 20:10

max


People also ask

How do I get the access token from refresh token?

To get a refresh token , you must include the offline_access scope when you initiate an authentication request through the /authorize endpoint. Be sure to initiate Offline Access in your API. For more information, read API Settings.

Can refresh token be used as access token?

Once they expire, client applications can use a refresh token to "refresh" the access token. That is, a refresh token is a credential artifact that lets a client application get new access tokens without having to ask the user to log in again.

How do I get access token and refresh token OAuth2?

Step 1 − First, the client authenticates with the authorization server by giving the authorization grant. Step 2 − Next, the authorization server authenticates the client, validates the authorization grant and issues the access token and refresh token to the client, if valid.


1 Answers

Note that Passport does not actively use the access token or refresh token, other than to fetch the user profile during login. You're application is responsible for using these tokens when making whatever API requests are necessary. As such, you can implement either method you describe, Passport is not involved in the process.

See also: https://github.com/jaredhanson/passport-google-oauth/issues/23

like image 173
Jared Hanson Avatar answered Nov 15 '22 00:11

Jared Hanson