Fixing the no-attribute again to match SAML requirments.
[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
31         binding = SAMLBindingFactory.getInstance(protocol, policies);
32         this.myName = myName;
33     }
34
35     public void receive(HttpServletRequest req)
36         throws SAMLException{
37
38         sharName=new StringBuffer();
39         SAMLRequest sReq = binding.receive(req, sharName);
40         SAMLAttributeQuery q = (SAMLAttributeQuery)sReq.getQuery();
41         resource = q.getResource();
42         reqID = sReq.getRequestId();
43         sub = q.getSubject();
44     }
45
46     public String getHandle(){
47         return sub.getName();
48     }
49
50     public String getResource(){
51         return resource;
52     }
53
54     public String getIssuer(){
55         return sub.getConfirmationData();
56     }
57
58     public String getShar(){
59         return sharName.toString();
60     }
61
62  
63     public void respond(HttpServletResponse resp, SAMLAttribute[] attrs, SAMLException exception)
64         throws IOException{
65     
66         SAMLException ourSE = null;
67         SAMLResponse sResp = null;
68         
69         try{
70
71             SAMLSubject rSubject = new SAMLSubject(sub.getName(),
72                                                sub.getNameQualifier(),
73                                                sub.getFormat(),
74                                                sub.getConfirmationMethods(),
75                                                sub.getConfirmationData());
76             
77             SAMLStatement[] statements = new SAMLStatement[1];
78             statements[0] = new SAMLAttributeStatement(rSubject, attrs);
79             
80
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
96             SAMLAssertion[] assertions= null;
97             if(attrs != null && attrs.length > 0){
98                 SAMLAssertion sAssertion = new SAMLAssertion(myName,
99                                              now,
100                                              then,
101                                              conditions,
102                                              statements,
103                                              /* sig */ null);
104                 assertions= new SAMLAssertion[1];
105                 assertions[0] = sAssertion;
106             }
107
108             sResp = new SAMLResponse(reqID,
109                                      /* recipient URL*/ null,
110                                      /* sig */ null,
111                                      assertions,
112                                      exception);
113         }catch (SAMLException se) {
114             ourSE = se;
115         }finally{
116             binding.respond(resp,sResp,ourSE);      
117         }
118     }
119
120     public void fail(HttpServletResponse resp, SAMLException exception)
121         throws IOException{
122
123         binding.respond(resp, null, exception);
124     }
125 }