0ba9f0e85ceaa7a10b42ffb60086fba38181c70d
[java-idp.git] / src / edu / internet2 / middleware / shibboleth / hs / HandleServiceSAML.java
1 package edu.internet2.middleware.shibboleth.hs;
2
3 import java.io.*;
4 import java.util.*;
5 import java.security.*;
6 import java.security.cert.*;
7 import edu.internet2.middleware.shibboleth.*;
8 import edu.internet2.middleware.shibboleth.common.*;
9 import org.opensaml.*;
10
11 public class HandleServiceSAML {
12
13     protected String domain;
14     protected String AAurl;
15     public String[] policies = { Constants.POLICY_CLUBSHIB };
16     private ShibPOSTProfile spp;
17     PrivateKey privateKey;
18     X509Certificate cert;
19
20     public HandleServiceSAML( String domain, String AAurl, String HSname,
21                               String KSpass, String KSkeyalias,
22                               String KSkeypass, String certalias, 
23                               InputStream is ) 
24         throws SAMLException, KeyStoreException, IOException, Exception
25     {
26         this.domain = domain;
27         this.AAurl = AAurl;
28         
29         KeyStore ks = KeyStore.getInstance("JKS");
30         ks.load( is, KSpass.toCharArray());
31         privateKey = (PrivateKey)ks.getKey(KSkeyalias, KSkeypass.toCharArray());
32         cert =(X509Certificate)ks.getCertificate(certalias);
33         
34         spp = ShibPOSTProfileFactory.getInstance( policies, HSname );
35     }
36     
37     public byte[] prepare ( String handle, String shireURL, 
38     String clientAddress, String authMethod, Date authInstant ) 
39         throws HandleException {
40
41         try { 
42             SAMLAuthorityBinding[] bindings = new SAMLAuthorityBinding[1];
43             bindings[0] = new SAMLAuthorityBinding
44                 ( SAMLBinding.SAML_SOAP_HTTPS, AAurl, 
45                   new QName(org.opensaml.XML.SAMLP_NS,"AttributeQuery") );
46             SAMLResponse r = spp.prepare 
47             ( shireURL, handle, domain, clientAddress, authMethod, 
48               authInstant, bindings, privateKey, cert, null, null
49               );
50             byte[] buf = r.toBase64();
51             
52             return buf;
53         }
54         catch (SAMLException ex) {
55             throw new HandleException( "Error creating SAML assertion: "+ex );
56         }
57         catch (IOException ex) {
58             throw new HandleException( "Error converting SAML assertion: "+ex);
59         }
60     }
61 }
62                                       
63                                 
64               
65                             
66
67