View Javadoc
1   /*
2    * Copyright (c) 2002-2021, City of 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.util.signrequest;
35  
36  import static fr.paris.lutece.util.signrequest.AbstractJWTAuthenticator.LOGGER;
37  import java.security.KeyFactory;
38  import java.security.KeyPair;
39  import java.security.NoSuchAlgorithmException;
40  import java.security.PrivateKey;
41  import java.security.interfaces.RSAPublicKey;
42  import java.security.spec.InvalidKeySpecException;
43  import java.security.spec.PKCS8EncodedKeySpec;
44  import java.security.spec.X509EncodedKeySpec;
45  import java.util.Base64;
46  import java.util.Map;
47  
48  public class JWTRSAPlainTextAuthenticator extends AbstractJWTRSAAuthenticator
49  {
50      String _strPlainTextPublicKey;
51      String _strPlainTextPrivateKey;
52  
53      /**
54       * Constructor
55       * 
56       * @param mapClaimsToCheck
57       *            The map of claims key/values to check in the JWT
58       * @param strJWTHttpHeader
59       *            The name of the header which contains the JWT
60       * @param lValidityPeriod
61       *            The validity period
62       * @param strEncryptionAlgorythmName
63       *            The name of the algorithm.
64       * @param strPlainTextPrivateKey
65       *            The plain text private key
66       * @param strPlainTextPublicKey
67       *            The plain text public key
68       */
69      public JWTRSAPlainTextAuthenticator( Map<String, String> mapClaimsToCheck, String strJWTHttpHeader, long lValidityPeriod, String strEncryptionAlgorythmName,
70              String strPlainTextPrivateKey, String strPlainTextPublicKey )
71      {
72          super( mapClaimsToCheck, strJWTHttpHeader, lValidityPeriod, strEncryptionAlgorythmName );
73          _strPlainTextPrivateKey = strPlainTextPrivateKey;
74          _strPlainTextPublicKey = strPlainTextPublicKey;
75  
76      }
77  
78      /**
79       * {@inheritDoc }
80       */
81      @Override
82      protected KeyPair getKeyPair( )
83      {
84          RSAPublicKey pubKey = null;
85          PrivateKey privKey = null;
86          try
87          {
88              KeyFactory kf = KeyFactory.getInstance( "RSA" );
89  
90              try
91              {
92                  X509EncodedKeySpec keySpecX509 = new X509EncodedKeySpec( Base64.getDecoder( ).decode( _strPlainTextPublicKey ) );
93                  pubKey = (RSAPublicKey) kf.generatePublic( keySpecX509 );
94              }
95              catch( InvalidKeySpecException e )
96              {
97                  LOGGER.error( "Unable to convert given plain text key to public java.security.Key", e );
98              }
99  
100             try
101             {
102                 PKCS8EncodedKeySpec keySpecPKCS8 = new PKCS8EncodedKeySpec( Base64.getDecoder( ).decode( _strPlainTextPrivateKey ) );
103                 privKey = kf.generatePrivate( keySpecPKCS8 );
104             }
105             catch( InvalidKeySpecException e )
106             {
107                 LOGGER.error( "Unable to convert given plain text key to public java.security.Key", e );
108             }
109         }
110         catch( NoSuchAlgorithmException e )
111         {
112             LOGGER.error( "Unable to obtain a KeyFactory for RSA", e );
113             return null;
114         }
115 
116         return new KeyPair( pubKey, privKey );
117     }
118 }