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.mgt.AuthenticatingSecurityManager;
20 
21 import hunt.shiro.mgt.RealmSecurityManager;
22 
23 import hunt.shiro.Exceptions;
24 import hunt.shiro.authc.AuthenticationInfo;
25 import hunt.shiro.authc.AuthenticationToken;
26 import hunt.shiro.authc.Authenticator;
27 import hunt.shiro.authc.pam.ModularRealmAuthenticator;
28 import hunt.shiro.util.LifecycleUtils;
29 
30 import hunt.Exceptions;
31 
32 
33 /**
34  * Shiro support of a {@link SecurityManager} class hierarchy that delegates all
35  * authentication operations to a wrapped {@link Authenticator Authenticator} instance.  That is, this class
36  * implements all the <tt>Authenticator</tt> methods in the {@link SecurityManager SecurityManager}
37  * interface, but in reality, those methods are merely passthrough calls to the underlying 'real'
38  * <tt>Authenticator</tt> instance.
39  *
40  * <p>All other <tt>SecurityManager</tt> (authorization, session, etc) methods are left to be implemented by subclasses.
41  *
42  * <p>In keeping with the other classes in this hierarchy and Shiro's desire to minimize configuration whenever
43  * possible, suitable default instances for all dependencies are created upon instantiation.
44  *
45  */
46 abstract class AuthenticatingSecurityManager : RealmSecurityManager {
47 
48     /**
49      * The internal <code>Authenticator</code> delegate instance that this SecurityManager instance will use
50      * to perform all authentication operations.
51      */
52     private Authenticator authenticator;
53 
54     /**
55      * Default no-arg constructor that initializes its internal
56      * <code>authenticator</code> instance to a
57      * {@link hunt.shiro.authc.pam.ModularRealmAuthenticator ModularRealmAuthenticator}.
58      */
59      this() {
60         super();
61         this.authenticator = new ModularRealmAuthenticator();
62     }
63 
64     /**
65      * Returns the delegate <code>Authenticator</code> instance that this SecurityManager uses to perform all
66      * authentication operations.  Unless overridden by the
67      * {@link #setAuthenticator(hunt.shiro.authc.Authenticator) setAuthenticator}, the default instance is a
68      * {@link hunt.shiro.authc.pam.ModularRealmAuthenticator ModularRealmAuthenticator}.
69      *
70      * @return the delegate <code>Authenticator</code> instance that this SecurityManager uses to perform all
71      *         authentication operations.
72      */
73      Authenticator getAuthenticator() {
74         return authenticator;
75     }
76 
77     /**
78      * Sets the delegate <code>Authenticator</code> instance that this SecurityManager uses to perform all
79      * authentication operations.  Unless overridden by this method, the default instance is a
80      * {@link hunt.shiro.authc.pam.ModularRealmAuthenticator ModularRealmAuthenticator}.
81      *
82      * @param authenticator the delegate <code>Authenticator</code> instance that this SecurityManager will use to
83      *                      perform all authentication operations.
84      * @throws IllegalArgumentException if the argument is <code>null</code>.
85      */
86      void setAuthenticator(Authenticator authenticator){
87         if (authenticator  is null) {
88             string msg = "Authenticator argument cannot be null.";
89             throw new IllegalArgumentException(msg);
90         }
91         this.authenticator = authenticator;
92     }
93 
94     /**
95      * Passes on the {@link #getRealms() realms} to the internal delegate <code>Authenticator</code> instance so
96      * that it may use them during authentication attempts.
97      */
98     override protected void afterRealmsSet() {
99         super.afterRealmsSet();
100         ModularRealmAuthenticator authenticatorCast = cast(ModularRealmAuthenticator) this.authenticator;
101         if (authenticatorCast !is null) {
102             authenticatorCast.setRealms(getRealms());
103         }
104     }
105 
106     /**
107      * Delegates to the wrapped {@link hunt.shiro.authc.Authenticator Authenticator} for authentication.
108      */
109      AuthenticationInfo authenticate(AuthenticationToken token){
110         return this.authenticator.authenticate(token);
111     }
112 
113     override void destroy() {
114         LifecycleUtils.destroy(cast(Object)getAuthenticator());
115         this.authenticator = null;
116         super.destroy();
117     }
118 }