Skip to content

Commit

Permalink
chore: cleanup query param implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
TheMrMilchmann committed Nov 14, 2024
1 parent 8872bcc commit 1456f9d
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 45 deletions.
36 changes: 3 additions & 33 deletions src/main/java/com/gw2auth/oauth2/server/util/QueryParam.java
Original file line number Diff line number Diff line change
@@ -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<String> 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);
Expand All @@ -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 {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
5 changes: 3 additions & 2 deletions src/test/java/com/gw2auth/oauth2/server/Matchers.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -35,8 +36,8 @@ public static Matcher<String> asUri(Matcher<? super UriComponents> 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());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -1499,8 +1501,9 @@ public void consentSubmitWithLessScopesThanRequested(SessionHandle sessionHandle

// read request information from redirected uri
final Map<String, String> 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));
Expand Down Expand Up @@ -2413,9 +2416,10 @@ private ResultActions performRetrieveTokenByCode(ApplicationClient applicationCl

private ResultActions performRetrieveTokenByCode(ApplicationClient applicationClient, String clientSecret, String redirectUri, URI redirectedURI, Map<String, String> subtokenByGw2ApiToken, Set<Gw2ApiPermission> 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);

Expand Down Expand Up @@ -2589,8 +2593,9 @@ private ResultActions performSubmitConsent(SessionHandle sessionHandle,

// read request information from redirected uri
final Map<String, String> 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));
Expand Down

0 comments on commit 1456f9d

Please sign in to comment.