@Override public User getUser() { return authService.getUser(); } }
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); } }
/** * Set the {@link KeycloakSecurityContext} used to generate the logged in Keycloak {@link User}. * * @param keycloakSecurityContext The context used to generate the logged in Keycloak {@link User}. */ void setSecurityContext(final KeycloakSecurityContext keycloakSecurityContext) { if (wrappedAuthService.isLoggedIn() && keycloakSecurityContext != null) { throw new AlreadyLoggedInException("Logged in as " + wrappedAuthService.getUser()); } this.keycloakSecurityContext = keycloakSecurityContext; keycloakUser = null; } }
@Override public void logout() { if (keycloakIsLoggedIn()) { keycloakLogout(); try { if (RpcContext.getMessage() != null) ((HttpServletRequest) RpcContext.getServletRequest()).logout(); } catch (ServletException e) { throw new AuthenticationException("An error occurred while attempting to log out of Keycloak."); } } else if (wrappedAuthService.isLoggedIn()) { wrappedAuthService.logout(); } }
@Override public boolean isLoggedIn() { return keycloakIsLoggedIn() || wrappedAuthService.isLoggedIn(); }
@Override public User getUser() { if (keycloakIsLoggedIn()) { return getKeycloakUser(); } else if (wrappedAuthService.isLoggedIn()) { return wrappedAuthService.getUser(); } else { return User.ANONYMOUS; } }
@Produces @RequestScoped public User getIdentity() { try { return authenticationService.getUser(); } catch (final IllegalStateException ex) { return new UserImpl("system"); } } }
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 @Produces @Default @ApplicationScoped public SessionInfo getSessionInfo(AuthenticationService authenticationService) { return new SessionInfoImpl("dummy-id", authenticationService.getUser()); }
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; }
public Path buildPath(final String serviceType, final String relativePath) { return userServicesBackend.buildPath(authService.getUser().getIdentifier(), serviceType, relativePath); } }
@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."); } }
public Path buildPath(final String serviceType) { return userServicesBackend.buildPath(authService.getUser().getIdentifier(), serviceType); }
@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); }
private User getUser() { try { return authenticationService.getUser(); } catch (final IllegalStateException ex) { return new UserImpl("system", asList(new RoleImpl("admin"))); } } }
@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); }
private User getUser() { try { return authenticationService.getUser(); } catch (final IllegalStateException ex) { return new UserImpl("system", asList(new RoleImpl("admin"))); } } }
@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"); }