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

The following examples show how to use org.keycloak.representations.idm.RealmRepresentation#setUsers() . 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: KeycloakDevModeRealmResourceManager.java    From quarkus with Apache License 2.0 8 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(2); // sec
    realm.setAccessTokenLifespan(3); // 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));
    return realm;
}
 
Example 2
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 3
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 4
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 5
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 6
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 7
Source File: DockerTestRealmSetup.java    From keycloak with Apache License 2.0 5 votes vote down vote up
public static void configureUser(final RealmRepresentation dockerRealm, final String username, final String password) {
    final UserRepresentation dockerUser = new UserRepresentation();
    dockerUser.setUsername(username);
    dockerUser.setEnabled(true);
    dockerUser.setEmail("[email protected]");
    dockerUser.setFirstName("docker");
    dockerUser.setLastName("user");

    final CredentialRepresentation dockerUserCreds = new CredentialRepresentation();
    dockerUserCreds.setType(CredentialRepresentation.PASSWORD);
    dockerUserCreds.setValue(password);
    dockerUser.setCredentials(Collections.singletonList(dockerUserCreds));

    dockerRealm.setUsers(Collections.singletonList(dockerUser));
}
 
Example 8
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 9
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);
}