Java Code Examples for org.keycloak.representations.idm.UserRepresentation#getId()

The following examples show how to use org.keycloak.representations.idm.UserRepresentation#getId() . 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: BruteForceTest.java    From keycloak with Apache License 2.0 6 votes vote down vote up
@Override
public void configureTestRealm(RealmRepresentation testRealm) {
    UserRepresentation user = RealmRepUtil.findUser(testRealm, "test-user@localhost");
    UserBuilder.edit(user).totpSecret("totpSecret");

    testRealm.setBruteForceProtected(true);
    testRealm.setFailureFactor(2);
    testRealm.setMaxDeltaTimeSeconds(20);
    testRealm.setMaxFailureWaitSeconds(100);
    testRealm.setWaitIncrementSeconds(5);
    //testRealm.setQuickLoginCheckMilliSeconds(0L);

    userId = user.getId();

    RealmRepUtil.findClientByClientId(testRealm, "test-app").setDirectAccessGrantsEnabled(true);
    testRealm.getUsers().add(UserBuilder.create().username("user2").email("user2@localhost").password("password").build());
}
 
Example 2
Source File: ServiceAccountTest.java    From keycloak with Apache License 2.0 6 votes vote down vote up
@Test
public void refreshTokenRefreshForDisabledServiceAccount() throws Exception {
    try {
        oauth.clientId("service-account-cl");
        OAuthClient.AccessTokenResponse response = oauth.doClientCredentialsGrantAccessTokenRequest("secret1");
        assertEquals(200, response.getStatusCode());

        ClientManager.realm(adminClient.realm("test")).clientId("service-account-cl").setServiceAccountsEnabled(false);

        response = oauth.doRefreshTokenRequest(response.getRefreshToken(), "secret1");
        assertEquals(400, response.getStatusCode());
    }
    finally {
        ClientManager.realm(adminClient.realm("test")).clientId("service-account-cl").setServiceAccountsEnabled(true);
        UserRepresentation user = ClientManager.realm(adminClient.realm("test")).clientId("service-account-cl").getServiceAccountUser();
        userId = user.getId();
        userName = user.getUsername();
    }
}
 
Example 3
Source File: AbstractX509AuthenticationTest.java    From keycloak with Apache License 2.0 5 votes vote down vote up
@Override
public void configureTestRealm(RealmRepresentation testRealm) {

    ClientRepresentation app = ClientBuilder.create()
            .id(KeycloakModelUtils.generateId())
            .clientId("resource-owner")
            .directAccessGrants()
            .secret("secret")
            .build();

    UserRepresentation user = UserBuilder.create()
            .id(KeycloakModelUtils.generateId())
            .username("Keycloak")
            .email("localhost@localhost")
            .enabled(true)
            .password("password")
            .addAttribute("x509_issuer_identity", "Keycloak Intermediate CA")
            .build();

    userId2 = user.getId();

    ClientRepresentation client = findTestApp(testRealm);
    URI baseUri = URI.create(client.getRedirectUris().get(0));
    URI redir = URI.create("https://localhost:" + System.getProperty("auth.server.https.port", "8543") + baseUri.getRawPath());
    client.getRedirectUris().add(redir.toString());

    testRealm.setBruteForceProtected(true);
    testRealm.setFailureFactor(2);

    RealmBuilder.edit(testRealm)
            .user(user)
            .client(app);
}
 
Example 4
Source File: LoginTest.java    From keycloak with Apache License 2.0 5 votes vote down vote up
@Override
public void configureTestRealm(RealmRepresentation testRealm) {
    UserRepresentation user = UserBuilder.create()
                                         .id("login-test")
                                         .username("login-test")
                                         .email("[email protected]")
                                         .enabled(true)
                                         .password("password")
                                         .build();
    userId = user.getId();

    UserRepresentation user2 = UserBuilder.create()
                                          .id("login-test2")
                                          .username("login-test2")
                                          .email("[email protected]")
                                          .enabled(true)
                                          .password("password")
                                          .build();
    user2Id = user2.getId();

    UserRepresentation admin = UserBuilder.create()
            .username("admin")
            .password("admin")
            .enabled(true)
            .build();
    HashMap<String, List<String>> clientRoles = new HashMap<>();
    clientRoles.put("realm-management", Arrays.asList("realm-admin"));
    admin.setClientRoles(clientRoles);

    RealmBuilder.edit(testRealm)
                .user(user)
                .user(user2)
                .user(admin);
}
 
Example 5
Source File: AssertEvents.java    From keycloak with Apache License 2.0 5 votes vote down vote up
public Matcher<String> defaultUserId() {
    return new TypeSafeMatcher<String>() {
        private String userId;

        @Override
        protected boolean matchesSafely(String item) {
            return item.equals(getUserId());
        }

        @Override
        public void describeTo(Description description) {
            description.appendText(getUserId());
        }

        private String getUserId() {
            if (userId == null) {
                UserRepresentation user = getUser(DEFAULT_USERNAME);
                if (user == null) {
                    throw new RuntimeException("Default user does not exist: " + DEFAULT_USERNAME + ". Make sure to add it to your test realm.");
                }
                userId = user.getId();
            }
            return userId;
        }

    };
}
 
Example 6
Source File: LDAPProvidersIntegrationNoImportTest.java    From keycloak with Apache License 2.0 5 votes vote down vote up
@Override
protected void assertFederatedUserLink(UserRepresentation user) {
    StorageId storageId = new StorageId(user.getId());
    Assert.assertFalse(storageId.isLocal());
    Assert.assertEquals(ldapModelId, storageId.getProviderId());

    // TODO: It should be possibly LDAP_ID (LDAP UUID) used as an externalId inside storageId...
    Assert.assertEquals(storageId.getExternalId(), user.getUsername());
    Assert.assertNull(user.getFederationLink());
}
 
Example 7
Source File: AbstractX509AuthenticationTest.java    From keycloak with Apache License 2.0 4 votes vote down vote up
@Before
public void configureFlows() {
    authMgmtResource = adminClient.realms().realm(REALM_NAME).flows();

    AuthenticationFlowRepresentation browserFlow = copyBrowserFlow();
    Assert.assertNotNull(browserFlow);

    AuthenticationFlowRepresentation directGrantFlow = createDirectGrantFlow();
    Assert.assertNotNull(directGrantFlow);

    setBrowserFlow(browserFlow);
    Assert.assertEquals(testRealm().toRepresentation().getBrowserFlow(), browserFlow.getAlias());

    setDirectGrantFlow(directGrantFlow);
    Assert.assertEquals(testRealm().toRepresentation().getDirectGrantFlow(), directGrantFlow.getAlias());
    Assert.assertEquals(0, directGrantFlow.getAuthenticationExecutions().size());

    // Add X509 cert authenticator to the direct grant flow
    directGrantExecution = addAssertExecution(directGrantFlow, ValidateX509CertificateUsernameFactory.PROVIDER_ID, REQUIRED);
    Assert.assertNotNull(directGrantExecution);

    directGrantFlow = authMgmtResource.getFlow(directGrantFlow.getId());
    Assert.assertNotNull(directGrantFlow.getAuthenticationExecutions());
    Assert.assertEquals(1, directGrantFlow.getAuthenticationExecutions().size());

    // Add X509 authenticator to the browser flow
    browserExecution = addAssertExecution(browserFlow, X509ClientCertificateAuthenticatorFactory.PROVIDER_ID, ALTERNATIVE);
    Assert.assertNotNull(browserExecution);

    // Raise the priority of the authenticator to position it right before
    // the Username/password authentication
    // TODO find a better, more explicit way to specify the position
    // of authenticator within the flow relative to other authenticators
    authMgmtResource.raisePriority(browserExecution.getId());
    // TODO raising the priority didn't generate the event?
    //assertAdminEvents.assertEvent(REALM_NAME, OperationType.UPDATE, AdminEventPaths.authRaiseExecutionPath(exec.getId()));

    UserRepresentation user = findUser("test-user@localhost");
    userId = user.getId();

    user.singleAttribute("x509_certificate_identity","-");
    user.singleAttribute("alternative_email", "test-user-altmail@localhost");
    user.singleAttribute("upn", "test_upn_name@localhost");
    updateUser(user);
}