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