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.slf4j.Logger;
022    import org.slf4j.LoggerFactory;
023    
024    import org.apache.shiro.authc.AuthenticationException;
025    import org.apache.shiro.authc.AuthenticationInfo;
026    import org.apache.shiro.authc.AuthenticationToken;
027    import org.apache.shiro.authc.UnknownAccountException;
028    import org.apache.shiro.realm.Realm;
029    
030    
031    /**
032     * <tt>AuthenticationStrategy</tt> implementation that requires <em>all</em> configured realms to
033     * <b>successfully</b> process the submitted <tt>AuthenticationToken</tt> during the log-in attempt.
034     * <p/>
035     * <p>If one or more realms do not support the submitted token, or one or more are unable to acquire
036     * <tt>AuthenticationInfo</tt> for the token, this implementation will immediately fail the log-in attempt for the
037     * associated subject (user).
038     *
039     * @author Les Hazlewood
040     * @since 0.2
041     */
042    public class AllSuccessfulStrategy extends AbstractAuthenticationStrategy {
043    
044        /** Private class log instance. */
045        private static final Logger log = LoggerFactory.getLogger(AllSuccessfulStrategy.class);
046    
047        /**
048         * Because all realms in this strategy must complete successfully, this implementation ensures that the given
049         * <code>Realm</code> {@link org.apache.shiro.realm.Realm#supports(org.apache.shiro.authc.AuthenticationToken) supports} the given
050         * <code>token</code> argument.  If it does not, this method throws an
051         * {@link UnsupportedTokenException UnsupportedTokenException} to end the authentication
052         * process immediately. If the realm does support the token, the <code>info</code> argument is returned immediately.
053         */
054        public AuthenticationInfo beforeAttempt(Realm realm, AuthenticationToken token, AuthenticationInfo info) throws AuthenticationException {
055            if (!realm.supports(token)) {
056                String msg = "Realm [" + realm + "] of type [" + realm.getClass().getName() + "] does not support " +
057                        " the submitted AuthenticationToken [" + token + "].  The [" + getClass().getName() +
058                        "] implementation requires all configured realm(s) to support and be able to process the submitted " +
059                        "AuthenticationToken.";
060                throw new UnsupportedTokenException(msg);
061            }
062    
063            return info;
064        }
065    
066        /**
067         * Merges the specified <code>info</code> into the <code>aggregate</code> argument and returns it (just as the
068         * parent implementation does), but additionally ensures the following:
069         * <ol>
070         * <li>if the <code>Throwable</code> argument is not <code>null</code>, re-throws it to immediately cancel the
071         * authentication process, since this strategy requires all realms to authenticate successfully.</li>
072         * <li>neither the <code>info</code> or <code>aggregate</code> argument is <code>null</code> to ensure that each
073         * realm did in fact authenticate successfully</li>
074         * </ol>
075         */
076        public AuthenticationInfo afterAttempt(Realm realm, AuthenticationToken token, AuthenticationInfo info, AuthenticationInfo aggregate, Throwable t)
077                throws AuthenticationException {
078            if (t != null) {
079                if (t instanceof AuthenticationException) {
080                    //propagate:
081                    throw ((AuthenticationException) t);
082                } else {
083                    String msg = "Unable to acquire account data from realm [" + realm + "].  The [" +
084                            getClass().getName() + " implementation requires all configured realm(s) to operate successfully " +
085                            "for a successful authentication.";
086                    throw new AuthenticationException(msg, t);
087                }
088            }
089            if (info == null) {
090                String msg = "Realm [" + realm + "] could not find any associated account data for the submitted " +
091                        "AuthenticationToken [" + token + "].  The [" + getClass().getName() + "] implementation requires " +
092                        "all configured realm(s) to acquire valid account data for a submitted token during the " +
093                        "log-in process.";
094                throw new UnknownAccountException(msg);
095            }
096    
097            log.debug("Account successfully authenticated using realm [{}]", realm);
098    
099            // If non-null account is returned, then the realm was able to authenticate the
100            // user - so merge the account with any accumulated before:
101            merge(info, aggregate);
102    
103            return aggregate;
104        }
105    }