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

The following examples show how to use org.keycloak.representations.idm.RealmRepresentation#setEnabled() . 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: 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 4
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 5
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 6
Source File: OpenShiftTokenReviewEndpointTest.java    From keycloak with Apache License 2.0 5 votes vote down vote up
@Test
public void realmDisabled() {
    RealmRepresentation r = testRealm().toRepresentation();
    try {
        new Review().runAfterTokenRequest(i -> {
            r.setEnabled(false);
            testRealm().update(r);
        }).invoke().assertError(401, null);


    } finally {
        r.setEnabled(true);
        testRealm().update(r);
    }
}
 
Example 7
Source File: TermsAndConditionsThemeTest.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 8
Source File: ConsentsTest.java    From keycloak with Apache License 2.0 5 votes vote down vote up
protected RealmRepresentation createProviderRealm() {
    RealmRepresentation realm = new RealmRepresentation();
    realm.setRealm(REALM_PROV_NAME);
    realm.setEnabled(true);

    return realm;
}
 
Example 9
Source File: FineGrainAdminUnitTest.java    From keycloak with Apache License 2.0 5 votes vote down vote up
@Test
public void testCreateRealmCreateClientWithMaster() 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);

    RealmRepresentation newRealm=new RealmRepresentation();
    newRealm.setRealm("anotherRealm");
    newRealm.setId("anotherRealm");
    newRealm.setEnabled(true);
    adminClient.realms().create(newRealm);

    try {
        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 = adminClient.realm("anotherRealm").clients().create(newClient);
        Assert.assertEquals(201, response.getStatus());
    } finally {
        adminClient.realm("anotherRealm").remove();

    }
}
 
Example 10
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 11
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 12
Source File: RealmInvalidationClusterTest.java    From keycloak with Apache License 2.0 5 votes vote down vote up
@Override
protected RealmRepresentation testEntityUpdates(RealmRepresentation realm, boolean backendFailover) {

    // realm name
    String originalName = realm.getRealm();
    realm.setRealm(realm.getRealm() + "_updated");
    realm = updateEntity(originalName, realm, getCurrentFailNode());
    verifyEntityUpdateDuringFailover(realm, backendFailover);

    // enabled
    realm.setEnabled(!realm.isEnabled());
    realm = updateEntityOnCurrentFailNode(realm, "enabled");
    verifyEntityUpdateDuringFailover(realm, backendFailover);

    // public key
    realm.setPublicKey("GENERATE");
    realm = updateEntityOnCurrentFailNode(realm, "public key");
    assertNotEquals("GENERATE", realm.getPublicKey());
    verifyEntityUpdateDuringFailover(realm, backendFailover);

    // require ssl
    realm.setSslRequired("all");
    realm = updateEntityOnCurrentFailNode(realm, "require ssl");
    verifyEntityUpdateDuringFailover(realm, backendFailover);

    // brute force detection
    realm.setBruteForceProtected(!realm.isBruteForceProtected());
    realm = updateEntityOnCurrentFailNode(realm, "brute force");
    verifyEntityUpdateDuringFailover(realm, backendFailover);

    // brute force detection - failure factor
    realm.setBruteForceProtected(true);
    realm.setFailureFactor(realm.getFailureFactor() + 1);
    realm = updateEntityOnCurrentFailNode(realm, "brute force failure factor");
    verifyEntityUpdateDuringFailover(realm, backendFailover);

    return realm;
}
 
Example 13
Source File: DockerTestRealmSetup.java    From keycloak with Apache License 2.0 5 votes vote down vote up
public static RealmRepresentation createRealm(final String realmId) {
    final RealmRepresentation createdRealm = new RealmRepresentation();
    createdRealm.setId(UUID.randomUUID().toString());
    createdRealm.setRealm(realmId);
    createdRealm.setEnabled(true);
    createdRealm.setAuthenticatorConfig(new ArrayList<>());

    return createdRealm;
}
 
Example 14
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 15
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 16
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 17
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 18
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 19
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);
}
 
Example 20
Source File: AbstractInvalidationClusterTest.java    From keycloak with Apache License 2.0 4 votes vote down vote up
protected RealmRepresentation createTestRealmRepresentation() {
    RealmRepresentation testRealm = new RealmRepresentation();
    testRealm.setRealm("test_" + RandomStringUtils.randomAlphabetic(5));
    testRealm.setEnabled(true);
    return testRealm;
}