Skip to content

Commit

Permalink
[ELY-2034] Add tests for the OpenID Connect mechanism
Browse files Browse the repository at this point in the history
  • Loading branch information
fjuma committed Sep 10, 2021
1 parent 074f55d commit 8b54365
Show file tree
Hide file tree
Showing 8 changed files with 803 additions and 11 deletions.
7 changes: 7 additions & 0 deletions .github/workflows/pr-ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ jobs:
uses: actions/setup-java@v1
with:
java-version: 11
- if: matrix.os == 'macos-latest'
name: Install docker
run: |
brew install docker docker-machine docker-compose
brew services start docker-machine
docker-machine create --driver virtualbox default
docker --version
- name: Cache Maven packages
uses: actions/cache@v2
with:
Expand Down
52 changes: 52 additions & 0 deletions http/oidc/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,58 @@
<artifactId>jose4j</artifactId>
</dependency>

<dependency>
<groupId>org.wildfly.security</groupId>
<artifactId>wildfly-elytron-tests</artifactId>
<type>test-jar</type>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.keycloak</groupId>
<artifactId>keycloak-admin-client</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.logmanager</groupId>
<artifactId>jboss-logmanager</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.slf4j</groupId>
<artifactId>slf4j-jboss-logmanager</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>net.sourceforge.htmlunit</groupId>
<artifactId>htmlunit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>mockwebserver</artifactId>
<scope>test</scope>
</dependency>

</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
/*
* JBoss, Home of Professional Open Source.
* Copyright 2021 Red Hat, Inc., and individual contributors
* as indicated by the @author tags.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.wildfly.security.http.oidc;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.keycloak.representations.AccessTokenResponse;
import org.keycloak.representations.idm.ClientRepresentation;
import org.keycloak.representations.idm.CredentialRepresentation;
import org.keycloak.representations.idm.RealmRepresentation;
import org.keycloak.representations.idm.RoleRepresentation;
import org.keycloak.representations.idm.RolesRepresentation;
import org.keycloak.representations.idm.UserRepresentation;

import io.restassured.RestAssured;

/**
* Keycloak configuration for testing.
*
* @author <a href="mailto:[email protected]">Farah Juma</a>
*/
public class KeycloakConfiguration {

private static final String USER_ROLE = "user";
private static final String ADMIN_ROLE = "admin";
public static final String ALICE = "alice";
public static final String ALICE_PASSWORD = "alice123+";
private static final String BOB = "bob";
private static final String BOB_PASSWORD = "bob123+";

/**
* Configure RealmRepresentation as follows:
* <ul>
* <li>Two realm roles ("admin", "user")</li>
* <li>Two users:<li>
* <ul>
* <li>user named alice and password alice123+ with "admin" and "user" role</li>
* <li>user named bob and password bob123+ with "user" role</li>
* </ul>
* </ul>
*/
public static RealmRepresentation getRealmRepresentation(final String realmName, String clientId, String clientSecret,
String clientHostName, int clientPort, String clientApp) {
return createRealm(realmName, clientId, clientSecret, clientHostName, clientPort, clientApp);
}

public static String getAdminAccessToken(String authServerUrl) {
return RestAssured
.given()
.param("grant_type", "password")
.param("username", KeycloakContainer.KEYCLOAK_ADMIN_USER)
.param("password", KeycloakContainer.KEYCLOAK_ADMIN_PASSWORD)
.param("client_id", "admin-cli")
.when()
.post(authServerUrl + "/realms/master/protocol/openid-connect/token")
.as(AccessTokenResponse.class).getToken();
}

private static RealmRepresentation createRealm(String name, String clientId, String clientSecret,
String clientHostName, int clientPort, String clientApp) {
RealmRepresentation realm = new RealmRepresentation();

realm.setRealm(name);
realm.setEnabled(true);
realm.setUsers(new ArrayList<>());
realm.setClients(new ArrayList<>());
realm.setAccessTokenLifespan(3);
realm.setSsoSessionMaxLifespan(3);

RolesRepresentation roles = new RolesRepresentation();
List<RoleRepresentation> realmRoles = new ArrayList<>();

roles.setRealm(realmRoles);
realm.setRoles(roles);

realm.getRoles().getRealm().add(new RoleRepresentation("user", null, false));
realm.getRoles().getRealm().add(new RoleRepresentation("admin", null, false));

realm.getClients().add(createWebAppClient(clientId, clientSecret, clientHostName, clientPort, clientApp));

realm.getUsers().add(createUser(ALICE, ALICE_PASSWORD, Arrays.asList(USER_ROLE, ADMIN_ROLE)));
realm.getUsers().add(createUser(BOB, BOB_PASSWORD, Arrays.asList(USER_ROLE)));
return realm;
}

private static ClientRepresentation createWebAppClient(String clientId, String clientSecret, String clientHostName, int clientPort, String clientApp) {
ClientRepresentation client = new ClientRepresentation();
client.setClientId(clientId);
client.setPublicClient(false);
client.setSecret(clientSecret);
//client.setRedirectUris(Arrays.asList("*"));
client.setRedirectUris(Arrays.asList("http://" + clientHostName + ":" + clientPort + "/" + clientApp));
client.setEnabled(true);
return client;
}

private static UserRepresentation createUser(String username, String password, List<String> realmRoles) {
UserRepresentation user = new UserRepresentation();
user.setUsername(username);
user.setEnabled(true);
user.setCredentials(new ArrayList<>());
user.setRealmRoles(realmRoles);
user.setEmail(username + "@gmail.com");

CredentialRepresentation credential = new CredentialRepresentation();
credential.setType(CredentialRepresentation.PASSWORD);
credential.setValue(password);
credential.setTemporary(false);
user.getCredentials().add(credential);
return user;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* JBoss, Home of Professional Open Source.
* Copyright 2021 Red Hat, Inc., and individual contributors
* as indicated by the @author tags.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.wildfly.security.http.oidc;

import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.wait.strategy.Wait;

/**
* KeycloakContainer for testing.
*
* @author <a href="mailto:[email protected]">Farah Juma</a>
*/
public class KeycloakContainer extends GenericContainer<KeycloakContainer> {
public static final String KEYCLOAK_ADMIN_USER = "admin";
public static final String KEYCLOAK_ADMIN_PASSWORD = "admin";
private static final String KEYCLOAK_AUTH_PATH = "/auth";

private static final String KEYCLOAK_IMAGE = "quay.io/keycloak/keycloak:latest";
private static final int KEYCLOAK_PORT_HTTP = 8080;
private static final int KEYCLOAK_PORT_HTTPS = 8443;

private boolean useHttps;

public KeycloakContainer() {
this(false);
}

public KeycloakContainer(final boolean useHttps) {
super(KEYCLOAK_IMAGE);
this.useHttps = useHttps;

}

@Override
protected void configure() {
withExposedPorts(KEYCLOAK_PORT_HTTP, KEYCLOAK_PORT_HTTPS);
waitingFor(Wait.forHttp("/auth").forPort(8080));
withEnv("KEYCLOAK_USER", KEYCLOAK_ADMIN_USER);
withEnv("KEYCLOAK_PASSWORD", KEYCLOAK_ADMIN_PASSWORD);
}

public String getAuthServerUrl() {
return String.format("http://%s:%s%s", getContainerIpAddress(), useHttps ? getMappedPort(KEYCLOAK_PORT_HTTPS) : getMappedPort(KEYCLOAK_PORT_HTTP), KEYCLOAK_AUTH_PATH);
}
}
Loading

0 comments on commit 8b54365

Please sign in to comment.