Clover Coverage Report
Coverage timestamp: Sun Mar 23 2008 08:24:39 GMT
90   295   21   7.5
38   135   0.36   12
12     2.67  
1    
 
 
  MultiAuthzTokenParser       Line # 72 90 21 18.6% 0.18571429
 
No Tests
 
1    /*
2    * Copyright (c) 2006, University of Kent
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions are met:
7    *
8    * Redistributions of source code must retain the above copyright notice, this
9    * list of conditions and the following disclaimer.
10    *
11    * Redistributions in binary form must reproduce the above copyright notice,
12    * this list of conditions and the following disclaimer in the documentation
13    * and/or other materials provided with the distribution.
14    *
15    * 1. Neither the name of the University of Kent nor the names of its
16    * contributors may be used to endorse or promote products derived from this
17    * software without specific prior written permission.
18    *
19    * 2. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
20    * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21    * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22    * PURPOSE ARE DISCLAIMED.
23    *
24    * 3. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
25    * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26    * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27    * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28    * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29    * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30    * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31    * POSSIBILITY OF SUCH DAMAGE.
32    *
33    * 4. YOU AGREE THAT THE EXCLUSIONS IN PARAGRAPHS 2 AND 3 ABOVE ARE REASONABLE
34    * IN THE CIRCUMSTANCES. IN PARTICULAR, YOU ACKNOWLEDGE (1) THAT THIS
35    * SOFTWARE HAS BEEN MADE AVAILABLE TO YOU FREE OF CHARGE, (2) THAT THIS
36    * SOFTWARE IS NOT "PRODUCT" QUALITY, BUT HAS BEEN PRODUCED BY A RESEARCH
37    * GROUP WHO DESIRE TO MAKE THIS SOFTWARE FREELY AVAILABLE TO PEOPLE WHO WISH
38    * TO USE IT, AND (3) THAT BECAUSE THIS SOFTWARE IS NOT OF "PRODUCT" QUALITY
39    * IT IS INEVITABLE THAT THERE WILL BE BUGS AND ERRORS, AND POSSIBLY MORE
40    * SERIOUS FAULTS, IN THIS SOFTWARE.
41    *
42    * 5. This license is governed, except to the extent that local laws
43    * necessarily apply, by the laws of England and Wales.
44    */
45   
46    /*
47    * MultiAuthTokenParser.java
48    *
49    * Created on 07 December 2007, 14:18
50    *
51    * To change this template, choose Tools | Template Manager
52    * and open the template in the editor.
53    */
54   
55    package issrg.pba;
56   
57    import issrg.pba.rbac.RoleBasedAuthzTokenParser;
58    import issrg.pba.rbac.RoleHierarchyPolicy;
59    import issrg.pba.rbac.SignatureVerifier;
60    import issrg.pba.PbaException;
61    import issrg.pba.RawCredential;
62   
63    import java.util.Hashtable;
64    import java.util.Vector;
65    import java.util.Enumeration;
66   
67    import org.apache.log4j.*;
68    /**
69    *
70    * @author Linying Su
71    */
 
72    public class MultiAuthzTokenParser implements RoleBasedAuthzTokenParser {
73   
74    private static Logger logger = Logger.getLogger("issrg.pba.MultiAuthzTokenParser");
75    private RoleHierarchyPolicy roleHierarchy = null;
76    private SignatureVerifier SV = null;
77    private Hashtable map = new Hashtable();
78    private Vector registration = new Vector();
79   
80    /** the existing token parsers*/
81   
82    private static String[] tokenParsers = {
83    "issrg.pba.rbac.x509.RoleBasedACParser",
84    "issrg.pba.rbac.x509.RoleBasedACBase64Parser",
85    "issrg.shibboleth.ShibbolethAuthTokenParser",
86    "issrg.simplePERMIS.SimplePERMISTokenParser",
87    "issrg.utils.saml.SAMLAATokenParser",
88    "issrg.voms.VOMSSAMLAATokenParser"
89    };
90   
91   
92    /** Creates a new instance of MultiAuthTokenParser */
 
93  4 toggle public MultiAuthzTokenParser() {
94  28 for (int i=0; i<tokenParsers.length; i++) {
95  24 this.registration.add(tokenParsers[i]);
96  24 logger.debug("register "+tokenParsers[i]);
97    }
98  4 this.map.put("issrg.utils.repository.VirtualBase64Repository", "issrg.pba.rbac.x509.RoleBasedACBase64Parser");
99  4 logger.debug("issrg.utils.repository.VirtualBase64Repository <= issrg.pba.rbac.x509.RoleBasedACBase64Parser");
100  4 this.map.put("issrg.utils.repository.VirtualRepository", "issrg.pba.rbac.x509.RoleBasedACParser");
101  4 logger.debug("issrg.utils.repository.VirtualRepository <= issrg.pba.rbac.x509.RoleBasedACParser");
102  4 this.map.put("issrg.utils.repository.WebDAVRepository", "issrg.pba.rbac.x509.RoleBasedACParser");
103  4 logger.debug("issrg.utils.repository.WebDAVRepository <= issrg.pba.rbac.x509.RoleBasedACParser");
104  4 this.map.put("issrg.utils.repository.LDAPRepository", "issrg.pba.rbac.x509.RoleBasedACParser");
105  4 logger.debug("issrg.utils.repository.LDAPRepository <= issrg.pba.rbac.x509.RoleBasedACParser");
106  4 this.map.put("issrg.utils.repository.FileRepository", "issrg.pba.rbac.x509.RoleBasedACParser");
107  4 logger.debug("issrg.utils.repository.FileRepository <= issrg.pba.rbac.x509.RoleBasedACParser");
108  4 this.map.put("issrg.utils.repository.SAMLRepository", "issrg.utils.saml.SAMLAATokenParser");
109  4 logger.debug("issrg.utils.repository.SAMLRepository <= issrg.utils.saml.SAMLAATokenParser");
110  4 this.map.put("issrg.voms.VOMSSAMLRepository", "issrg.voms.VOMSSAMLAATokenParser");
111  4 logger.debug("issrg.voms.VOMSSAMLRepository <= issrg.voms.VOMSSAMLAATokenParser");
112    }
113   
114    /**
115    * This method sets the Authorisation Token Parsing Rules by directly
116    * specifying the Role Hierarchy to be used.
117    *
118    * @param rhpn - the RoleHierarchyPolicy to be used; if null, decoding the
119    * credentias will always fail
120    */
 
121  2 toggle public void setRoleHierarchy(RoleHierarchyPolicy rhpn){
122  2 this.roleHierarchy = rhpn;
123    }
124   
125   
126    /**
127    * This method returns the Authorisation Token Parsing Rules, as a Map with
128    * a single entry with the key issrg.pba.rbac.RoleHierarchyPolicy.class and
129    * the value being the RoleHierarchyPolicy used by this RoleBasedACParser.
130    *
131    * @return Map of rules; this implementation fills a single entry with the
132    * key issrg.pba.rbac.RoleHierarchyPolicy.class and the value being the
133    * RoleHierarchyPolicy; if the RoleHierarchyPolicy has not been set,
134    * the Map does not contain any entries
135    */
136   
 
137  0 toggle public java.util.Map getAuthTokenParsingRules(){
138  0 java.util.Map m = new java.util.Hashtable();
139  0 if (roleHierarchy!=null) m.put(issrg.pba.rbac.RoleHierarchyPolicy.class, roleHierarchy);
140  0 return m;
141    }
142   
143    /**
144    * This method sets the SignatureVerifier to be used to validate the X.509
145    * Attribute Certificates.
146    *
147    * @param signatureVerifier is the SignatureVerifier used to validate the
148    * ACs; if null, no signature verification will be performed (SHOULD NOT
149    * BE USED IN PRODUCTION SCENARIOS!)
150    */
 
151  2 toggle public void setSignatureVerifier(SignatureVerifier signatureVerifier){
152  2 this.SV = signatureVerifier;
153    }
154   
155    /**
156    * This method returns the SignatureVerifier used by this RoleBasedACParser.
157    *
158    * @return SignatureVerifier used by this RoleBasedACParser, or null, if
159    * no signature verification is being performed.
160    */
 
161  0 toggle public SignatureVerifier getSignatureVerifier(){
162  0 return SV;
163    }
164   
165    /**
166    * This method sets the Authorisation Token Parsing Rules. It should
167    * contain a issrg.pba.rbac.RoleHierarchyPolicy in the entry
168    * with the key issrg.pba.rbac.RoleHierarchyPolicy.class.
169    *
170    * @param m - the Map of rules with a RoleHierarchyPolicy inside
171    */
 
172  2 toggle public void setAuthTokenParsingRules(java.util.Map m){
173  2 setRoleHierarchy((RoleHierarchyPolicy) m.get(RoleHierarchyPolicy.class));
174    }
175   
176    /**
177    * This method decodes a given RawCredential object. It can not be null.
178    *
179    * @return the ParsedToken representing a Attribute Certificate
180    *
181    * @throws PbaException, if no Role Hierarchy Policy has been specified
182    * through the constructor or either of the methods, or if there was a
183    * problem when decoding the Authorisation Token
184    */
 
185  0 toggle public ParsedToken decode(Object token) throws PbaException {
186  0 logger.debug(this.getClass().getName()+".decode()");
187  0 if (token == null) return null;
188  0 if (this.roleHierarchy==null)
189  0 throw new PbaException("Failed to decode an object: no Role Hierarchy was specified");
190  0 if (!RawCredential.class.isAssignableFrom(token.getClass())) {
191  0 logger.debug("the token is not a RawCredential object");
192  0 throw new issrg.pba.PbaException("Failed to decode an object: it is not a RAWCredential");
193    }
194   
195  0 Object credential = ((RawCredential)token).getCredential();
196  0 String repositoryType = ((RawCredential)token).getType();
197  0 logger.debug("To find a token parser for "+repositoryType);
198  0 String parserType = this.getParserByType(repositoryType);
199  0 logger.debug("the parser is "+parserType);
200   
201  0 AuthzTokenParser authParser;
202   
203  0 try {
204  0 Class authTokenParser = Class.forName(parserType);
205  0 authParser = (AuthzTokenParser)authTokenParser.newInstance();
206  0 authParser.setAuthTokenParsingRules(this.getAuthTokenParsingRules());
207  0 authParser.setSignatureVerifier(this.getSignatureVerifier());
208    } catch (Exception e) {
209  0 throw new PbaException("unknow repository type :"+e);
210    }
211   
212  0 ParsedToken p = authParser.decode(credential);
213  0 return p;
214    }
215   
216    /**
217    * this method returns the default class name of a token parser.
218    * @params type is the class name of a repository from where a RawCredential comes.
219    */
220   
 
221  0 toggle private String getParserByType(String type) throws PbaException {
222  0 String parser = (String)this.map.get(type);
223  0 if (parser != null) return parser;
224  0 if (this.registered(type)) return type;
225  0 throw new PbaException("can not find a parser for "+type);
226    }
227   
228    /**
229    * A RawCredential can return the name of an auth token parser straightway.
230    * @param type is the class name of an auth token parser.
231    * @return true if the parser is defined in PERMIS; otherwise false.
232    */
233   
 
234  0 toggle private boolean registered(String type) {
235  0 for (int i=0; i<this.registration.size(); i++) {
236  0 if (type.equals(this.registration.get(i))) return true;
237    }
238  0 return false;
239    }
240   
241    /**
242    * this method is used to register an auth token parser.
243    * @param className is the class name of the auth token parser.
244    */
245   
 
246  0 toggle public void register(String className) {
247  0 String name = className.trim();
248  0 if (this.registration.isEmpty()) this.registration.add(name);
249    else {
250  0 boolean found = false;
251  0 for (int i=0; i<this.registration.size(); i++) {
252  0 String parser = (String)this.registration.get(i);
253  0 if (name.equals(parser)) found = true;
254  0 if (found) break;
255    }
256  0 if (found) return;
257  0 this.registration.add(name);
258    }
259    }
260   
261    /**
262    * this method is used to unregister an auth token parser.
263    * @param className is the class name of the auth token parser.
264    */
265   
 
266  0 toggle public void unregister(String className) {
267  0 String name = className.trim();
268  0 Vector keys = new Vector();
269  0 if (this.registration.isEmpty()) return;
270  0 else this.registration.remove(name);
271  0 Enumeration enumer = this.map.keys();
272  0 while (enumer.hasMoreElements()) {
273  0 String key = (String)enumer.nextElement();
274  0 String value = (String)this.map.get(key);
275  0 if (name.equals(value)) keys.add(key);
276    }
277  0 if (keys.isEmpty()) return;
278  0 String[] keyArry = new String[keys.size()];
279  0 keyArry = (String[])keys.toArray(keyArry);
280  0 for (int i=0; i<keyArry.length; i++) this.map.remove(keyArry[i]);
281    }
282   
283    /**
284    * this method is used to associate a repository with an auth token parser to .
285    * @param parser is the class name of the auth token parser.
286    * @param repos is the class name of the repository.
287    */
288   
 
289  0 toggle public void associate(String repository,String parser) {
290  0 String key = repository.trim();
291  0 String value = parser.trim();
292  0 this.map.put(key,value);
293    }
294    }
295