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

The following examples show how to use org.keycloak.representations.idm.RealmRepresentation#setId() . 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: OwnerReplacementTest.java    From keycloak with Apache License 2.0 7 votes vote down vote up
@Override
public void addTestRealms(List<RealmRepresentation> testRealms) {
    log.debug("Adding test realm for import from testrealm.json");
    RealmRepresentation testRealm = loadJson(getClass().getResourceAsStream("/testrealm.json"), RealmRepresentation.class);
    testRealms.add(testRealm);

    UserRepresentation user = UserBuilder.create()
            .username("foo@user")
            .email("[email protected]")
            .password("password")
            .build();

    RealmRepresentation realm2 = RealmBuilder.create()
            .name("foo")
            .user(user)
            .build();
    realm2.setId("foo");
    testRealms.add(realm2);
}
 
Example 2
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 3
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 4
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 5
Source File: ExportImportTest.java    From keycloak with Apache License 2.0 5 votes vote down vote up
@Override
public void addTestRealms(List<RealmRepresentation> testRealms) {
    RealmRepresentation testRealm1 = loadJson(getClass().getResourceAsStream("/testrealm.json"), RealmRepresentation.class);
    testRealm1.getUsers().add(makeUser("user1"));
    testRealm1.getUsers().add(makeUser("user2"));
    testRealm1.getUsers().add(makeUser("user3"));

    testRealm1.getUsers().add(
            UserBuilder.create()
                    .username("user-requiredOTP")
                    .email("User-requiredOTP" + "@test.com")
                    .password("password")
                    .requiredAction(UserModel.RequiredAction.CONFIGURE_TOTP.name())
                    .build()
    );
    testRealm1.getUsers().add(
            UserBuilder.create()
                    .username("user-requiredWebAuthn")
                    .email("User-requiredWebAuthn" + "@test.com")
                    .password("password")
                    .requiredAction(WebAuthnRegisterFactory.PROVIDER_ID)
                    .build()
    );

    testRealm1.getSmtpServer().put("password", "secret");

    setEventsConfig(testRealm1);
    testRealms.add(testRealm1);

    RealmRepresentation testRealm2 = loadJson(getClass().getResourceAsStream("/model/testrealm.json"), RealmRepresentation.class);
    testRealm2.setId("test-realm");
    testRealms.add(testRealm2);
}
 
Example 6
Source File: RealmSetup.java    From keycloak-custom-protocol-mapper-example with Apache License 2.0 5 votes vote down vote up
public void execute() {
    RealmRepresentation realmRepresentation = new RealmRepresentation();
    realmRepresentation.setDisplayName(REALM);
    realmRepresentation.setId(REALM);
    realmRepresentation.setClients(createClients());
    realmRepresentation.setLoginWithEmailAllowed(true);
    realmRepresentation.setEnabled(true);
    realmRepresentation.setRealm(REALM);
    this.keycloak.realms().create(realmRepresentation);
}
 
Example 7
Source File: ClientTokenExchangeTest.java    From keycloak with Apache License 2.0 5 votes vote down vote up
@Override
public void addTestRealms(List<RealmRepresentation> testRealms) {
    RealmRepresentation testRealmRep = new RealmRepresentation();
    testRealmRep.setId(TEST);
    testRealmRep.setRealm(TEST);
    testRealmRep.setEnabled(true);
    testRealms.add(testRealmRep);
}
 
Example 8
Source File: ClientTokenExchangeSAML2Test.java    From keycloak with Apache License 2.0 5 votes vote down vote up
@Override
public void addTestRealms(List<RealmRepresentation> testRealms) {
    RealmRepresentation testRealmRep = new RealmRepresentation();
    testRealmRep.setId(TEST);
    testRealmRep.setRealm(TEST);
    testRealmRep.setEnabled(true);
    testRealmRep.setPrivateKey(REALM_PRIVATE_KEY);
    testRealmRep.setPublicKey(REALM_PUBLIC_KEY);
    testRealmRep.setAccessCodeLifespan(60); // Used as default assertion lifespan
    testRealms.add(testRealmRep);
}
 
Example 9
Source File: PolicyEvaluationCompositeRoleTest.java    From keycloak with Apache License 2.0 5 votes vote down vote up
@Override
public void addTestRealms(List<RealmRepresentation> testRealms) {
    RealmRepresentation testRealmRep = new RealmRepresentation();
    testRealmRep.setId(TEST);
    testRealmRep.setRealm(TEST);
    testRealmRep.setEnabled(true);
    testRealms.add(testRealmRep);
}
 
Example 10
Source File: ImportTest.java    From keycloak with Apache License 2.0 5 votes vote down vote up
@Override
public void configureTestRealm(RealmRepresentation testRealmParm) {

    log.infof("testrealm2 imported");
    RealmRepresentation testRealm = loadJson(getClass().getResourceAsStream("/model/testrealm2.json"), RealmRepresentation.class);
    adminClient.realms().create(testRealm);

    log.infof("testrealm-demo imported");
    testRealm = loadJson(getClass().getResourceAsStream("/model/testrealm-demo.json"), RealmRepresentation.class);
    testRealm.setRealm("demo");
    testRealm.setId("demo");
    adminClient.realms().create(testRealm);
}
 
Example 11
Source File: CompositeRolesModelTest.java    From keycloak with Apache License 2.0 5 votes vote down vote up
@Override
public void configureTestRealm(RealmRepresentation testRealm) {
    log.infof("testcomposites imported");
    RealmRepresentation newRealm = loadJson(getClass().getResourceAsStream("/model/testcomposites2.json"), RealmRepresentation.class);
    newRealm.setId("TestComposites");
    adminClient.realms().create(newRealm);

}
 
Example 12
Source File: AbstractAuthTest.java    From keycloak with Apache License 2.0 5 votes vote down vote up
@Override
public void addTestRealms(List<RealmRepresentation> testRealms) {
    RealmRepresentation testRealmRep = new RealmRepresentation();
    testRealmRep.setId(TEST);
    testRealmRep.setRealm(TEST);
    testRealmRep.setEnabled(true);
    testRealms.add(testRealmRep);
}
 
Example 13
Source File: RequiredActionsTest.java    From keycloak with Apache License 2.0 5 votes vote down vote up
@Override
public void addTestRealms(List<RealmRepresentation> testRealms) {
    super.addTestRealms(testRealms);

    RealmRepresentation testRealmRep = new RealmRepresentation();
    testRealmRep.setId(GRANT_REALM);
    testRealmRep.setRealm(GRANT_REALM);
    configureInternationalizationForRealm(testRealmRep);
    testRealmRep.setEnabled(true);

    testRealms.add(testRealmRep);
}
 
Example 14
Source File: LinkedAccountsTest.java    From keycloak with Apache License 2.0 5 votes vote down vote up
@Override
public void addTestRealms(List<RealmRepresentation> testRealms) {
    super.addTestRealms(testRealms);
    RealmRepresentation realm1 = testRealms.get(0);

    realm1.addIdentityProvider(createIdentityProviderRepresentation(SOCIAL_IDP_ALIAS,
            GoogleIdentityProviderFactory.PROVIDER_ID));

    String oidcRoot = getAuthServerRoot() + "realms/" + REALM2_NAME + "/protocol/openid-connect/";

    IdentityProviderRepresentation systemIdp = createIdentityProviderRepresentation(SYSTEM_IDP_ALIAS,
            OIDCIdentityProviderFactory.PROVIDER_ID);
    systemIdp.getConfig().put("clientId", CLIENT_ID);
    systemIdp.getConfig().put("clientSecret", CLIENT_SECRET);
    systemIdp.getConfig().put("clientAuthMethod", OIDCLoginProtocol.CLIENT_SECRET_POST);
    systemIdp.getConfig().put("authorizationUrl", oidcRoot + "auth");
    systemIdp.getConfig().put("tokenUrl", oidcRoot + "token");
    realm1.addIdentityProvider(systemIdp);

    ClientRepresentation client = ClientBuilder.create()
            .clientId(CLIENT_ID)
            .secret(CLIENT_SECRET)
            .redirectUris(getAuthServerRoot() + "realms/" + TEST + "/broker/" + SYSTEM_IDP_ALIAS + "/endpoint")
            .build();

    // using REALM2 as an identity provider
    RealmRepresentation realm2 = new RealmRepresentation();
    realm2.setId(REALM2_NAME);
    realm2.setRealm(REALM2_NAME);
    realm2.setEnabled(true);
    realm2.setClients(Collections.singletonList(client));
    realm2.setUsers(Collections.singletonList(homerUser));
    testRealms.add(realm2);
}
 
Example 15
Source File: FineGrainAdminUnitTest.java    From keycloak with Apache License 2.0 5 votes vote down vote up
@Override
public void addTestRealms(List<RealmRepresentation> testRealms) {
    RealmRepresentation testRealmRep = new RealmRepresentation();
    testRealmRep.setId(TEST);
    testRealmRep.setRealm(TEST);
    testRealmRep.setEnabled(true);
    testRealms.add(testRealmRep);
}
 
Example 16
Source File: IllegalAdminUpgradeTest.java    From keycloak with Apache License 2.0 5 votes vote down vote up
@Override
public void addTestRealms(List<RealmRepresentation> testRealms) {
    RealmRepresentation testRealmRep = new RealmRepresentation();
    testRealmRep.setId(TEST);
    testRealmRep.setRealm(TEST);
    testRealmRep.setEnabled(true);
    testRealms.add(testRealmRep);
}
 
Example 17
Source File: RealmTest.java    From keycloak with Apache License 2.0 5 votes vote down vote up
@Test
public void renameRealm() {
    RealmRepresentation rep = new RealmRepresentation();
    rep.setId("old");
    rep.setRealm("old");

    try {
        adminClient.realms().create(rep);

        rep.setRealm("new");
        adminClient.realm("old").update(rep);

        // Check client in master realm renamed
        Assert.assertEquals(0, adminClient.realm("master").clients().findByClientId("old-realm").size());
        Assert.assertEquals(1, adminClient.realm("master").clients().findByClientId("new-realm").size());

        ClientRepresentation adminConsoleClient = adminClient.realm("new").clients().findByClientId(Constants.ADMIN_CONSOLE_CLIENT_ID).get(0);
        assertEquals(Constants.AUTH_ADMIN_URL_PROP, adminConsoleClient.getRootUrl());
        assertEquals("/admin/new/console/", adminConsoleClient.getBaseUrl());
        assertEquals("/admin/new/console/*", adminConsoleClient.getRedirectUris().get(0));

        ClientRepresentation accountClient = adminClient.realm("new").clients().findByClientId(Constants.ACCOUNT_MANAGEMENT_CLIENT_ID).get(0);
        assertEquals(Constants.AUTH_BASE_URL_PROP, accountClient.getRootUrl());
        assertEquals("/realms/new/account/", accountClient.getBaseUrl());
        assertEquals("/realms/new/account/*", accountClient.getRedirectUris().get(0));
    } finally {
        adminClient.realms().realm(rep.getRealm()).remove();
    }
}
 
Example 18
Source File: AbstractAuthenticationTest.java    From keycloak with Apache License 2.0 4 votes vote down vote up
@Override
public void addTestRealms(List<RealmRepresentation> testRealms) {
    RealmRepresentation testRealmRep = RealmBuilder.create().name(REALM_NAME).testEventListener().build();
    testRealmRep.setId(REALM_NAME);
    testRealms.add(testRealmRep);
}
 
Example 19
Source File: FineGrainAdminUnitTest.java    From keycloak with Apache License 2.0 4 votes vote down vote up
@Test
public void testCreateRealmCreateClient() throws Exception {
    ClientRepresentation rep = new ClientRepresentation();
    rep.setName("fullScopedClient");
    rep.setClientId("fullScopedClient");
    rep.setFullScopeAllowed(true);
    rep.setSecret("618268aa-51e6-4e64-93c4-3c0bc65b8171");
    rep.setProtocol("openid-connect");
    rep.setPublicClient(false);
    rep.setEnabled(true);
    adminClient.realm("master").clients().create(rep);

    Keycloak realmClient = AdminClientUtil.createAdminClient(suiteContext.isAdapterCompatTesting(),
            "master", "admin", "admin", "fullScopedClient", "618268aa-51e6-4e64-93c4-3c0bc65b8171");
    try {
        RealmRepresentation newRealm=new RealmRepresentation();
        newRealm.setRealm("anotherRealm");
        newRealm.setId("anotherRealm");
        newRealm.setEnabled(true);
        realmClient.realms().create(newRealm);

        ClientRepresentation newClient = new ClientRepresentation();

        newClient.setName("newClient");
        newClient.setClientId("newClient");
        newClient.setFullScopeAllowed(true);
        newClient.setSecret("secret");
        newClient.setProtocol("openid-connect");
        newClient.setPublicClient(false);
        newClient.setEnabled(true);
        Response response = realmClient.realm("anotherRealm").clients().create(newClient);
        Assert.assertEquals(403, response.getStatus());

        realmClient.close();
        realmClient = AdminClientUtil.createAdminClient(suiteContext.isAdapterCompatTesting(),
                "master", "admin", "admin", "fullScopedClient", "618268aa-51e6-4e64-93c4-3c0bc65b8171");
        response = realmClient.realm("anotherRealm").clients().create(newClient);
        Assert.assertEquals(201, response.getStatus());
    } finally {
        adminClient.realm("anotherRealm").remove();
        realmClient.close();
    }


}
 
Example 20
Source File: AbstractClientRegistrationTest.java    From keycloak with Apache License 2.0 4 votes vote down vote up
@Override
public void addTestRealms(List<RealmRepresentation> testRealms) {
    RealmRepresentation rep = new RealmRepresentation();
    rep.setEnabled(true);
    rep.setId(REALM_NAME);
    rep.setRealm(REALM_NAME);
    rep.setUsers(new LinkedList<UserRepresentation>());

    LinkedList<CredentialRepresentation> credentials = new LinkedList<>();
    CredentialRepresentation password = new CredentialRepresentation();
    password.setType(CredentialRepresentation.PASSWORD);
    password.setValue("password");
    credentials.add(password);

    UserRepresentation user = new UserRepresentation();
    user.setEnabled(true);
    user.setUsername("manage-clients");
    user.setCredentials(credentials);
    user.setClientRoles(Collections.singletonMap(Constants.REALM_MANAGEMENT_CLIENT_ID, Collections.singletonList(AdminRoles.MANAGE_CLIENTS)));

    rep.getUsers().add(user);

    UserRepresentation user2 = new UserRepresentation();
    user2.setEnabled(true);
    user2.setUsername("create-clients");
    user2.setCredentials(credentials);
    user2.setClientRoles(Collections.singletonMap(Constants.REALM_MANAGEMENT_CLIENT_ID, Collections.singletonList(AdminRoles.CREATE_CLIENT)));

    rep.getUsers().add(user2);

    UserRepresentation user3 = new UserRepresentation();
    user3.setEnabled(true);
    user3.setUsername("no-access");
    user3.setCredentials(credentials);

    rep.getUsers().add(user3);

    UserRepresentation appUser = new UserRepresentation();
    appUser.setEnabled(true);
    appUser.setUsername("test-user");
    appUser.setEmail("test-user@localhost");
    appUser.setCredentials(credentials);

    rep.getUsers().add(appUser);

    testRealms.add(rep);
}