Skip to content

Commit

Permalink
Simplify wiremock config in oidc
Browse files Browse the repository at this point in the history
  • Loading branch information
manofthepeace committed Sep 8, 2023
1 parent d1e8f60 commit 26e0e8e
Showing 1 changed file with 12 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static com.github.tomakehurst.wiremock.client.WireMock.containing;
import static com.github.tomakehurst.wiremock.client.WireMock.get;
import static com.github.tomakehurst.wiremock.client.WireMock.matching;
import static com.github.tomakehurst.wiremock.client.WireMock.post;
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
import static com.github.tomakehurst.wiremock.client.WireMock.urlPathMatching;
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;
Expand All @@ -12,7 +13,6 @@

import java.security.cert.X509Certificate;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
Expand All @@ -25,10 +25,6 @@
import org.jose4j.keys.X509Util;

import com.github.tomakehurst.wiremock.WireMockServer;
import com.github.tomakehurst.wiremock.client.WireMock;
import com.github.tomakehurst.wiremock.core.WireMockConfiguration;
import com.github.tomakehurst.wiremock.extension.responsetemplating.ResponseTemplateTransformer;
import com.github.tomakehurst.wiremock.extension.responsetemplating.TemplateEngine;
import com.google.common.collect.ImmutableSet;

import io.quarkus.test.common.QuarkusTestResourceLifecycleManager;
Expand All @@ -55,11 +51,7 @@ public class OidcWiremockTestResource implements QuarkusTestResourceLifecycleMan
@Override
public Map<String, String> start() {

WireMockConfiguration wireMockConfiguration = wireMockConfig();
server = new WireMockServer(wireMockConfiguration
.extensions(new ResponseTemplateTransformer(TemplateEngine.defaultTemplateEngine(), false,
wireMockConfiguration.filesRoot(), Collections.emptyList()))
.dynamicPort());
server = new WireMockServer(wireMockConfig().dynamicPort());
server.start();

server.stubFor(
Expand Down Expand Up @@ -245,10 +237,9 @@ private void defineUserInfoStubForJwt() {

private void defineValidIntrospectionMockTokenStubForUserWithRoles(String user, Set<String> roles) {
long exp = now() + 300;
server.stubFor(WireMock.post("/auth/realms/quarkus/protocol/openid-connect/token/introspect")
server.stubFor(post("/auth/realms/quarkus/protocol/openid-connect/token/introspect")
.withRequestBody(matching("token=" + user + "&token_type_hint=access_token"))
.willReturn(WireMock
.aResponse()
.willReturn(aResponse()
.withHeader("Content-Type", "application/json")
.withBody(
"{\"active\":true,\"scope\":\"" + roles.stream().collect(joining(" ")) + "\",\"username\":\""
Expand All @@ -262,10 +253,9 @@ private static final long now() {
}

private void defineInvalidIntrospectionMockTokenStubForUserWithRoles(String user, Set<String> roles) {
server.stubFor(WireMock.post("/auth/realms/quarkus/protocol/openid-connect/token/introspect")
server.stubFor(post("/auth/realms/quarkus/protocol/openid-connect/token/introspect")
.withRequestBody(matching("token=" + user + "&token_type_hint=access_token"))
.willReturn(WireMock
.aResponse()
.willReturn(aResponse()
.withHeader("Content-Type", "application/json")
.withBody(
"{\"active\":true,\"scope\":\"" + roles.stream().collect(joining(" ")) + "\",\"username\":\""
Expand All @@ -274,14 +264,14 @@ private void defineInvalidIntrospectionMockTokenStubForUserWithRoles(String user
}

private void defineJwtBearerGrantTokenStub() {
server.stubFor(WireMock.post("/auth/realms/quarkus/jwt-bearer-token")
server.stubFor(post("/auth/realms/quarkus/jwt-bearer-token")
.withRequestBody(containing("client_id=quarkus-app"))
.withRequestBody(containing("client_secret=secret"))
.withRequestBody(containing("grant_type=urn%3Aietf%3Aparams%3Aoauth%3Agrant-type%3Ajwt-bearer"))
.withRequestBody(containing("scope=https%3A%2F%2Fgraph.microsoft.com%2Fuser.read+offline_access"))
.withRequestBody(containing("requested_token_use=on_behalf_of"))
.withRequestBody(containing("assertion"))
.willReturn(WireMock.aResponse()
.willReturn(aResponse()
.withHeader("Content-Type", "application/json")
.withBody("{\n" +
" \"access_token\": \""
Expand All @@ -290,9 +280,9 @@ private void defineJwtBearerGrantTokenStub() {
}

private void defineCodeFlowAuthorizationMockTokenStub() {
server.stubFor(WireMock.post("/auth/realms/quarkus/token")
server.stubFor(post("/auth/realms/quarkus/token")
.withRequestBody(containing("authorization_code"))
.willReturn(WireMock.aResponse()
.willReturn(aResponse()
.withHeader("Content-Type", "application/json")
.withBody("{\n" +
" \"access_token\": \""
Expand All @@ -304,9 +294,9 @@ private void defineCodeFlowAuthorizationMockTokenStub() {
}

private void defineCodeFlowAuthorizationMockEncryptedTokenStub() {
server.stubFor(WireMock.post("/auth/realms/quarkus/encrypted-id-token")
server.stubFor(post("/auth/realms/quarkus/encrypted-id-token")
.withRequestBody(containing("authorization_code"))
.willReturn(WireMock.aResponse()
.willReturn(aResponse()
.withHeader("Content-Type", "application/json")
.withBody("{\n" +
" \"access_token\": \""
Expand Down

0 comments on commit 26e0e8e

Please sign in to comment.