1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one 3 * or more contributor license agreements. See the NOTICE file 4 * distributed with this work for additional information 5 * regarding copyright ownership. The ASF licenses this file 6 * to you under the Apache License, Version 2.0 (the 7 * "License"); you may not use this file except in compliance 8 * with the License. You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, 13 * software distributed under the License is distributed on an 14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 * KIND, either express or implied. See the License for the 16 * specific language governing permissions and limitations 17 * under the License. 18 */ 19 package org.apache.shiro.authc.pam; 20 21 import org.apache.shiro.authc.*; 22 import org.apache.shiro.realm.Realm; 23 24 import java.util.Collection; 25 26 27 /** 28 * Abstract base implementation for Shiro's concrete <code>AuthenticationStrategy</code> 29 * implementations. 30 * 31 * @since 0.9 32 */ 33 public abstract class AbstractAuthenticationStrategy implements AuthenticationStrategy { 34 35 /** 36 * Simply returns <code>new {@link org.apache.shiro.authc.SimpleAuthenticationInfo SimpleAuthenticationInfo}();</code>, which supports 37 * aggregating account data across realms. 38 */ 39 public AuthenticationInfo beforeAllAttempts(Collection<? extends Realm> realms, AuthenticationToken token) throws AuthenticationException { 40 return new SimpleAuthenticationInfo(); 41 } 42 43 /** 44 * Simply returns the <code>aggregate</code> method argument, without modification. 45 */ 46 public AuthenticationInfo beforeAttempt(Realm realm, AuthenticationToken token, AuthenticationInfo aggregate) throws AuthenticationException { 47 return aggregate; 48 } 49 50 /** 51 * Base implementation that will aggregate the specified <code>singleRealmInfo</code> into the 52 * <code>aggregateInfo</code> and then returns the aggregate. Can be overridden by subclasses for custom behavior. 53 */ 54 public AuthenticationInfo afterAttempt(Realm realm, AuthenticationToken token, AuthenticationInfo singleRealmInfo, AuthenticationInfo aggregateInfo, Throwable t) throws AuthenticationException { 55 AuthenticationInfo info; 56 if (singleRealmInfo == null) { 57 info = aggregateInfo; 58 } else { 59 if (aggregateInfo == null) { 60 info = singleRealmInfo; 61 } else { 62 info = merge(singleRealmInfo, aggregateInfo); 63 } 64 } 65 66 return info; 67 } 68 69 /** 70 * Merges the specified <code>info</code> argument into the <code>aggregate</code> argument and then returns an 71 * aggregate for continued use throughout the login process. 72 * <p/> 73 * This implementation merely checks to see if the specified <code>aggregate</code> argument is an instance of 74 * {@link org.apache.shiro.authc.MergableAuthenticationInfo MergableAuthenticationInfo}, and if so, calls 75 * <code>aggregate.merge(info)</code> If it is <em>not</em> an instance of 76 * <code>MergableAuthenticationInfo</code>, an {@link IllegalArgumentException IllegalArgumentException} is thrown. 77 * Can be overridden by subclasses for custom merging behavior if implementing the 78 * {@link org.apache.shiro.authc.MergableAuthenticationInfo MergableAuthenticationInfo} is not desired for some reason. 79 */ 80 protected AuthenticationInfo merge(AuthenticationInfo info, AuthenticationInfo aggregate) { 81 if( aggregate instanceof MergableAuthenticationInfo ) { 82 ((MergableAuthenticationInfo)aggregate).merge(info); 83 return aggregate; 84 } else { 85 throw new IllegalArgumentException( "Attempt to merge authentication info from multiple realms, but aggregate " + 86 "AuthenticationInfo is not of type MergableAuthenticationInfo." ); 87 } 88 } 89 90 /** 91 * Simply returns the <code>aggregate</code> argument without modification. Can be overridden for custom behavior. 92 */ 93 public AuthenticationInfo afterAllAttempts(AuthenticationToken token, AuthenticationInfo aggregate) throws AuthenticationException { 94 return aggregate; 95 } 96 }