View Javadoc
1   /*
2    * Copyright (c) 2002-2017, 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.grukeydiversification.web.rs.dto;
35  
36  import java.util.Map;
37  
38  import org.apache.commons.lang.StringUtils;
39  
40  import fr.paris.lutece.plugins.identitystore.web.rs.dto.AttributeDto;
41  import fr.paris.lutece.plugins.identitystore.web.rs.dto.IdentityDto;
42  import fr.paris.lutece.portal.service.util.AppException;
43  import fr.paris.lutece.portal.service.util.AppLogService;
44  import fr.paris.lutece.util.keydiversification.DiversificationService;
45  import fr.paris.lutece.util.keydiversification.KeyDiversificationException;
46  
47  /**
48   * <p>
49   * Decorator of a {@link IdentityDto} in order to encrypt attributes
50   * </p>
51   * <p>
52   * The encrypted attributes are :
53   * <ul>
54   * <li>the customer id</li>
55   * </ul>
56   * </p>
57   *
58   */
59  public class EncryptedIdentityDto extends IdentityDto
60  {
61  
62      /**
63       * Generated serial ID
64       */
65      private static final long serialVersionUID = -8236870738465724413L;
66  
67      private final IdentityDto _identityDto;
68      private String _strCustomerId;
69  
70      /**
71       * Constructor
72       * 
73       * @param identityDto
74       *            the {@link IdentityDto} to encrypt
75       * @param strEncryptionKey
76       *            the encryption key used to encrypt the {@code IdentityDto}
77       * @throws AppException
78       *             if there is an error during the encryption
79       */
80      public EncryptedIdentityDto( IdentityDto identityDto, String strEncryptionKey )
81      {
82          super( );
83          _identityDto = identityDto;
84  
85          if ( StringUtils.isBlank( strEncryptionKey ) )
86          {
87              throw new AppException( "Encrytion on Identity is enabled but the encryption key is blank!" );
88          }
89  
90          if ( _identityDto.getCustomerId( ) != null )
91          {
92              try
93              {
94                  _strCustomerId = DiversificationService.getSPKey( _identityDto.getCustomerId( ), strEncryptionKey );
95              }
96              catch( KeyDiversificationException e )
97              {
98                  String strMessage = "Error during encryption of Identity";
99                  StringBuilder stringBuilder = new StringBuilder( strMessage ).append( " : connectionId = " ).append( _identityDto.getConnectionId( ) )
100                         .append( ", customerId = " ).append( _identityDto.getCustomerId( ) );
101                 AppLogService.error( stringBuilder.toString( ), e );
102 
103                 throw new AppException( strMessage );
104             }
105         }
106     }
107 
108     /**
109      * {@inheritDoc }
110      */
111     @Override
112     public Map<String, AttributeDto> getAttributes( )
113     {
114         return _identityDto.getAttributes( );
115     }
116 
117     /**
118      * {@inheritDoc }
119      */
120     @Override
121     public void setAttributes( Map<String, AttributeDto> mapAttributes )
122     {
123         _identityDto.setAttributes( mapAttributes );
124     }
125 
126     /**
127      * {@inheritDoc }
128      */
129     @Override
130     public String getConnectionId( )
131     {
132         return _identityDto.getConnectionId( );
133     }
134 
135     /**
136      * {@inheritDoc }
137      */
138     @Override
139     public void setConnectionId( String connectionId )
140     {
141         _identityDto.setConnectionId( connectionId );
142     }
143 
144     /**
145      * {@inheritDoc }
146      */
147     @Override
148     public String getCustomerId( )
149     {
150         return _strCustomerId;
151     }
152 
153     /**
154      * {@inheritDoc }
155      */
156     @Override
157     public void setCustomerId( String strCustomerId )
158     {
159         _strCustomerId = strCustomerId;
160     }
161 
162 }