From 4f29c119f7b1ce5665a1729f25ff65fd78c30040 Mon Sep 17 00:00:00 2001 From: caalador Date: Mon, 10 Oct 2022 10:38:07 +0300 Subject: [PATCH] fix: Redirect if refresh token is in push response also (#14777) (#14786) Fixes #14753 --- .../DefaultConnectionStateHandler.java | 38 +++++++++++-------- 1 file changed, 22 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 027294a5ee2..710f46a27cb 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 @@ -398,18 +398,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); @@ -428,10 +417,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); + } } @@ -598,4 +587,21 @@ public void pushClosed(PushConnection pushConnection, Console.log("Push connection closed"); } + 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; + } }