1/*2 * Licensed to the Apache Software Foundation (ASF) under one or more3 * contributor license agreements. See the NOTICE file distributed with4 * this work for additional information regarding copyright ownership.5 * The ASF licenses this file to You under the Apache License, Version 2.06 * (the "License"); you may not use this file except in compliance with7 * the License. You may obtain a copy of the License at8*9* http://www.apache.org/licenses/LICENSE-2.010*11* Unless required by applicable law or agreed to in writing, software12* distributed under the License is distributed on an "AS IS" BASIS,13* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.14* See the License for the specific language governing permissions and15* limitations under the License.16*/17packageorg.apache.jetspeed.security.spi.impl;
1819import java.sql.Timestamp;
20import java.util.Date;
2122import org.apache.jetspeed.security.AlgorithmUpgradePasswordEncodingService;
23import org.apache.jetspeed.security.SecurityException;
24import org.apache.jetspeed.security.om.InternalCredential;
25import org.apache.jetspeed.security.spi.PasswordCredentialProvider;
2627/***28 * <p>29 * Encodes (encrypts) an {@link InternalCredential} password using the configured {@link PasswordCredentialProvider#getEncoder() encoder}30 * if it is loaded unencoded from the persistent store.</p>31 * <p>32 * This interceptor is useful when credentials need to be preset in the persistent store (like through scripts) or33 * migrated unencoded from a different storage.</p>34 * 35 * @author <a href="mailto:ate@douma.nu">Ate Douma</a>36 * @version $Id$37 */38publicclassEncodePasswordOnFirstLoadInterceptorextendsAbstractInternalPasswordCredentialInterceptorImpl39 {
40/***41 * @return true if now encoded42 * @see org.apache.jetspeed.security.spi.InternalPasswordCredentialInterceptor#afterLoad(org.apache.jetspeed.security.spi.PasswordCredentialProvider, java.lang.String, org.apache.jetspeed.security.om.InternalCredential)43 */44publicboolean afterLoad(PasswordCredentialProvider pcProvider, String userName, InternalCredential credential)
45 throws SecurityException
46 {
47boolean updated = false;
48if (!credential.isEncoded() && pcProvider.getEncoder() != null )
49 {
50 credential.setValue(pcProvider.getEncoder().encode(userName,credential.getValue()));
51 credential.setEncoded(true);
5253if ( pcProvider.getEncoder() instanceof AlgorithmUpgradePasswordEncodingService)
54 {
55// For the AlgorithmUpgradePBEPasswordService to be able to distinguise between56// old and new encoded passwords, it evaluates the last and previous authentication timestamps.57// With an automatic encoding (using the new encoding schema) the last authentication must be58// set to null (as the user hasn't been authenticated yet again, which leaves the previous59// authentication timestamp for indicating when the (new) encoding took place.60 credential.setPreviousAuthenticationDate(new Timestamp(new Date().getTime()));
61 credential.setLastAuthenticationDate(null);
62 }
63 updated = true;
64 }
65return updated;
66 }
67 }