Merge pull request #2838 from abstractj/KEYCLOAK-2908

KEYCLOAK-2908 - Migration of oidc package
This commit is contained in:
Stian Thorgersen 2016-05-20 08:56:43 +02:00
commit fcfe8582de
3 changed files with 52 additions and 28 deletions

View file

@ -84,6 +84,20 @@ public class TestingResourceProvider implements RealmResourceProvider {
return Response.ok().build(); return Response.ok().build();
} }
@POST
@Path("/remove-user-sessions")
@Produces(MediaType.APPLICATION_JSON)
public Response removeUserSessions(@QueryParam("realm") final String realmName) {
RealmManager realmManager = new RealmManager(session);
RealmModel realm = realmManager.getRealmByName(realmName);
if (realm == null) {
throw new NotFoundException("Realm not found");
}
session.sessions().removeUserSessions(realm);
return Response.ok().build();
}
@GET @GET
@Path("/get-user-session") @Path("/get-user-session")
@Produces(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON)

View file

@ -77,6 +77,11 @@ public interface TestingResource {
@Produces(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON)
Response removeUserSession(@QueryParam("realm") final String realm, @QueryParam("session") final String sessionId); Response removeUserSession(@QueryParam("realm") final String realm, @QueryParam("session") final String sessionId);
@POST
@Path("/remove-user-sessions")
@Produces(MediaType.APPLICATION_JSON)
Response removeUserSessions(@QueryParam("realm") final String realm);
@GET @GET
@Path("/get-user-session") @Path("/get-user-session")
@Produces(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON)

View file

@ -16,21 +16,19 @@
*/ */
package org.keycloak.testsuite.oidc; package org.keycloak.testsuite.oidc;
import org.junit.ClassRule; import org.junit.Before;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.keycloak.OAuth2Constants; import org.keycloak.OAuth2Constants;
import org.keycloak.models.KeycloakSession;
import org.keycloak.models.RealmModel;
import org.keycloak.protocol.oidc.OIDCLoginProtocolService; import org.keycloak.protocol.oidc.OIDCLoginProtocolService;
import org.keycloak.representations.AccessTokenResponse; import org.keycloak.representations.AccessTokenResponse;
import org.keycloak.representations.UserInfo; import org.keycloak.representations.UserInfo;
import org.keycloak.services.managers.RealmManager; import org.keycloak.representations.idm.RealmRepresentation;
import org.keycloak.testsuite.rule.KeycloakRule; import org.keycloak.testsuite.AbstractKeycloakTest;
import org.keycloak.testsuite.rule.WebResource; import org.keycloak.testsuite.AssertEvents;
import org.keycloak.testsuite.rule.WebRule; import org.keycloak.testsuite.util.ClientManager;
import org.keycloak.testsuite.util.RealmBuilder;
import org.keycloak.util.BasicAuthHelper; import org.keycloak.util.BasicAuthHelper;
import org.openqa.selenium.WebDriver;
import javax.ws.rs.client.Client; import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder; import javax.ws.rs.client.ClientBuilder;
@ -42,35 +40,44 @@ import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status; import javax.ws.rs.core.Response.Status;
import javax.ws.rs.core.UriBuilder; import javax.ws.rs.core.UriBuilder;
import java.net.URI; import java.net.URI;
import java.util.List;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
import static org.keycloak.testsuite.admin.AbstractAdminTest.loadJson;
import static org.keycloak.testsuite.util.OAuthClient.AUTH_SERVER_ROOT;
/** /**
* @author pedroigor * @author pedroigor
*/ */
public class UserInfoTest { public class UserInfoTest extends AbstractKeycloakTest {
@ClassRule
public static KeycloakRule keycloakRule = new KeycloakRule(new KeycloakRule.KeycloakSetup() {
@Override
public void config(RealmManager manager, RealmModel adminstrationRealm, RealmModel appRealm) {
appRealm.getClientByClientId("test-app").setDirectAccessGrantsEnabled(true);
}
});
@Rule @Rule
public WebRule webRule = new WebRule(this); public AssertEvents events = new AssertEvents(this);
@WebResource @Override
protected WebDriver driver; public void beforeAbstractKeycloakTest() throws Exception {
super.beforeAbstractKeycloakTest();
}
@Before
public void clientConfiguration() {
ClientManager.realm(adminClient.realm("test")).clientId("test-app").directAccessGrant(true);
}
@Override
public void addTestRealms(List<RealmRepresentation> testRealms) {
RealmRepresentation realmRepresentation = loadJson(getClass().getResourceAsStream("/testrealm.json"), RealmRepresentation.class);
RealmBuilder realm = RealmBuilder.edit(realmRepresentation).testEventListener();
testRealms.add(realm.build());
}
@Test @Test
public void testSuccessfulUserInfoRequest() throws Exception { public void testSuccessfulUserInfoRequest() throws Exception {
Client client = ClientBuilder.newClient(); Client client = ClientBuilder.newClient();
UriBuilder builder = UriBuilder.fromUri(org.keycloak.testsuite.Constants.AUTH_SERVER_ROOT); UriBuilder builder = UriBuilder.fromUri(AUTH_SERVER_ROOT);
URI grantUri = OIDCLoginProtocolService.tokenUrl(builder).build("test"); URI grantUri = OIDCLoginProtocolService.tokenUrl(builder).build("test");
WebTarget grantTarget = client.target(grantUri); WebTarget grantTarget = client.target(grantUri);
AccessTokenResponse accessTokenResponse = executeGrantAccessTokenRequest(grantTarget); AccessTokenResponse accessTokenResponse = executeGrantAccessTokenRequest(grantTarget);
@ -93,14 +100,12 @@ public class UserInfoTest {
@Test @Test
public void testSessionExpired() throws Exception { public void testSessionExpired() throws Exception {
Client client = ClientBuilder.newClient(); Client client = ClientBuilder.newClient();
UriBuilder builder = UriBuilder.fromUri(org.keycloak.testsuite.Constants.AUTH_SERVER_ROOT); UriBuilder builder = UriBuilder.fromUri(AUTH_SERVER_ROOT);
URI grantUri = OIDCLoginProtocolService.tokenUrl(builder).build("test"); URI grantUri = OIDCLoginProtocolService.tokenUrl(builder).build("test");
WebTarget grantTarget = client.target(grantUri); WebTarget grantTarget = client.target(grantUri);
AccessTokenResponse accessTokenResponse = executeGrantAccessTokenRequest(grantTarget); AccessTokenResponse accessTokenResponse = executeGrantAccessTokenRequest(grantTarget);
KeycloakSession session = keycloakRule.startSession(); testingClient.testing().removeUserSessions("test");
keycloakRule.startSession().sessions().removeUserSessions(session.realms().getRealm("test"));
keycloakRule.stopSession(session, true);
Response response = executeUserInfoRequest(accessTokenResponse.getToken()); Response response = executeUserInfoRequest(accessTokenResponse.getToken());
@ -141,7 +146,7 @@ public class UserInfoTest {
} }
private Response executeUserInfoRequest(String accessToken) { private Response executeUserInfoRequest(String accessToken) {
UriBuilder builder = UriBuilder.fromUri(org.keycloak.testsuite.Constants.AUTH_SERVER_ROOT); UriBuilder builder = UriBuilder.fromUri(AUTH_SERVER_ROOT);
UriBuilder uriBuilder = OIDCLoginProtocolService.tokenServiceBaseUrl(builder); UriBuilder uriBuilder = OIDCLoginProtocolService.tokenServiceBaseUrl(builder);
URI userInfoUri = uriBuilder.path(OIDCLoginProtocolService.class, "issueUserInfo").build("test"); URI userInfoUri = uriBuilder.path(OIDCLoginProtocolService.class, "issueUserInfo").build("test");
Client client = ClientBuilder.newClient(); Client client = ClientBuilder.newClient();