Add rememberMe to a user session representation(#13408) (#13765)

Closes #13408
This commit is contained in:
Lex Cao 2022-08-23 21:28:52 +08:00 committed by GitHub
parent 254483bc5d
commit 6b1c64a1a9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 38 additions and 5 deletions

View file

@ -31,6 +31,7 @@ public class UserSessionRepresentation {
private String ipAddress;
private long start;
private long lastAccess;
private boolean rememberMe;
private Map<String, String> clients = new HashMap<>();
public String getId() {
@ -48,7 +49,7 @@ public class UserSessionRepresentation {
public void setUsername(String username) {
this.username = username;
}
public String getUserId() {
return userId;
}
@ -81,6 +82,14 @@ public class UserSessionRepresentation {
this.lastAccess = lastAccess;
}
public boolean isRememberMe() {
return rememberMe;
}
public void setRememberMe(boolean rememberMe) {
this.rememberMe = rememberMe;
}
public Map<String, String> getClients() {
return clients;
}

View file

@ -641,6 +641,7 @@ public class ModelToRepresentation {
rep.setUsername(session.getUser().getUsername());
rep.setUserId(session.getUser().getId());
rep.setIpAddress(session.getIpAddress());
rep.setRememberMe(session.isRememberMe());
for (AuthenticatedClientSessionModel clientSession : session.getAuthenticatedClientSessions().values()) {
ClientModel client = clientSession.getClient();
rep.getClients().put(client.getId(), client.getClientId());
@ -694,7 +695,7 @@ public class ModelToRepresentation {
rep.setNotBefore(clientModel.getNotBefore());
rep.setNodeReRegistrationTimeout(clientModel.getNodeReRegistrationTimeout());
rep.setClientAuthenticatorType(clientModel.getClientAuthenticatorType());
// adding the secret if non public or bearer only
if (clientModel.isBearerOnly() || clientModel.isPublicClient()) {
rep.setSecret(null);
@ -937,7 +938,7 @@ public class ModelToRepresentation {
public static <R extends AbstractPolicyRepresentation> R toRepresentation(Policy policy, AuthorizationProvider authorization, boolean genericRepresentation, boolean export) {
return toRepresentation(policy, authorization, genericRepresentation, export, false);
}
public static <R extends AbstractPolicyRepresentation> R toRepresentation(Policy policy, AuthorizationProvider authorization, boolean genericRepresentation, boolean export, boolean allFields) {
PolicyProviderFactory providerFactory = authorization.getProviderFactory(policy.getType());
R representation;
@ -962,7 +963,7 @@ public class ModelToRepresentation {
representation.setType(policy.getType());
representation.setDecisionStrategy(policy.getDecisionStrategy());
representation.setLogic(policy.getLogic());
if (allFields) {
representation.setResourcesData(policy.getResources().stream()
.map(resource -> toRepresentation(resource, policy.getResourceServer(), authorization, true))

View file

@ -24,17 +24,19 @@ import org.keycloak.admin.client.resource.ClientResource;
import org.keycloak.common.Profile;
import org.keycloak.events.admin.OperationType;
import org.keycloak.events.admin.ResourceType;
import org.keycloak.representations.idm.RealmRepresentation;
import org.keycloak.representations.idm.UserRepresentation;
import org.keycloak.representations.idm.UserSessionRepresentation;
import org.keycloak.testsuite.arquillian.annotation.DisableFeature;
import org.keycloak.testsuite.auth.page.AuthRealm;
import org.keycloak.testsuite.auth.page.account.AccountManagement;
import org.keycloak.testsuite.util.AdminEventPaths;
import java.util.List;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.keycloak.testsuite.auth.page.AuthRealm.TEST;
/**
@ -105,6 +107,27 @@ public class SessionTest extends AbstractClientTest {
assertNotNull(rep.getIpAddress());
assertNotNull(rep.getLastAccess());
assertNotNull(rep.getStart());
assertFalse(rep.isRememberMe());
testRealmAccountManagementPage.signOut();
}
@Test
public void testGetUserSessionsWithRememberMe() {
RealmRepresentation realm = adminClient.realm(TEST).toRepresentation();
realm.setRememberMe(true);
adminClient.realm(TEST).update(realm);
testRealmAccountManagementPage.navigateTo();
loginPage.form().rememberMe(true);
loginPage.form().login(testUser);
ClientResource account = findClientResourceById("account");
List<UserSessionRepresentation> sessions = account.getUserSessions(0, 5);
assertEquals(1, sessions.size());
UserSessionRepresentation rep = sessions.get(0);
assertTrue(rep.isRememberMe());
testRealmAccountManagementPage.signOut();
}