Added edu.internet2.middleware.eduPerson.Init.init();
[java-idp.git] / src / edu / internet2 / middleware / shibboleth / aa / AASaml.java
1 package edu.internet2.middleware.shibboleth.aa;
2
3 import java.util.*;
4 import java.io.IOException;
5 import javax.servlet.*;
6 import javax.servlet.http.*;
7 import edu.internet2.middleware.shibboleth.*;
8 import edu.internet2.middleware.shibboleth.common.Constants;
9 import edu.internet2.middleware.shibboleth.common.SAMLBindingFactory;
10
11 import org.w3c.dom.*;
12 import org.opensaml.*;
13
14
15 public class AASaml {
16
17     String[] policies = { Constants.POLICY_CLUBSHIB };
18     String protocol = SAMLBinding.SAML_SOAP_HTTPS;
19     String myName;
20     StringBuffer sharName;
21     String resource;
22     String reqID;
23     SAMLSubject sub;
24     SAMLBinding binding;
25     
26
27     public AASaml(String myName){
28         
29         Init.init();
30         edu.internet2.middleware.eduPerson.Init.init();
31
32         binding = SAMLBindingFactory.getInstance(protocol, policies);
33         this.myName = myName;
34     }
35
36     public void receive(HttpServletRequest req)
37         throws SAMLException{
38
39         sharName=new StringBuffer();
40         SAMLRequest sReq = binding.receive(req, sharName);
41         SAMLAttributeQuery q = (SAMLAttributeQuery)sReq.getQuery();
42         resource = q.getResource();
43         reqID = sReq.getRequestId();
44         sub = q.getSubject();
45     }
46
47     public String getHandle(){
48         return sub.getName();
49     }
50
51     public String getResource(){
52         return resource;
53     }
54
55     public String getIssuer(){
56         return sub.getConfirmationData();
57     }
58
59     public String getShar(){
60         return sharName.toString();
61     }
62
63  
64     public void respond(HttpServletResponse resp, SAMLAttribute[] attrs, SAMLException exception)
65         throws IOException{
66     
67         SAMLException ourSE = null;
68         SAMLResponse sResp = null;
69         
70         try{
71
72             SAMLSubject rSubject = new SAMLSubject(sub.getName(),
73                                                sub.getNameQualifier(),
74                                                sub.getFormat(),
75                                                sub.getConfirmationMethods(),
76                                                sub.getConfirmationData());
77             
78             SAMLStatement sStatement = new SAMLAttributeStatement(rSubject, attrs);
79             SAMLStatement[] statements = new SAMLStatement[1];
80             statements[0] = sStatement;
81             Date now = new Date();
82             Date  then = null;
83             if(attrs != null && attrs.length > 0){
84                 long min = attrs[0].getLifetime();
85                 for(int i = 1; i < attrs.length; i++){
86                     long t = attrs[i].getLifetime();
87                     if(t > 0 && t < min)
88                         min = t;
89                 }
90                 if(min > 0)
91                     then = new Date(now.getTime() + min);
92             }
93             SAMLCondition[] conditions = new SAMLCondition[1];
94             conditions[0] = new SAMLAudienceRestrictionCondition(policies);
95             SAMLAssertion sAssertion = new SAMLAssertion(myName,
96                                              now,
97                                              then,
98                                              conditions,
99                                              statements,
100                                              /* sig */ null);
101             SAMLAssertion[] assertions= new SAMLAssertion[1];
102             assertions[0] = sAssertion;
103         
104             sResp = new SAMLResponse(reqID,
105                                      /* recipient URL*/ null,
106                                      /* sig */ null,
107                                      assertions,
108                                      exception);
109         }catch (SAMLException se) {
110             ourSE = se;
111         }finally{
112             binding.respond(resp,sResp,ourSE);      
113         }
114     }
115 }