View Javadoc
1   /*
2    * Copyright (c) 2002-2014, Mairie de Paris
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met:
8    *
9    *  1. Redistributions of source code must retain the above copyright notice
10   *     and the following disclaimer.
11   *
12   *  2. Redistributions in binary form must reproduce the above copyright notice
13   *     and the following disclaimer in the documentation and/or other materials
14   *     provided with the distribution.
15   *
16   *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
17   *     contributors may be used to endorse or promote products derived from
18   *     this software without specific prior written permission.
19   *
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30   * POSSIBILITY OF SUCH DAMAGE.
31   *
32   * License 1.0
33   */
34  package fr.paris.lutece.plugins.mylutece.modules.saml.authentication.engine;
35  
36  import fr.paris.lutece.plugins.mylutece.modules.saml.authentication.config.Constants;
37  import fr.paris.lutece.plugins.mylutece.modules.saml.authentication.exceptions.SAMLTokenExtractorException;
38  import fr.paris.lutece.portal.service.util.AppLogService;
39  
40  import org.opensaml.Configuration;
41  
42  import org.opensaml.saml2.core.Response;
43  
44  import org.opensaml.xml.io.Unmarshaller;
45  import org.opensaml.xml.io.UnmarshallerFactory;
46  import org.opensaml.xml.io.UnmarshallingException;
47  import org.opensaml.xml.parse.BasicParserPool;
48  import org.opensaml.xml.parse.XMLParserException;
49  import org.opensaml.xml.util.Base64;
50  
51  import org.w3c.dom.Document;
52  import org.w3c.dom.Element;
53  
54  import java.io.StringReader;
55  import java.io.UnsupportedEncodingException;
56  
57  import javax.servlet.http.HttpServletRequest;
58  
59  
60  public class SAMLTokenExtractor implements Constants
61  {
62      /**
63       * M�thode de construction de la SAMLResponse � partir d'une string XML
64       *
65       * @param xmlToken
66       * @throws SAMLTokenExtractorException
67       */
68      public static Response extractSAMLResponse( HttpServletRequest request )
69          throws SAMLTokenExtractorException
70      {
71          Response response = null;
72  
73          // Extraction de la SAMLResponse de la requete HTTP
74          String SAMLResponseB64Str = request.getParameter( SAML_RESPONSE_REQUEST_PARAM );
75  
76          // Decodage URL encoding et Base64
77          String SAMLResponseStr = null;
78  
79          try
80          {
81              SAMLResponseStr = new String( Base64.decode( SAMLResponseB64Str ), "UTF-8" );
82          }
83          catch ( UnsupportedEncodingException e1 )
84          {
85              String message = "Mauvais encodage de la Response : " + e1.getLocalizedMessage(  );
86              AppLogService.info( message );
87              throw new SAMLTokenExtractorException( message );
88          }
89  
90          // Get parser pool manager
91          BasicParserPool ppMgr = new BasicParserPool(  );
92          ppMgr.setNamespaceAware( true );
93  
94          Document inCommonMDDoc;
95  
96          try
97          {
98              inCommonMDDoc = ppMgr.parse( new StringReader( SAMLResponseStr ) );
99  
100             Element ResponseRoot = inCommonMDDoc.getDocumentElement(  );
101 
102             // Get apropriate unmarshaller
103             UnmarshallerFactory unmarshallerFactory = Configuration.getUnmarshallerFactory(  );
104             Unmarshaller unmarshaller = unmarshallerFactory.getUnmarshaller( ResponseRoot );
105 
106             // Unmarshall using the document root element, an EntitiesDescriptor
107             // in this case
108             response = (Response) unmarshaller.unmarshall( ResponseRoot );
109         }
110         catch ( XMLParserException e )
111         {
112             String message = "Erreur de parsing de la SAMLResponse : " + e.getLocalizedMessage(  );
113             AppLogService.info( message );
114             throw new SAMLTokenExtractorException( message );
115         }
116         catch ( UnmarshallingException e )
117         {
118             String message = "Erreur de unmarshalling de la SAMLResponse : " + e.getLocalizedMessage(  );
119             AppLogService.info( message );
120             throw new SAMLTokenExtractorException( message );
121         }
122 
123         return response;
124     }
125 }