@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(); } }
private void initializeCacheFromServer() { logger.debug("Attempting to initialize User cache from server."); userServiceCaller.call(new RemoteCallback<User>() { @Override public void callback(final User user) { logger.debug("Response received. Initializing user to " + String.valueOf(user)); setCachedUser(user); InitVotes.voteFor(SecurityContext.class); } }, new ErrorCallback<Object>() { @Override public boolean error(final Object message, final Throwable throwable) { logger.warn("Error received while attempting to populate cache: " + throwable.getMessage()); InitVotes.voteFor(SecurityContext.class); return false; } }).getUser(); }
@Override public boolean isLoggedIn() { return keycloakIsLoggedIn() || wrappedAuthService.isLoggedIn(); }
@Produces @RequestScoped public User getIdentity() { try { return authenticationService.getUser(); } catch (final IllegalStateException ex) { return new UserImpl("system"); } } }
@Override public User getUser() { if (keycloakIsLoggedIn()) { return getKeycloakUser(); } else if (wrappedAuthService.isLoggedIn()) { return wrappedAuthService.getUser(); } else { return User.ANONYMOUS; } }
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 void initializeCacheFromServer() { logger.debug("Attempting to initialize User cache from server."); userServiceCaller.call(new RemoteCallback<User>() { @Override public void callback(final User user) { logger.debug("Response received. Initializing user to " + String.valueOf(user)); setCachedUser(user); InitVotes.voteFor(SecurityContext.class); } }, new ErrorCallback<Object>() { @Override public boolean error(final Object message, final Throwable throwable) { logger.warn("Error received while attempting to populate cache: " + throwable.getMessage()); InitVotes.voteFor(SecurityContext.class); return false; } }).getUser(); }
public Path buildPath(final String serviceType, final String relativePath) { return userServicesBackend.buildPath(authService.getUser().getIdentifier(), serviceType, relativePath); } }
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) { return userServicesBackend.buildPath(authService.getUser().getIdentifier(), serviceType); }
@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."); } }
@Override @Produces @Default @ApplicationScoped public SessionInfo getSessionInfo(AuthenticationService authenticationService) { return new SessionInfoImpl("dummy-id", authenticationService.getUser()); }
@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"); }
private User getUser() { try { return authenticationService.getUser(); } catch (final IllegalStateException ex) { return new UserImpl("system", asList(new RoleImpl("admin"))); } } }
private User getUser() { try { return authenticationService.getUser(); } catch (final IllegalStateException ex) { return new UserImpl("system", asList(new RoleImpl("admin"))); } } }
private SessionInfo getSessionInfo( final String sessionId ) { return new SafeSessionInfo( new SessionInfoImpl( sessionId, authenticationService.getUser() ) ); }
private SessionInfo getSessionInfo( final String sessionId ) { return new SafeSessionInfo( new SessionInfoImpl( sessionId, authenticationService.getUser() ) ); }