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

The following examples show how to use org.keycloak.representations.idm.RealmRepresentation#setRegistrationAllowed() . 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: InvalidationCrossDCTest.java    From keycloak with Apache License 2.0 5 votes vote down vote up
@Test
public void realmInvalidationTest() throws Exception {
    enableDcOnLoadBalancer(DC.FIRST);
    enableDcOnLoadBalancer(DC.SECOND);

    RealmRepresentation realmDc0 = getAdminClientForStartedNodeInDc(0).realms().realm(REALM_NAME).toRepresentation();
    RealmRepresentation realmDc1 = getAdminClientForStartedNodeInDc(1).realms().realm(REALM_NAME).toRepresentation();

    // Test same realm on both DCs
    Assert.assertNull(realmDc0.getDisplayName());
    Assert.assertTrue(realmDc0.isRegistrationAllowed());
    Assert.assertNull(realmDc1.getDisplayName());
    Assert.assertTrue(realmDc1.isRegistrationAllowed());

    // Update realm on DC0
    realmDc0.setRegistrationAllowed(false);
    realmDc0.setDisplayName("Cool Realm!");
    getAdminClientForStartedNodeInDc(0).realms().realm(REALM_NAME).update(realmDc0);

    // Assert updated on both DC0 and DC1 (here retry is needed. We need to wait until invalidation message arrives)
    realmDc0 = getAdminClientForStartedNodeInDc(0).realms().realm(REALM_NAME).toRepresentation();
    Assert.assertEquals("Cool Realm!", realmDc0.getDisplayName());
    Assert.assertFalse(realmDc0.isRegistrationAllowed());

    AtomicInteger i = new AtomicInteger(0);
    Retry.execute(() -> {
        i.incrementAndGet();
        RealmRepresentation realmDcc1 = getAdminClientForStartedNodeInDc(1).realms().realm(REALM_NAME).toRepresentation();
        Assert.assertEquals("Cool Realm!", realmDcc1.getDisplayName());
        Assert.assertFalse(realmDcc1.isRegistrationAllowed());
    }, 50, 50);

    log.infof("realmInvalidationTest: Passed after '%d' iterations", i.get());
}
 
Example 2
Source File: LoginPageTest.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 = testRealms.get(0);
    testRealmRep.setDisplayNameHtml("Test realm <b>HTML</b>");
    testRealmRep.setRememberMe(true);
    testRealmRep.setResetPasswordAllowed(true);
    testRealmRep.setRegistrationAllowed(true);
}
 
Example 3
Source File: TermsAndConditionsTest.java    From keycloak with Apache License 2.0 5 votes vote down vote up
@Test
public void testSelfRegisteredUser() {
    // enable self-registration
    RealmResource realmResource = adminClient.realm(REALM);
    RealmRepresentation realmRepresentation = realmResource.toRepresentation();
    realmRepresentation.setRegistrationAllowed(true);
    realmResource.update(realmRepresentation);
    
    // enable terms
    setRequiredActionEnabled(REALM, RequiredActions.TERMS_AND_CONDITIONS, true, true);
    
    // self-register
    CredentialRepresentation mrBurnsPassword = new CredentialRepresentation();
    mrBurnsPassword.setType(CredentialRepresentation.PASSWORD);
    mrBurnsPassword.setValue("Excellent.");
    
    List<CredentialRepresentation> credentials = new ArrayList<CredentialRepresentation>();
    credentials.add(mrBurnsPassword);
    
    UserRepresentation mrBurns = new UserRepresentation();
    mrBurns.setUsername("mrburns");
    mrBurns.setFirstName("Montgomery");
    mrBurns.setLastName("Burns");
    mrBurns.setEmail("[email protected]");
    mrBurns.setCredentials(credentials);
    
    testRealmAdminConsolePage.navigateTo();
    testRealmLoginPage.form().register();
    
    registrationPage.register(mrBurns);
    
    // test t&c
    Assert.assertTrue(termsAndConditionsPage.isCurrent());
    
    // disable terms
    setRequiredActionEnabled(REALM, RequiredActions.TERMS_AND_CONDITIONS, false, false);
}
 
Example 4
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 5
Source File: RealmTest.java    From keycloak with Apache License 2.0 4 votes vote down vote up
@Test
public void updateRealm() {
    // first change
    RealmRepresentation rep = realm.toRepresentation();
    rep.setSsoSessionIdleTimeout(123);
    rep.setSsoSessionMaxLifespan(12);
    rep.setSsoSessionIdleTimeoutRememberMe(33);
    rep.setSsoSessionMaxLifespanRememberMe(34);
    rep.setAccessCodeLifespanLogin(1234);
    rep.setActionTokenGeneratedByAdminLifespan(2345);
    rep.setActionTokenGeneratedByUserLifespan(3456);
    rep.setRegistrationAllowed(true);
    rep.setRegistrationEmailAsUsername(true);
    rep.setEditUsernameAllowed(true);
    rep.setUserManagedAccessAllowed(true);

    realm.update(rep);
    assertAdminEvents.assertEvent(realmId, OperationType.UPDATE, Matchers.nullValue(String.class), rep, ResourceType.REALM);

    rep = realm.toRepresentation();

    assertEquals(123, rep.getSsoSessionIdleTimeout().intValue());
    assertEquals(12, rep.getSsoSessionMaxLifespan().intValue());
    assertEquals(33, rep.getSsoSessionIdleTimeoutRememberMe().intValue());
    assertEquals(34, rep.getSsoSessionMaxLifespanRememberMe().intValue());
    assertEquals(1234, rep.getAccessCodeLifespanLogin().intValue());
    assertEquals(2345, rep.getActionTokenGeneratedByAdminLifespan().intValue());
    assertEquals(3456, rep.getActionTokenGeneratedByUserLifespan().intValue());
    assertEquals(Boolean.TRUE, rep.isRegistrationAllowed());
    assertEquals(Boolean.TRUE, rep.isRegistrationEmailAsUsername());
    assertEquals(Boolean.TRUE, rep.isEditUsernameAllowed());
    assertEquals(Boolean.TRUE, rep.isUserManagedAccessAllowed());

    // second change
    rep.setRegistrationAllowed(false);
    rep.setRegistrationEmailAsUsername(false);
    rep.setEditUsernameAllowed(false);
    rep.setUserManagedAccessAllowed(false);

    realm.update(rep);
    assertAdminEvents.assertEvent(realmId, OperationType.UPDATE, Matchers.nullValue(String.class), rep, ResourceType.REALM);

    rep = realm.toRepresentation();
    assertEquals(Boolean.FALSE, rep.isRegistrationAllowed());
    assertEquals(Boolean.FALSE, rep.isRegistrationEmailAsUsername());
    assertEquals(Boolean.FALSE, rep.isEditUsernameAllowed());
    assertEquals(Boolean.FALSE, rep.isUserManagedAccessAllowed());
}