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