From 8f383aabb762fec9130c06cbc261a6505bd735fc Mon Sep 17 00:00:00 2001 From: Leon Linhart Date: Mon, 23 Oct 2023 14:13:56 +0200 Subject: [PATCH] fix: use `RequestCache` to clear `REDIRECT_URI` cookie without reflective lookups --- .../service/user/Gw2AuthTokenUserService.java | 11 ++++++++-- .../gw2auth/oauth2/server/util/Constants.java | 17 -------------- .../oauth2/server/util/CookieHelper.java | 22 ------------------- 3 files changed, 9 insertions(+), 41 deletions(-) diff --git a/src/main/java/com/gw2auth/oauth2/server/service/user/Gw2AuthTokenUserService.java b/src/main/java/com/gw2auth/oauth2/server/service/user/Gw2AuthTokenUserService.java index f68da731..97d359db 100644 --- a/src/main/java/com/gw2auth/oauth2/server/service/user/Gw2AuthTokenUserService.java +++ b/src/main/java/com/gw2auth/oauth2/server/service/user/Gw2AuthTokenUserService.java @@ -8,8 +8,10 @@ import com.gw2auth.oauth2.server.util.Pair; import com.gw2auth.oauth2.server.util.SymEncryption; import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.oauth2.jwt.Jwt; +import org.springframework.security.web.savedrequest.RequestCache; import org.springframework.stereotype.Service; import javax.crypto.SecretKey; @@ -28,14 +30,16 @@ public class Gw2AuthTokenUserService implements Clocked { private final RequestSessionMetadataExtractor requestSessionMetadataExtractor; private final SessionMetadataService sessionMetadataService; private final AccountService accountService; + private final RequestCache requestCache; private Clock clock; @Autowired - public Gw2AuthTokenUserService(Gw2AuthInternalJwtConverter jwtConverter, RequestSessionMetadataExtractor requestSessionMetadataExtractor, SessionMetadataService sessionMetadataService, AccountService accountService) { + public Gw2AuthTokenUserService(Gw2AuthInternalJwtConverter jwtConverter, RequestSessionMetadataExtractor requestSessionMetadataExtractor, SessionMetadataService sessionMetadataService, AccountService accountService, RequestCache requestCache) { this.jwtConverter = jwtConverter; this.requestSessionMetadataExtractor = requestSessionMetadataExtractor; this.sessionMetadataService = sessionMetadataService; this.accountService = accountService; + this.requestCache = requestCache; this.clock = Clock.systemUTC(); } @@ -116,7 +120,10 @@ public Optional resolveUserForToken(HttpServletRequest request, S user = new Gw2AuthUserV2(account.id(), accountFederation.issuer(), accountFederation.idAtIssuer(), sessionId, currentSessionMetadata, encryptionKeyBytes); request.setAttribute(REQUEST_ATTRIBUTE_NAME, user); - CookieHelper.clearCookieIfPresent(request, AuthenticationHelper.getCurrentResponse().orElseThrow(), Constants.REDIRECT_URI_COOKIE_NAME); + HttpServletResponse response = AuthenticationHelper.getCurrentResponse().orElseThrow(); + if (this.requestCache.getRequest(request, response) != null) { + this.requestCache.removeRequest(request, response); + } return Optional.of(user); } diff --git a/src/main/java/com/gw2auth/oauth2/server/util/Constants.java b/src/main/java/com/gw2auth/oauth2/server/util/Constants.java index 5415dda3..76d8e1d4 100644 --- a/src/main/java/com/gw2auth/oauth2/server/util/Constants.java +++ b/src/main/java/com/gw2auth/oauth2/server/util/Constants.java @@ -1,24 +1,7 @@ package com.gw2auth.oauth2.server.util; -import org.springframework.security.web.savedrequest.CookieRequestCache; - -import java.lang.reflect.Field; - public final class Constants { public static final String LOGOUT_URL = "/auth/logout"; public static final String ACCESS_TOKEN_COOKIE_NAME = "BEARER"; - public static final String REDIRECT_URI_COOKIE_NAME; - - static { - try { - final Field field = CookieRequestCache.class.getDeclaredField("COOKIE_NAME"); - final boolean wasAccessible = field.canAccess(null); - field.setAccessible(true); - REDIRECT_URI_COOKIE_NAME = (String) field.get(null); - field.setAccessible(wasAccessible); - } catch (ReflectiveOperationException e) { - throw new RuntimeException(e); - } - } } diff --git a/src/main/java/com/gw2auth/oauth2/server/util/CookieHelper.java b/src/main/java/com/gw2auth/oauth2/server/util/CookieHelper.java index 2c7facc1..50a4b83a 100644 --- a/src/main/java/com/gw2auth/oauth2/server/util/CookieHelper.java +++ b/src/main/java/com/gw2auth/oauth2/server/util/CookieHelper.java @@ -19,28 +19,6 @@ public static void addCookie(HttpServletRequest request, HttpServletResponse res response.addCookie(cookie); } - public static void clearCookie(HttpServletRequest request, HttpServletResponse response, String name) { - final Cookie cookie = new Cookie(name, null); - cookie.setMaxAge(0); - cookie.setPath(getRequestContext(request)); - cookie.setSecure(request.isSecure()); - cookie.setHttpOnly(true); - - response.addCookie(cookie); - } - - public static void clearCookieIfPresent(HttpServletRequest request, HttpServletResponse response, String name) { - final Cookie[] cookies = request.getCookies(); - if (cookies != null) { - for (Cookie cookie : cookies) { - if (cookie.getName().equals(name)) { - clearCookie(request, response, name); - break; - } - } - } - } - private static String getRequestContext(HttpServletRequest request) { String contextPath = request.getContextPath(); return contextPath.isEmpty() ? "/" : contextPath;