1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.student.security.filter; |
17 | |
|
18 | |
import java.io.ByteArrayInputStream; |
19 | |
import java.io.IOException; |
20 | |
|
21 | |
import javax.servlet.FilterChain; |
22 | |
import javax.servlet.ServletException; |
23 | |
import javax.servlet.ServletRequest; |
24 | |
import javax.servlet.ServletResponse; |
25 | |
import javax.servlet.http.HttpServletRequest; |
26 | |
import javax.servlet.http.HttpServletResponse; |
27 | |
import javax.xml.parsers.DocumentBuilder; |
28 | |
import javax.xml.parsers.DocumentBuilderFactory; |
29 | |
|
30 | |
import org.jasig.cas.client.validation.Assertion; |
31 | |
import org.kuali.student.security.saml.service.SamlIssuerService; |
32 | |
import org.kuali.student.security.util.SamlUtils; |
33 | |
import org.opensaml.SAMLAssertion; |
34 | |
import org.springframework.security.cas.authentication.CasAuthenticationToken; |
35 | |
import org.springframework.security.core.context.SecurityContextHolder; |
36 | |
import org.springframework.web.filter.GenericFilterBean; |
37 | |
import org.w3c.dom.Document; |
38 | |
|
39 | 0 | public class ProxyTicketRetrieverFilter extends GenericFilterBean { |
40 | |
|
41 | 0 | private String proxyTargetService = null; |
42 | |
private SamlIssuerService samlIssuerService; |
43 | 0 | private boolean useCasProxyMechanism = false; |
44 | |
|
45 | |
public void doFilter(ServletRequest request, ServletResponse response, |
46 | |
FilterChain chain) throws IOException, ServletException { |
47 | 0 | if (request instanceof HttpServletRequest && response instanceof HttpServletResponse) { |
48 | 0 | doFilterHttp((HttpServletRequest) request, |
49 | |
(HttpServletResponse) response, chain); |
50 | |
} else { |
51 | |
|
52 | |
} |
53 | 0 | } |
54 | |
|
55 | |
public void doFilterHttp(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws IOException, ServletException { |
56 | |
|
57 | 0 | CasAuthenticationToken cat = (CasAuthenticationToken) SecurityContextHolder.getContext().getAuthentication(); |
58 | |
|
59 | 0 | if(cat != null && !isSAMLInSecurityContext()){ |
60 | |
|
61 | |
|
62 | 0 | Assertion casAssertion = null; |
63 | 0 | String proxyTicket = null; |
64 | 0 | String principal = null; |
65 | |
|
66 | 0 | System.out.println("ProxyTicketRetrieverFilter : inside if"); |
67 | 0 | casAssertion = cat.getAssertion(); |
68 | 0 | if(casAssertion != null){ |
69 | 0 | System.out.println("ProxyTicketRetrieverFilter : casAssertion is not null"); |
70 | 0 | if(useCasProxyMechanism){ |
71 | 0 | proxyTicket = casAssertion.getPrincipal().getProxyTicketFor(proxyTargetService); |
72 | |
} else { |
73 | 0 | principal = casAssertion.getPrincipal().getName(); |
74 | |
} |
75 | |
} |
76 | |
|
77 | 0 | Document signedSAMLDoc = null; |
78 | 0 | SAMLAssertion samlAssertion = null; |
79 | 0 | String signedSAMLRet = null; |
80 | |
|
81 | |
try{ |
82 | 0 | System.out.println("ProxyTicketRetrieverFilter : Proxy Ticket Returned from CAS " + proxyTicket); |
83 | 0 | if(useCasProxyMechanism){ |
84 | 0 | signedSAMLRet = samlIssuerService.validateCasProxyTicket(proxyTicket, proxyTargetService); |
85 | |
} else { |
86 | 0 | signedSAMLRet = samlIssuerService.getSamlPrincipal(principal); |
87 | |
} |
88 | |
|
89 | 0 | DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); |
90 | 0 | dbf.setNamespaceAware(true); |
91 | |
|
92 | 0 | DocumentBuilder db = dbf.newDocumentBuilder(); |
93 | 0 | ByteArrayInputStream bais = new ByteArrayInputStream(signedSAMLRet.getBytes()); |
94 | |
|
95 | 0 | signedSAMLDoc = db.parse(bais); |
96 | 0 | samlAssertion = SamlUtils.unsignAssertion(signedSAMLDoc); |
97 | |
|
98 | 0 | } catch(Exception e){ |
99 | 0 | throw new ServletException(e); |
100 | 0 | } |
101 | |
|
102 | |
|
103 | 0 | cat.setDetails(samlAssertion); |
104 | |
} |
105 | 0 | filterChain.doFilter(request, response); |
106 | 0 | } |
107 | |
|
108 | |
private boolean isSAMLInSecurityContext(){ |
109 | 0 | CasAuthenticationToken cat = (CasAuthenticationToken) SecurityContextHolder.getContext().getAuthentication(); |
110 | 0 | if(cat.getDetails() instanceof SAMLAssertion){ |
111 | 0 | return true; |
112 | |
} |
113 | 0 | return false; |
114 | |
} |
115 | |
|
116 | |
|
117 | |
|
118 | |
|
119 | |
|
120 | |
|
121 | |
|
122 | |
public String getProxyTargetService() { |
123 | 0 | return proxyTargetService; |
124 | |
} |
125 | |
|
126 | |
public void setProxyTargetService(String proxyTargetService) { |
127 | 0 | this.proxyTargetService = proxyTargetService; |
128 | 0 | } |
129 | |
|
130 | |
public SamlIssuerService getSamlIssuerService() { |
131 | 0 | return samlIssuerService; |
132 | |
} |
133 | |
|
134 | |
public void setSamlIssuerService(SamlIssuerService samlIssuerService) { |
135 | 0 | this.samlIssuerService = samlIssuerService; |
136 | 0 | } |
137 | |
|
138 | |
public boolean getUseCasProxyMechanism() { |
139 | 0 | return useCasProxyMechanism; |
140 | |
} |
141 | |
|
142 | |
public void setUseCasProxyMechanism(boolean useCasProxyMechanism) { |
143 | 0 | this.useCasProxyMechanism = useCasProxyMechanism; |
144 | 0 | } |
145 | |
|
146 | |
} |