Search in sources :

Example 6 with UserRepresentation

use of org.keycloak.representations.account.UserRepresentation in project keycloak by keycloak.

the class AccountRestServiceTest method testUpdateProfileCannotChangeThroughAttributes.

@Test
public void testUpdateProfileCannotChangeThroughAttributes() throws IOException {
    UserRepresentation user = getUser();
    String originalUsername = user.getUsername();
    Map<String, List<String>> originalAttributes = new HashMap<>(user.getAttributes());
    try {
        user.getAttributes().put("username", Collections.singletonList("Username"));
        user.getAttributes().put("attr2", Collections.singletonList("val2"));
        user = updateAndGet(user);
        assertEquals(user.getUsername(), originalUsername);
    } finally {
        RealmRepresentation realmRep = adminClient.realm("test").toRepresentation();
        realmRep.setEditUsernameAllowed(true);
        adminClient.realm("test").update(realmRep);
        user.setUsername(originalUsername);
        user.setAttributes(originalAttributes);
        SimpleHttp.Response response = SimpleHttp.doPost(getAccountUrl(null), httpClient).auth(tokenUtil.getToken()).json(user).asResponse();
        System.out.println(response.asString());
        assertEquals(204, response.getStatus());
    }
}
Also used : SimpleHttp(org.keycloak.broker.provider.util.SimpleHttp) HashMap(java.util.HashMap) RealmRepresentation(org.keycloak.representations.idm.RealmRepresentation) List(java.util.List) UserRepresentation(org.keycloak.representations.account.UserRepresentation) Test(org.junit.Test) AbstractAuthenticationTest(org.keycloak.testsuite.admin.authentication.AbstractAuthenticationTest)

Example 7 with UserRepresentation

use of org.keycloak.representations.account.UserRepresentation in project keycloak by keycloak.

the class AccountRestServiceTest method testUpdateProfileWithRegistrationEmailAsUsername.

// KEYCLOAK-7572
@Test
public void testUpdateProfileWithRegistrationEmailAsUsername() throws IOException {
    RealmRepresentation realmRep = adminClient.realm("test").toRepresentation();
    realmRep.setRegistrationEmailAsUsername(true);
    adminClient.realm("test").update(realmRep);
    UserRepresentation user = getUser();
    String originalFirstname = user.getFirstName();
    try {
        user.setFirstName("Homer1");
        user = updateAndGet(user);
        assertEquals("Homer1", user.getFirstName());
    } finally {
        user.setFirstName(originalFirstname);
        int status = SimpleHttp.doPost(getAccountUrl(null), httpClient).auth(tokenUtil.getToken()).json(user).asStatus();
        assertEquals(204, status);
    }
}
Also used : RealmRepresentation(org.keycloak.representations.idm.RealmRepresentation) UserRepresentation(org.keycloak.representations.account.UserRepresentation) Test(org.junit.Test) AbstractAuthenticationTest(org.keycloak.testsuite.admin.authentication.AbstractAuthenticationTest)

Example 8 with UserRepresentation

use of org.keycloak.representations.account.UserRepresentation in project keycloak by keycloak.

the class AccountRestServiceTest method testUpdateProfileEmailChangeSetsEmailVerified.

/**
 * Reproducer for bugs KEYCLOAK-17424 and KEYCLOAK-17582
 */
@Test
public void testUpdateProfileEmailChangeSetsEmailVerified() throws IOException {
    UserRepresentation user = getUser();
    String originalEmail = user.getEmail();
    try {
        RealmRepresentation realmRep = adminClient.realm("test").toRepresentation();
        realmRep.setRegistrationEmailAsUsername(false);
        adminClient.realm("test").update(realmRep);
        // set flag over adminClient to initial value
        UserResource userResource = adminClient.realm("test").users().get(user.getId());
        org.keycloak.representations.idm.UserRepresentation ur = userResource.toRepresentation();
        ur.setEmailVerified(true);
        userResource.update(ur);
        // make sure flag is correct before the test
        user = getUser();
        assertEquals(true, user.isEmailVerified());
        // Update without email change - flag not reset to false
        user.setEmail(originalEmail);
        user = updateAndGet(user);
        assertEquals(originalEmail, user.getEmail());
        assertEquals(true, user.isEmailVerified());
        // Update email - flag must be reset to false
        user.setEmail("bobby@localhost");
        user = updateAndGet(user);
        assertEquals("bobby@localhost", user.getEmail());
        assertEquals(false, user.isEmailVerified());
    } finally {
        RealmRepresentation realmRep = adminClient.realm("test").toRepresentation();
        realmRep.setEditUsernameAllowed(true);
        adminClient.realm("test").update(realmRep);
        user.setEmail(originalEmail);
        SimpleHttp.Response response = SimpleHttp.doPost(getAccountUrl(null), httpClient).auth(tokenUtil.getToken()).json(user).asResponse();
        System.out.println(response.asString());
        assertEquals(204, response.getStatus());
    }
}
Also used : SimpleHttp(org.keycloak.broker.provider.util.SimpleHttp) RealmRepresentation(org.keycloak.representations.idm.RealmRepresentation) UserResource(org.keycloak.admin.client.resource.UserResource) UserRepresentation(org.keycloak.representations.account.UserRepresentation) Test(org.junit.Test) AbstractAuthenticationTest(org.keycloak.testsuite.admin.authentication.AbstractAuthenticationTest)

Example 9 with UserRepresentation

use of org.keycloak.representations.account.UserRepresentation in project keycloak by keycloak.

the class AccountRestServiceTest method testProfilePermissions.

@Test
public void testProfilePermissions() throws IOException {
    TokenUtil noaccessToken = new TokenUtil("no-account-access", "password");
    TokenUtil viewToken = new TokenUtil("view-account-access", "password");
    // Read with no access
    assertEquals(403, SimpleHttp.doGet(getAccountUrl(null), httpClient).header("Accept", "application/json").auth(noaccessToken.getToken()).asStatus());
    // Update with no access
    assertEquals(403, SimpleHttp.doPost(getAccountUrl(null), httpClient).auth(noaccessToken.getToken()).json(new UserRepresentation()).asStatus());
    // Update with read only
    assertEquals(403, SimpleHttp.doPost(getAccountUrl(null), httpClient).auth(viewToken.getToken()).json(new UserRepresentation()).asStatus());
}
Also used : TokenUtil(org.keycloak.testsuite.util.TokenUtil) UserRepresentation(org.keycloak.representations.account.UserRepresentation) Test(org.junit.Test) AbstractAuthenticationTest(org.keycloak.testsuite.admin.authentication.AbstractAuthenticationTest)

Example 10 with UserRepresentation

use of org.keycloak.representations.account.UserRepresentation in project keycloak by keycloak.

the class AccountRestService method account.

/**
 * Get account information.
 *
 * @return
 */
@Path("/")
@GET
@Produces(MediaType.APPLICATION_JSON)
@NoCache
public UserRepresentation account(@QueryParam("userProfileMetadata") final Boolean userProfileMetadata) {
    auth.requireOneOf(AccountRoles.MANAGE_ACCOUNT, AccountRoles.VIEW_PROFILE);
    UserModel user = auth.getUser();
    UserRepresentation rep = new UserRepresentation();
    rep.setId(user.getId());
    rep.setUsername(user.getUsername());
    rep.setFirstName(user.getFirstName());
    rep.setLastName(user.getLastName());
    rep.setEmail(user.getEmail());
    rep.setEmailVerified(user.isEmailVerified());
    UserProfileProvider provider = session.getProvider(UserProfileProvider.class);
    UserProfile profile = provider.create(UserProfileContext.ACCOUNT, user);
    rep.setAttributes(profile.getAttributes().getReadable(false));
    if (userProfileMetadata == null || userProfileMetadata.booleanValue())
        rep.setUserProfileMetadata(createUserProfileMetadata(profile));
    return rep;
}
Also used : UserModel(org.keycloak.models.UserModel) UserProfile(org.keycloak.userprofile.UserProfile) UserProfileProvider(org.keycloak.userprofile.UserProfileProvider) UserRepresentation(org.keycloak.representations.account.UserRepresentation) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) NoCache(org.jboss.resteasy.annotations.cache.NoCache)

Aggregations

UserRepresentation (org.keycloak.representations.account.UserRepresentation)21 Test (org.junit.Test)18 AbstractAuthenticationTest (org.keycloak.testsuite.admin.authentication.AbstractAuthenticationTest)11 RealmRepresentation (org.keycloak.representations.idm.RealmRepresentation)9 SimpleHttp (org.keycloak.broker.provider.util.SimpleHttp)6 HashMap (java.util.HashMap)5 List (java.util.List)5 UserProfileAttributeMetadata (org.keycloak.representations.account.UserProfileAttributeMetadata)3 VerifyProfileTest (org.keycloak.testsuite.forms.VerifyProfileTest)3 UserResource (org.keycloak.admin.client.resource.UserResource)2 RealmModel (org.keycloak.models.RealmModel)2 ArrayList (java.util.ArrayList)1 BadRequestException (javax.ws.rs.BadRequestException)1 GET (javax.ws.rs.GET)1 Path (javax.ws.rs.Path)1 Produces (javax.ws.rs.Produces)1 NoCache (org.jboss.resteasy.annotations.cache.NoCache)1 UserModel (org.keycloak.models.UserModel)1 TokenUtil (org.keycloak.testsuite.util.TokenUtil)1 UserProfile (org.keycloak.userprofile.UserProfile)1