Search in sources :

Example 66 with EventRepresentation

use of org.keycloak.representations.idm.EventRepresentation in project keycloak by keycloak.

the class ClientAuthSignedJWTTest method testCodeToTokenRequestSuccess.

private void testCodeToTokenRequestSuccess(String algorithm, boolean useJwksUri) throws Exception {
    ClientRepresentation clientRepresentation = app2;
    ClientResource clientResource = getClient(testRealm.getRealm(), clientRepresentation.getId());
    clientRepresentation = clientResource.toRepresentation();
    try {
        // setup Jwks
        KeyPair keyPair;
        if (useJwksUri) {
            keyPair = setupJwksUrl(algorithm, clientRepresentation, clientResource);
        } else {
            keyPair = setupJwks(algorithm, clientRepresentation, clientResource);
        }
        PublicKey publicKey = keyPair.getPublic();
        PrivateKey privateKey = keyPair.getPrivate();
        // test
        oauth.clientId("client2");
        oauth.doLogin("test-user@localhost", "password");
        EventRepresentation loginEvent = events.expectLogin().client("client2").assertEvent();
        String code = oauth.getCurrentQuery().get(OAuth2Constants.CODE);
        OAuthClient.AccessTokenResponse response = doAccessTokenRequest(code, createSignedRequestToken("client2", getRealmInfoUrl(), privateKey, publicKey, algorithm));
        assertEquals(200, response.getStatusCode());
        oauth.verifyToken(response.getAccessToken());
        oauth.parseRefreshToken(response.getRefreshToken());
        events.expectCodeToToken(loginEvent.getDetails().get(Details.CODE_ID), loginEvent.getSessionId()).client("client2").detail(Details.CLIENT_AUTH_METHOD, JWTClientAuthenticator.PROVIDER_ID).assertEvent();
    } finally {
        // Revert jwks settings
        if (useJwksUri) {
            revertJwksUriSettings(clientRepresentation, clientResource);
        } else {
            revertJwksSettings(clientRepresentation, clientResource);
        }
    }
}
Also used : KeyPair(java.security.KeyPair) PrivateKey(java.security.PrivateKey) OAuthClient(org.keycloak.testsuite.util.OAuthClient) PublicKey(java.security.PublicKey) EventRepresentation(org.keycloak.representations.idm.EventRepresentation) ClientResource(org.keycloak.admin.client.resource.ClientResource) ClientRepresentation(org.keycloak.representations.idm.ClientRepresentation)

Example 67 with EventRepresentation

use of org.keycloak.representations.idm.EventRepresentation in project keycloak by keycloak.

the class AccountFormServiceTest method viewLog.

@Test
public void viewLog() {
    setEventsEnabled(true);
    List<EventRepresentation> expectedEvents = new LinkedList<>();
    loginPage.open();
    loginPage.clickRegister();
    registerPage.register("view", "log", "view-log@localhost", "view-log", "password", "password");
    expectedEvents.add(events.poll());
    expectedEvents.add(events.poll());
    profilePage.open();
    profilePage.updateProfile("view", "log2", "view-log@localhost");
    expectedEvents.add(events.poll());
    logPage.open();
    Assert.assertTrue(logPage.isCurrent());
    List<List<String>> actualEvents = logPage.getEvents();
    Assert.assertEquals(expectedEvents.size(), actualEvents.size());
    for (EventRepresentation e : expectedEvents) {
        boolean match = false;
        for (List<String> a : logPage.getEvents()) {
            if (e.getType().toString().replace('_', ' ').toLowerCase().equals(a.get(1)) && e.getIpAddress().equals(a.get(2)) && e.getClientId().equals(a.get(3))) {
                match = true;
                break;
            }
        }
        if (!match) {
            Assert.fail("Event not found " + e.getType());
        }
    }
    setEventsEnabled(false);
}
Also used : EventRepresentation(org.keycloak.representations.idm.EventRepresentation) List(java.util.List) LinkedList(java.util.LinkedList) Matchers.containsString(org.hamcrest.Matchers.containsString) LinkedList(java.util.LinkedList) Test(org.junit.Test) AbstractTestRealmKeycloakTest(org.keycloak.testsuite.AbstractTestRealmKeycloakTest)

Example 68 with EventRepresentation

use of org.keycloak.representations.idm.EventRepresentation in project keycloak by keycloak.

the class AppInitiatedActionResetPasswordTest method resetPassword.

@Test
public void resetPassword() throws Exception {
    loginPage.open();
    loginPage.login("test-user@localhost", "password");
    events.expectLogin().assertEvent();
    doAIA();
    changePasswordPage.assertCurrent();
    assertTrue(changePasswordPage.isCancelDisplayed());
    changePasswordPage.changePassword("new-password", "new-password");
    events.expectRequiredAction(EventType.UPDATE_PASSWORD).assertEvent();
    assertKcActionStatus(SUCCESS);
    EventRepresentation loginEvent = events.expectLogin().assertEvent();
    oauth.openLogout();
    events.expectLogout(loginEvent.getSessionId()).assertEvent();
    loginPage.open();
    loginPage.login("test-user@localhost", "new-password");
    events.expectLogin().assertEvent();
}
Also used : EventRepresentation(org.keycloak.representations.idm.EventRepresentation) Test(org.junit.Test)

Example 69 with EventRepresentation

use of org.keycloak.representations.idm.EventRepresentation in project keycloak by keycloak.

the class RequiredActionEmailVerificationTest method verifyEmailRegister.

@Test
public void verifyEmailRegister() throws IOException, MessagingException {
    loginPage.open();
    loginPage.clickRegister();
    registerPage.register("firstName", "lastName", "email@mail.com", "verifyEmail", "password", "password");
    String userId = events.expectRegister("verifyEmail", "email@mail.com").assertEvent().getUserId();
    verifyEmailPage.assertCurrent();
    Assert.assertEquals(1, greenMail.getReceivedMessages().length);
    MimeMessage message = greenMail.getReceivedMessages()[0];
    EventRepresentation sendEvent = events.expectRequiredAction(EventType.SEND_VERIFY_EMAIL).user(userId).detail(Details.USERNAME, "verifyemail").detail("email", "email@mail.com").assertEvent();
    String mailCodeId = sendEvent.getDetails().get(Details.CODE_ID);
    String verificationUrl = getPasswordResetEmailLink(message);
    driver.navigate().to(verificationUrl.trim());
    Assert.assertEquals(RequestType.AUTH_RESPONSE, appPage.getRequestType());
    events.expectRequiredAction(EventType.VERIFY_EMAIL).user(userId).detail(Details.USERNAME, "verifyemail").detail(Details.EMAIL, "email@mail.com").detail(Details.CODE_ID, mailCodeId).assertEvent();
    events.expectLogin().user(userId).session(mailCodeId).detail(Details.USERNAME, "verifyemail").assertEvent();
}
Also used : MimeMessage(javax.mail.internet.MimeMessage) EventRepresentation(org.keycloak.representations.idm.EventRepresentation) Test(org.junit.Test) AbstractTestRealmKeycloakTest(org.keycloak.testsuite.AbstractTestRealmKeycloakTest) AuthenticationSessionFailoverClusterTest(org.keycloak.testsuite.cluster.AuthenticationSessionFailoverClusterTest)

Example 70 with EventRepresentation

use of org.keycloak.representations.idm.EventRepresentation in project keycloak by keycloak.

the class RequiredActionEmailVerificationTest method verifyEmailResend.

@Test
public void verifyEmailResend() throws IOException, MessagingException {
    loginPage.open();
    loginPage.login("test-user@localhost", "password");
    verifyEmailPage.assertCurrent();
    Assert.assertEquals(1, greenMail.getReceivedMessages().length);
    EventRepresentation sendEvent = events.expectRequiredAction(EventType.SEND_VERIFY_EMAIL).detail("email", "test-user@localhost").assertEvent();
    String mailCodeId = sendEvent.getDetails().get(Details.CODE_ID);
    verifyEmailPage.clickResendEmail();
    verifyEmailPage.assertCurrent();
    events.expectRequiredAction(EventType.SEND_VERIFY_EMAIL).detail(Details.CODE_ID, mailCodeId).detail("email", "test-user@localhost").assertEvent();
    Assert.assertEquals(2, greenMail.getReceivedMessages().length);
    MimeMessage message = greenMail.getLastReceivedMessage();
    String verificationUrl = getPasswordResetEmailLink(message);
    driver.navigate().to(verificationUrl.trim());
    appPage.assertCurrent();
    Assert.assertEquals(RequestType.AUTH_RESPONSE, appPage.getRequestType());
    events.expectRequiredAction(EventType.VERIFY_EMAIL).user(testUserId).detail(Details.USERNAME, "test-user@localhost").detail(Details.EMAIL, "test-user@localhost").detail(Details.CODE_ID, mailCodeId).assertEvent();
    events.expectLogin().user(testUserId).session(mailCodeId).detail(Details.USERNAME, "test-user@localhost").assertEvent();
}
Also used : MimeMessage(javax.mail.internet.MimeMessage) EventRepresentation(org.keycloak.representations.idm.EventRepresentation) Test(org.junit.Test) AbstractTestRealmKeycloakTest(org.keycloak.testsuite.AbstractTestRealmKeycloakTest) AuthenticationSessionFailoverClusterTest(org.keycloak.testsuite.cluster.AuthenticationSessionFailoverClusterTest)

Aggregations

EventRepresentation (org.keycloak.representations.idm.EventRepresentation)164 Test (org.junit.Test)124 OAuthClient (org.keycloak.testsuite.util.OAuthClient)93 AbstractKeycloakTest (org.keycloak.testsuite.AbstractKeycloakTest)60 AbstractTestRealmKeycloakTest (org.keycloak.testsuite.AbstractTestRealmKeycloakTest)44 RefreshToken (org.keycloak.representations.RefreshToken)27 ClientResource (org.keycloak.admin.client.resource.ClientResource)26 AccessToken (org.keycloak.representations.AccessToken)26 ClientRepresentation (org.keycloak.representations.idm.ClientRepresentation)25 IDToken (org.keycloak.representations.IDToken)23 Matchers.containsString (org.hamcrest.Matchers.containsString)15 AbstractAdminTest (org.keycloak.testsuite.admin.AbstractAdminTest)15 Response (javax.ws.rs.core.Response)13 OIDCClientRepresentation (org.keycloak.representations.oidc.OIDCClientRepresentation)13 AccessTokenResponse (org.keycloak.testsuite.util.OAuthClient.AccessTokenResponse)12 IOException (java.io.IOException)11 RealmResource (org.keycloak.admin.client.resource.RealmResource)11 AssertEvents (org.keycloak.testsuite.AssertEvents)10 JWSInput (org.keycloak.jose.jws.JWSInput)9 TestAuthenticationChannelRequest (org.keycloak.testsuite.rest.representation.TestAuthenticationChannelRequest)9