Last active
July 25, 2019 21:30
-
-
Save schube/84c461d2240353c1558bf18ec4c749de to your computer and use it in GitHub Desktop.
JWT Dispatcher
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.schubec.bingooo.client.rest.dispatcher; | |
| import org.fusesource.restygwt.client.Method; | |
| import org.fusesource.restygwt.client.MethodCallback; | |
| import org.gwtproject.event.shared.SimpleEventBus; | |
| import com.google.gwt.core.client.GWT; | |
| import com.google.gwt.http.client.Request; | |
| import com.google.gwt.http.client.RequestCallback; | |
| import com.google.gwt.http.client.RequestException; | |
| import com.google.gwt.http.client.Response; | |
| import com.google.gwt.user.client.Cookies; | |
| import com.schubec.bingooo.client.CookieKeys; | |
| import com.schubec.bingooo.client.event.RuntimeErrorEvent; | |
| import com.schubec.bingooo.client.rest.LoginService; | |
| import com.schubec.bingooo.client.rest.ServiceFactory; | |
| import com.schubec.bingooo.client.rest.models.JWTToken; | |
| import com.schubec.bingooo.client.rest.models.UserCredentials; | |
| /** | |
| * Based on https://stackoverflow.com/questions/35349799/restygwt-custom-dispatcher-doesnt-call-registered-filters | |
| * | |
| */ | |
| public class RefreshTokenDispatcherCallback implements RequestCallback { | |
| protected RequestCallback requestCallback; | |
| private Method mainmethod; | |
| private SimpleEventBus eventBus; | |
| public RefreshTokenDispatcherCallback(Method method, SimpleEventBus eventBus) { | |
| //GWT.log("_________> ForbiddenDispatcherCallback " + method.toString()); | |
| this.mainmethod = method; | |
| this.requestCallback = method.builder.getCallback(); | |
| this.eventBus = eventBus; | |
| } | |
| @Override | |
| public void onResponseReceived(Request request, Response response) { | |
| //GWT.log("_________> ForbiddenDispatcherCallback -> onResponseReceived"); | |
| //GWT.log(response.getStatusText() + "/"+ response.getStatusCode() + ". Request was " + request.toString()); | |
| if ( response.getStatusCode() == Response.SC_OK) { | |
| //GWT.log("onResponseReceived with Statuscode 200/OK"); | |
| requestCallback.onResponseReceived(request, response); | |
| } else if ( response.getStatusCode() == Response.SC_UNAUTHORIZED) { | |
| //Token erneuern | |
| //GWT.log("_________> ForbiddenDispatcherCallback -> onResponseReceived -> Hole neuen JWT"); | |
| LoginService loginservice = ServiceFactory.getLoginService(); | |
| loginservice.refreshtoken(UserCredentials.INSTANCE.getRefreshToken(), new MethodCallback<JWTToken>() { | |
| @Override | |
| public void onSuccess(Method method, JWTToken response) { | |
| //GWT.log("_________> ForbiddenDispatcherCallback -> onResponseReceived -> Hole neuen JWT -> success"); | |
| UserCredentials.INSTANCE.setAccessToken(response.getAccessToken()); | |
| UserCredentials.INSTANCE.setRefreshToken(response.getRefreshToken()); | |
| Cookies.setCookie(CookieKeys.JWT_REFRESH_TOKEN, response.getRefreshToken(), response.getRefreshtokenExpiresAt()); | |
| try { | |
| //Rewrite the Authorization Header... | |
| mainmethod.header("Authorization", "Bearer " + response.getAccessToken()); | |
| //...and send it again! | |
| mainmethod.builder.send(); | |
| } catch (RequestException e) { | |
| // TODO Auto-generated catch block | |
| e.printStackTrace(); | |
| } | |
| } | |
| @Override | |
| public void onFailure(Method method, Throwable exception) { | |
| //GWT.log("_________> ForbiddenDispatcherCallback -> onResponseReceived -> Hole neuen JWT -> error"); | |
| eventBus.fireEvent(new RuntimeErrorEvent("Authentifizierungsfehler", "Refreshtoken konnte nicht verlängert werden")); | |
| Cookies.removeCookie(CookieKeys.PAF); | |
| Cookies.removeCookie(CookieKeys.JWT_REFRESH_TOKEN); | |
| } | |
| }); | |
| } else { | |
| GWT.log("onResponseReceived with Statuscode: " + response.getStatusCode()); | |
| requestCallback.onResponseReceived(request, response); | |
| } | |
| } | |
| @Override | |
| public void onError(Request request, Throwable exception) { | |
| //GWT.log("_________> ForbiddenDispatcherCallback -> onError"); | |
| requestCallback.onError(request, exception); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment