1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
55
56
57
58
59
60
61
62
63
64
65
66
67
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
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 }