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  import java.util.Collection;
31  import java.util.HashMap;
32  import java.util.List;
33  import java.util.Map;
34  import javax.security.auth.callback.Callback;
35  import javax.security.auth.callback.CallbackHandler;
36  
37  import net.sf.jguard.core.authentication.callbacks.HookCallbackHandler;
38  import net.sf.jguard.core.authentication.schemes.AuthenticationSchemeHandler;
39  
40  /**
41   * encapsulate callbacks grabbed with the provided {@link AuthenticationBinding} {@link CallbackHandler}'s
42   * implementation, by some provided credentials. it permits to act as the user which own these credentials.
43   * It wraps AuthenticationBindingsFactory and CallbackHandler.
44   * @author <a href="mailto:diabolo512@users.sourceforge.net">Charles Gay</a>
45   */
46  public class ImpersonationAuthenticationBindings extends AuthenticationBindingsWrapper {
47      private ImpersonationAuthenticationBindingsFactory factory = null;
48      private List<Callback> callbacks = null;
49      private Map requestAttributes = null;
50      private Map applicationAttributes = null;
51          /**
52           * 
53           * @param authNBindings wrapped AuthenticationBindings
54           * @param cbks
55           */
56  	public ImpersonationAuthenticationBindings(AuthenticationBindings authNBindings,List<Callback> cbks){
57  		super(authNBindings);
58  		this.callbacks = cbks;
59          factory = new ImpersonationAuthenticationBindingsFactory(authNBindings.getAuthenticationBindingsFactory());
60          requestAttributes = new HashMap();
61          applicationAttributes = new HashMap();
62          
63  	}
64  
65      @Override
66      public Object getRequestAttribute(String key){
67          return requestAttributes.get(key);
68      }
69  
70      @Override
71      public void setRequestAttribute(String key,Object value){
72          requestAttributes.put(key, value);
73      }
74  
75      @Override
76      public void removeRequestAttribute(String key){
77          requestAttributes.remove(key);
78      }
79  
80  
81     @Override
82     public void setApplicationAttribute(String key,Object value){
83          applicationAttributes.put(key, value);
84     }
85     @Override
86     public Object getApplicationAttribute(String key){
87         Object value = applicationAttributes.get(key);
88         if(value==null){
89          return authNBindings.getApplicationAttribute(key);
90         }else{
91             return value;
92         }
93     }
94     @Override
95     public void removeApplicationAttribute(String key){
96          applicationAttributes.remove(key);
97     }
98     
99  
100 
101     @Override
102 	public CallbackHandler getCallbackHandler(){
103 		return new HookCallbackHandler(callbacks);
104 	}
105 
106 	public List<Callback> getCallbacks() {
107 		return callbacks;
108 	}
109 
110 	public void setCallbacks(List<Callback> callbacks) {
111 		this.callbacks = callbacks;
112 	}
113         
114     @Override
115     public AuthenticationBindingsFactory getAuthenticationBindingsFactory() {
116             return factory;
117     }
118 
119 
120     public void addAuthenticationSchemeHandlerToFactory(Collection<AuthenticationSchemeHandler> handlers){
121         factory.addAuthenticationSchemeHandler(handlers);
122     }
123 
124     
125 }