public class AuthenticationModuleTest { @Tested AuthenticationModule authentication; @Mocked AuthenticationServicePort authenticationService; @Mocked AuthenticationService port; @Test public void validateUser() { final String userName = "tester"; final String password = "12345"; authentication.setUserName(userName); authentication.setPassword(password); new Expectations() {{ port.login(userName, password); result = true; }}; boolean validated = authentication.validateUser(); assertTrue(validated); } }
private boolean authenticate(final HttpServletRequest req) { final String authHead = req.getHeader("Authorization"); if (authHead != null) { final int index = authHead.indexOf(' '); final String[] credentials = new String(Base64.decodeBase64(authHead.substring(index)), Charsets.UTF_8).split(":", -1); try { authenticationService.login(credentials[0], credentials[1]); return true; } catch (final FailedAuthenticationException e) { return false; } } return false; }
private boolean authenticate(final HttpServletRequest req) { final String authHead = req.getHeader("Authorization"); if (authHead != null) { final int index = authHead.indexOf(' '); final String[] credentials = new String(Base64.decodeBase64(authHead.substring(index)), Charsets.UTF_8).split(":", -1); try { authenticationService.login(credentials[0], credentials[1]); return true; } catch (final FailedAuthenticationException e) { return false; } } return false; }
@Override public User login(final String username, final String password) { if (!keycloakIsLoggedIn()) { return wrappedAuthService.login(username, password); } else { throw new AlreadyLoggedInException("Already logged in through Keycloak."); } }
@Test public void testEmptyPassword() throws Exception { String username = "fakeUser"; String password = ""; String authData = username + ":" + password; String authEncoded = Base64.encodeBase64String(authData.getBytes()); when(request.getHeader("Authorization")).thenReturn("Basic " + authEncoded); final BasicAuthSecurityFilter filter = new BasicAuthSecurityFilter(); filter.authenticationService = authenticationService; filter.doFilter(request, response, chain); verify(authenticationService, times(1)).login(username, password); }
@Test public void testEmptyPassword() throws Exception { String username = "fakeUser"; String password = ""; String authData = username + ":" + password; String authEncoded = Base64.encodeBase64String(authData.getBytes()); when(request.getHeader("Authorization")).thenReturn("Basic " + authEncoded); final BasicAuthSecurityFilter filter = new BasicAuthSecurityFilter(); filter.authenticationService = authenticationService; filter.doFilter(request, response, chain); verify(authenticationService, times(1)).login(username, password); }
@Test public void testCustomAuthenticatorBean() throws Exception { // this simulates the existence of a @IOServiceAuth AuthenticationService bean when(authenticationManagers.isUnsatisfied()).thenReturn(false); AuthenticationService mockAuthenticationService = mock(AuthenticationService.class); when(authenticationManagers.get()).thenReturn(mockAuthenticationService); setupBean.setup(); FileSystemAuthenticator authenticator = MockSecuredFilesystemProvider.LATEST_INSTANCE.authenticator; authenticator.authenticate("fake", "fake"); // make sure the call went to the one we provided verify(mockAuthenticationService).login("fake", "fake"); }
@Test public void testCustomAuthenticatorBean() throws Exception { // this simulates the existence of a @IOServiceAuth AuthenticationService bean when(authenticationManagers.isUnsatisfied()).thenReturn(false); AuthenticationService mockAuthenticationService = mock(AuthenticationService.class); when(authenticationManagers.get()).thenReturn(mockAuthenticationService); setupBean.setup(); FileSystemAuthenticator authenticator = MockSecuredFilesystemProvider.LATEST_INSTANCE.authenticator; authenticator.authenticate("fake", "fake"); // make sure the call went to the one we provided verify(mockAuthenticationService).login("fake", "fake"); }
sfp.setAuthenticator((username, password) -> { try { final User result = authenticationManager.login(username, password); if (result != null) {