diff --git a/src/main/java/com/gw2auth/oauth2/server/util/QueryParam.java b/src/main/java/com/gw2auth/oauth2/server/util/QueryParam.java index e581e83..4b7ea06 100644 --- a/src/main/java/com/gw2auth/oauth2/server/util/QueryParam.java +++ b/src/main/java/com/gw2auth/oauth2/server/util/QueryParam.java @@ -1,24 +1,11 @@ package com.gw2auth.oauth2.server.util; -import org.jspecify.annotations.Nullable; - import java.net.URLDecoder; import java.nio.charset.StandardCharsets; -import java.util.Optional; -public interface QueryParam { +public sealed interface QueryParam { String name(); - @Nullable String value(); - boolean hasValue(); - - default Optional optionalValue() { - if (hasValue()) { - return Optional.of(value()); - } else { - return Optional.empty(); - } - } static QueryParam parse(String rawPair) { final String[] pair = Utils.split(rawPair, "=", 2).map((part) -> URLDecoder.decode(part, StandardCharsets.UTF_8)).toArray(String[]::new); @@ -30,24 +17,7 @@ static QueryParam parse(String rawPair) { } } - record QueryParamWithValue(String name, String value) implements QueryParam { + record QueryParamWithValue(String name, String value) implements QueryParam {} - @Override - public boolean hasValue() { - return true; - } - } - - record QueryParamWithoutValue(String name) implements QueryParam { - - @Override - public @Nullable String value() { - return null; - } - - @Override - public boolean hasValue() { - return false; - } - } + record QueryParamWithoutValue(String name) implements QueryParam {} } diff --git a/src/test/java/com/gw2auth/oauth2/server/Gw2AuthLoginExtension.java b/src/test/java/com/gw2auth/oauth2/server/Gw2AuthLoginExtension.java index 6b6dc6a..5a74a19 100644 --- a/src/test/java/com/gw2auth/oauth2/server/Gw2AuthLoginExtension.java +++ b/src/test/java/com/gw2auth/oauth2/server/Gw2AuthLoginExtension.java @@ -164,9 +164,10 @@ private ResultActions loginInternal(SessionHandle sessionHandle, String loginURL final String location = Objects.requireNonNull(result.getResponse().getRedirectedUrl()); final String state = Utils.parseQuery(new URI(location).parseServerAuthority().getRawQuery()) - .filter(QueryParam::hasValue) + .map(queryParam -> queryParam instanceof QueryParam.QueryParamWithValue qpwv ? qpwv : null) + .filter(Objects::nonNull) .filter((queryParam) -> queryParam.name().equals(OAuth2ParameterNames.STATE)) - .map(QueryParam::value) + .map(QueryParam.QueryParamWithValue::value) .findFirst() .orElseThrow(); diff --git a/src/test/java/com/gw2auth/oauth2/server/Matchers.java b/src/test/java/com/gw2auth/oauth2/server/Matchers.java index 2c934f5..63058a5 100644 --- a/src/test/java/com/gw2auth/oauth2/server/Matchers.java +++ b/src/test/java/com/gw2auth/oauth2/server/Matchers.java @@ -1,5 +1,6 @@ package com.gw2auth.oauth2.server; +import com.gw2auth.oauth2.server.util.QueryParam.QueryParamWithValue; import com.gw2auth.oauth2.server.util.Utils; import org.hamcrest.Description; import org.hamcrest.Matcher; @@ -35,8 +36,8 @@ public static Matcher asUri(Matcher matcher) { .fragment(uri.getFragment()); Utils.parseQuery(uri.getRawQuery()).forEach((queryParam) -> { - if (queryParam.hasValue()) { - builder.queryParam(queryParam.name(), queryParam.value()); + if (queryParam instanceof QueryParamWithValue(String name, String value)) { + builder.queryParam(name, value); } else { builder.queryParam(queryParam.name()); } diff --git a/src/test/java/com/gw2auth/oauth2/server/oauth2/OAuth2ServerTest.java b/src/test/java/com/gw2auth/oauth2/server/oauth2/OAuth2ServerTest.java index 18da959..e0340bc 100644 --- a/src/test/java/com/gw2auth/oauth2/server/oauth2/OAuth2ServerTest.java +++ b/src/test/java/com/gw2auth/oauth2/server/oauth2/OAuth2ServerTest.java @@ -30,6 +30,7 @@ import com.gw2auth.oauth2.server.service.application.client.account.ApplicationClientAccount; import com.gw2auth.oauth2.server.service.application.client.authorization.ApplicationClientAuthorizationServiceImpl; import com.gw2auth.oauth2.server.util.QueryParam; +import com.gw2auth.oauth2.server.util.QueryParam.QueryParamWithValue; import com.gw2auth.oauth2.server.util.Utils; import com.nimbusds.jwt.JWT; import com.nimbusds.jwt.JWTParser; @@ -1328,9 +1329,10 @@ public void consentSubmitWithUnexpectedGW2APIException(SessionHandle sessionHand // retrieve the initial access and refresh token final String codeParam = Utils.parseQuery(URI.create(Objects.requireNonNull(result.getResponse().getRedirectedUrl())).getRawQuery()) - .filter(QueryParam::hasValue) + .map(queryParam -> queryParam instanceof QueryParamWithValue qpwv ? qpwv : null) + .filter(Objects::nonNull) .filter((queryParam) -> queryParam.name().equals(OAuth2ParameterNames.CODE)) - .map(QueryParam::value) + .map(QueryParamWithValue::value) .findFirst() .orElse(null); @@ -1499,8 +1501,9 @@ public void consentSubmitWithLessScopesThanRequested(SessionHandle sessionHandle // read request information from redirected uri final Map params = Utils.parseQuery(URI.create(result.getResponse().getRedirectedUrl()).getRawQuery()) - .filter(QueryParam::hasValue) - .collect(Collectors.toMap(QueryParam::name, QueryParam::value)); + .map(queryParam -> queryParam instanceof QueryParamWithValue qpwv ? qpwv : null) + .filter(Objects::nonNull) + .collect(Collectors.toMap(QueryParam::name, QueryParamWithValue::value)); assertTrue(params.containsKey(OAuth2ParameterNames.CLIENT_ID)); assertTrue(params.containsKey(OAuth2ParameterNames.STATE)); @@ -2413,9 +2416,10 @@ private ResultActions performRetrieveTokenByCode(ApplicationClient applicationCl private ResultActions performRetrieveTokenByCode(ApplicationClient applicationClient, String clientSecret, String redirectUri, URI redirectedURI, Map subtokenByGw2ApiToken, Set expectedGw2ApiPermissions) throws Exception { final String codeParam = Utils.parseQuery(redirectedURI.getRawQuery()) - .filter(QueryParam::hasValue) + .map(queryParam -> queryParam instanceof QueryParamWithValue qpwv ? qpwv : null) + .filter(Objects::nonNull) .filter((queryParam) -> queryParam.name().equals(OAuth2ParameterNames.CODE)) - .map(QueryParam::value) + .map(QueryParamWithValue::value) .findFirst() .orElse(null); @@ -2589,8 +2593,9 @@ private ResultActions performSubmitConsent(SessionHandle sessionHandle, // read request information from redirected uri final Map params = Utils.parseQuery(redirectedURI.getRawQuery()) - .filter(QueryParam::hasValue) - .collect(Collectors.toMap(QueryParam::name, QueryParam::value)); + .map(queryParam -> queryParam instanceof QueryParam.QueryParamWithValue qpwv ? qpwv : null) + .filter(Objects::nonNull) + .collect(Collectors.toMap(QueryParam::name, QueryParamWithValue::value)); assertTrue(params.containsKey(OAuth2ParameterNames.CLIENT_ID)); assertTrue(params.containsKey(OAuth2ParameterNames.STATE));