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