Coverage Report - org.kuali.student.security.filter.ProxyTicketRetrieverFilter
 
Classes in this File Line Coverage Branch Coverage Complexity
ProxyTicketRetrieverFilter
0%
0/48
0%
0/14
2.111
 
 1  
 /**
 2  
  * Copyright 2010 The Kuali Foundation Licensed under the
 3  
  * Educational Community License, Version 2.0 (the "License"); you may
 4  
  * not use this file except in compliance with the License. You may
 5  
  * obtain a copy of the License at
 6  
  *
 7  
  * http://www.osedu.org/licenses/ECL-2.0
 8  
  *
 9  
  * Unless required by applicable law or agreed to in writing,
 10  
  * software distributed under the License is distributed on an "AS IS"
 11  
  * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 12  
  * or implied. See the License for the specific language governing
 13  
  * permissions and limitations under the License.
 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  
             // This is not a SAML Assertion. It is CAS specific way to hold information about the authenticated user.
 52  
             // The information is returned from the CAS server as a response to a validation request.
 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  
              // place saml in security context
 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  
 }