Skip to content

Commit

Permalink
Litt småfiksing for Java interop
Browse files Browse the repository at this point in the history
  • Loading branch information
zapodot committed Dec 27, 2021
1 parent d84d93c commit 149bb3b
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ data class AccessTokenRequest(
/**
* Ønsket audience for access token. Valgfritt.
*/
val audience: String?) {
val audience: String? = null) {

companion object {
/**
Expand Down Expand Up @@ -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<String>): AccessTokenRequestBuilder {
fun scopes(scopes: MutableSet<String>): AccessTokenRequestBuilder {
this.scopes = scopes.toSet()
return this
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -94,23 +93,23 @@ class Maskinportenklient(privateKey: PrivateKey, certificate: X509Certificate, p
}

override fun getAccessToken(scopes: Collection<String>): String? {
return getTokenForRequest(builder().scopes(HashSet(scopes)).build())
return getTokenForRequest(AccessTokenRequest(scopes = scopes.toSet()))
}

override fun getAccessToken(vararg scopes: String): String? {
return getAccessToken(scopesToCollection(*scopes))
}

override fun getDelegatedAccessToken(consumerOrg: String, scopes: Collection<String>): 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? {
return getDelegatedAccessToken(consumerOrg, scopesToCollection(*scopes))
}

override fun getAccessTokenWithAudience(audience: String, scopes: Collection<String>): 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? {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;


Expand All @@ -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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -85,7 +85,7 @@ public OidcMockExpectation() {

@Override
public HttpResponse handle(HttpRequest httpRequest) throws Exception {
final List<NameValuePair> formParamPairs = URLEncodedUtils.parse(httpRequest.getBodyAsString(), StandardCharsets.UTF_8);
final List<NameValuePair> 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();
Expand Down

0 comments on commit 149bb3b

Please sign in to comment.