001/*
002    Licensed to the Apache Software Foundation (ASF) under one
003    or more contributor license agreements.  See the NOTICE file
004    distributed with this work for additional information
005    regarding copyright ownership.  The ASF licenses this file
006    to you under the Apache License, Version 2.0 (the
007    "License"); you may not use this file except in compliance
008    with the License.  You may obtain a copy of the License at
009
010       http://www.apache.org/licenses/LICENSE-2.0
011
012    Unless required by applicable law or agreed to in writing,
013    software distributed under the License is distributed on an
014    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015    KIND, either express or implied.  See the License for the
016    specific language governing permissions and limitations
017    under the License.
018 */
019package org.apache.wiki.auth;
020
021import org.apache.logging.log4j.LogManager;
022import org.apache.logging.log4j.Logger;
023import org.apache.wiki.auth.user.UserProfile;
024import org.apache.wiki.util.TextUtil;
025
026import java.util.Date;
027import java.util.Properties;
028import java.util.concurrent.TimeUnit;
029
030
031/**
032 * Decides when a password is too old, as set by {@value #PROP_EXPIRY_DAYS}. It is off unless a number of days is configured. Only the
033 * profiles of the wiki's user database have a password that can expire; it is the date the password was last changed
034 * ({@link UserProfile#getPasswordChanged()}) that counts.
035 *
036 * @since 3.0.1
037 */
038public final class PasswordExpiry {
039
040    private static final Logger LOG = LogManager.getLogger( PasswordExpiry.class );
041
042    /** Property with the number of days after which a password expires. Zero, the default, means that passwords do not expire. */
043    public static final String PROP_EXPIRY_DAYS = "jspwiki.credentials.expiry.days";
044
045    private final long expiryMillis;
046
047    /**
048     * Reads {@value #PROP_EXPIRY_DAYS}. A value that is not a number, or is negative, is logged and passwords do not expire.
049     *
050     * @param props the wiki properties
051     */
052    public PasswordExpiry( final Properties props ) {
053        long millis = 0;
054        final String value = TextUtil.getStringProperty( props, PROP_EXPIRY_DAYS, "0" ).trim();
055        try {
056            final long days = Long.parseLong( value );
057            if( days < 0 ) {
058                LOG.error( "{}={} is negative; passwords do not expire", PROP_EXPIRY_DAYS, value );
059            } else {
060                millis = TimeUnit.DAYS.toMillis( days );
061            }
062        } catch( final NumberFormatException e ) {
063            LOG.error( "{}={} is not a number; passwords do not expire", PROP_EXPIRY_DAYS, value );
064        }
065        expiryMillis = millis;
066    }
067
068    /**
069     * Returns the number of days after which a password expires.
070     *
071     * @return the number of days, or 0 if passwords do not expire
072     */
073    public int getDays() {
074        return ( int )TimeUnit.MILLISECONDS.toDays( expiryMillis );
075    }
076
077    /**
078     * Tells if passwords expire.
079     *
080     * @return {@code true} if a number of days is configured
081     */
082    public boolean isEnabled() {
083        return expiryMillis > 0;
084    }
085
086    /**
087     * Tells if the password of a profile is older than the configured number of days. The password of a profile that does not know
088     * when it was changed does not expire.
089     *
090     * @param profile the profile
091     * @param now the current time
092     * @return {@code true} if passwords expire and the password of the profile has
093     */
094    public boolean isExpired( final UserProfile profile, final Date now ) {
095        final Date changed = profile.getPasswordChanged();
096        return isEnabled() && changed != null && now.getTime() - changed.getTime() > expiryMillis;
097    }
098
099}