View Javadoc

1   /*
2   jGuard is a security framework based on top of jaas (java authentication and authorization security).
3   it is written for web applications, to resolve simply, access control problems.
4   version $Name$
5   http://sourceforge.net/projects/jguard/
6   
7   Copyright (C) 2004  Charles GAY
8   
9   This library is free software; you can redistribute it and/or
10  modify it under the terms of the GNU Lesser General Public
11  License as published by the Free Software Foundation; either
12  version 2.1 of the License, or (at your option) any later version.
13  
14  This library is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  Lesser General Public License for more details.
18  
19  You should have received a copy of the GNU Lesser General Public
20  License along with this library; if not, write to the Free Software
21  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22  
23  
24  jGuard project home page:
25  http://sourceforge.net/projects/jguard/
26  
27  */
28  package net.sf.jguard.core.authentication.bindings;
29  
30  
31  import net.sf.jguard.core.CoreConstants;
32  import net.sf.jguard.core.authentication.AccessContext;
33  import net.sf.jguard.core.authentication.AuthenticationUtils;
34  
35  import javax.security.auth.login.Configuration;
36  
37  
38  public abstract class AbstractAuthenticationBindings implements AuthenticationBindings {
39  
40  
41      protected String authScheme = null;
42      private String scope;
43      protected AccessContext context;
44      protected AuthenticationBindingsFactory factory;
45  
46      public AbstractAuthenticationBindings(AuthenticationBindingsFactory factory, String authenticationScope, AccessContext context) {
47          super();
48          if (CoreConstants.JVM_SCOPE.equalsIgnoreCase(authenticationScope)) {
49              scope = CoreConstants.JVM_SCOPE;
50          } else {
51              scope = CoreConstants.LOCAL_SCOPE;
52          }
53          this.context = context;
54          this.factory = factory;
55  
56      }
57  
58  
59      /**
60       * grab the {@link AuthenticationUtils} from the session, or create a new one if no one exist.
61       *
62       * @return AuthenticationUtils
63       */
64      public AuthenticationUtils getAuthenticationUtils() {
65          AuthenticationUtils authenticationUtils = null;
66          if (this instanceof StatefulAuthenticationBindings) {
67              authenticationUtils = (AuthenticationUtils) ((StatefulAuthenticationBindings) this).getSessionAttribute(CoreConstants.AUTHN_UTILS);
68              //auth.getSubject() can return  null if the user in session has been deleted by authenticationManager
69          }
70  
71  
72          if (authenticationUtils == null) {
73              Configuration configuration = (Configuration) this.getApplicationAttribute(CoreConstants.JGUARD_CONFIGURATION);
74              if (CoreConstants.LOCAL_SCOPE.equals(scope) && configuration != null) {
75                  authenticationUtils = new AuthenticationUtils(configuration);
76              } else {
77                  authenticationUtils = new AuthenticationUtils();
78              }
79              if (this instanceof StatefulAuthenticationBindings) {
80                  ((StatefulAuthenticationBindings) this).setSessionAttribute(CoreConstants.AUTHN_UTILS, authenticationUtils);
81              }
82          }
83          return authenticationUtils;
84      }
85  
86  
87      public AccessContext getContext() {
88          return context;
89      }
90  
91      public AuthenticationBindingsFactory getAuthenticationBindingsFactory() {
92          return factory;
93      }
94  
95      /**
96       * @return the local
97       */
98      public String getScope() {
99          return scope;
100     }
101 
102 }