Java Code Examples for org.keycloak.representations.idm.RealmRepresentation#setEventsListeners()

The following examples show how to use org.keycloak.representations.idm.RealmRepresentation#setEventsListeners() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AbstractAdminTest.java    From keycloak with Apache License 2.0 6 votes vote down vote up
@Override
public void addTestRealms(List<RealmRepresentation> testRealms) {
    super.addTestRealms(testRealms);

    RealmRepresentation adminRealmRep = new RealmRepresentation();
    adminRealmRep.setId(REALM_NAME);
    adminRealmRep.setRealm(REALM_NAME);
    adminRealmRep.setEnabled(true);
    Map<String, String> config = new HashMap<>();
    config.put("from", "[email protected]");
    config.put("host", "localhost");
    config.put("port", "3025");
    adminRealmRep.setSmtpServer(config);

    List<String> eventListeners = new ArrayList<>();
    eventListeners.add(JBossLoggingEventListenerProviderFactory.ID);
    eventListeners.add(EventsListenerProviderFactory.PROVIDER_ID);
    adminRealmRep.setEventsListeners(eventListeners);

    testRealms.add(adminRealmRep);
}
 
Example 2
Source File: AbstractAdminCrossDCTest.java    From keycloak with Apache License 2.0 6 votes vote down vote up
@Override
public void addTestRealms(List<RealmRepresentation> testRealms) {
    log.debug("--DC: AbstractAdminCrossDCTest.addTestRealms - adding realm: " + REALM_NAME);
    super.addTestRealms(testRealms);

    RealmRepresentation adminRealmRep = new RealmRepresentation();
    adminRealmRep.setId(REALM_NAME);
    adminRealmRep.setRealm(REALM_NAME);
    adminRealmRep.setEnabled(true);
    Map<String, String> config = new HashMap<>();
    config.put("from", "[email protected]");
    config.put("host", "localhost");
    config.put("port", "3025");
    adminRealmRep.setSmtpServer(config);

    List<String> eventListeners = new ArrayList<>();
    eventListeners.add(JBossLoggingEventListenerProviderFactory.ID);
    eventListeners.add(EventsListenerProviderFactory.PROVIDER_ID);
    adminRealmRep.setEventsListeners(eventListeners);

    testRealms.add(adminRealmRep);
}
 
Example 3
Source File: AbstractSpringBootTest.java    From keycloak with Apache License 2.0 6 votes vote down vote up
@Override
public void addTestRealms(List<RealmRepresentation> testRealms) {
    RealmRepresentation realm = new RealmRepresentation();

    realm.setId(REALM_ID);
    realm.setRealm(REALM_NAME);
    realm.setEnabled(true);

    realm.setPublicKey(REALM_PUBLIC_KEY);
    realm.setPrivateKey(REALM_PRIVATE_KEY);

    realm.setClients(Collections.singletonList(createClient()));

    List<String> eventListeners = new ArrayList<>();
    eventListeners.add("jboss-logging");
    eventListeners.add("event-queue");
    realm.setEventsListeners(eventListeners);

    testRealms.add(realm);
}
 
Example 4
Source File: DemoServletsAdapterTest.java    From keycloak with Apache License 2.0 5 votes vote down vote up
private void expectResultOfClientAuthenticatedInClientSecretJwt(String targetClientId, AbstractPageWithInjectedUrl portal) {
    RealmRepresentation realm = testRealmResource().toRepresentation();
    realm.setEventsEnabled(true);
    realm.setEnabledEventTypes(Arrays.asList("LOGIN", "CODE_TO_TOKEN"));
    realm.setEventsListeners(Arrays.asList("jboss-logging", "event-queue"));
    testRealmResource().update(realm); 

    portal.navigateTo();
    assertCurrentUrlStartsWithLoginUrlOf(testRealmPage);
    testRealmLoginPage.form().login("[email protected]", "password");

    String userId = ApiUtil.findUserByUsername(testRealmResource(), "[email protected]").getId();

    assertEvents.expectLogin()
    .realm(realm.getId())
    .client(targetClientId)
    .user(userId)
    .detail(Details.USERNAME, "[email protected]")
    .detail(Details.CONSENT, Details.CONSENT_VALUE_NO_CONSENT_REQUIRED)
    .detail(Details.REDIRECT_URI,
            org.hamcrest.Matchers.anyOf(org.hamcrest.Matchers.equalTo(portal.getInjectedUrl().toString()),
                    org.hamcrest.Matchers.equalTo(portal.getInjectedUrl().toString() + "/")))
    .removeDetail(Details.CODE_ID)
    .assertEvent();

    assertEvents.expectCodeToToken(null, null)
    .realm(realm.getId())
    .client(targetClientId)
    .user(userId)
    .session(AssertEvents.isUUID())
    .clearDetails()
    .assertEvent();
}
 
Example 5
Source File: DemoServletsAdapterTest.java    From keycloak with Apache License 2.0 5 votes vote down vote up
private void expectResultOfClientNotAuthenticatedInClientSecretJwt(String targetClientId, String expectedErrorString) {
    RealmRepresentation realm = testRealmResource().toRepresentation();
    realm.setEventsEnabled(true);
    realm.setEnabledEventTypes(Arrays.asList("LOGIN", "CODE_TO_TOKEN_ERROR"));
    realm.setEventsListeners(Arrays.asList("jboss-logging", "event-queue"));
    testRealmResource().update(realm);

    clientSecretJwtSecurePortal.navigateTo();
    assertCurrentUrlStartsWithLoginUrlOf(testRealmPage);
    testRealmLoginPage.form().login("[email protected]", "password");

    String userId = ApiUtil.findUserByUsername(testRealmResource(), "[email protected]").getId();

    assertEvents.expectLogin()
            .realm(realm.getId())
            .client(targetClientId)
            .user(userId)
            .detail(Details.USERNAME, "[email protected]")
            .detail(Details.CONSENT, Details.CONSENT_VALUE_NO_CONSENT_REQUIRED)
            .detail(Details.REDIRECT_URI,
                    org.hamcrest.Matchers.anyOf(org.hamcrest.Matchers.equalTo(clientSecretJwtSecurePortal.getInjectedUrl().toString()),
                            org.hamcrest.Matchers.equalTo(clientSecretJwtSecurePortal.getInjectedUrl().toString() + "/")))
            .removeDetail(Details.CODE_ID)
            .assertEvent();

    assertEvents.expectCodeToToken(null, null)
            .realm(realm.getId())
            .client(targetClientId)
            .user((String)null)
            .error(expectedErrorString)
            .clearDetails()
            .assertEvent(); 
}
 
Example 6
Source File: DemoServletsAdapterTest.java    From keycloak with Apache License 2.0 4 votes vote down vote up
@Test
public void grantServerBasedApp() {
    ClientResource clientResource = ApiUtil.findClientResourceByClientId(testRealmResource(), "customer-portal");
    ClientRepresentation client = clientResource.toRepresentation();
    client.setConsentRequired(true);
    clientResource.update(client);

    RealmRepresentation realm = testRealmResource().toRepresentation();
    realm.setEventsEnabled(true);
    realm.setEnabledEventTypes(Arrays.asList("REVOKE_GRANT", "LOGIN"));
    realm.setEventsListeners(Arrays.asList("jboss-logging", "event-queue"));
    testRealmResource().update(realm);

    customerPortal.navigateTo();

    loginPage.form().login("[email protected]", "password");

    assertTrue(oAuthGrantPage.isCurrent());

    oAuthGrantPage.accept();

    waitForPageToLoad();
    assertLogged();

    String userId = ApiUtil.findUserByUsername(testRealmResource(), "[email protected]").getId();

    assertEvents.expectLogin()
            .realm(realm.getId())
            .client("customer-portal")
            .user(userId)
            .detail(Details.USERNAME, "[email protected]")
            .detail(Details.CONSENT, Details.CONSENT_VALUE_CONSENT_GRANTED)
            .detail(Details.REDIRECT_URI,
                    org.hamcrest.Matchers.anyOf(org.hamcrest.Matchers.equalTo(customerPortal.getInjectedUrl().toString()),
                            org.hamcrest.Matchers.equalTo(customerPortal.getInjectedUrl().toString() + "/")))
            .removeDetail(Details.CODE_ID)
            .assertEvent();

    assertEvents.expectCodeToToken(null, null)
            .realm(realm.getId())
            .client("customer-portal")
            .user(userId)
            .session(AssertEvents.isUUID())
            .removeDetail(Details.CODE_ID)
            .assertEvent();

    applicationsPage.navigateTo();
    applicationsPage.revokeGrantForApplication("customer-portal");

    customerPortal.navigateTo();

    assertTrue(oAuthGrantPage.isCurrent());

    assertEvents.expect(EventType.REVOKE_GRANT)
            .realm(realm.getId())
            .client("account")
            .user(userId)
            .detail(Details.REVOKED_CLIENT, "customer-portal")
            .assertEvent();

    assertEvents.assertEmpty();

    // Revert consent
    client = clientResource.toRepresentation();
    client.setConsentRequired(false);
    clientResource.update(client);
}
 
Example 7
Source File: DemoServletsAdapterTest.java    From keycloak with Apache License 2.0 4 votes vote down vote up
@Test
public void historyOfAccessResourceTest() throws IOException {
    RealmRepresentation realm = testRealmResource().toRepresentation();
    realm.setEventsEnabled(true);
    realm.setEnabledEventTypes(Arrays.asList("LOGIN", "LOGIN_ERROR", "LOGOUT", "CODE_TO_TOKEN"));
    realm.setEventsListeners(Arrays.asList("jboss-logging", "event-queue"));
    testRealmResource().update(realm);

    customerPortal.navigateTo();

    testRealmLoginPage.form().login("[email protected]", "password");

    waitForPageToLoad();
    assertLogged();

    String userId = ApiUtil.findUserByUsername(testRealmResource(), "[email protected]").getId();

    assertEvents.expectLogin()
            .realm(realm.getId())
            .client("customer-portal")
            .user(userId)
            .detail(Details.USERNAME, "[email protected]")
            .detail(Details.CONSENT, Details.CONSENT_VALUE_NO_CONSENT_REQUIRED)
            .detail(Details.REDIRECT_URI,
                    org.hamcrest.Matchers.anyOf(org.hamcrest.Matchers.equalTo(customerPortal.getInjectedUrl().toString()),
                            org.hamcrest.Matchers.equalTo(customerPortal.getInjectedUrl().toString() + "/")))
            .removeDetail(Details.CODE_ID)
            .assertEvent();

    assertEvents.expectCodeToToken(null, null)
            .realm(realm.getId())
            .client("customer-portal")
            .user(userId)
            .session(AssertEvents.isUUID())
            .removeDetail(Details.CODE_ID)
            .assertEvent();


    driver.navigate().to(testRealmPage.getOIDCLogoutUrl() + "?redirect_uri=" + customerPortal);
    assertCurrentUrlStartsWithLoginUrlOf(testRealmPage);

    assertEvents.expectLogout(null)
            .realm(realm.getId())
            .user(userId)
            .session(AssertEvents.isUUID())
            .detail(Details.REDIRECT_URI,
                    org.hamcrest.Matchers.anyOf(org.hamcrest.Matchers.equalTo(customerPortal.getInjectedUrl().toString()),
                            org.hamcrest.Matchers.equalTo(customerPortal.getInjectedUrl().toString() + "/")))
            .assertEvent();

    assertEvents.assertEmpty();

    String serverLogPath = null;

    String appServer = System.getProperty("app.server");
    if (appServer != null && (appServer.equals("wildfly") || appServer.equals("eap6") || appServer.equals("eap"))) {
        serverLogPath = System.getProperty("app.server.home") + "/standalone-test/log/server.log";
    }

    String appServerUrl;
    if (Boolean.parseBoolean(System.getProperty("app.server.ssl.required"))) {
        appServerUrl = "https://localhost:" + System.getProperty("app.server.https.port", "8543") + "/";
    } else {
        appServerUrl = "http://localhost:" + System.getProperty("app.server.http.port", "8280") + "/";
    }

    if (serverLogPath != null) {
        log.info("Checking app server log at: " + serverLogPath);
        File serverLog = new File(serverLogPath);
        String serverLogContent = FileUtils.readFileToString(serverLog, "UTF-8");
        UserRepresentation bburke = ApiUtil.findUserByUsername(testRealmResource(), "[email protected]");

        //the expected log message has DEBUG level
        assertThat(serverLogContent, containsString("User '" + bburke.getId() + "' invoking '" + appServerUrl + "customer-db/' on client 'customer-db'"));
    } else {
        log.info("Checking app server log on app-server: \"" + System.getProperty("app.server") + "\" is not supported.");
    }
}