use of org.apache.archiva.redback.users.UserManager in project archiva by apache.
the class ArchivaLockedAdminEnvironmentCheck method validateEnvironment.
/**
* This environment check will unlock system administrator accounts that are locked on the restart of the
* application when the environment checks are processed.
*
* @param violations
*/
@Override
public void validateEnvironment(List<String> violations) {
if (!checked) {
for (UserManager userManager : userManagers) {
if (userManager.isReadOnly()) {
continue;
}
List<String> roles = new ArrayList<>();
roles.add(RedbackRoleConstants.SYSTEM_ADMINISTRATOR_ROLE);
List<UserAssignment> systemAdminstrators;
try {
systemAdminstrators = rbacManager.getUserAssignmentsForRoles(roles);
for (UserAssignment userAssignment : systemAdminstrators) {
try {
User admin = userManager.findUser(userAssignment.getPrincipal());
if (admin.isLocked()) {
log.info("Unlocking system administrator: {}", admin.getUsername());
admin.setLocked(false);
userManager.updateUser(admin);
}
} catch (UserNotFoundException ne) {
log.warn("Dangling UserAssignment -> {}", userAssignment.getPrincipal());
} catch (UserManagerException e) {
log.warn("fail to find user {} for admin unlock check: {}", userAssignment.getPrincipal(), e.getMessage());
}
}
} catch (RbacManagerException e) {
log.warn("Exception when checking for locked admin user: {}", e.getMessage(), e);
}
checked = true;
}
}
}
use of org.apache.archiva.redback.users.UserManager in project archiva by apache.
the class ArchivaConfigurableUsersManager method findUser.
@Override
public User findUser(String username, boolean useCache) throws UserNotFoundException, UserManagerException {
User user = null;
if (useUsersCache() && useCache) {
user = usersCache.get(username);
if (user != null) {
return user;
}
}
Exception lastException = null;
for (UserManager userManager : userManagerPerId.values()) {
try {
user = userManager.findUser(username);
if (user != null) {
if (useUsersCache()) {
usersCache.put(username, user);
}
return user;
}
} catch (UserNotFoundException e) {
lastException = e;
} catch (Exception e) {
lastException = e;
}
}
if (user == null) {
if (lastException != null) {
if (lastException instanceof UserNotFoundException) {
throw (UserNotFoundException) lastException;
}
throw new UserManagerException(lastException.getMessage(), lastException);
}
}
return user;
}
use of org.apache.archiva.redback.users.UserManager in project archiva by apache.
the class ArchivaConfigurableUsersManager method initialize.
@PostConstruct
@Override
public void initialize() {
try {
List<String> userManagerImpls = redbackRuntimeConfigurationAdmin.getRedbackRuntimeConfiguration().getUserManagerImpls();
log.info("use userManagerImpls: '{}'", userManagerImpls);
userManagerPerId = new LinkedHashMap<>(userManagerImpls.size());
for (String id : userManagerImpls) {
UserManager userManagerImpl = applicationContext.getBean("userManager#" + id, UserManager.class);
setUserManagerImpl(userManagerImpl);
userManagerPerId.put(id, userManagerImpl);
}
this.usersCache.clear();
this.useUsersCache = redbackRuntimeConfigurationAdmin.getRedbackRuntimeConfiguration().isUseUsersCache();
} catch (RepositoryAdminException e) {
// revert to a default one ?
log.error(e.getMessage(), e);
throw new RuntimeException(e.getMessage(), e);
}
}
use of org.apache.archiva.redback.users.UserManager in project archiva by apache.
the class ArchivaUserManagerAuthenticator method authenticate.
@Override
public AuthenticationResult authenticate(AuthenticationDataSource ds) throws AuthenticationException, AccountLockedException, MustChangePasswordException {
boolean authenticationSuccess = false;
String username = null;
Exception resultException = null;
PasswordBasedAuthenticationDataSource source = (PasswordBasedAuthenticationDataSource) ds;
List<AuthenticationFailureCause> authnResultErrors = new ArrayList<>();
for (UserManager userManager : userManagers) {
try {
log.debug("Authenticate: {} with userManager: {}", source, userManager.getId());
User user = userManager.findUser(source.getUsername());
username = user.getUsername();
if (user.isLocked()) {
// throw new AccountLockedException( "Account " + source.getUsername() + " is locked.", user );
AccountLockedException e = new AccountLockedException("Account " + source.getUsername() + " is locked.", user);
log.warn("{}", e.getMessage());
resultException = e;
authnResultErrors.add(new AuthenticationFailureCause(AuthenticationConstants.AUTHN_LOCKED_USER_EXCEPTION, e.getMessage()));
}
if (user.isPasswordChangeRequired() && source.isEnforcePasswordChange()) {
// throw new MustChangePasswordException( "Password expired.", user );
MustChangePasswordException e = new MustChangePasswordException("Password expired.", user);
log.warn("{}", e.getMessage());
resultException = e;
authnResultErrors.add(new AuthenticationFailureCause(AuthenticationConstants.AUTHN_MUST_CHANGE_PASSWORD_EXCEPTION, e.getMessage()));
}
PasswordEncoder encoder = securityPolicy.getPasswordEncoder();
log.debug("PasswordEncoder: {}", encoder.getClass().getName());
boolean isPasswordValid = encoder.isPasswordValid(user.getEncodedPassword(), source.getPassword());
if (isPasswordValid) {
log.debug("User {} provided a valid password", source.getUsername());
try {
securityPolicy.extensionPasswordExpiration(user);
authenticationSuccess = true;
// REDBACK-151 do not make unnessesary updates to the user object
if (user.getCountFailedLoginAttempts() > 0) {
user.setCountFailedLoginAttempts(0);
if (!userManager.isReadOnly()) {
userManager.updateUser(user);
}
}
return new AuthenticationResult(true, source.getUsername(), null);
} catch (MustChangePasswordException e) {
user.setPasswordChangeRequired(true);
// throw e;
resultException = e;
authnResultErrors.add(new AuthenticationFailureCause(AuthenticationConstants.AUTHN_MUST_CHANGE_PASSWORD_EXCEPTION, e.getMessage()).user(user));
}
} else {
log.warn("Password is Invalid for user {} and userManager '{}'.", source.getUsername(), userManager.getId());
authnResultErrors.add(new AuthenticationFailureCause(AuthenticationConstants.AUTHN_NO_SUCH_USER, "Password is Invalid for user " + source.getUsername() + ".").user(user));
try {
securityPolicy.extensionExcessiveLoginAttempts(user);
} finally {
if (!userManager.isReadOnly()) {
userManager.updateUser(user);
}
}
// return new AuthenticationResult( false, source.getUsername(), null, authnResultExceptionsMap );
}
} catch (UserNotFoundException e) {
log.warn("Login for user {} and userManager {} failed. user not found.", source.getUsername(), userManager.getId());
resultException = e;
authnResultErrors.add(new AuthenticationFailureCause(AuthenticationConstants.AUTHN_NO_SUCH_USER, "Login for user " + source.getUsername() + " failed. user not found."));
} catch (Exception e) {
log.warn("Login for user {} and userManager {} failed, message: {}", source.getUsername(), userManager.getId(), e.getMessage());
e.printStackTrace();
resultException = e;
authnResultErrors.add(new AuthenticationFailureCause(AuthenticationConstants.AUTHN_RUNTIME_EXCEPTION, "Login for user " + source.getUsername() + " failed, message: " + e.getMessage()));
}
}
return new AuthenticationResult(authenticationSuccess, username, resultException, authnResultErrors);
}
use of org.apache.archiva.redback.users.UserManager in project archiva by apache.
the class ArchivaServletAuthenticatorTest method testIsAuthorizedUserHasWriteAccess.
@Test
public void testIsAuthorizedUserHasWriteAccess() throws Exception {
createUser(USER_ALPACA, "Al 'Archiva' Paca");
assignRepositoryManagerRole(USER_ALPACA, "corporate");
UserManager userManager = securitySystem.getUserManager();
User user = userManager.findUser(USER_ALPACA);
AuthenticationResult result = new AuthenticationResult(true, USER_ALPACA, null);
SecuritySession session = new DefaultSecuritySession(result, user);
boolean isAuthorized = servletAuth.isAuthorized(request, session, "corporate", ArchivaRoleConstants.OPERATION_REPOSITORY_UPLOAD);
assertTrue(isAuthorized);
restoreGuestInitialValues(USER_ALPACA);
}
Aggregations