View Javadoc
1   /*
2    * Copyright (c) 2002-2019, 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.appcenter.modules.identitystore.service;
35  
36  import java.io.IOException;
37  import java.util.Locale;
38  
39  import javax.servlet.http.HttpServletRequest;
40  
41  import com.fasterxml.jackson.core.JsonProcessingException;
42  import com.fasterxml.jackson.databind.JsonNode;
43  import com.fasterxml.jackson.databind.ObjectMapper;
44  
45  import fr.paris.lutece.plugins.appcenter.modules.identitystore.business.IdentitystoreData;
46  import fr.paris.lutece.plugins.appcenter.modules.identitystore.business.IdentitystoreDatas;
47  import fr.paris.lutece.plugins.appcenter.modules.identitystore.business.IdentitystoreDemand;
48  import fr.paris.lutece.plugins.appcenter.service.task.AppCenterTaskFunctional;
49  import fr.paris.lutece.plugins.appcenter.service.task.AppcenterTask;
50  import fr.paris.lutece.portal.service.util.AppPropertiesService;
51  import fr.paris.lutece.util.apimanager.client.ApiManagerAuthenticator;
52  import fr.paris.lutece.util.apimanager.client.ApiManagerService;
53  import fr.paris.lutece.util.apimanager.client.Application;
54  import fr.paris.lutece.util.apimanager.client.SubscriberApplication;
55  import fr.paris.lutece.util.httpaccess.HttpAccessException;
56  
57  public class IdentitystoreTask extends AppcenterTask
58  {
59  
60      private final static String API_IDENTITY_RECETTE_KEY = AppPropertiesService.getProperty( "appcenter-identitystore.api_identity_recette_key" );
61      private final static String API_IDENTITY_RECETTE_SECRET = AppPropertiesService.getProperty( "appcenter-identitystore.api_identity_recette_secret" );
62      private final static String API_IDENTITY_RECETTE_URL = AppPropertiesService.getProperty( "appcenter-identitystore.api_identity_recette_url" );
63      private final static String AM_IDENTITY_RECETTE_URL = AppPropertiesService.getProperty( "appcenter-identitystore.am_identity_recette_url" );
64      private final static String API_IDENTITY_RECETTE_ID = AppPropertiesService.getProperty( "appcenter-identitystore.api_identity_recette_id" );
65  
66      private final static String API_IDENTITY_PRODUCTION_KEY = AppPropertiesService.getProperty( "appcenter-identitystore.api_identity_production_key" );
67      private final static String API_IDENTITY_PRODUCTION_SECRET = AppPropertiesService.getProperty( "appcenter-identitystore.api_identity_production_secret" );
68      private final static String API_IDENTITY_PRODUCTION_URL = AppPropertiesService.getProperty( "appcenter-identitystore.api_identity_production_url" );
69      private final static String AM_IDENTITY_PRODUCTION_URL = AppPropertiesService.getProperty( "appcenter-identitystore.am_identity_production_url" );
70      private final static String API_IDENTITY_PRODUCTION_ID = AppPropertiesService.getProperty( "appcenter-identitystore.api_identity_production_id" );
71  
72      private final static String AM_IDENTITY_PRODUCTION_USERNAME = AppPropertiesService.getProperty( "appcenter-identitystore.am_identity_production_username" );
73      private final static String API_IDENTITY_PRODUCTION_PASSWORD = AppPropertiesService
74              .getProperty( "appcenter-identitystore.api_identity_production_password" );
75      private final static String AM_IDENTITY_RECETTE_USERNAME = AppPropertiesService.getProperty( "appcenter-identitystore.am_identity_recette_username" );
76      private final static String API_IDENTITY_RECETTE_PASSWORD = AppPropertiesService.getProperty( "appcenter-identitystore.api_identity_recette_password" );
77  
78      private String _strApiUrl;
79      private String _strAmUrl;
80      private String _strApiIdentifier;
81  
82      @Override
83      public String getTitle( Locale locale )
84      {
85          // TODO
86          return "Identitystore Task";
87      }
88  
89      @Override
90      public void processTask( int nIdResourceHistory, HttpServletRequest request, Locale locale )
91      {
92  
93          AppCenterTaskFunctional<IdentitystoreData, IdentitystoreDatas, IdentitystoreDemand> funct = ( requestParam, localeParam, applicationDataParam,
94                  applicationDatasParam, demandParam ) -> addAttributeRights( requestParam, applicationDataParam );
95          super.processTask( nIdResourceHistory, request, locale, IdentitystoreData.class, IdentitystoreDatas.class, IdentitystoreDemand.class, funct );
96  
97      }
98  
99      public void addAttributeRights( HttpServletRequest request, IdentitystoreData identitystoreData )
100     {
101 
102         identitystoreData.setAttributeRights( IdentityStoreDemandService.getMapAttributeRights( request ) );
103 
104         if ( identitystoreData.getCreateApplication( ) )
105         {
106             try
107             {
108                 createAppApiManager( identitystoreData );
109 
110             }
111             catch( HttpAccessException | IOException e )
112             {
113 
114                 throw new RuntimeException( e );
115             }
116         }
117     }
118 
119     private void createAppApiManager( IdentitystoreData identity ) throws HttpAccessException, JsonProcessingException, IOException
120     {
121 
122         ApiManagerAuthenticator authenticaor = buildAuthenticator( identity );
123         String token = authenticaor.getToken( _strApiUrl );
124 
125         String response = ApiManagerService.createApplication( token, _strAmUrl, new Application( identity.getApplicationName( ) ) );
126         ObjectMapper objectMapper = new ObjectMapper( );
127         JsonNode jsonNode = objectMapper.readTree( response );
128         String strApplicationId = jsonNode.get( "applicationId" ).asText( );
129         SubscriberApplication subscriber = new SubscriberApplication( _strApiIdentifier, strApplicationId );
130         ApiManagerService.subscribeApplication( token, _strAmUrl, subscriber );
131         String apiKey = ApiManagerService.generateApiKey( token, _strAmUrl, strApplicationId );
132         identity.setApiManagerSecurityKey( apiKey );
133 
134     }
135 
136     private ApiManagerAuthenticator buildAuthenticator( IdentitystoreData identity )
137     {
138 
139         if ( identity.getEnvironment( ).equals( "prod" ) )
140         {
141 
142             _strApiUrl = API_IDENTITY_PRODUCTION_URL;
143             _strAmUrl = AM_IDENTITY_PRODUCTION_URL;
144             _strApiIdentifier = API_IDENTITY_PRODUCTION_ID;
145             return new ApiManagerAuthenticator( AM_IDENTITY_PRODUCTION_USERNAME, API_IDENTITY_PRODUCTION_PASSWORD, API_IDENTITY_PRODUCTION_KEY,
146                     API_IDENTITY_PRODUCTION_SECRET );
147 
148         }
149         else
150         {
151 
152             _strApiUrl = API_IDENTITY_RECETTE_URL;
153             _strAmUrl = AM_IDENTITY_RECETTE_URL;
154             _strApiIdentifier = API_IDENTITY_RECETTE_ID;
155             return new ApiManagerAuthenticator( AM_IDENTITY_RECETTE_USERNAME, API_IDENTITY_RECETTE_PASSWORD, API_IDENTITY_RECETTE_KEY,
156                     API_IDENTITY_RECETTE_SECRET );
157 
158         }
159     }
160 }