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 */
019 package org.apache.shiro.authc.pam;
020
021 import org.apache.shiro.authc.*;
022 import org.apache.shiro.realm.Realm;
023
024 import java.util.Collection;
025
026
027 /**
028 * Abstract base implementation for Shiro's concrete <code>AuthenticationStrategy</code>
029 * implementations.
030 *
031 * @author Jeremy Haile
032 * @author Les Hazlewood
033 * @since 0.9
034 */
035 public abstract class AbstractAuthenticationStrategy implements AuthenticationStrategy {
036
037 /**
038 * Simply returns <code>new {@link org.apache.shiro.authc.SimpleAuthenticationInfo SimpleAuthenticationInfo}();</code>, which supports
039 * aggregating account data across realms.
040 */
041 public AuthenticationInfo beforeAllAttempts(Collection<? extends Realm> realms, AuthenticationToken token) throws AuthenticationException {
042 return new SimpleAuthenticationInfo();
043 }
044
045 /**
046 * Simply returns the <code>aggregate</code> method argument, without modification.
047 */
048 public AuthenticationInfo beforeAttempt(Realm realm, AuthenticationToken token, AuthenticationInfo aggregate) throws AuthenticationException {
049 return aggregate;
050 }
051
052 /**
053 * Base implementation that will aggregate the specified <code>singleRealmInfo</code> into the
054 * <code>aggregateInfo</code> and then returns the aggregate. Can be overridden by subclasses for custom behavior.
055 */
056 public AuthenticationInfo afterAttempt(Realm realm, AuthenticationToken token, AuthenticationInfo singleRealmInfo, AuthenticationInfo aggregateInfo, Throwable t) throws AuthenticationException {
057 AuthenticationInfo info;
058 if (singleRealmInfo == null) {
059 info = aggregateInfo;
060 } else {
061 if (aggregateInfo == null) {
062 info = singleRealmInfo;
063 } else {
064 info = merge(singleRealmInfo, aggregateInfo);
065 }
066 }
067
068 return info;
069 }
070
071 /**
072 * Merges the specified <code>info</code> argument into the <code>aggregate</code> argument and then returns an
073 * aggregate for continued use throughout the login process.
074 * <p/>
075 * This implementation merely checks to see if the specified <code>aggregate</code> argument is an instance of
076 * {@link org.apache.shiro.authc.MergableAuthenticationInfo MergableAuthenticationInfo}, and if so, calls
077 * <code>aggregate.merge(info)</code> If it is <em>not</em> an instance of
078 * <code>MergableAuthenticationInfo</code>, an {@link IllegalArgumentException IllegalArgumentException} is thrown.
079 * Can be overridden by subclasses for custom merging behavior if implementing the
080 * {@link org.apache.shiro.authc.MergableAuthenticationInfo MergableAuthenticationInfo} is not desired for some reason.
081 */
082 protected AuthenticationInfo merge(AuthenticationInfo info, AuthenticationInfo aggregate) {
083 if( aggregate instanceof MergableAuthenticationInfo ) {
084 ((MergableAuthenticationInfo)aggregate).merge(info);
085 return aggregate;
086 } else {
087 throw new IllegalArgumentException( "Attempt to merge authentication info from multiple realms, but aggregate " +
088 "AuthenticationInfo is not of type MergableAuthenticationInfo." );
089 }
090 }
091
092 /**
093 * Simply returns the <code>aggregate</code> argument without modification. Can be overridden for custom behavior.
094 */
095 public AuthenticationInfo afterAllAttempts(AuthenticationToken token, AuthenticationInfo aggregate) throws AuthenticationException {
096 return aggregate;
097 }
098 }