Skip to content

Commit e934d56

Browse files
committed
TOMEE-4592 - handle token refresh failure more gracefully
(cherry picked from commit 6c6a805)
1 parent 9bf917b commit e934d56

2 files changed

Lines changed: 33 additions & 5 deletions

File tree

tomee/tomee-security/src/main/java/org/apache/tomee/security/cdi/OpenIdAuthenticationMechanism.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,11 @@ protected AuthenticationStatus handleExpiredTokens(HttpServletRequest request, H
167167
protected AuthenticationStatus refreshTokens(HttpServletRequest request, HttpServletResponse response, HttpMessageContext httpMessageContext) {
168168
try (Client client = ClientBuilder.newClient()) {
169169
RefreshToken refreshToken = openIdContext.getRefreshToken()
170-
.orElseThrow(() -> new IllegalArgumentException("Cannot refresh tokens, no refresh_token received"));
170+
.orElse(null);
171+
172+
if (refreshToken == null) {
173+
throw new IllegalStateException("Cannot refresh tokens, no refresh_token received");
174+
}
171175

172176
Form form = new Form()
173177
.param(OpenIdConstant.CLIENT_ID, definition.clientId())
@@ -182,9 +186,9 @@ protected AuthenticationStatus refreshTokens(HttpServletRequest request, HttpSer
182186
return handleTokenResponse(tokenResponse, httpMessageContext);
183187

184188
} catch (Exception e) {
189+
LOGGER.warning("Token refresh failed, logging out the current subject", e);
185190
cleanSubject(request, response, httpMessageContext);
186-
187-
throw e;
191+
return AuthenticationStatus.SEND_FAILURE;
188192
}
189193
}
190194

tomee/tomee-security/src/test/java/org/apache/tomee/security/cdi/OpenIdAuthenticationMechanismTest.java

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
import jakarta.enterprise.context.ApplicationScoped;
3232
import jakarta.enterprise.inject.Vetoed;
3333
import jakarta.inject.Inject;
34+
import jakarta.security.enterprise.AuthenticationStatus;
35+
import jakarta.security.enterprise.authentication.mechanism.http.HttpMessageContext;
3436
import jakarta.security.enterprise.authentication.mechanism.http.OpenIdAuthenticationMechanismDefinition;
3537
import jakarta.security.enterprise.authentication.mechanism.http.openid.DisplayType;
3638
import jakarta.security.enterprise.authentication.mechanism.http.openid.PromptType;
@@ -40,11 +42,12 @@
4042
import static org.junit.Assert.assertEquals;
4143
import static org.junit.Assert.assertThrows;
4244
import static org.junit.Assert.assertTrue;
45+
import static org.mockito.Mockito.mock;
4346
import static org.mockito.Mockito.when;
4447

4548
@Vetoed
4649
@RunWith(ApplicationComposer.class)
47-
@Classes(cdi = true, value = {OpenIdAuthenticationMechanism.class, TomEEOpenIdContext.class, OpenIdAuthenticationMechanismTest.SimpleStorageHandler.class})
50+
@Classes(cdi = true, value = {OpenIdAuthenticationMechanismTest.TestOpenIdAuthenticationMechanism.class, TomEEOpenIdContext.class, OpenIdAuthenticationMechanismTest.SimpleStorageHandler.class})
4851
public class OpenIdAuthenticationMechanismTest {
4952

5053
@Inject
@@ -72,6 +75,8 @@ public void configureMockedDefinition() {
7275
when(definition.display()).thenReturn(null);
7376
when(definition.prompt()).thenReturn(new PromptType[0]);
7477
when(definition.extraParameters()).thenReturn(new String[0]);
78+
when(definition.logout().notifyProvider()).thenReturn(false);
79+
when(definition.logout().redirectURI()).thenReturn("");
7580
}
7681

7782
@Test
@@ -140,6 +145,25 @@ public void authorizationExtraParametersMalformed() {
140145
assertThrows(IllegalArgumentException.class, () -> authenticationMechanism.buildAuthorizationUri(null, null));
141146
}
142147

148+
@Test
149+
public void refreshTokenFailureDoesNotThrow() {
150+
HttpServletRequest request = mock(HttpServletRequest.class);
151+
HttpServletResponse response = mock(HttpServletResponse.class);
152+
HttpMessageContext messageContext = mock(HttpMessageContext.class, Answers.RETURNS_DEEP_STUBS);
153+
when(request.getRequestURL()).thenReturn(new StringBuffer("https://example.com/app"));
154+
155+
assertEquals(AuthenticationStatus.SEND_FAILURE,
156+
authenticationMechanism.refreshTokens(request, response, messageContext));
157+
}
158+
159+
@ApplicationScoped
160+
public static class TestOpenIdAuthenticationMechanism extends OpenIdAuthenticationMechanism {
161+
@Override
162+
public void cleanSubject(HttpServletRequest request, HttpServletResponse response, HttpMessageContext httpMessageContext) {
163+
// no-op for this focused failure-path test
164+
}
165+
}
166+
143167
@ApplicationScoped
144168
protected static class SimpleStorageHandler extends OpenIdStorageHandler {
145169

@@ -170,4 +194,4 @@ public void delete(HttpServletRequest request, HttpServletResponse response, Str
170194

171195
}
172196
}
173-
}
197+
}

0 commit comments

Comments
 (0)