Search in sources :

Example 26 with TimeDuration

use of password.pwm.util.java.TimeDuration in project pwm by pwm-project.

the class PwmEnvironment method waitForFileLock.

public void waitForFileLock() throws PwmUnrecoverableException {
    final int maxWaitSeconds = this.getFlags().contains(ApplicationFlag.CommandLineInstance) ? 1 : Integer.parseInt(getConfig().readAppProperty(AppProperty.APPLICATION_FILELOCK_WAIT_SECONDS));
    final Instant startTime = Instant.now();
    final TimeDuration attemptInterval = new TimeDuration(5021, TimeUnit.MILLISECONDS);
    while (!this.isFileLocked() && TimeDuration.fromCurrent(startTime).isShorterThan(maxWaitSeconds, TimeUnit.SECONDS)) {
        attemptFileLock();
        if (!isFileLocked()) {
            LOGGER.debug("can't establish application file lock after " + TimeDuration.fromCurrent(startTime).asCompactString() + ", will retry;");
            attemptInterval.pause();
        }
    }
    if (!isFileLocked()) {
        final String errorMsg = "unable to obtain application path file lock";
        final ErrorInformation errorInformation = new ErrorInformation(PwmError.ERROR_STARTUP_ERROR, errorMsg);
        throw new PwmUnrecoverableException(errorInformation);
    }
}
Also used : ErrorInformation(password.pwm.error.ErrorInformation) Instant(java.time.Instant) TimeDuration(password.pwm.util.java.TimeDuration) PwmUnrecoverableException(password.pwm.error.PwmUnrecoverableException)

Example 27 with TimeDuration

use of password.pwm.util.java.TimeDuration in project pwm by pwm-project.

the class CertificateChecker method checkCertificate.

public static void checkCertificate(final X509Certificate certificate, final long warnDurationMs) throws PwmOperationalException {
    if (certificate == null) {
        return;
    }
    try {
        certificate.checkValidity();
    } catch (CertificateException e) {
        final StringBuilder errorMsg = new StringBuilder();
        errorMsg.append("certificate for subject ");
        errorMsg.append(certificate.getSubjectDN().getName());
        errorMsg.append(" is not valid: ");
        errorMsg.append(e.getMessage());
        final ErrorInformation errorInformation = new ErrorInformation(PwmError.ERROR_CERTIFICATE_ERROR, errorMsg.toString(), new String[] { errorMsg.toString() });
        throw new PwmOperationalException(errorInformation);
    }
    final Date expireDate = certificate.getNotAfter();
    final TimeDuration durationUntilExpire = TimeDuration.fromCurrent(expireDate);
    if (durationUntilExpire.isShorterThan(warnDurationMs)) {
        final StringBuilder errorMsg = new StringBuilder();
        errorMsg.append("certificate for subject ");
        errorMsg.append(certificate.getSubjectDN().getName());
        errorMsg.append(" will expire on: ");
        errorMsg.append(JavaHelper.toIsoDate(expireDate));
        errorMsg.append(" (").append(durationUntilExpire.asCompactString()).append(" from now)");
        final ErrorInformation errorInformation = new ErrorInformation(PwmError.ERROR_CERTIFICATE_ERROR, errorMsg.toString(), new String[] { errorMsg.toString() });
        throw new PwmOperationalException(errorInformation);
    }
}
Also used : ErrorInformation(password.pwm.error.ErrorInformation) CertificateException(java.security.cert.CertificateException) TimeDuration(password.pwm.util.java.TimeDuration) Date(java.util.Date) PwmOperationalException(password.pwm.error.PwmOperationalException)

Example 28 with TimeDuration

use of password.pwm.util.java.TimeDuration in project pwm by pwm-project.

the class HelpdeskVerificationStateBean method purgeOldRecords.

void purgeOldRecords() {
    for (final Iterator<HelpdeskValidationRecord> iterator = records.iterator(); iterator.hasNext(); ) {
        final HelpdeskValidationRecord record = iterator.next();
        final Date timestamp = record.getTimestamp();
        final TimeDuration age = TimeDuration.fromCurrent(timestamp);
        if (age.isLongerThan(maximumAge)) {
            iterator.remove();
        }
    }
}
Also used : TimeDuration(password.pwm.util.java.TimeDuration) Date(java.util.Date)

Example 29 with TimeDuration

use of password.pwm.util.java.TimeDuration in project pwm by pwm-project.

the class PeopleSearchDataReader method makeSearchResultsImpl.

private SearchResultBean makeSearchResultsImpl(final PwmRequest pwmRequest, final String username, final boolean includeDisplayName) throws ChaiUnavailableException, PwmUnrecoverableException {
    final Instant startTime = Instant.now();
    if (username == null || username.length() < 1) {
        return new SearchResultBean();
    }
    final boolean useProxy = useProxy();
    final UserSearchEngine userSearchEngine = pwmRequest.getPwmApplication().getUserSearchEngine();
    final SearchConfiguration searchConfiguration;
    {
        final SearchConfiguration.SearchConfigurationBuilder builder = SearchConfiguration.builder();
        builder.contexts(pwmRequest.getConfig().readSettingAsStringArray(PwmSetting.PEOPLE_SEARCH_SEARCH_BASE));
        builder.enableContextValidation(false);
        builder.username(username);
        builder.enableValueEscaping(false);
        builder.filter(getSearchFilter(pwmRequest.getConfig()));
        builder.enableSplitWhitespace(true);
        if (!useProxy) {
            builder.ldapProfile(pwmRequest.getPwmSession().getUserInfo().getUserIdentity().getLdapProfileID());
            builder.chaiProvider(pwmRequest.getPwmSession().getSessionManager().getChaiProvider());
        }
        searchConfiguration = builder.build();
    }
    final UserSearchResults results;
    final boolean sizeExceeded;
    try {
        final List<FormConfiguration> searchForm = pwmRequest.getConfig().readSettingAsForm(PwmSetting.PEOPLE_SEARCH_RESULT_FORM);
        final int maxResults = (int) pwmRequest.getConfig().readSettingAsLong(PwmSetting.PEOPLE_SEARCH_RESULT_LIMIT);
        final Locale locale = pwmRequest.getLocale();
        results = userSearchEngine.performMultiUserSearchFromForm(locale, searchConfiguration, maxResults, searchForm, pwmRequest.getSessionLabel());
        sizeExceeded = results.isSizeExceeded();
    } catch (PwmOperationalException e) {
        final ErrorInformation errorInformation = e.getErrorInformation();
        LOGGER.error(pwmRequest.getSessionLabel(), errorInformation.toDebugStr());
        throw new PwmUnrecoverableException(errorInformation);
    }
    final List<Map<String, Object>> resultOutput = new ArrayList<>(results.resultsAsJsonOutput(pwmRequest.getPwmApplication(), null));
    if (includeDisplayName) {
        for (final Map<String, Object> map : resultOutput) {
            final String userKey = (String) map.get("userKey");
            if (userKey != null) {
                final UserIdentity userIdentity = UserIdentity.fromKey(userKey, pwmRequest.getPwmApplication());
                final String displayValue = figureDisplaynameValue(pwmRequest, userIdentity);
                map.put("_displayName", displayValue);
            }
        }
    }
    final TimeDuration searchDuration = TimeDuration.fromCurrent(startTime);
    LOGGER.trace(pwmRequest.getPwmSession(), "finished rest peoplesearch search in " + searchDuration.asCompactString() + " not using cache, size=" + results.getResults().size());
    final SearchResultBean searchResultBean = new SearchResultBean();
    searchResultBean.setSearchResults(resultOutput);
    searchResultBean.setSizeExceeded(sizeExceeded);
    final String aboutMessage = LocaleHelper.getLocalizedMessage(pwmRequest.getLocale(), Display.Display_SearchResultsInfo.getKey(), pwmRequest.getConfig(), Display.class, new String[] { String.valueOf(results.getResults().size()), searchDuration.asLongString(pwmRequest.getLocale()) });
    searchResultBean.setAboutResultMessage(aboutMessage);
    return searchResultBean;
}
Also used : Locale(java.util.Locale) Instant(java.time.Instant) UserSearchEngine(password.pwm.ldap.search.UserSearchEngine) UserSearchResults(password.pwm.ldap.search.UserSearchResults) UserIdentity(password.pwm.bean.UserIdentity) ArrayList(java.util.ArrayList) SearchConfiguration(password.pwm.ldap.search.SearchConfiguration) PwmUnrecoverableException(password.pwm.error.PwmUnrecoverableException) PwmOperationalException(password.pwm.error.PwmOperationalException) ErrorInformation(password.pwm.error.ErrorInformation) FormConfiguration(password.pwm.config.value.data.FormConfiguration) TimeDuration(password.pwm.util.java.TimeDuration) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) TreeMap(java.util.TreeMap)

Example 30 with TimeDuration

use of password.pwm.util.java.TimeDuration in project pwm by pwm-project.

the class UpdateProfileUtil method checkForTokenVerificationProgress.

static ProcessStatus checkForTokenVerificationProgress(final PwmRequest pwmRequest, final UpdateProfileBean updateProfileBean, final UpdateProfileProfile updateProfileProfile) throws PwmUnrecoverableException, ServletException, IOException {
    final Map<String, TokenDestinationItem.Type> requiredTokenValidations = determineTokenValidationsRequired(pwmRequest, updateProfileBean, updateProfileProfile);
    if (!requiredTokenValidations.isEmpty()) {
        final Set<String> remainingValidations = new HashSet<>(requiredTokenValidations.keySet());
        remainingValidations.removeAll(updateProfileBean.getCompletedTokenFields());
        if (!remainingValidations.isEmpty()) {
            if (StringUtil.isEmpty(updateProfileBean.getCurrentTokenField())) {
                updateProfileBean.setCurrentTokenField(remainingValidations.iterator().next());
                updateProfileBean.setTokenSent(false);
            }
            if (!updateProfileBean.isTokenSent()) {
                final TokenDestinationItem tokenDestinationItem = tokenDestinationItemForCurrentValidation(pwmRequest, updateProfileBean, updateProfileProfile);
                final TimeDuration tokenLifetime = tokenDestinationItem.getType() == TokenDestinationItem.Type.email ? updateProfileProfile.getTokenDurationEmail(pwmRequest.getConfig()) : updateProfileProfile.getTokenDurationSMS(pwmRequest.getConfig());
                TokenUtil.initializeAndSendToken(pwmRequest, TokenUtil.TokenInitAndSendRequest.builder().userInfo(pwmRequest.getPwmSession().getUserInfo()).tokenDestinationItem(tokenDestinationItem).emailToSend(PwmSetting.EMAIL_UPDATEPROFILE_VERIFICATION).tokenType(TokenType.UPDATE).smsToSend(PwmSetting.SMS_UPDATE_PROFILE_TOKEN_TEXT).tokenLifetime(tokenLifetime).build());
                updateProfileBean.setTokenSent(true);
            }
            forwardToEnterCode(pwmRequest, updateProfileProfile, updateProfileBean);
            return ProcessStatus.Halt;
        }
    }
    return ProcessStatus.Continue;
}
Also used : TokenType(password.pwm.svc.token.TokenType) TimeDuration(password.pwm.util.java.TimeDuration) TokenDestinationItem(password.pwm.bean.TokenDestinationItem) HashSet(java.util.HashSet)

Aggregations

TimeDuration (password.pwm.util.java.TimeDuration)75 Instant (java.time.Instant)28 PwmUnrecoverableException (password.pwm.error.PwmUnrecoverableException)22 ErrorInformation (password.pwm.error.ErrorInformation)19 PwmException (password.pwm.error.PwmException)14 ArrayList (java.util.ArrayList)12 LinkedHashMap (java.util.LinkedHashMap)12 IOException (java.io.IOException)9 Configuration (password.pwm.config.Configuration)8 PwmOperationalException (password.pwm.error.PwmOperationalException)8 ChaiUnavailableException (com.novell.ldapchai.exception.ChaiUnavailableException)7 Map (java.util.Map)7 UserIdentity (password.pwm.bean.UserIdentity)7 HashMap (java.util.HashMap)6 HashSet (java.util.HashSet)6 List (java.util.List)6 MacroMachine (password.pwm.util.macro.MacroMachine)6 BigDecimal (java.math.BigDecimal)5 Date (java.util.Date)5 Locale (java.util.Locale)5