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());
}
}
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);
}
}
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());
}
}
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());
}
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;
}
Aggregations