30dd93a774a2249f2ae0c6cd316a2778bc00ace4
[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 = null;
78             if(attrs != null && attrs.length > 0){
79                 statements = new SAMLStatement[1];
80                 statements[0] = new SAMLAttributeStatement(rSubject, attrs);
81             }
82
83             Date now = new Date();
84             Date  then = null;
85             if(attrs != null && attrs.length > 0){
86                 long min = attrs[0].getLifetime();
87                 for(int i = 1; i < attrs.length; i++){
88                     long t = attrs[i].getLifetime();
89                     if(t > 0 && t < min)
90                         min = t;
91                 }
92                 if(min > 0)
93                     then = new Date(now.getTime() + min);
94             }
95             SAMLCondition[] conditions = new SAMLCondition[1];
96             conditions[0] = new SAMLAudienceRestrictionCondition(policies);
97             SAMLAssertion sAssertion = new SAMLAssertion(myName,
98                                              now,
99                                              then,
100                                              conditions,
101                                              statements,
102                                              /* sig */ null);
103             SAMLAssertion[] assertions= new SAMLAssertion[1];
104             assertions[0] = sAssertion;
105         
106             sResp = new SAMLResponse(reqID,
107                                      /* recipient URL*/ null,
108                                      /* sig */ null,
109                                      assertions,
110                                      exception);
111         }catch (SAMLException se) {
112             ourSE = se;
113         }finally{
114             binding.respond(resp,sResp,ourSE);      
115         }
116     }
117
118     public void fail(HttpServletResponse resp, SAMLException exception)
119         throws IOException{
120
121         binding.respond(resp, null, exception);
122     }
123 }