3939 * </pre>
4040 */
4141public class MqttExample {
42- /** Load a PKCS8 encoded keyfile from the given path. */
43- private static PrivateKey loadKeyFile (String filename , String algorithm ) throws Exception {
44- byte [] keyBytes = Files .readAllBytes (Paths .get (filename ));
42+ /** Create a Cloud IoT Core JWT for the given project id, signed with the given private key. */
43+ private static String createJwtRsa (String projectId , String privateKeyFile ) throws Exception {
44+ DateTime now = new DateTime ();
45+ // Create a JWT to authenticate this device. The device will be disconnected after the token
46+ // expires, and will have to reconnect with a new token. The audience field should always be set
47+ // to the GCP project id.
48+ JwtBuilder jwtBuilder =
49+ Jwts .builder ()
50+ .setIssuedAt (now .toDate ())
51+ .setExpiration (now .plusMinutes (20 ).toDate ())
52+ .setAudience (projectId );
53+
54+ byte [] keyBytes = Files .readAllBytes (Paths .get (privateKeyFile ));
4555 PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec (keyBytes );
46- KeyFactory kf = KeyFactory .getInstance (algorithm );
47- return kf .generatePrivate (spec );
56+ KeyFactory kf = KeyFactory .getInstance ("RSA256" );
57+
58+ return jwtBuilder .signWith (SignatureAlgorithm .RS256 , kf .generatePrivate (spec )).compact ();
4859 }
4960
50- /** Create a Cloud IoT Core JWT for the given project id, signed with the given private key. */
51- private static String createJwt (String projectId , String privateKeyFile , String algorithm )
52- throws Exception {
61+ private static String createJwtEs (String projectId , String privateKeyFile ) throws Exception {
5362 DateTime now = new DateTime ();
5463 // Create a JWT to authenticate this device. The device will be disconnected after the token
5564 // expires, and will have to reconnect with a new token. The audience field should always be set
@@ -60,16 +69,11 @@ private static String createJwt(String projectId, String privateKeyFile, String
6069 .setExpiration (now .plusMinutes (20 ).toDate ())
6170 .setAudience (projectId );
6271
63- if (algorithm .equals ("RS256" )) {
64- PrivateKey privateKey = loadKeyFile (privateKeyFile , "RSA" );
65- return jwtBuilder .signWith (SignatureAlgorithm .RS256 , privateKey ).compact ();
66- } else if (algorithm .equals ("ES256" )) {
67- PrivateKey privateKey = loadKeyFile (privateKeyFile , "EC" );
68- return jwtBuilder .signWith (SignatureAlgorithm .ES256 , privateKey ).compact ();
69- } else {
70- throw new IllegalArgumentException (
71- "Invalid algorithm " + algorithm + ". Should be one of 'RS256' or 'ES256'." );
72- }
72+ byte [] keyBytes = Files .readAllBytes (Paths .get (privateKeyFile ));
73+ PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec (keyBytes );
74+ KeyFactory kf = KeyFactory .getInstance ("ES256" );
75+
76+ return jwtBuilder .signWith (SignatureAlgorithm .ES256 , kf .generatePrivate (spec )).compact ();
7377 }
7478
7579 public static void main (String [] args ) throws Exception {
@@ -102,8 +106,17 @@ public static void main(String[] args) throws Exception {
102106 // Paho client library to send the password field. The password field is used to transmit a JWT
103107 // to authorize the device.
104108 connectOptions .setUserName ("unused" );
105- connectOptions .setPassword (
106- createJwt (options .projectId , options .privateKeyFile , options .algorithm ).toCharArray ());
109+
110+ if (options .algorithm == "RSA256" ) {
111+ connectOptions .setPassword (
112+ createJwtRsa (options .projectId , options .privateKeyFile ).toCharArray ());
113+ } else if (options .algorithm == "ES256" ) {
114+ connectOptions .setPassword (
115+ createJwtEs (options .projectId , options .privateKeyFile ).toCharArray ());
116+ } else {
117+ throw new IllegalArgumentException (
118+ "Invalid algorithm " + options .algorithm + ". Should be one of 'RS256' or 'ES256'." );
119+ }
107120
108121 // Create a client, and connect to the Google MQTT bridge.
109122 MqttClient client = new MqttClient (mqttServerAddress , mqttClientId , new MemoryPersistence ());
0 commit comments