GWT : How to prevent client-side state from being cleared when a redirection (for e.g : OAuth2) happens?

469 Views Asked by At

I am implementing OAuth2 authentication for one of my GWT projects (let's call it GWT-app). The application responsible for authentication is a third-party application based on Spring Framework-3.1.2.RELEASE and uses its OAuth2 implementation included in Spring Security-3.1.3.RELEASE (let's call it OAuth-app).

GWT-app is a management application for managing user and stores. Each user has one or more manager accounts. A manager account may have one or more stores attached to it so that he can manage them altogether from one screen of the GWT-app.

It means that the user may potentially have to authenticate many times through different manager accounts to display stores by manager account on the app.

So on the StoreManagement.java page of my GWT-app, I have the following :

private final Map<String, ManagerAndStoresProxy> managerAndStores = Maps.newTreeMap(Ordering.natural());

after authentication, this map should be populated with Manager name and a List of attached stores for each.

OAUTH2 FLOW

The Authorization-code flow is used for authentication. When GWT-app wants to authenticate someone, it redirects the user to a login page on OAuth-app. The user enters his credentials and will be presented with an authorization page asking him whether he wants to allow GWT-app to access his information. If the user clicks on authorize, he is redirected back to the GWT-app with an access token.

THE PROBLEM

The only issue is that since GWT-app is redirecting the user to another application for authentication, the first time the authentication works and the map is populated correctly but if I have to authenticate another account then the previously authenticated account is cleared and can not be retrieved because of the redirection.

The question is then : how can I proceed to persist this map and retrieve it back when a second (third, fourth and so on) authentication flow is over (i.e after the url-redirection of OAuth-app).

WHAT I HAVE TRIED SO FAR

What I did so far is that when the user wants to proceed with authentication from the StoreManagement.java page, he is presented with a popup redirected to the OAuth-app authentication page. The access token is then retrieved from this popup but I have to send it back to the parent window (i.e StoreManagement.java page). This can be done via a HTTP GET parameter but this method reloads the parent page and the result is the same: the previously authenticated account is cleared.

QUESTION

What is the best way to handle this situation without clearing the previous context and without requiring any server-side session mechanism whatsoever ?

2

There are 2 best solutions below

3
On

If your app is not limited by old browsers in terms of compatibility then you can try to store all necessary things in HTML5 Web Storage.

2
On

The root of the problem is using the authorization code grant; the implicit grant (aka client-side flow) would be a much better fit.