blob: 9d1e2ce4c1827d38cc15bbd863def3c3817ee850 [file] [log] [blame]
<!DOCTYPE HTML>
<html lang="en">
<head>
<title>Source code</title>
<link rel="stylesheet" type="text/css" href="../../../../../../stylesheet.css" title="Style">
</head>
<body>
<main role="main">
<div class="sourceContainer">
<pre><span class="sourceLineNo">001</span><a id="line.1">/*</a>
<span class="sourceLineNo">002</span><a id="line.2"> * Licensed to the Apache Software Foundation (ASF) under one</a>
<span class="sourceLineNo">003</span><a id="line.3"> * or more contributor license agreements. See the NOTICE file</a>
<span class="sourceLineNo">004</span><a id="line.4"> * distributed with this work for additional information</a>
<span class="sourceLineNo">005</span><a id="line.5"> * regarding copyright ownership. The ASF licenses this file</a>
<span class="sourceLineNo">006</span><a id="line.6"> * to you under the Apache License, Version 2.0 (the</a>
<span class="sourceLineNo">007</span><a id="line.7"> * "License"); you may not use this file except in compliance</a>
<span class="sourceLineNo">008</span><a id="line.8"> * with the License. You may obtain a copy of the License at</a>
<span class="sourceLineNo">009</span><a id="line.9"> *</a>
<span class="sourceLineNo">010</span><a id="line.10"> * http://www.apache.org/licenses/LICENSE-2.0</a>
<span class="sourceLineNo">011</span><a id="line.11"> *</a>
<span class="sourceLineNo">012</span><a id="line.12"> * Unless required by applicable law or agreed to in writing,</a>
<span class="sourceLineNo">013</span><a id="line.13"> * software distributed under the License is distributed on an</a>
<span class="sourceLineNo">014</span><a id="line.14"> * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY</a>
<span class="sourceLineNo">015</span><a id="line.15"> * KIND, either express or implied. See the License for the</a>
<span class="sourceLineNo">016</span><a id="line.16"> * specific language governing permissions and limitations</a>
<span class="sourceLineNo">017</span><a id="line.17"> * under the License.</a>
<span class="sourceLineNo">018</span><a id="line.18"> */</a>
<span class="sourceLineNo">019</span><a id="line.19">package org.apache.shiro.authc.pam;</a>
<span class="sourceLineNo">020</span><a id="line.20"></a>
<span class="sourceLineNo">021</span><a id="line.21">import org.apache.shiro.authc.AuthenticationException;</a>
<span class="sourceLineNo">022</span><a id="line.22">import org.apache.shiro.authc.AuthenticationInfo;</a>
<span class="sourceLineNo">023</span><a id="line.23">import org.apache.shiro.authc.AuthenticationToken;</a>
<span class="sourceLineNo">024</span><a id="line.24">import org.apache.shiro.subject.PrincipalCollection;</a>
<span class="sourceLineNo">025</span><a id="line.25"></a>
<span class="sourceLineNo">026</span><a id="line.26">/**</a>
<span class="sourceLineNo">027</span><a id="line.27"> * &lt;tt&gt;AuthenticationStrategy&lt;/tt&gt; implementation that requires &lt;em&gt;at least one&lt;/em&gt; configured realm to</a>
<span class="sourceLineNo">028</span><a id="line.28"> * successfully process the submitted &lt;tt&gt;AuthenticationToken&lt;/tt&gt; during the log-in attempt.</a>
<span class="sourceLineNo">029</span><a id="line.29"> * &lt;p/&gt;</a>
<span class="sourceLineNo">030</span><a id="line.30"> * &lt;p&gt;This means any number of configured realms do not have to support the submitted log-in token, or they may</a>
<span class="sourceLineNo">031</span><a id="line.31"> * be unable to acquire &lt;tt&gt;AuthenticationInfo&lt;/tt&gt; for the token, but as long as at least one can do both, this</a>
<span class="sourceLineNo">032</span><a id="line.32"> * Strategy implementation will allow the log-in process to be successful.</a>
<span class="sourceLineNo">033</span><a id="line.33"> * &lt;p/&gt;</a>
<span class="sourceLineNo">034</span><a id="line.34"> * &lt;p&gt;Note that this implementation will aggregate the account data from &lt;em&gt;all&lt;/em&gt; successfully consulted</a>
<span class="sourceLineNo">035</span><a id="line.35"> * realms during the authentication attempt. If you want only the account data from the first successfully</a>
<span class="sourceLineNo">036</span><a id="line.36"> * consulted realm and want to ignore all subsequent realms, use the</a>
<span class="sourceLineNo">037</span><a id="line.37"> * {@link FirstSuccessfulStrategy FirstSuccessfulAuthenticationStrategy} instead.</a>
<span class="sourceLineNo">038</span><a id="line.38"> *</a>
<span class="sourceLineNo">039</span><a id="line.39"> * @see FirstSuccessfulStrategy FirstSuccessfulAuthenticationStrategy</a>
<span class="sourceLineNo">040</span><a id="line.40"> * @since 0.2</a>
<span class="sourceLineNo">041</span><a id="line.41"> */</a>
<span class="sourceLineNo">042</span><a id="line.42">public class AtLeastOneSuccessfulStrategy extends AbstractAuthenticationStrategy {</a>
<span class="sourceLineNo">043</span><a id="line.43"></a>
<span class="sourceLineNo">044</span><a id="line.44"> private static boolean isEmpty(PrincipalCollection pc) {</a>
<span class="sourceLineNo">045</span><a id="line.45"> return pc == null || pc.isEmpty();</a>
<span class="sourceLineNo">046</span><a id="line.46"> }</a>
<span class="sourceLineNo">047</span><a id="line.47"></a>
<span class="sourceLineNo">048</span><a id="line.48"> /**</a>
<span class="sourceLineNo">049</span><a id="line.49"> * Ensures that the &lt;code&gt;aggregate&lt;/code&gt; method argument is not &lt;code&gt;null&lt;/code&gt; and</a>
<span class="sourceLineNo">050</span><a id="line.50"> * &lt;code&gt;aggregate.{@link org.apache.shiro.authc.AuthenticationInfo#getPrincipals() getPrincipals()}&lt;/code&gt;</a>
<span class="sourceLineNo">051</span><a id="line.51"> * is not &lt;code&gt;null&lt;/code&gt;, and if either is &lt;code&gt;null&lt;/code&gt;, throws an AuthenticationException to indicate</a>
<span class="sourceLineNo">052</span><a id="line.52"> * that none of the realms authenticated successfully.</a>
<span class="sourceLineNo">053</span><a id="line.53"> */</a>
<span class="sourceLineNo">054</span><a id="line.54"> public AuthenticationInfo afterAllAttempts(AuthenticationToken token, AuthenticationInfo aggregate) throws AuthenticationException {</a>
<span class="sourceLineNo">055</span><a id="line.55"> //we know if one or more were able to successfully authenticate if the aggregated account object does not</a>
<span class="sourceLineNo">056</span><a id="line.56"> //contain null or empty data:</a>
<span class="sourceLineNo">057</span><a id="line.57"> if (aggregate == null || isEmpty(aggregate.getPrincipals())) {</a>
<span class="sourceLineNo">058</span><a id="line.58"> throw new AuthenticationException("Authentication token of type [" + token.getClass() + "] " +</a>
<span class="sourceLineNo">059</span><a id="line.59"> "could not be authenticated by any configured realms. Please ensure that at least one realm can " +</a>
<span class="sourceLineNo">060</span><a id="line.60"> "authenticate these tokens.");</a>
<span class="sourceLineNo">061</span><a id="line.61"> }</a>
<span class="sourceLineNo">062</span><a id="line.62"></a>
<span class="sourceLineNo">063</span><a id="line.63"> return aggregate;</a>
<span class="sourceLineNo">064</span><a id="line.64"> }</a>
<span class="sourceLineNo">065</span><a id="line.65">}</a>
</pre>
</div>
</main>
</body>
</html>