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.DefaultSubjectFactory;
20 
21 import hunt.shiro.mgt.SubjectFactory;
22 import hunt.shiro.mgt.SecurityManager;
23 
24 import hunt.shiro.session.Session;
25 import hunt.shiro.subject.PrincipalCollection;
26 import hunt.shiro.subject.Subject;
27 import hunt.shiro.subject.SubjectContext;
28 import hunt.shiro.subject.support.DelegatingSubject;
29 
30 import hunt.logging.Logger;
31 
32 
33 /**
34  * Default {@link SubjectFactory SubjectFactory} implementation that creates {@link hunt.shiro.subject.support.DelegatingSubject DelegatingSubject}
35  * instances.
36  *
37  */
38 class DefaultSubjectFactory : SubjectFactory {
39 
40     this() {
41     }
42 
43      Subject createSubject(SubjectContext context) {
44         SecurityManager securityManager = context.resolveSecurityManager();
45         Session session = context.resolveSession();
46         bool sessionCreationEnabled = context.isSessionCreationEnabled();
47         PrincipalCollection principals = context.resolvePrincipals();
48         bool authenticated = context.resolveAuthenticated();
49         string host;
50 
51         try {
52             host = context.resolveHost();
53         } catch(Throwable t) {
54             warning(t.msg);
55             version(HUNT_AUTH_DEBUG) warning(t);
56         }
57 
58         version(HUNT_SHIRO_DEBUG) tracef("Creating a new subject: authenticated=%s, host=%s, ", authenticated, host);
59 
60         return new DelegatingSubject(principals, authenticated, host, session, sessionCreationEnabled, securityManager);
61     }
62 
63     /**
64      * deprecated("") since 1.2 - override {@link #createSubject(hunt.shiro.subject.SubjectContext)} directly if you
65      *             need to instantiate a custom {@link Subject} class.
66      */
67     deprecated("")
68     protected Subject newSubjectInstance(PrincipalCollection principals, bool authenticated, string host,
69                                          Session session, SecurityManager securityManager) {
70         return new DelegatingSubject(principals, authenticated, host, session, true, securityManager);
71     }
72 
73 }