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.SecurityUtils;
20 
21 import hunt.shiro.mgt.SecurityManager;
22 import hunt.shiro.subject.Subject;
23 import hunt.shiro.util.ThreadContext;
24 
25 import hunt.shiro.Exceptions;
26 
27 import hunt.logging.Logger;
28 
29 import core.thread;
30 import std.format;
31 
32 /**
33  * Accesses the currently accessible {@code Subject} for the calling code depending on runtime environment.
34  *
35  */
36 struct SecurityUtils {
37     
38     private __gshared SecurityManager[string] securityManagers;
39 
40     /**
41      * Returns the currently accessible {@code Subject} available to the calling code depending on
42      * runtime environment.
43      * <p/>
44      * This method is provided as a way of obtaining a {@code Subject} without having to resort to
45      * implementation-specific methods.  It also allows the Shiro team to change the underlying implementation of
46      * this method in the future depending on requirements/updates without affecting your code that uses it.
47      *
48      * @return the currently accessible {@code Subject} accessible to the calling code.
49      * @throws IllegalStateException if no {@link Subject Subject} instance or
50      *                               {@link SecurityManager SecurityManager} instance is available with which to obtain
51      *                               a {@code Subject}, which which is considered an invalid application configuration
52      *                               - a Subject should <em>always</em> be available to the caller.
53      */
54     static Subject getSubject(string managerName) {
55         SecurityManager sm = getSecurityManager(managerName);
56         if(sm is null) {
57             string msg = format("No manager found for: %s. Create it now.", managerName);
58             warningf(msg);
59             throw new Exception(msg);
60         }
61 
62         Subject subject = ThreadContext.getSubject(managerName);
63         if (subject is null) {
64             version(HUNT_SHIRO_DEBUG) warningf("bind a subject for manager %s", managerName);
65             subject = (new SubjectBuilder(sm)).buildSubject();
66             ThreadContext.bind(managerName, subject);
67         }
68         return subject;
69     }
70 
71     static Subject newSubject(string managerName, string sessionId, string host = "") {
72         SecurityManager sm = getSecurityManager(managerName);
73         assert(sm !is null);
74         return new SubjectBuilder(sm).sessionId(sessionId).host(host).buildSubject();
75     }
76 
77     /**
78      * Sets a VM (static) singleton SecurityManager, specifically for transparent use in the
79      * {@link #getSubject() getSubject()} implementation.
80      * <p/>
81      * <b>This method call exists mainly for framework development support.  Application developers should rarely,
82      * if ever, need to call this method.</b>
83      * <p/>
84      * The Shiro development team prefers that SecurityManager instances are non-static application singletons
85      * and <em>not</em> VM static singletons.  Application singletons that do not use static memory require some sort
86      * of application configuration framework to maintain the application-wide SecurityManager instance for you
87      * (for example, Spring or EJB3 environments) such that the object reference does not need to be static.
88      * <p/>
89      * In these environments, Shiro acquires Subject data based on the currently executing Thread via its own
90      * framework integration code, and this is the preferred way to use Shiro.
91      * <p/>
92      * However in some environments, such as a standalone desktop application or Applets that do not use Spring or
93      * EJB or similar config frameworks, a VM-singleton might make more sense (although the former is still preferred).
94      * In these environments, setting the SecurityManager via this method will automatically enable the
95      * {@link #getSubject() getSubject()} call to function with little configuration.
96      * <p/>
97      * For example, in these environments, this will work:
98      * <pre>
99      * DefaultSecurityManager securityManager = new {@link hunt.shiro.mgt.DefaultSecurityManager DefaultSecurityManager}();
100      * securityManager.setRealms( ... ); //one or more Realms
101      * <b>SecurityUtils.setSecurityManager( securityManager );</b></pre>
102      * <p/>
103      * And then anywhere in the application code, the following call will return the application's Subject:
104      * <pre>
105      * Subject currentUser = SecurityUtils.getSubject();</pre>
106      *
107      * @param securityManager the securityManager instance to set as a VM static singleton.
108      */
109     static void setSecurityManager(string name, SecurityManager securityManager) {
110         securityManagers[name] = securityManager;
111     }
112 
113     /**
114      * Returns the SecurityManager accessible to the calling code.
115      * <p/>
116      * This implementation favors acquiring a thread-bound {@code SecurityManager} if it can find one.  If one is
117      * not available to the executing thread, it will attempt to use the static singleton if available (see the
118      * {@link #setSecurityManager setSecurityManager} method for more on the static singleton).
119      * <p/>
120      * If neither the thread-local or static singleton instances are available, this method
121      * {@code UnavailableSecurityManagerException} to indicate an error - a SecurityManager should always be accessible
122      * to calling code in an application. If it is not, it is likely due to a Shiro configuration problem.
123      *
124      * @return the SecurityManager accessible to the calling code.
125      * @throws UnavailableSecurityManagerException
126      *          if there is no {@code SecurityManager} instance available to the
127      *          calling code, which typically indicates an invalid application configuration.
128      */
129     static SecurityManager getSecurityManager(string name) {
130 
131         auto itemPtr = name in securityManagers;
132         if(itemPtr is null) {
133             warningf("No SecurityManager found for %s", name);
134             return null;
135         }
136         
137         return *itemPtr;
138     }
139 }