Changes SAML Exception handling
[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         binding = SAMLBindingFactory.getInstance(protocol, policies);
31         this.myName = myName;
32     }
33
34     public void receive(HttpServletRequest req)
35         throws SAMLException{
36
37         sharName=new StringBuffer();
38         SAMLRequest sReq = binding.receive(req, sharName);
39         SAMLAttributeQuery q = (SAMLAttributeQuery)sReq.getQuery();
40         resource = q.getResource();
41         reqID = sReq.getRequestId();
42         sub = q.getSubject();
43     }
44
45     public String getHandle(){
46         return sub.getName();
47     }
48
49     public String getResource(){
50         return resource;
51     }
52
53     public String getIssuer(){
54         return sub.getConfirmationData();
55     }
56
57     public String getShar(){
58         return sharName.toString();
59     }
60
61  
62     public void respond(HttpServletResponse resp, SAMLAttribute[] attrs, SAMLException exception)
63         throws IOException{
64     
65         SAMLException ourSE = null;
66         SAMLResponse sResp = null;
67         
68         try{
69
70             SAMLSubject rSubject = new SAMLSubject(sub.getName(),
71                                                sub.getNameQualifier(),
72                                                sub.getFormat(),
73                                                sub.getConfirmationMethods(),
74                                                sub.getConfirmationData());
75             
76             SAMLStatement sStatement = new SAMLAttributeStatement(rSubject, attrs);
77             SAMLStatement[] statements = new SAMLStatement[1];
78             statements[0] = sStatement;
79             Date now = new Date();
80             Date  then = null;
81             if(attrs != null && attrs.length > 0){
82                 long min = attrs[0].getLifetime();
83                 for(int i = 1; i < attrs.length; i++){
84                     long t = attrs[i].getLifetime();
85                     if(t > 0 && t < min)
86                         min = t;
87                 }
88                 if(min > 0)
89                     then = new Date(now.getTime() + min);
90             }
91             SAMLCondition[] conditions = new SAMLCondition[1];
92             conditions[0] = new SAMLAudienceRestrictionCondition(policies);
93             SAMLAssertion sAssertion = new SAMLAssertion(myName,
94                                              now,
95                                              then,
96                                              conditions,
97                                              statements,
98                                              /* sig */ null);
99             SAMLAssertion[] assertions= new SAMLAssertion[1];
100             assertions[0] = sAssertion;
101         
102             sResp = new SAMLResponse(reqID,
103                                      /* recipient URL*/ null,
104                                      /* sig */ null,
105                                      assertions,
106                                      exception);
107         }catch (SAMLException se) {
108             ourSE = se;
109         }finally{
110             binding.respond(resp,sResp,ourSE);      
111         }
112     }
113 }