Skip to content

Commit 46724e2

Browse files
gguusskurtisvg
authored andcommitted
Adds credential refresh to MQTT example (GoogleCloudPlatform#917)
1 parent f64e4f3 commit 46724e2

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

iot/api-client/mqtt_example/src/main/java/com/google/cloud/iot/examples/MqttExample.java

+19-1
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ public static void main(String[] args) throws Exception {
127127
// to authorize the device.
128128
connectOptions.setUserName("unused");
129129

130-
System.out.println(options.algorithm);
130+
DateTime iat = new DateTime();
131131
if (options.algorithm.equals("RS256")) {
132132
connectOptions.setPassword(
133133
createJwtRsa(options.projectId, options.privateKeyFile).toCharArray());
@@ -161,6 +161,24 @@ public static void main(String[] args) throws Exception {
161161
"Publishing %s message %d/%d: '%s'\n",
162162
options.messageType, i, options.numMessages, payload);
163163

164+
// Refresh the connection credentials before the JWT expires.
165+
long secsSinceRefresh = ((new DateTime()).getMillis() - iat.getMillis()) / 1000;
166+
if (secsSinceRefresh > (options.tokenExpMins * 60)) {
167+
System.out.format("\tRefreshing token after: %d seconds\n", secsSinceRefresh);
168+
iat = new DateTime();
169+
if (options.algorithm.equals("RS256")) {
170+
connectOptions.setPassword(
171+
createJwtRsa(options.projectId, options.privateKeyFile).toCharArray());
172+
} else if (options.algorithm.equals("ES256")) {
173+
connectOptions.setPassword(
174+
createJwtEs(options.projectId, options.privateKeyFile).toCharArray());
175+
} else {
176+
throw new IllegalArgumentException(
177+
"Invalid algorithm " + options.algorithm
178+
+ ". Should be one of 'RS256' or 'ES256'.");
179+
}
180+
}
181+
164182
// Publish "payload" to the MQTT topic. qos=1 means at least once delivery. Cloud IoT Core
165183
// also supports qos=0 for at most once delivery.
166184
MqttMessage message = new MqttMessage(payload.getBytes());

iot/api-client/mqtt_example/src/main/java/com/google/cloud/iot/examples/MqttExampleOptions.java

+12
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public class MqttExampleOptions {
3030
String algorithm;
3131
String cloudRegion = "us-central1";
3232
int numMessages = 100;
33+
int tokenExpMins = 20;
3334
String mqttBridgeHostname = "mqtt.googleapis.com";
3435
short mqttBridgePort = 8883;
3536
String messageType = "event";
@@ -101,6 +102,13 @@ public static MqttExampleOptions fromFlags(String[] args) {
101102
.hasArg()
102103
.desc("MQTT bridge hostname.")
103104
.build());
105+
options.addOption(
106+
Option.builder()
107+
.type(Number.class)
108+
.longOpt("token_exp_minutes")
109+
.hasArg()
110+
.desc("Minutes to JWT token refresh (token expiration time).")
111+
.build());
104112
options.addOption(
105113
Option.builder()
106114
.type(Number.class)
@@ -133,6 +141,10 @@ public static MqttExampleOptions fromFlags(String[] args) {
133141
if (commandLine.hasOption("num_messages")) {
134142
res.numMessages = ((Number) commandLine.getParsedOptionValue("num_messages")).intValue();
135143
}
144+
if (commandLine.hasOption("token_exp_minutes")) {
145+
res.tokenExpMins =
146+
((Number) commandLine.getParsedOptionValue("token_exp_minutes")).intValue();
147+
}
136148
if (commandLine.hasOption("mqtt_bridge_hostname")) {
137149
res.mqttBridgeHostname = commandLine.getOptionValue("mqtt_bridge_hostname");
138150
}

0 commit comments

Comments
 (0)