@Override
public Subject authenticate(final String accountName, Object credentials) throws AuthenticationException {
final Account account = getAccount(accountName);
if(account == null) {
throw new AuthenticationException(AuthenticationException.ACCOUNT_NOT_FOUND, "Account '" + accountName + "' not found.");
}
if("SYSTEM".equals(accountName) || (!allowGuestAuthentication && "guest".equals(accountName))) {
throw new AuthenticationException(AuthenticationException.ACCOUNT_NOT_FOUND, "Account '" + accountName + "' can not be used.");
}
if(!account.isEnabled()) {
throw new AuthenticationException(AuthenticationException.ACCOUNT_LOCKED, "Account '" + accountName + "' is disabled.");
}
final Subject subject = new SubjectImpl((AccountImpl) account, credentials);
if(!subject.isAuthenticated()) {
throw new AuthenticationException(AuthenticationException.WRONG_PASSWORD, "Wrong password for user [" + accountName + "] ");
}
return subject;
}