Clover Coverage Report
Coverage timestamp: Sun Mar 23 2008 08:24:39 GMT
111   211   31   22.2
64   133   0.32   5
5     7  
1    
 
 
  SAMLURLHandler       Line # 71 111 31 4.4% 0.044444446
 
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    * SAMLURLHandler.java
47    *
48    * Created on 06 December 2007, 21:43
49    *
50    * To change this template, choose Tools | Template Manager
51    * and open the template in the editor.
52    */
53   
54    package issrg.pba.rbac;
55   
56    import issrg.utils.repository.AttributeRepository;
57    import issrg.saml.SAMLMetaInfo;
58    import issrg.saml.SAMLSecurity;
59    import issrg.security.SecurityException;
60    import issrg.utils.repository.*;
61    import issrg.utils.StringTokeniser;
62   
63    import java.net.*;
64    import java.util.Hashtable;
65   
66    import org.apache.log4j.Logger;
67    /**
68    *
69    * @author Linying Su
70    */
 
71    public class SAMLURLHandler extends URLHandler {
72   
73    private static Logger logger = Logger.getLogger(SAMLURLHandler.class);
74    public String configFile = null;
75    private String protocol = null;
76    private Hashtable params = new Hashtable();
77   
78    /** Creates a new instance of SAMLURLHandler */
79   
 
80  2 toggle public SAMLURLHandler(String protocolIn,String config) throws BadURLException {
81  2 this.protocol = protocolIn;
82  2 this.configFile = config;
83  2 String p = protocolIn.trim().toLowerCase();
84  0 if (p.indexOf("saml+")==-1) throw new BadURLException("wrong protocol in "+protocolIn);
85    }
86   
 
87  0 toggle public AttributeRepository getRepository(String url) throws BadURLException {
88  0 try {
89  0 SAMLMetaInfo info = new SAMLMetaInfo();
90  0 SAMLSecurity security = new SAMLSecurity();
91  0 url = this.initialise(url,info,security);
92  0 URL up = new URL(url);
93  0 return new SAMLRepository(up,info,security);
94    } catch (MalformedURLException me) {
95  0 throw new BadURLException("Wrong URL syntax: "+url+" is encountered");
96    } catch (SecurityException se) {
97  0 throw new BadURLException("error:"+se);
98    } catch (Exception e) {
99  0 throw new BadURLException("error:"+e);
100    }
101   
102    }
103   
 
104  0 toggle public String initialise(String url,SAMLMetaInfo info,SAMLSecurity security) throws Exception {
105  0 logger.info("process meta data from the url "+url);
106  0 if (this.configFile != null) info.load(this.configFile);
107   
108  0 int index = url.lastIndexOf("+");
109  0 if (index>0) url = url.substring(index+1);
110  0 index = url.indexOf("?");
111  0 if (index>0) {
112  0 String tmp = url;
113  0 url = url.substring(0,index);
114  0 String query = tmp.substring(index+1);
115  0 update(params,info,query);
116    }
117   
118  0 int login = info.getLoginMethod();
119  0 int authn = info.getAuthnMethod();
120  0 if (login==info.INTERACTIVE) security.login(authn);
121  0 else if (login==info.PROGRAMMABLE) {
122  0 if (authn == info.KEYSTORE) {
123  0 String pass = (String)params.get("password");
124  0 if (pass==null) throw new Exception("password is required");
125  0 String file = (String)params.get("keystorefile");
126  0 if (file==null) throw new Exception("keystore file name is required");
127  0 String type = (String)params.get("keystoretype");
128  0 int t = type.toUpperCase().equals("JKS")?security.JKS:security.PKCS12;
129  0 security.LoginByProg(file,pass,t);
130  0 } else if (authn == info.KEYPAIR) {
131  0 String key = (String)params.get("keyfile");
132  0 if (key==null) throw new Exception("private key file name is required");
133  0 String file = (String)params.get("pkcfile");
134  0 if (file==null) throw new Exception("PKC file name is required");
135  0 String type = (String)params.get("keytype");
136  0 int t = type.toUpperCase().equals("DSA")?security.DSA:security.RSA;
137  0 security.LoginByProg(t,file,key);
138  0 } else if (authn == info.ENCRYPTEDKEYPAIR) {
139  0 String key = (String)params.get("keyfile");
140  0 if (key==null) throw new Exception("private key file name is required");
141  0 String file = (String)params.get("pkcfile");
142  0 if (file==null) throw new Exception("PKC file name is required");
143  0 String type = (String)params.get("keytype");
144  0 int t = type.toUpperCase().equals("DSA")?security.DSA:security.RSA;
145  0 security.LoginByProg(t,file,key);
146  0 String pass = (String)params.get("password");
147  0 if (pass==null) throw new Exception("password is required");
148  0 security.LoginByProg(t,file,key,pass);
149  0 } else if (authn == info.PROXY) {
150  0 String proxy = (String)params.get("proxyfile");
151  0 if (proxy==null) throw new Exception("proxy key file name is required");
152  0 security.LoginByProg(proxy);
153  0 } else throw new Exception("illegal authn method");
154  0 } else throw new Exception("illegal login method");
155  0 URL t = new URL(url);
156  0 info.setProtocol(t.getProtocol());
157  0 info.setHost(t.getHost());
158  0 info.setPort(t.getPort());
159  0 info.setFilePath(t.getPath());
160  0 return url;
161    }
162   
 
163  2 toggle public String getProtocol() {
164  2 return this.protocol;
165    }
166    /**
167    * this method updates the SAMLMetaInfo object with a string,
168    * which includes parameters such as "password=secret;authn=3"
169    * all of the recognisable parameters are "password","keystorefile","keystoretype","pkcfile","keyfile","keytype","proxyfile","configfile",
170    * "authn","login","querytype","attributes","signassertion","signrequest"
171    */
172   
 
173  0 toggle public void update(Hashtable table, SAMLMetaInfo info,String data) throws Exception {
174   
175  0 String[] params = StringTokeniser.process(data,";");
176  0 String name = null;
177  0 String value = null;
178  0 for (int i=0; i<params.length; i++) {
179  0 int index = params[i].indexOf("=");
180  0 if (index == -1) {
181  0 logger.debug("warning : "+params[i]+" is not defined");
182  0 continue;
183    } else {
184  0 name = params[i].substring(0,index).trim();
185  0 value = params[i].substring(index+1).trim();
186  0 logger.debug(name+" = "+value);
187  0 if (name.equals("config")) {
188  0 info.load(value);
189  0 } else if (name.equals("authn")) {
190  0 int v = new Integer(value).intValue();
191  0 info.setAuthnMethod(v);
192  0 } else if (name.equals("login")) {
193  0 int v = new Integer(value).intValue();
194  0 info.setLoginMethod(v);
195  0 } else if (name.equals("querytype")) {
196  0 int v = new Integer(value).intValue();
197  0 info.setQueryType(v);
198  0 } else if (name.equals("attributes")) {
199  0 String[] names = StringTokeniser.process(value,",");
200  0 info.setAttributeNames(names);
201  0 } else if (name.equals("signassertion")) {
202  0 boolean v = (value.toLowerCase().equals("yes"))?true:false;
203  0 info.setSignedAssertion(v);
204  0 } else if (name.equals("signrequest")) {
205  0 boolean v = (value.toLowerCase().equals("yes"))?true:false;
206  0 info.setSignedRequest(v);
207  0 } else table.put(name,value);
208    }
209    }
210    }
211    }