Skip to content

Commit

Permalink
fix: Redirect if refresh token is in push response also (#14777) (#14780
Browse files Browse the repository at this point in the history
)

Fixes #14753

Co-authored-by: Artur <artur@vaadin.com>
  • Loading branch information
vaadin-bot and Artur- committed Oct 11, 2022
1 parent 587ce1e commit 122a3d1
Showing 1 changed file with 23 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
}

}

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 122a3d1

Please sign in to comment.