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 module hunt.shiro.authc.pam.ModularRealmAuthenticator;
20 
21 import hunt.shiro.authc.pam.AtLeastOneSuccessfulStrategy;
22 import hunt.shiro.authc.pam.AuthenticationStrategy;
23 
24 
25 import hunt.shiro.authc.AbstractAuthenticator;
26 import hunt.shiro.authc.AuthenticationInfo;
27 import hunt.shiro.authc.AuthenticationToken;
28 import hunt.shiro.authc.LogoutAware;
29 
30 import hunt.shiro.Exceptions;
31 import hunt.shiro.realm.Realm;
32 import hunt.shiro.subject.PrincipalCollection;
33 import hunt.shiro.util.CollectionUtils;
34 
35 import hunt.Exceptions;
36 import hunt.collection;
37 import hunt.logging.Logger;
38 
39 import std.conv;
40 import std.string;
41 
42 
43 /**
44  * A {@code ModularRealmAuthenticator} delegates account lookups to a pluggable (modular) collection of
45  * {@link Realm}s.  This enables PAM (Pluggable Authentication Module) behavior in Shiro.
46  * In addition to authorization duties, a Shiro Realm can also be thought of a PAM 'module'.
47  * <p/>
48  * Using this Authenticator allows you to &quot;plug-in&quot; your own
49  * {@code Realm}s as you see fit.  Common realms are those based on accessing
50  * LDAP, relational databases, file systems, etc.
51  * <p/>
52  * If only one realm is configured (this is often the case for most applications), authentication success is naturally
53  * only dependent upon invoking this one Realm's
54  * {@link Realm#getAuthenticationInfo(hunt.shiro.authc.AuthenticationToken)} method.
55  * <p/>
56  * But if two or more realms are configured, PAM behavior is implemented by iterating over the collection of realms
57  * and interacting with each over the course of the authentication attempt.  As this is more complicated, this
58  * authenticator allows customized behavior for interpreting what happens when interacting with multiple realms - for
59  * example, you might require all realms to be successful during the attempt, or perhaps only at least one must be
60  * successful, or some other interpretation.  This customized behavior can be performed via the use of a
61  * {@link #setAuthenticationStrategy(AuthenticationStrategy) AuthenticationStrategy}, which
62  * you can inject as a property of this class.
63  * <p/>
64  * The strategy object provides callback methods that allow you to
65  * determine what constitutes a success or failure in a multi-realm (PAM) scenario.  And because this only makes sense
66  * in a multi-realm scenario, the strategy object is only utilized when more than one Realm is configured.
67  * <p/>
68  * As most multi-realm applications require at least one Realm authenticates successfully, the default
69  * implementation is the {@link AtLeastOneSuccessfulStrategy}.
70  *
71  * @see #setRealms
72  * @see AtLeastOneSuccessfulStrategy
73  * @see AllSuccessfulStrategy
74  * @see FirstSuccessfulStrategy
75  */
76 class ModularRealmAuthenticator : AbstractAuthenticator {
77 
78     /*--------------------------------------------
79     |             C O N S T A N T S             |
80     ============================================*/
81 
82 
83     /*--------------------------------------------
84     |    I N S T A N C E   V A R I A B L E S    |
85     ============================================*/
86     /**
87      * List of realms that will be iterated through when a user authenticates.
88      */
89     private Realm[] realms;
90 
91     /**
92      * The authentication strategy to use during authentication attempts, defaults to a
93      * {@link hunt.shiro.authc.pam.AtLeastOneSuccessfulStrategy} instance.
94      */
95     private AuthenticationStrategy authenticationStrategy;
96 
97     /*--------------------------------------------
98     |         C O N S T R U C T O R S           |
99     ============================================*/
100 
101     /**
102      * Default no-argument constructor which
103      * {@link #setAuthenticationStrategy(AuthenticationStrategy) enables}  an
104      * {@link hunt.shiro.authc.pam.AtLeastOneSuccessfulStrategy} by default.
105      */
106      this() {
107         this.authenticationStrategy = new AtLeastOneSuccessfulStrategy();
108     }
109 
110     /*--------------------------------------------
111     |  A C C E S S O R S / M O D I F I E R S    |
112     ============================================*/
113 
114     /**
115      * Sets all realms used by this Authenticator, providing PAM (Pluggable Authentication Module) configuration.
116      *
117      * @param realms the realms to consult during authentication attempts.
118      */
119      void setRealms(Realm[] realms) {
120         this.realms = realms;
121     }
122 
123     /**
124      * Returns the realm(s) used by this {@code Authenticator} during an authentication attempt.
125      *
126      * @return the realm(s) used by this {@code Authenticator} during an authentication attempt.
127      */
128     protected Realm[] getRealms() {
129         return this.realms;
130     }
131 
132     /**
133      * Returns the {@code AuthenticationStrategy} utilized by this modular authenticator during a multi-realm
134      * log-in attempt.  This object is only used when two or more Realms are configured.
135      * <p/>
136      * Unless overridden by
137      * the {@link #setAuthenticationStrategy(AuthenticationStrategy)} method, the default implementation
138      * is the {@link hunt.shiro.authc.pam.AtLeastOneSuccessfulStrategy}.
139      *
140      * @return the {@code AuthenticationStrategy} utilized by this modular authenticator during a log-in attempt.
141      */
142      AuthenticationStrategy getAuthenticationStrategy() {
143         return authenticationStrategy;
144     }
145 
146     /**
147      * Allows overriding the default {@code AuthenticationStrategy} utilized during multi-realm log-in attempts.
148      * This object is only used when two or more Realms are configured.
149      *
150      * @param authenticationStrategy the strategy implementation to use during log-in attempts.
151      */
152      void setAuthenticationStrategy(AuthenticationStrategy authenticationStrategy) {
153         this.authenticationStrategy = authenticationStrategy;
154     }
155 
156     /*--------------------------------------------
157     |               M E T H O D S               |
158     --------------------------------------------*/
159 
160     /**
161      * Used by the internal {@link #doAuthenticate} implementation to ensure that the {@code realms} property
162      * has been set.  The default implementation ensures the property is not null and not empty.
163      *
164      * @throws IllegalStateException if the {@code realms} property is configured incorrectly.
165      */
166 
167     protected void assertRealmsConfigured(){
168         Realm[] realms = getRealms();
169         if (realms.empty()) {
170             warningf("No realms have been configured!");
171             string msg = "Configuration error:  No realms have been configured!  One or more realms must be " ~
172                     "present to execute an authentication attempt.";
173             throw new IllegalStateException(msg);
174         }
175     }
176 
177     /**
178      * Performs the authentication attempt by interacting with the single configured realm, which is significantly
179      * simpler than performing multi-realm logic.
180      *
181      * @param realm the realm to consult for AuthenticationInfo.
182      * @param token the submitted AuthenticationToken representing the subject's (user's) log-in principals and credentials.
183      * @return the AuthenticationInfo associated with the user account corresponding to the specified {@code token}
184      */
185     protected AuthenticationInfo doSingleRealmAuthentication(Realm realm, AuthenticationToken token) {
186         if (!realm.supports(token)) {
187             string msg = "Realm [" ~ (cast(Object)realm).toString() ~ "] does not support authentication token [" ~
188                     (cast(Object)token).toString() ~ "].  Please ensure that the appropriate Realm implementation is " ~
189                     "configured correctly or that the realm accepts AuthenticationTokens of this type.";
190             throw new UnsupportedTokenException(msg);
191         }
192         AuthenticationInfo info = realm.getAuthenticationInfo(token);
193         if (info  is null) {
194             string msg = "Realm [" ~ (cast(Object)realm).toString() ~ "] was unable to find account data for the " ~
195                     "submitted AuthenticationToken [" ~ (cast(Object)token).toString() ~ "].";
196             throw new UnknownAccountException(msg);
197         }
198         return info;
199     }
200 
201     /**
202      * Performs the multi-realm authentication attempt by calling back to a {@link AuthenticationStrategy} object
203      * as each realm is consulted for {@code AuthenticationInfo} for the specified {@code token}.
204      *
205      * @param realms the multiple realms configured on this Authenticator instance.
206      * @param token  the submitted AuthenticationToken representing the subject's (user's) log-in principals and credentials.
207      * @return an aggregated AuthenticationInfo instance representing account data across all the successfully
208      *         consulted realms.
209      */
210     protected AuthenticationInfo doMultiRealmAuthentication(Realm[] realms, AuthenticationToken token) {
211 
212         AuthenticationStrategy strategy = getAuthenticationStrategy();
213         AuthenticationInfo aggregate = strategy.beforeAllAttempts(realms, token);
214 
215         version(HUNT_SHIRO_DEBUG) {
216             tracef("Iterating through %s realms for PAM authentication", realms.length);
217         }
218 
219         foreach(Realm realm ; realms) {
220             aggregate = strategy.beforeAttempt(realm, token, aggregate);
221 
222             if (realm.supports(token)) {
223                 version(HUNT_SHIRO_DEBUG) {
224                     tracef("Attempting to authenticate token [%s] using realm [%s]", token, realm);
225                 }
226 
227                 AuthenticationInfo info = null;
228                 Throwable t = null;
229                 try {
230                     info = realm.getAuthenticationInfo(token);
231                 } catch (Throwable throwable) {
232                     t = throwable;
233                     version(HUNT_DEBUG) {
234                         string msg = "Realm [" ~ (cast(Object)realm).toString() ~ 
235                             "] threw an exception during a multi-realm authentication attempt: %s";
236                         warningf(msg, t.msg);
237                     }
238                     
239                     version(HUNT_SHIRO_DEBUG) {
240                         warning(t);
241                     }
242                 }
243 
244                 aggregate = strategy.afterAttempt(realm, token, info, aggregate, t);
245 
246             } else {
247                 version(HUNT_SHIRO_DEBUG) {
248                     infof("Realm [%s] does not support token %s.  Skipping realm.", realm, token);
249                 }
250             }
251         }
252 
253         aggregate = strategy.afterAllAttempts(token, aggregate);
254 
255         return aggregate;
256     }
257 
258 
259     /**
260      * Attempts to authenticate the given token by iterating over the internal collection of
261      * {@link Realm}s.  For each realm, first the {@link Realm#supports(hunt.shiro.authc.AuthenticationToken)}
262      * method will be called to determine if the realm supports the {@code authenticationToken} method argument.
263      * <p/>
264      * If a realm does support
265      * the token, its {@link Realm#getAuthenticationInfo(hunt.shiro.authc.AuthenticationToken)}
266      * method will be called.  If the realm returns a non-null account, the token will be
267      * considered authenticated for that realm and the account data recorded.  If the realm returns {@code null},
268      * the next realm will be consulted.  If no realms support the token or all supporting realms return null,
269      * an {@link AuthenticationException} will be thrown to indicate that the user could not be authenticated.
270      * <p/>
271      * After all realms have been consulted, the information from each realm is aggregated into a single
272      * {@link AuthenticationInfo} object and returned.
273      *
274      * @param authenticationToken the token containing the authentication principal and credentials for the
275      *                            user being authenticated.
276      * @return account information attributed to the authenticated user.
277      * @throws IllegalStateException   if no realms have been configured at the time this method is invoked
278      * @throws AuthenticationException if the user could not be authenticated or the user is denied authentication
279      *                                 for the given principal and credentials.
280      */
281     override protected AuthenticationInfo doAuthenticate(AuthenticationToken authenticationToken){
282         assertRealmsConfigured();
283         Realm[] realms = getRealms();
284         if (realms.length == 1) {
285             return doSingleRealmAuthentication(realms[0], authenticationToken);
286         } else {
287             return doMultiRealmAuthentication(realms, authenticationToken);
288         }
289     }
290 
291     /**
292      * First calls <code>super.onLogout(principals)</code> to ensure a logout notification is issued, and for each
293      * wrapped {@code Realm} that implements the {@link LogoutAware LogoutAware} interface, calls
294      * <code>((LogoutAware)realm).onLogout(principals)</code> to allow each realm the opportunity to perform
295      * logout/cleanup operations during an user-logout.
296      * <p/>
297      * Shiro's Realm implementations all implement the {@code LogoutAware} interface by default and can be
298      * overridden for realm-specific logout logic.
299      *
300      * @param principals the application-specific Subject/user identifier.
301      */
302     override void onLogout(PrincipalCollection principals) {
303         super.onLogout(principals);
304         Realm[] realms = getRealms();
305         if (!realms.empty()) {
306             foreach(Realm realm ; realms) {
307                 LogoutAware realmCast = cast(LogoutAware) realm;
308                 if (realmCast !is null) {
309                     realmCast.onLogout(principals);
310                 }
311             }
312         }
313     }
314 }