community-features icon indicating copy to clipboard operation
community-features copied to clipboard

[FEAT] Add token refresh API call to GraphQl and REST API

Open damienwebdev opened this issue 4 years ago • 18 comments

Description (*)

As a user, when I am authenticated via token, I would like to be able to refresh my token (either get a new one, or refresh the expiration on my existing token). Currently, with the REST API and GraphQl APIs, there's no known way to do this.

Expected behavior (*)

As a developer, I can securely refresh a customer's authentication token.

Benefits

Basic usability and security improvement for customers. Otherwise, users are forced to re-auth every hour which is obnoxious.

Additional information

cc: @zetlen

Moved from https://github.com/magento/magento2/issues/26860

damienwebdev avatar Feb 13 '20 18:02 damienwebdev

This is very important! Magento's usual mechanism for managing login lifetime has been PHP sessions using session ID cookies. The session maintained by the frontend area is heavy, so other areas such as GraphQL do not maintain it, even after a user has logged in by supplying a bearer token. (GraphQL will honor a valid Magento session, however.)

Because this session mechanism isn't available in GraphQL, it's also not available in PWA. (PWA does not use the Magento front controller for performance reasons.) Without a full API for expiring and refreshing bearer tokens, PWA user logins simply expire when the tokens naturally expire.

This is against the ecommerce best practice of maintaining "warm" authentication of logged-in users for personalization purposes. Upgrading the login to "hot" so that shoppers can access PII should require a fresh login, but the store should remember the user's identity for a much longer period. A refresh token gives us a natural place to store auth credentials for partial user permissions in a "warm auth" scenario.

zetlen avatar Feb 26 '20 15:02 zetlen

Related to:

https://github.com/magento/magento2/issues/30820 https://github.com/magento/magento2/issues/29922 https://github.com/magento/magento2/issues/26112

damienwebdev avatar Mar 05 '21 18:03 damienwebdev

@zetlen This really needed! We are developing PWAs for so long and the basic feature for token refresh is missing in Magento.

I would really like to work on it if anyone can guide me?

milindsingh avatar Apr 05 '21 20:04 milindsingh

we actually really need this, is anyone working on it?

eperezbanana avatar May 19 '21 19:05 eperezbanana

Right now is not there way to refresh the customer token authorize with the same token?

how you can build an APP without refreshing the token?

Supposing that in the APP you logged in just one time forever.

alexvais avatar Jul 28 '21 15:07 alexvais

Any update on this ?

mohdaali27 avatar Dec 08 '21 08:12 mohdaali27

Is there any progress on this topic? I am looking into this for a mobile app, which is supposed to access the Magento API.

As stated in the authentication methods documentation "Registered users use token-based authentication to make web API calls using a mobile application". This results in obtaining tokens with a short lifetime and no possibility to refresh those. With the current API implementation there is no way to prevent the user from being logged out after the TTL of this access token has expired.

As @zetlen sums up correctly, it goes against best practice in Ecommerce not to provide a way to keep a users session alive.

kayoslab avatar Jan 28 '22 10:01 kayoslab

I'm one more person who agreed with @kayoslab and @zetlen that without proper json web token-based authentication which should include authentication and refresh tokens, currently magento 2 token-based authentication is useless.

The only way I see is to implement custom module for JWT and extend current rest api with new endpoints.

electroidru avatar Jan 31 '22 14:01 electroidru

Regarding JWT this is still as current as back when it was written 5 years ago: https://paragonie.com/blog/2017/03/jwt-json-web-tokens-is-bad-standard-that-everyone-should-avoid

Vinai avatar Feb 11 '22 17:02 Vinai

Regarding JWT this is still as current as back when it was written 5 years ago: https://paragonie.com/blog/2017/03/jwt-json-web-tokens-is-bad-standard-that-everyone-should-avoid

I don't think that't directly related. It's just claim to use cookie over https. However if we are talking about mobile app development as example there's cookies isn't usable. If you need more security you can implement Proof Key for Code Exchange for example (https://datatracker.ietf.org/doc/html/rfc7636).

Anyway I don't mind if another format will be used, but with proper OAuth2 protocol where access and refresh tokens will be available.

electroidru avatar Feb 12 '22 10:02 electroidru

Hey Magento team, it's 2 years since began on this issue, any updates?

yeras-is avatar May 26 '22 09:05 yeras-is

@real34 @medigeek @andimov

yeras-is avatar May 26 '22 09:05 yeras-is

Any updates ?

timothyk0908 avatar Sep 20 '22 15:09 timothyk0908

This issue will likely only be closed if an external contributor creates a 3rd-party open source package.

damienwebdev avatar Sep 20 '22 23:09 damienwebdev

Any updates in 2023? Without refresh tokens, there seems to be no other way, but to force customers to login again and again after their tokens have expired, which does not seem like a proper solution.

MartsTech avatar Jan 25 '23 13:01 MartsTech

Can this be escalated, i would really want to use a PWA to login to Magento but the only thing that's blocking me is te lack of a decent login flow / refresh token

JonasWijne avatar Mar 24 '23 14:03 JonasWijne

I have added refresh tokens for our project. We use magento as a backend for our Next.js frontstore and we use graphql.

Please have a look at https://github.com/FY0u11/magento-oauth-refresh-token.

May be it will help someone.

P.S. I'm not added any settings in admin panel for this module. Also this works only for graphql and not for REST API. And also there can be some errors/bugs, also I'm not sure that my code is right approach to implement refresh tokens either.

FY0u11 avatar May 03 '23 14:05 FY0u11

https://github.com/maaarghk/magento-jwt-refresh-service

composer require maaarghk/magento-jwt-refresh-service

Does not currently do customer tokens but PRs are welcome.

This module goes to the effort of ensuring that refreshing the token does not update the issue date

maaarghk avatar Jul 14 '23 17:07 maaarghk