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.ConfigProperties;
37  import fr.paris.lutece.plugins.mylutece.modules.saml.authentication.metadata.IDPMetadataManager;
38  import fr.paris.lutece.plugins.mylutece.modules.saml.authentication.metadata.SPMetadataManager;
39  import fr.paris.lutece.portal.service.util.AppException;
40  import fr.paris.lutece.portal.service.util.AppLogService;
41  
42  import org.opensaml.Configuration;
43  import org.opensaml.DefaultBootstrap;
44  
45  import org.opensaml.saml2.metadata.provider.DOMMetadataProvider;
46  import org.opensaml.saml2.metadata.provider.MetadataProviderException;
47  
48  import org.opensaml.security.MetadataCredentialResolver;
49  
50  import org.opensaml.xml.ConfigurationException;
51  import org.opensaml.xml.security.keyinfo.KeyInfoCredentialResolver;
52  import org.opensaml.xml.signature.impl.ExplicitKeySignatureTrustEngine;
53  
54  
55  public class BootStrap
56  {
57      private static BootStrap instance = null;
58      private IDPMetadataManager idpMetaDataManager = null;
59      private SPMetadataManager spMetaDataManager = null;
60      private ExplicitKeySignatureTrustEngine trustEngine = null;
61  
62      protected BootStrap(  )
63      {
64          // Initialize the OpenSAML library
65          try
66          {
67              DefaultBootstrap.bootstrap(  );
68          }
69          catch ( ConfigurationException e )
70          {
71              String message = "Erreur d'initialisation de OpenSAML" + e.getLocalizedMessage(  );
72              AppLogService.error( message );
73              throw new AppException( message, e );
74          }
75  
76          // Initialize the configuration
77          try
78          {
79              ConfigProperties.init(  );
80          }
81          catch ( Exception e )
82          {
83              String message = "Erreur d'initialisation des propri�t�s du plugin MyLutece-SAML" +
84                  e.getLocalizedMessage(  );
85              AppLogService.error( message );
86              throw new AppException( message, e );
87          }
88  
89          // Initialisation des Metadonn�es
90          initializeIDPMetaData( null );
91          initializeSPMetaData( null );
92      }
93  
94      public ExplicitKeySignatureTrustEngine getTrustEngine(  )
95      {
96          return trustEngine;
97      }
98  
99      public IDPMetadataManager getIdpMetaDataManager(  )
100     {
101         return idpMetaDataManager;
102     }
103 
104     public SPMetadataManager getSpMetaDataManager(  )
105     {
106         return spMetaDataManager;
107     }
108 
109     public static void init(  )
110     {
111         if ( instance == null )
112         {
113             instance = new BootStrap(  );
114         }
115     }
116 
117     public static BootStrap getInstance(  )
118     {
119         return instance;
120     }
121 
122     public void initializeIDPMetaData( String inFilePath )
123     {
124         // Initialize the MetaData
125         // Recuperation des MetaData
126         idpMetaDataManager = new IDPMetadataManager(  );
127         idpMetaDataManager.loadMetadata( inFilePath );
128 
129         // Construction d'un MetaDataProvider a partir de ces MetaData
130         DOMMetadataProvider mdProvider = new DOMMetadataProvider( idpMetaDataManager.getMetaData(  ).getDOM(  ) );
131 
132         try
133         {
134             mdProvider.initialize(  );
135         }
136         catch ( MetadataProviderException e )
137         {
138             String message = "Erreur d'initialisation des MetaDataProvider" + e.getLocalizedMessage(  );
139             AppLogService.error( message );
140             throw new AppException( message, e );
141         }
142 
143         // Creation d'un MetadataCredentialResolver
144         MetadataCredentialResolver mdCredResolver = new MetadataCredentialResolver( mdProvider );
145 
146         // Creation d'un KeyInfoCredentialResolver
147         KeyInfoCredentialResolver keyInfoCredResolver = Configuration.getGlobalSecurityConfiguration(  )
148                                                                      .getDefaultKeyInfoCredentialResolver(  );
149         // Creation d'un ExplicitKeySignatureTrustEngine
150         trustEngine = new ExplicitKeySignatureTrustEngine( mdCredResolver, keyInfoCredResolver );
151     }
152 
153     public void initializeSPMetaData( String inFilePath )
154     {
155         // Initialize the MetaData
156         spMetaDataManager = new SPMetadataManager(  );
157         spMetaDataManager.loadMetadata( inFilePath );
158     }
159 }