From 149bb3b4be977a5ff4266e1150b0197f4a0f6f48 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sondre=20Eikanger=20Kval=C3=B8?= Date: Mon, 27 Dec 2021 10:19:15 +0100 Subject: [PATCH] =?UTF-8?q?Litt=20sm=C3=A5fiksing=20for=20Java=20interop?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../fiks/maskinporten/AccessTokenRequest.kt | 4 ++-- .../fiks/maskinporten/Maskinportenklient.kt | 7 +++--- .../AccessTokenRequestBuilderTest.java | 22 +++++++++++++++++++ .../maskinporten/MaskinportenklientTest.java | 4 ++-- 4 files changed, 29 insertions(+), 8 deletions(-) diff --git a/maskinporten-client/src/main/kotlin/no/ks/fiks/maskinporten/AccessTokenRequest.kt b/maskinporten-client/src/main/kotlin/no/ks/fiks/maskinporten/AccessTokenRequest.kt index 0008e8f..208efc4 100644 --- a/maskinporten-client/src/main/kotlin/no/ks/fiks/maskinporten/AccessTokenRequest.kt +++ b/maskinporten-client/src/main/kotlin/no/ks/fiks/maskinporten/AccessTokenRequest.kt @@ -16,7 +16,7 @@ data class AccessTokenRequest( /** * Ønsket audience for access token. Valgfritt. */ - val audience: String?) { + val audience: String? = null) { companion object { /** @@ -49,7 +49,7 @@ class AccessTokenRequestBuilder { /** * Legger til et set med scopes som skal brukes i forespørsel mot Maskinporten. Minst et scope må oppgies */ - fun scopes(scopes: java.util.Set): AccessTokenRequestBuilder { + fun scopes(scopes: MutableSet): AccessTokenRequestBuilder { this.scopes = scopes.toSet() return this } diff --git a/maskinporten-client/src/main/kotlin/no/ks/fiks/maskinporten/Maskinportenklient.kt b/maskinporten-client/src/main/kotlin/no/ks/fiks/maskinporten/Maskinportenklient.kt index b065a4b..e90a690 100644 --- a/maskinporten-client/src/main/kotlin/no/ks/fiks/maskinporten/Maskinportenklient.kt +++ b/maskinporten-client/src/main/kotlin/no/ks/fiks/maskinporten/Maskinportenklient.kt @@ -11,7 +11,6 @@ import net.jodah.expiringmap.ExpirationPolicy import net.jodah.expiringmap.ExpiringEntryLoader import net.jodah.expiringmap.ExpiringMap import net.jodah.expiringmap.ExpiringValue -import no.ks.fiks.maskinporten.AccessTokenRequest.Companion.builder import no.ks.fiks.maskinporten.error.MaskinportenClientTokenRequestException import no.ks.fiks.maskinporten.error.MaskinportenTokenRequestException import org.apache.hc.client5.http.config.RequestConfig @@ -94,7 +93,7 @@ class Maskinportenklient(privateKey: PrivateKey, certificate: X509Certificate, p } override fun getAccessToken(scopes: Collection): String? { - return getTokenForRequest(builder().scopes(HashSet(scopes)).build()) + return getTokenForRequest(AccessTokenRequest(scopes = scopes.toSet())) } override fun getAccessToken(vararg scopes: String): String? { @@ -102,7 +101,7 @@ class Maskinportenklient(privateKey: PrivateKey, certificate: X509Certificate, p } override fun getDelegatedAccessToken(consumerOrg: String, scopes: Collection): String? { - return getTokenForRequest(builder().scopes(HashSet(scopes)).consumerOrg(consumerOrg).build()) + return getTokenForRequest(AccessTokenRequest(scopes = scopes.toSet(), consumerOrg = consumerOrg)) } override fun getDelegatedAccessToken(consumerOrg: String, vararg scopes: String): String? { @@ -110,7 +109,7 @@ class Maskinportenklient(privateKey: PrivateKey, certificate: X509Certificate, p } override fun getAccessTokenWithAudience(audience: String, scopes: Collection): String? { - return getTokenForRequest(builder().scopes(HashSet(scopes)).audience(audience).build()) + return getTokenForRequest(AccessTokenRequest(scopes = scopes.toSet(), audience = audience)) } override fun getAccessTokenWithAudience(audience: String, vararg scopes: String): String? { diff --git a/maskinporten-client/src/test/java/no/ks/fiks/maskinporten/AccessTokenRequestBuilderTest.java b/maskinporten-client/src/test/java/no/ks/fiks/maskinporten/AccessTokenRequestBuilderTest.java index b460d54..7b38c43 100644 --- a/maskinporten-client/src/test/java/no/ks/fiks/maskinporten/AccessTokenRequestBuilderTest.java +++ b/maskinporten-client/src/test/java/no/ks/fiks/maskinporten/AccessTokenRequestBuilderTest.java @@ -3,6 +3,9 @@ import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; +import java.util.Arrays; +import java.util.HashSet; + import static org.assertj.core.api.Assertions.assertThat; @@ -25,4 +28,23 @@ void builderTest() { assertThat(accessTokenRequest.getAudience()).isEqualTo(audience); assertThat(accessTokenRequest.getConsumerOrg()).isEqualTo(consumerOrg); } + + @DisplayName("Tester at builder fungerer som forventet også når vi oppgir scopes som Set") + @Test + @SuppressWarnings("unchecked") + void builderWitchScopeSet() { + final String scopeOne = "first"; + final String scopeTwo = "second"; + final String audience = "audience"; + final String consumerOrg = "999999999"; + final AccessTokenRequest accessTokenRequest = AccessTokenRequest.builder() + .scopes(new HashSet(Arrays.asList(scopeOne, scopeTwo))) + .audience(audience) + .consumerOrg(consumerOrg) + .build(); + assertThat(accessTokenRequest.getScopes()).containsExactly(scopeOne, scopeTwo); + assertThat(accessTokenRequest.getAudience()).isEqualTo(audience); + assertThat(accessTokenRequest.getConsumerOrg()).isEqualTo(consumerOrg); + } + } diff --git a/maskinporten-client/src/test/java/no/ks/fiks/maskinporten/MaskinportenklientTest.java b/maskinporten-client/src/test/java/no/ks/fiks/maskinporten/MaskinportenklientTest.java index 6f428ce..edd2c34 100644 --- a/maskinporten-client/src/test/java/no/ks/fiks/maskinporten/MaskinportenklientTest.java +++ b/maskinporten-client/src/test/java/no/ks/fiks/maskinporten/MaskinportenklientTest.java @@ -25,7 +25,7 @@ import org.apache.hc.client5.http.impl.classic.CloseableHttpClient; import org.apache.hc.client5.http.impl.classic.HttpClientBuilder; import org.apache.hc.core5.http.NameValuePair; -import org.apache.hc.core5.net.URLEncodedUtils; +import org.apache.hc.core5.net.WWWFormCodec; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Timeout; @@ -85,7 +85,7 @@ public OidcMockExpectation() { @Override public HttpResponse handle(HttpRequest httpRequest) throws Exception { - final List formParamPairs = URLEncodedUtils.parse(httpRequest.getBodyAsString(), StandardCharsets.UTF_8); + final List formParamPairs = WWWFormCodec.parse(httpRequest.getBodyAsString(), StandardCharsets.UTF_8); final String assertion = formParamPairs.stream().filter(nv -> "assertion".equals(nv.getName())).map(NameValuePair::getValue).findFirst().orElseThrow(() -> new IllegalArgumentException("Fant ikke parameter \"assertion\"")); final JWTClaimsSet jwtClaimsSet = SignedJWT.parse(assertion).getJWTClaimsSet();