Skip to content

Commit

Permalink
Added dynamic access token generation
Browse files Browse the repository at this point in the history
Signed-off-by: Cem Nura <[email protected]>
  • Loading branch information
cemnura committed Mar 12, 2021
1 parent a972c78 commit 7d0cf6c
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
package io.quarkus.it.keycloak;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.IOException;

import org.junit.jupiter.api.Test;

import com.gargoylesoftware.htmlunit.SilentCssErrorHandler;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlForm;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.util.Cookie;

import io.quarkus.test.common.QuarkusTestResource;
import io.quarkus.test.junit.QuarkusTest;
import org.junit.jupiter.api.Test;

import java.io.IOException;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

@QuarkusTest
@QuarkusTestResource(KeycloakTestResource.class)
Expand All @@ -30,7 +32,7 @@ public void testCodeFlow() throws IOException {

page = form.getInputByValue("login").click();

assertEquals("Welcome to Test App", page.getTitleText());
assertEquals("alice", page.getBody().asText());
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,5 @@
package io.quarkus.it.keycloak;

import com.github.tomakehurst.wiremock.WireMockServer;
import com.github.tomakehurst.wiremock.client.WireMock;
import com.github.tomakehurst.wiremock.common.ConsoleNotifier;
import com.github.tomakehurst.wiremock.extension.responsetemplating.ResponseTemplateTransformer;
import com.google.common.collect.ImmutableSet;
import io.quarkus.test.common.QuarkusTestResourceLifecycleManager;
import org.jboss.logging.Logger;

import javax.ws.rs.core.MediaType;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
import static com.github.tomakehurst.wiremock.client.WireMock.containing;
import static com.github.tomakehurst.wiremock.client.WireMock.get;
Expand All @@ -23,6 +10,25 @@
import static java.util.Collections.emptySet;
import static java.util.stream.Collectors.joining;

import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import javax.ws.rs.core.MediaType;

import org.jboss.logging.Logger;

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

import io.quarkus.test.common.QuarkusTestResourceLifecycleManager;
import io.smallrye.jwt.build.Jwt;

public class KeycloakTestResource implements QuarkusTestResourceLifecycleManager {

private static final Logger LOG = Logger.getLogger(KeycloakTestResource.class);
Expand Down Expand Up @@ -50,8 +56,10 @@ public Map<String, String> start() {
+ "/auth/realms/quarkus/protocol/openid-connect/token/introspect\",\n" +
" \"authorization_endpoint\": \"" + server.baseUrl() + "/auth/realms/quarkus\"," +
" \"token_endpoint\": \"" + server.baseUrl() + "/auth/realms/quarkus/token\"," +
" \"issuer\" : \"https://server.example.com\"" +
" \"introspection_endpoint\": \"" + server.baseUrl() +
" \"issuer\" : \"https://server.example.com\"," +
" \"introspection_endpoint\": \"" + server.baseUrl()
+ "/auth/realms/quarkus/protocol/openid-connect/token/introspect\""
+
"}")));

server.stubFor(
Expand Down Expand Up @@ -151,14 +159,24 @@ private void defineCodeFlowAuthorizationMockTokenStub() {
.willReturn(WireMock.aResponse()
.withHeader("Content-Type", MediaType.APPLICATION_JSON)
.withBody("{\n" +
" \"access_token\": \"eyJraWQiOiIxIiwidHlwIjoiSldUIiwiYWxnIjoiUlMyNTYifQ.eyJwcmVmZXJyZWRfdXNlcm5hbWUiOiJhbGljZSIsImdyb3VwcyI6WyJhZG1pbiIsInVzZXIiXSwiaWF0IjoxNjE1NDIzMDgxLCJleHAiOjE2MTU0MjMzODEsImp0aSI6IjIwMTgyNTMxLTQ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xltI6AAhk8DlGo18voqogasg4uEgp1FhUdmWenaNQ\"\n"
+
" \"id_token\": \"" + getAccessToken("alice", new HashSet<>(Arrays.asList("user", "admin")))
+ "\"\n" +
"}")));
}

private String getAccessToken(String userName, Set<String> groups) {
return Jwt.preferredUserName(userName)
.groups(groups)
.issuer("https://server.example.com")
.audience("https://service.example.com")
.jws()
.keyId("1")
.sign("privateKey.jwk");
}

@Override
public synchronized void stop() {
if (server != null) {
Expand Down

0 comments on commit 7d0cf6c

Please sign in to comment.