Skip to content

Commit

Permalink
Make mock match endpoint and add stub for basic user
Browse files Browse the repository at this point in the history
  • Loading branch information
carlesarnal committed Jan 16, 2024
1 parent 858dccc commit ac317b7
Showing 1 changed file with 12 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,11 @@ public Map<String, String> start() {
stubForClient(NO_ROLE_CLIENT_ID, "test1");


//Stub for clients with credentials as header
stubForClient(BASIC_USER, BASIC_PASSWORD);

//Stub for basic user
server.stubFor(WireMock.post("/auth/realms/" + realm + "/protocol/openid-connect/token")
server.stubFor(WireMock.post("/auth/realms/" + realm + "/protocol/openid-connect/token/")
.withRequestBody(WireMock.containing("grant_type=client_credentials"))
.withRequestBody(WireMock.containing("client_id=" + BASIC_USER))
.withRequestBody(WireMock.containing("client_secret=" + BASIC_PASSWORD))
Expand All @@ -109,7 +112,7 @@ public Map<String, String> start() {
"}")));

//Stub for basic user a
server.stubFor(WireMock.post("/auth/realms/" + realm + "/protocol/openid-connect/token")
server.stubFor(WireMock.post("/auth/realms/" + realm + "/protocol/openid-connect/token/")
.withRequestBody(WireMock.containing("grant_type=client_credentials"))
.withRequestBody(WireMock.containing("client_id=" + BASIC_USER_A))
.withRequestBody(WireMock.containing("client_secret=" + BASIC_PASSWORD))
Expand All @@ -123,7 +126,7 @@ public Map<String, String> start() {
"}")));

//Stub for basic user b
server.stubFor(WireMock.post("/auth/realms/" + realm + "/protocol/openid-connect/token")
server.stubFor(WireMock.post("/auth/realms/" + realm + "/protocol/openid-connect/token/")
.withRequestBody(WireMock.containing("grant_type=client_credentials"))
.withRequestBody(WireMock.containing("client_id=" + BASIC_USER_B))
.withRequestBody(WireMock.containing("client_secret=" + BASIC_PASSWORD))
Expand All @@ -138,7 +141,7 @@ public Map<String, String> start() {


//Wrong credentials stub
server.stubFor(WireMock.post("/auth/realms/" + realm + "/protocol/openid-connect/token")
server.stubFor(WireMock.post("/auth/realms/" + realm + "/protocol/openid-connect/token/")
.withRequestBody(WireMock.containing("grant_type=client_credentials"))
.withRequestBody(WireMock.containing("client_id=" + WRONG_CREDS_CLIENT_ID))
.willReturn(WireMock.aResponse()
Expand All @@ -149,7 +152,7 @@ public Map<String, String> start() {

this.authServerUrl = server.baseUrl() + "/auth";
LOGGER.info("Keycloak started in mock mode: {}", authServerUrl);
this.tokenEndpoint = authServerUrl + "/realms/" + realm + "/protocol/openid-connect/token";
this.tokenEndpoint = authServerUrl + "/realms/" + realm + "/protocol/openid-connect/token/";

Map<String, String> props = new HashMap<>();

Expand All @@ -171,7 +174,7 @@ private ResponseDefinitionBuilder wellKnownResponse() {
.withBody("{\n" +
" \"jwks_uri\": \"" + server.baseUrl()
+ "/auth/realms/" + realm + "/protocol/openid-connect/certs\",\n"
+ " \"token_endpoint\": \"" + server.baseUrl() + "/auth/realms/" + realm + "/protocol/openid-connect/token\" "
+ " \"token_endpoint\": \"" + server.baseUrl() + "/auth/realms/" + realm + "/protocol/openid-connect/token/\" "
+ "}");
}

Expand All @@ -198,7 +201,7 @@ private String generateJwtToken(String userName, String orgId) {
}

private void stubForClient(String client) {
server.stubFor(WireMock.post("/auth/realms/" + realm + "/protocol/openid-connect/token")
server.stubFor(WireMock.post("/auth/realms/" + realm + "/protocol/openid-connect/token/")
.withRequestBody(WireMock.containing("grant_type=client_credentials"))
.withRequestBody(WireMock.containing("client_id=" + client))
.willReturn(WireMock.aResponse()
Expand All @@ -212,7 +215,7 @@ private void stubForClient(String client) {
}

private void stubForClient(String client, String clientSecret) {
server.stubFor(WireMock.post("/auth/realms/" + realm + "/protocol/openid-connect/token")
server.stubFor(WireMock.post("/auth/realms/" + realm + "/protocol/openid-connect/token/")
.withHeader("Authorization", WireMock.containing(buildBasicAuthHeader(client, clientSecret)))
.withRequestBody(WireMock.containing("grant_type=client_credentials"))
.willReturn(WireMock.aResponse()
Expand All @@ -226,7 +229,7 @@ private void stubForClient(String client, String clientSecret) {
}

private void stubForClientWithWrongCreds(String client, String clientSecret) {
server.stubFor(WireMock.post("/auth/realms/" + realm + "/protocol/openid-connect/token")
server.stubFor(WireMock.post("/auth/realms/" + realm + "/protocol/openid-connect/token/")
.withRequestBody(WireMock.containing("grant_type=client_credentials"))
.withHeader("Authorization", WireMock.containing(buildBasicAuthHeader(client, clientSecret)))
.willReturn(WireMock.aResponse()
Expand Down

0 comments on commit ac317b7

Please sign in to comment.