|
| 1 | +package com.github.bcgov.keycloak.testsuite.authenticators; |
| 2 | + |
| 3 | +import static org.mockito.Mockito.mock; |
| 4 | +import static org.mockito.Mockito.when; |
| 5 | +import static org.mockito.ArgumentMatchers.any; |
| 6 | +import static org.mockito.Mockito.times; |
| 7 | +import static org.mockito.Mockito.verify; |
| 8 | + |
| 9 | +import org.mockito.Mockito; |
| 10 | +import org.mockito.MockedStatic; |
| 11 | +import org.junit.jupiter.api.Test; |
| 12 | +import org.junit.jupiter.api.BeforeEach; |
| 13 | + |
| 14 | +import com.github.bcgov.keycloak.authenticators.UserSessionRemover; |
| 15 | +import org.keycloak.authentication.AuthenticationFlowContext; |
| 16 | +import org.keycloak.models.KeycloakSession; |
| 17 | +import org.keycloak.models.AuthenticatedClientSessionModel; |
| 18 | +import org.keycloak.models.RealmModel; |
| 19 | +import org.keycloak.services.managers.AuthenticationManager; |
| 20 | +import org.keycloak.models.ClientModel; |
| 21 | +import org.keycloak.sessions.AuthenticationSessionModel; |
| 22 | +import org.keycloak.models.UserSessionProvider; |
| 23 | +import org.keycloak.models.AuthenticatedClientSessionModel; |
| 24 | +import org.keycloak.models.UserSessionModel; |
| 25 | +import org.keycloak.models.KeycloakContext; |
| 26 | +import java.util.HashMap; |
| 27 | +import java.util.Map; |
| 28 | + |
| 29 | +public class UserSessionRemoverTest { |
| 30 | + private static final UserSessionRemover userSessionRemover = new UserSessionRemover(); |
| 31 | + |
| 32 | + private AuthenticationFlowContext context; |
| 33 | + private KeycloakSession session; |
| 34 | + private RealmModel realm; |
| 35 | + private AuthenticationSessionModel authSession; |
| 36 | + private UserSessionProvider userSessionProvider; |
| 37 | + private KeycloakSession keycloakSession; |
| 38 | + private ClientModel client; |
| 39 | + private KeycloakContext keycloakContext; |
| 40 | + private AuthenticationManager.AuthResult authResult; |
| 41 | + private UserSessionModel userSessionModel; |
| 42 | + private AuthenticatedClientSessionModel authenticatedClientSessionModel; |
| 43 | + |
| 44 | + @BeforeEach |
| 45 | + public void setup() { |
| 46 | + // Initialize mocks for necessary objects |
| 47 | + context = mock(AuthenticationFlowContext.class); |
| 48 | + realm = mock(RealmModel.class); |
| 49 | + authSession = mock(AuthenticationSessionModel.class); |
| 50 | + userSessionProvider = mock(UserSessionProvider.class); |
| 51 | + keycloakSession = mock(KeycloakSession.class); |
| 52 | + keycloakContext = mock(KeycloakContext.class); |
| 53 | + client = mock(ClientModel.class); |
| 54 | + authResult = mock(AuthenticationManager.AuthResult.class); |
| 55 | + userSessionModel = mock(UserSessionModel.class); |
| 56 | + authenticatedClientSessionModel = mock(AuthenticatedClientSessionModel.class); |
| 57 | + |
| 58 | + |
| 59 | + // Set up common behavior of the mocks |
| 60 | + when(context.getSession()).thenReturn(keycloakSession); |
| 61 | + when(context.getRealm()).thenReturn(realm); |
| 62 | + when(context.getAuthenticationSession()).thenReturn(authSession); |
| 63 | + when(keycloakSession.sessions()).thenReturn(userSessionProvider); |
| 64 | + when(context.getSession()).thenReturn(keycloakSession); |
| 65 | + when(keycloakSession.getContext()).thenReturn(keycloakContext); |
| 66 | + when(keycloakContext.getClient()).thenReturn(client); |
| 67 | + when(authResult.getSession()).thenReturn(userSessionModel); |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + public void testSkipClientSessionCheckWhenNullAuthResult() throws Exception { |
| 72 | + try (MockedStatic<AuthenticationManager> authenticationManager = Mockito.mockStatic(AuthenticationManager.class)) { |
| 73 | + authenticationManager.when(() -> AuthenticationManager.authenticateIdentityCookie( |
| 74 | + any(KeycloakSession.class), any(RealmModel.class), any(Boolean.class) |
| 75 | + )).thenReturn(null); |
| 76 | + userSessionRemover.authenticate(context); |
| 77 | + |
| 78 | + // Keycloak Session Context check skipped if no Auth Session |
| 79 | + verify(keycloakSession, times(0)).getContext(); |
| 80 | + verify(userSessionProvider, times(0)).removeUserSession(any(RealmModel.class), any(UserSessionModel.class)); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + public void testRemovesUserSessionsWhenMultipleClientSessionsExist() throws Exception { |
| 86 | + when(client.getId()).thenReturn("client1"); |
| 87 | + Map<String, AuthenticatedClientSessionModel> authenticatedClientSessions = new HashMap<>(); |
| 88 | + authenticatedClientSessions.put("client1", authenticatedClientSessionModel); |
| 89 | + authenticatedClientSessions.put("client2", authenticatedClientSessionModel); |
| 90 | + |
| 91 | + when(userSessionModel.getAuthenticatedClientSessions()).thenReturn(authenticatedClientSessions); |
| 92 | + |
| 93 | + try (MockedStatic<AuthenticationManager> authenticationManager = Mockito.mockStatic(AuthenticationManager.class)) { |
| 94 | + authenticationManager.when(() -> AuthenticationManager.authenticateIdentityCookie( |
| 95 | + any(KeycloakSession.class), any(RealmModel.class), any(Boolean.class) |
| 96 | + )).thenReturn(authResult); |
| 97 | + |
| 98 | + userSessionRemover.authenticate(context); |
| 99 | + |
| 100 | + verify(keycloakSession, times(1)).getContext(); |
| 101 | + verify(userSessionProvider, times(1)).removeUserSession(any(RealmModel.class), any(UserSessionModel.class)); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + @Test |
| 106 | + public void testRemovesUserSessionsWhenSingleDifferentClientSessionFound() throws Exception { |
| 107 | + when(client.getId()).thenReturn("client1"); |
| 108 | + Map<String, AuthenticatedClientSessionModel> authenticatedClientSessions = new HashMap<>(); |
| 109 | + authenticatedClientSessions.put("client2", authenticatedClientSessionModel); |
| 110 | + |
| 111 | + when(userSessionModel.getAuthenticatedClientSessions()).thenReturn(authenticatedClientSessions); |
| 112 | + |
| 113 | + try (MockedStatic<AuthenticationManager> authenticationManager = Mockito.mockStatic(AuthenticationManager.class)) { |
| 114 | + authenticationManager.when(() -> AuthenticationManager.authenticateIdentityCookie( |
| 115 | + any(KeycloakSession.class), any(RealmModel.class), any(Boolean.class) |
| 116 | + )).thenReturn(authResult); |
| 117 | + userSessionRemover.authenticate(context); |
| 118 | + |
| 119 | + verify(keycloakSession, times(1)).getContext(); |
| 120 | + verify(userSessionProvider, times(1)).removeUserSession(any(RealmModel.class), any(UserSessionModel.class)); |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + @Test |
| 125 | + public void testLeavesExistingSessionWhenOnlyAssociatedToAuthenticatingClient() throws Exception { |
| 126 | + when(client.getId()).thenReturn("client1"); |
| 127 | + Map<String, AuthenticatedClientSessionModel> authenticatedClientSessions = new HashMap<>(); |
| 128 | + authenticatedClientSessions.put("client1", authenticatedClientSessionModel); |
| 129 | + |
| 130 | + when(userSessionModel.getAuthenticatedClientSessions()).thenReturn(authenticatedClientSessions); |
| 131 | + |
| 132 | + try (MockedStatic<AuthenticationManager> authenticationManager = Mockito.mockStatic(AuthenticationManager.class)) { |
| 133 | + authenticationManager.when(() -> AuthenticationManager.authenticateIdentityCookie( |
| 134 | + any(KeycloakSession.class), any(RealmModel.class), any(Boolean.class) |
| 135 | + )).thenReturn(authResult); |
| 136 | + userSessionRemover.authenticate(context); |
| 137 | + |
| 138 | + // Verify the keycloak session context is invoked to check client sessions |
| 139 | + verify(keycloakSession, times(1)).getContext(); |
| 140 | + |
| 141 | + // Remove user session should be skipped |
| 142 | + verify(userSessionProvider, times(0)).removeUserSession(any(RealmModel.class), any(UserSessionModel.class)); |
| 143 | + } |
| 144 | + } |
| 145 | +} |
0 commit comments