Skip to content

Commit 476565a

Browse files
authored
Adds JWT refresh to http example. (GoogleCloudPlatform#906)
1 parent edaf105 commit 476565a

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

iot/api-client/http_example/src/main/java/com/google/cloud/iot/examples/HttpExample.java

+16-2
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ public static void main(String[] args) throws Exception {
145145

146146
// Create the corresponding JWT depending on the selected algorithm.
147147
String token;
148+
DateTime iat = new DateTime();
148149
if (options.algorithm.equals("RS256")) {
149150
token = createJwtRsa(options.projectId, options.privateKeyFile);
150151
} else if (options.algorithm.equals("ES256")) {
@@ -154,15 +155,28 @@ public static void main(String[] args) throws Exception {
154155
"Invalid algorithm " + options.algorithm + ". Should be one of 'RS256' or 'ES256'.");
155156
}
156157

158+
String urlPath = String.format("%s/%s/", options.httpBridgeAddress, options.apiVersion);
159+
System.out.format("Using URL: '%s'\n", urlPath);
160+
157161
// Publish numMessages messages to the HTTP bridge.
158162
for (int i = 1; i <= options.numMessages; ++i) {
159163
String payload = String.format("%s/%s-payload-%d", options.registryId, options.deviceId, i);
160164
System.out.format(
161165
"Publishing %s message %d/%d: '%s'\n",
162166
options.messageType, i, options.numMessages, payload);
163167

164-
String urlPath = String.format("%s/%s/", options.httpBridgeAddress, options.apiVersion);
165-
System.out.format("Using URL: '%s'\n", urlPath);
168+
// Refresh the authentication token if the token has expired.
169+
long secsSinceRefresh = ((new DateTime()).getMillis() - iat.getMillis()) / 1000;
170+
if (secsSinceRefresh > (options.tokenExpMins * 60)) {
171+
System.out.format("\tRefreshing token after: %d seconds\n", secsSinceRefresh);
172+
iat = new DateTime();
173+
174+
if (options.algorithm.equals("RS256")) {
175+
token = createJwtRsa(options.projectId, options.privateKeyFile);
176+
} else if (options.algorithm.equals("ES256")) {
177+
token = createJwtEs(options.projectId, options.privateKeyFile);
178+
}
179+
}
166180

167181
publishMessage(payload, urlPath, options.messageType, token, options.projectId,
168182
options.cloudRegion, options.registryId, options.deviceId);

iot/api-client/http_example/src/main/java/com/google/cloud/iot/examples/HttpExampleOptions.java

+12
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public class HttpExampleOptions {
3030
String algorithm;
3131
String cloudRegion = "us-central1";
3232
int numMessages = 100;
33+
int tokenExpMins = 20;
3334
String httpBridgeAddress = "https://cloudiot-device.googleapis.com";
3435
String apiVersion = "v1beta1";
3536
String messageType = "event";
@@ -94,6 +95,13 @@ public static HttpExampleOptions fromFlags(String[] args) {
9495
.hasArg()
9596
.desc("Number of messages to publish.")
9697
.build());
98+
options.addOption(
99+
Option.builder()
100+
.type(Number.class)
101+
.longOpt("token_exp_minutes")
102+
.hasArg()
103+
.desc("Minutes to JWT token refresh (token expiration time).")
104+
.build());
97105
options.addOption(
98106
Option.builder()
99107
.type(String.class)
@@ -133,6 +141,10 @@ public static HttpExampleOptions 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("http_bridge_address")) {
137149
res.httpBridgeAddress = commandLine.getOptionValue("http_bridge_address");
138150
}

0 commit comments

Comments
 (0)