From 122a3d1446e5d7561c98213fa08dc1b525ef1039 Mon Sep 17 00:00:00 2001 From: Vaadin Bot Date: Tue, 11 Oct 2022 10:34:57 +0200 Subject: [PATCH] fix: Redirect if refresh token is in push response also (#14777) (#14780) Fixes #14753 Co-authored-by: Artur --- .../DefaultConnectionStateHandler.java | 39 +++++++++++-------- 1 file changed, 23 insertions(+), 16 deletions(-) diff --git a/flow-client/src/main/java/com/vaadin/client/communication/DefaultConnectionStateHandler.java b/flow-client/src/main/java/com/vaadin/client/communication/DefaultConnectionStateHandler.java index db44c1d8ffb..735c3ea8da3 100644 --- a/flow-client/src/main/java/com/vaadin/client/communication/DefaultConnectionStateHandler.java +++ b/flow-client/src/main/java/com/vaadin/client/communication/DefaultConnectionStateHandler.java @@ -331,18 +331,7 @@ public void xhrInvalidContent(XhrConnectionError xhrConnectionError) { endRequest(); String responseText = xhrConnectionError.getXhr().getResponseText(); - /* - * A servlet filter or equivalent may have intercepted the request and - * served non-UIDL content (for instance, a login page if the session - * has expired.) If the response contains a magic substring, do a - * synchronous refresh. See #8241. - */ - MatchResult refreshToken = RegExp - .compile(UIDL_REFRESH_TOKEN + "(:\\s*(.*?))?(\\s|$)") - .exec(responseText); - if (refreshToken != null) { - WidgetUtil.redirect(refreshToken.getGroup(2)); - } else { + if (!redirectIfRefreshToken(responseText)) { handleUnrecoverableCommunicationError( "Invalid JSON response from server: " + responseText, xhrConnectionError); @@ -361,10 +350,10 @@ public void pushInvalidContent(PushConnection pushConnection, endRequest(); } - // Do nothing special for now. Should likely do the same as - // xhrInvalidContent - handleUnrecoverableCommunicationError( - "Invalid JSON from server: " + message, null); + if (!redirectIfRefreshToken(message)) { + handleUnrecoverableCommunicationError( + "Invalid JSON from server: " + message, null); + } } @@ -537,6 +526,24 @@ private void resumeHeartbeats() { registry.getApplicationConfiguration().getHeartbeatInterval()); } + private boolean redirectIfRefreshToken(String message) { + /* + * A servlet filter or equivalent may have intercepted the request and + * served non-UIDL content (for instance, a login page if the session + * has expired.) If the response contains a magic substring, do a + * synchronous refresh. See #8241. + */ + MatchResult refreshToken = RegExp + .compile(UIDL_REFRESH_TOKEN + "(:\\s*(.*?))?(\\s|$)") + .exec(message); + if (refreshToken != null) { + WidgetUtil.redirect(refreshToken.getGroup(2)); + return true; + } + + return false; + } + private void registerConnectionStateEventHandlers() { Browser.getWindow().addEventListener("offline", event -> // Browser goes offline: CONNECTION_LOST and stop heartbeats