Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to refresh access token with grant? #149

Closed
samuelmtimbo opened this issue Dec 9, 2019 · 8 comments
Closed

How to refresh access token with grant? #149

samuelmtimbo opened this issue Dec 9, 2019 · 8 comments

Comments

@samuelmtimbo
Copy link

samuelmtimbo commented Dec 9, 2019

Pardon my naive, as I am still getting used to OAuth2 and grant, but I am failing to find a way to refresh an expired access token using grant. I expect that to be a fairly common scenario.

@simov
Copy link
Owner

simov commented Dec 9, 2019

Hi @samuelmtimbo, refreshing an access token is pretty trivial. Pick an HTTP client and make request to the refresh token endpoint of your provider, you don't need Grant for that. Check out these docs or the documentation of the provider you are using.

@samuelmtimbo
Copy link
Author

Hi @simov. The docs point out the process of refreshing an access token has similarities with the process of getting the access token for the first time. It requires sending the client_id/client_secret combination and uses the same endpoint (authorize_url in oauth.json) to name a couple. I feel that by implementing this logic separately in my server I would be duplicating work already done inside Grant.

Isn't Grant's purpose to abstract away this sort of communication with multiple providers? I would suggest a "/refresh/[provider]" internal endpoint. I can send a PR if that makes sense to you.

@simov
Copy link
Owner

simov commented Dec 9, 2019

The problem is that you have to store your refresh_token somewhere. Grant is only concerned with getting you to a point where you have your access_token and refresh_token, but where you store them after that is up to you.

That might be the session itself, in case you are using external session store, but it might be something else. So that's implementation detail on your end, and that's why you have to implement that endpoint yourself. On the bright side it shouldn't be that difficult to do so, and as you have guessed that sort of endpoint is going to work for 99% of the available OAuth2.0 providers.

@samuelmtimbo
Copy link
Author

That makes sense. What if such endpoint received the refresh_token in the request body? The handler's only job would be using the credentials and endpoints already available to Grant to issue a request with grant_type: “refresh_token” and parse the response with the same logic of "connect/[provider]/callback".

@samuelmtimbo
Copy link
Author

The main reason behind this feature is that it would evolve alongside with Grant, growing as new providers are added and possible custom logic that come with them.

@simov
Copy link
Owner

simov commented Dec 9, 2019

Just keep in mind that storing the refresh_token in the browser is not a good idea. So that kind of defies the purpose of having an endpoint that accepts it.

I think it's best to pass some user identifier to the refresh endpoint and based on that find the refresh_token in your store and make the request. So again you need some state to read from.

@samuelmtimbo
Copy link
Author

I understand. I'd rather not influence a bad practice. Thanks for the attention!

@simov
Copy link
Owner

simov commented Dec 10, 2019

Not a problem @samuelmtimbo, and I appreciate your feedback! It's not a bad idea either, it's just that most likely it won't be implemented in Grant. Might be a separate library or a Grant compatible middleware like grant-profile and grant-oidc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants