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