3
3
import com .auth0 .jwt .*;
4
4
import com .auth0 .jwt .algorithms .*;
5
5
import com .auth0 .jwt .exceptions .*;
6
- import java .nio .file .*;
7
- import java .io .*;
8
6
import java .security .*;
9
7
import java .security .spec .*;
10
8
import java .security .interfaces .*;
11
9
import org .apache .commons .logging .*;
12
10
import org .apache .commons .logging .impl .*;
13
- import java .util .Map ;
14
11
import java .util .List ;
12
+ import java .util .Map ;
13
+ import java .util .Hashtable ;
14
+ import javax .naming .Context ;
15
+ import javax .naming .InitialContext ;
15
16
import com .microlib .dataformat .*;
17
+ import com .microlib .jndi .service .*;
18
+ import java .io .UnsupportedEncodingException ;
19
+ import javax .naming .NamingException ;
16
20
17
21
public class JwtService implements ExecInterface {
18
22
19
23
int nLoop = 0 ;
20
24
private boolean bRunning = false ;
21
25
private String name ;
22
26
private static org .apache .commons .logging .Log log ;
27
+ final static String jndiName = "java/KeyStore" ;
28
+ private KeyPairStoreImpl keyStore ;
29
+
23
30
24
31
public boolean isRunning () {
25
32
return bRunning ;
@@ -56,36 +63,48 @@ public String doProcess(Map<String, Object> map) {
56
63
// create a token with our generated private rsa key
57
64
// use the key-id to retrieve the correct kea
58
65
if (null == map .get ("key-id" )) {
66
+ log .error ("no key-id found " );
59
67
response = json .message ("ERROR no key-id found " , "KO" );
60
68
} else {
61
- byte [] keyBytes = Files .readAllBytes (new File (map .get ("key-id" ).toString () + "/private_key.der" ).toPath ());
62
- PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec (keyBytes );
63
- KeyFactory kf = KeyFactory .getInstance ("RSA" );
64
- RSAPrivateKey key = (RSAPrivateKey ) kf .generatePrivate (spec );
65
- String token = JWT .create ().withIssuer ("auth0" ).sign (Algorithm .RSA256 (key ));
66
- log .info ("Signed Token : " + token );
67
- response = json .message ("Signed Token " + token , "OK" );
69
+ byte [] keyBytes = keyStore .getPrivateByteArray (map .get ("key-id" ).toString ());
70
+ if (null == keyBytes ) {
71
+ log .error ("key-id " + map .get ("key-id" ).toString () + " not found" );
72
+ response = json .message ("ERROR key-id " + map .get ("key-id" ).toString () + " not found " , "KO" );
73
+ } else {
74
+ PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec (keyBytes );
75
+ KeyFactory kf = KeyFactory .getInstance ("RSA" );
76
+ RSAPrivateKey key = (RSAPrivateKey ) kf .generatePrivate (spec );
77
+ String token = JWT .create ().withIssuer ("auth0" ).sign (Algorithm .RSA256 (key ));
78
+ log .info ("Signed Token : " + token );
79
+ response = json .message ("Signed Token " + token , "OK" );
80
+ }
68
81
}
69
- } catch (JWTCreationException | IOException | NoSuchAlgorithmException | InvalidKeySpecException exception ) {
82
+ } catch (JWTCreationException | NoSuchAlgorithmException | InvalidKeySpecException exception ) {
70
83
log .error (exception );
71
84
response = json .message ("ERROR " + exception .toString (), "KO" );
72
85
}
73
86
} else if (map .get ("action" ).toString ().equals ("verifyToken" )) {
74
87
try {
75
88
if (null == map .get ("key-id" )) {
89
+ log .error ("no key-id found " );
76
90
response = json .message ("ERROR no key-id found " , "KO" );
77
91
} else {
78
- byte [] keyBytes = Files .readAllBytes (new File (map .get ("key-id" ).toString () + "/public_key.der" ).toPath ());
79
- X509EncodedKeySpec spec = new X509EncodedKeySpec (keyBytes );
80
- KeyFactory kf = KeyFactory .getInstance ("RSA" );
81
- RSAPublicKey key = (RSAPublicKey ) kf .generatePublic (spec );
82
- String token = map .get ("token" ).toString ();
83
- JWTVerifier verifier = JWT .require (Algorithm .RSA256 (key )).withIssuer ("auth0" ).build ();
84
- JWT jwt = (JWT ) verifier .verify (token );
85
- log .info ("Token verified " + jwt );
86
- response = json .message ("Token verified " , "OK" );
92
+ byte [] keyBytes = keyStore .getPublicByteArray (map .get ("key-id" ).toString ());
93
+ if (null == keyBytes ) {
94
+ log .error ("key-id " + map .get ("key-id" ).toString () + " not found" );
95
+ response = json .message ("ERROR key-id " + map .get ("key-id" ).toString () + " not found " , "KO" );
96
+ } else {
97
+ X509EncodedKeySpec spec = new X509EncodedKeySpec (keyBytes );
98
+ KeyFactory kf = KeyFactory .getInstance ("RSA" );
99
+ RSAPublicKey key = (RSAPublicKey ) kf .generatePublic (spec );
100
+ String token = map .get ("token" ).toString ();
101
+ JWTVerifier verifier = JWT .require (Algorithm .RSA256 (key )).withIssuer ("auth0" ).build ();
102
+ JWT jwt = (JWT ) verifier .verify (token );
103
+ log .info ("Token verified " + jwt );
104
+ response = json .message ("Token verified " , "OK" );
105
+ }
87
106
}
88
- } catch (JWTVerificationException | JWTCreationException | IOException | NoSuchAlgorithmException
107
+ } catch (JWTVerificationException | JWTCreationException | NoSuchAlgorithmException
89
108
| InvalidKeySpecException exception ) {
90
109
log .error (exception );
91
110
response = json .message ("ERROR " + exception .toString (), "KO" );
@@ -97,6 +116,14 @@ public String doProcess(Map<String, Object> map) {
97
116
}
98
117
99
118
public void init (String sIn ) {
100
- log = LogFactory .getLog (JwtService .class );
119
+ try {
120
+ log = LogFactory .getLog (JwtService .class );
121
+ Hashtable <String , String > ht = new Hashtable <String , String >();
122
+ ht .put ("java.naming.factory.initial" , "com.microlib.jndi.DSInitCtxFactory" );
123
+ Context ctx = new InitialContext (ht );
124
+ keyStore = (KeyPairStoreImpl ) ctx .lookup (jndiName );
125
+ } catch (NamingException e ) {
126
+ // we can't assume that the log is available
127
+ }
101
128
}
102
129
}
0 commit comments