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

The following examples show how to use org.keycloak.representations.idm.RealmRepresentation#setRealm() . 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: KeycloakRealmResourceManager.java    From quarkus with Apache License 2.0 6 votes vote down vote up
private static RealmRepresentation createRealm(String name) {
    RealmRepresentation realm = new RealmRepresentation();

    realm.setRealm(name);
    realm.setEnabled(true);
    realm.setUsers(new ArrayList<>());
    realm.setClients(new ArrayList<>());
    realm.setAccessTokenLifespan(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.getRoles().getRealm().add(new RoleRepresentation("confidential", null, false));

    return realm;
}
 
Example 2
Source File: KeycloakTestResource.java    From quarkus with Apache License 2.0 6 votes vote down vote up
private static RealmRepresentation createRealm(String name) {
    RealmRepresentation realm = new RealmRepresentation();

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

    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.getRoles().getRealm().add(new RoleRepresentation("confidential", null, false));

    return realm;
}
 
Example 3
Source File: KeycloakRealmResourceManager.java    From quarkus with Apache License 2.0 6 votes vote down vote up
private static RealmRepresentation createRealm(String name) {
    RealmRepresentation realm = new RealmRepresentation();

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

    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.getRoles().getRealm().add(new RoleRepresentation("confidential", null, false));

    return realm;
}
 
Example 4
Source File: KeycloakTestResource.java    From quarkus with Apache License 2.0 6 votes vote down vote up
private static RealmRepresentation createRealm(String name) {
    RealmRepresentation realm = new RealmRepresentation();

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

    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.getRoles().getRealm().add(new RoleRepresentation("confidential", null, false));

    return realm;
}
 
Example 5
Source File: RealmTest.java    From keycloak with Apache License 2.0 6 votes vote down vote up
/**
 * KEYCLOAK-1990 1991
 * @throws Exception
 */
@Test
public void renameRealmTest() throws Exception {
    RealmRepresentation realm1 = new RealmRepresentation();
    realm1.setRealm("test-immutable");
    adminClient.realms().create(realm1);
    realm1 = adminClient.realms().realm("test-immutable").toRepresentation();
    realm1.setRealm("test-immutable-old");
    adminClient.realms().realm("test-immutable").update(realm1);
    realm1 = adminClient.realms().realm("test-immutable-old").toRepresentation();

    RealmRepresentation realm2 = new RealmRepresentation();
    realm2.setRealm("test-immutable");
    adminClient.realms().create(realm2);
    realm2 = adminClient.realms().realm("test-immutable").toRepresentation();

    adminClient.realms().realm("test-immutable-old").remove();
    adminClient.realms().realm("test-immutable").remove();
}
 
Example 6
Source File: DatasetTest.java    From keycloak with Apache License 2.0 6 votes vote down vote up
@Test
@Ignore
public void pojoToMap() throws IOException {
    RealmRepresentation realm = new RealmRepresentation();
    realm.setRealm("realm_0");
    realm.setEnabled(true);

    logger().info("REP JSON:");
    logger().info(writeValueAsString(realm));

    TypeReference typeRef = new TypeReference<Map<String, Object>>() {
    };
    ObjectMapper mapper = new ObjectMapper();
    mapper.setSerializationInclusion(Include.NON_NULL);
    Map<String, Object> map = mapper.convertValue(realm, typeRef);
    map.put("index", 1000);

    logger().info("MAP:");
    logger().info(map);

    logger().info("MAP JSON:");
    logger().info(writeValueAsString(map));

}
 
Example 7
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 8
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 9
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 10
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 11
Source File: ClientModelTest.java    From keycloak with Apache License 2.0 5 votes vote down vote up
@Override
public void addTestRealms(List<RealmRepresentation> testRealms) {
    RealmRepresentation realm = new RealmRepresentation();
    realm.setRealm(realmName);
    realm.setEnabled(true);
    testRealms.add(realm);
}
 
Example 12
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 13
Source File: TermsAndConditionsTest.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.setRealm(REALM);
    testRealmRep.setEnabled(true);
    testRealms.add(testRealmRep);
}
 
Example 14
Source File: TestsHelper.java    From keycloak with Apache License 2.0 5 votes vote down vote up
public static boolean importTestRealm(String username, String password) throws IOException {
    testRealm = appName + "-realm";
    RealmRepresentation realmRepresentation = new RealmRepresentation();
    realmRepresentation.setRealm(testRealm);
    realmRepresentation.setEnabled(true);
    Keycloak keycloak = Keycloak.getInstance(
            keycloakBaseUrl,
            "master",
            username,
            password,
            "admin-cli");
    keycloak.realms().create(realmRepresentation);
    generateInitialAccessToken(keycloak);
    return true;
}
 
Example 15
Source File: KcSamlBrokerConfiguration.java    From keycloak with Apache License 2.0 5 votes vote down vote up
@Override
public RealmRepresentation createProviderRealm() {
    RealmRepresentation realm = new RealmRepresentation();

    realm.setEnabled(true);
    realm.setRealm(REALM_PROV_NAME);

    return realm;
}
 
Example 16
Source File: KeycloakRealmResourceManager.java    From quarkus with Apache License 2.0 5 votes vote down vote up
private static RealmRepresentation createRealm(String name) {
    RealmRepresentation realm = new RealmRepresentation();

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

    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.getRoles().getRealm().add(new RoleRepresentation("confidential", null, false));

    realm.getClients().add(createClient("quarkus-app"));
    realm.getClients().add(createClientJwt("quarkus-app-jwt"));
    realm.getUsers().add(createUser("alice", "user"));
    realm.getUsers().add(createUser("admin", "user", "admin"));
    realm.getUsers().add(createUser("jdoe", "user", "confidential"));

    return realm;
}
 
Example 17
Source File: RealmTest.java    From keycloak with Apache License 2.0 4 votes vote down vote up
@Test
public void createRealmEmpty() {
    RealmRepresentation rep = new RealmRepresentation();
    rep.setRealm("new-realm");

    adminClient.realms().create(rep);

    Assert.assertNames(adminClient.realms().findAll(), "master", AuthRealm.TEST, REALM_NAME, "new-realm");

    List<String> clients = adminClient.realms().realm("new-realm").clients().findAll().stream().map(ClientRepresentation::getClientId).collect(Collectors.toList());
    assertThat(clients, containsInAnyOrder("account", "account-console", "admin-cli", "broker", "realm-management", "security-admin-console"));

    adminClient.realms().realm("new-realm").remove();

    Assert.assertNames(adminClient.realms().findAll(), "master", AuthRealm.TEST, REALM_NAME);
}
 
Example 18
Source File: RealmTest.java    From keycloak with Apache License 2.0 4 votes vote down vote up
@Test(expected = BadRequestException.class)
public void createRealmRejectReservedChar() {
    RealmRepresentation rep = new RealmRepresentation();
    rep.setRealm("new-re;alm");
    adminClient.realms().create(rep);
}
 
Example 19
Source File: RealmsConfigurationLoader.java    From keycloak with Apache License 2.0 4 votes vote down vote up
private static void readRealm(JsonParser p) throws IOException {

        // as soon as we encounter users, roles, clients we create a CreateRealmJob
        // TODO: if after that point in a realm we encounter realm attribute, we report a warning but continue

        boolean skip = false;
        try {
            RealmRepresentation r = new RealmRepresentation();
            JsonToken t = p.nextToken();
            outer:
            while (t != JsonToken.END_OBJECT && !skip) {

                //System.out.println(t + ", name: " + p.getCurrentName() + ", text: '" + p.getText() + "', value: " + p.getValueAsString());

                switch (p.getCurrentName()) {
                    case "realm":
                        r.setRealm(getStringValue(p));
                        skip = !started && realmSkipped(r.getRealm()) ;
                        if (skip) {
                            break outer;
                        }
                        break;
                    case "enabled":
                        r.setEnabled(getBooleanValue(p));
                        break;
                    case "accessTokenLifespan":
                        r.setAccessCodeLifespan(getIntegerValue(p));
                        break;
                    case "registrationAllowed":
                        r.setRegistrationAllowed(getBooleanValue(p));
                        break;
                    case "passwordPolicy":
                        r.setPasswordPolicy(getStringValue(p));
                        break;
                    case "sslRequired":
                        r.setSslRequired(getStringValue(p));
                        break;
                    case "users":
                        ensureRealm(r);
                        if (seekToStart()) {
                            enqueueFetchRealmRoles(r);
                            completePending();
                        }
                        readUsers(r, p);
                        break;
                    case "roles":
                        ensureRealm(r);
                        readRoles(r, p);
                        break;
                    case "clients":
                        ensureRealm(r);
                        readClients(r, p);
                        completePending();
                        if (seekToStart()) {
                            enqueueFetchMissingClients(r);
                            completePending();
                        }
                        break;
                    default: {
                        // if we don't understand the field we ignore it - but report that
                        log.warn("Realm attribute ignored: " + p.getCurrentName());
                        consumeAttribute(p);
                        continue; // skip p.nextToken() at end of loop - consumeAttribute() already did it
                    }
                }

                t = p.nextToken();
            }

            if (skip) {
                log.info("Realm skipped: " + r.getRealm());
                consumeParent(p);
            }

        } finally {
            // we wait for realm to complete
            completePending();

            // reset realm specific cache
            realmCreated = false;
            clientIdMap.clear();
            realmRoleIdMap.clear();
            clientRoleIdMap.clear();
        }
    }
 
Example 20
Source File: UserTest.java    From keycloak with Apache License 2.0 3 votes vote down vote up
@Test
public void testAccessUserFromOtherRealm() {
    RealmRepresentation firstRealm = new RealmRepresentation();

    firstRealm.setRealm("first-realm");

    adminClient.realms().create(firstRealm);

    realm = adminClient.realm(firstRealm.getRealm());
    realmId = realm.toRepresentation().getId();

    UserRepresentation firstUser = new UserRepresentation();

    firstUser.setUsername("first");
    firstUser.setEmail("[email protected]");

    firstUser.setId(createUser(firstUser, false));

    RealmRepresentation secondRealm = new RealmRepresentation();

    secondRealm.setRealm("second-realm");

    adminClient.realms().create(secondRealm);

    adminClient.realm(firstRealm.getRealm()).users().get(firstUser.getId()).update(firstUser);

    try {
        adminClient.realm(secondRealm.getRealm()).users().get(firstUser.getId()).toRepresentation();
        fail("Should not have access to firstUser from another realm");
    } catch (NotFoundException nfe) {
        // ignore
    }
}