39
39
* </pre>
40
40
*/
41
41
public 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 ));
45
55
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 ();
48
59
}
49
60
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 {
53
62
DateTime now = new DateTime ();
54
63
// Create a JWT to authenticate this device. The device will be disconnected after the token
55
64
// 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
60
69
.setExpiration (now .plusMinutes (20 ).toDate ())
61
70
.setAudience (projectId );
62
71
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 ();
73
77
}
74
78
75
79
public static void main (String [] args ) throws Exception {
@@ -102,8 +106,17 @@ public static void main(String[] args) throws Exception {
102
106
// Paho client library to send the password field. The password field is used to transmit a JWT
103
107
// to authorize the device.
104
108
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
+ }
107
120
108
121
// Create a client, and connect to the Google MQTT bridge.
109
122
MqttClient client = new MqttClient (mqttServerAddress , mqttClientId , new MemoryPersistence ());
0 commit comments