From 6dbbbc16232670fff5ab13717ae9d01310e78615 Mon Sep 17 00:00:00 2001 From: Gus Class Date: Thu, 2 Nov 2017 15:19:14 -0700 Subject: [PATCH 1/2] Adds token refresh to HTTP example. --- iot/http_example/README.md | 1 + .../cloudiot_http_example_nodejs.js | 18 +++++++++++++++++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/iot/http_example/README.md b/iot/http_example/README.md index a4e2ed9b55..923fd61140 100644 --- a/iot/http_example/README.md +++ b/iot/http_example/README.md @@ -34,6 +34,7 @@ Options: Either RS256 (RSA) or ES256 (Eliptic Curve) --cloud_region [region] GCP cloud region --num_messages [num] Number of messages to publish. + --token_exp_mins [num] Minutes to JWT token expiration. --http_bridge_address [address] HTTP bridge address. --message_type [events|state] The message type to publish. diff --git a/iot/http_example/cloudiot_http_example_nodejs.js b/iot/http_example/cloudiot_http_example_nodejs.js index 3b98e5ec83..4233bd8de0 100644 --- a/iot/http_example/cloudiot_http_example_nodejs.js +++ b/iot/http_example/cloudiot_http_example_nodejs.js @@ -66,6 +66,12 @@ var argv = require(`yargs`) requiresArg: true, type: 'number' }, + token_exp_mins: { + default: 20, + description: 'Minutes to JWT token expiration.', + requiresArg: true, + type: 'number' + }, http_bridge_address: { default: 'cloudiot-device.googleapis.com', description: 'HTTP bridge address.', @@ -145,6 +151,15 @@ function publishAsync (messageCount, numMessages) { // If we have published fewer than numMessage messages, publish payload // messageCount + 1. setTimeout(function () { + let secsFromIssue = parseInt(Date.now() / 1000) - iatTime; + if (secsFromIssue > argv.token_exp_mins * 60) { + iatTime = parseInt(Date.now() / 1000); + console.log(`\tRefreshing token after ${secsFromIssue} seconds.`); + + authToken = createJwt(argv.project_id, argv.private_key_file, + argv.algorithm); + } + publishAsync(messageCount + 1, numMessages); }, delayMs); } @@ -161,7 +176,8 @@ const devicePath = `projects/${argv.project_id}/locations/${argv.cloud_region}/r const pathSuffix = argv.message_type === 'events' ? ':publishEvent' : ':setState'; const url = `https://${argv.http_bridge_address}/v1beta1/${devicePath}${pathSuffix}`; -const authToken = createJwt(argv.project_id, argv.private_key_file, argv.algorithm); +let iatTime = parseInt(Date.now() / 1000); +let authToken = createJwt(argv.project_id, argv.private_key_file, argv.algorithm); // Publish messages. publishAsync(1, argv.num_messages); From 5a2824846b47f62f8b998cd845c19e92b44dd483 Mon Sep 17 00:00:00 2001 From: Gus Class Date: Fri, 3 Nov 2017 11:02:10 -0700 Subject: [PATCH 2/2] Review feedback --- iot/http_example/cloudiot_http_example_nodejs.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/iot/http_example/cloudiot_http_example_nodejs.js b/iot/http_example/cloudiot_http_example_nodejs.js index 4233bd8de0..50a88b4288 100644 --- a/iot/http_example/cloudiot_http_example_nodejs.js +++ b/iot/http_example/cloudiot_http_example_nodejs.js @@ -156,8 +156,7 @@ function publishAsync (messageCount, numMessages) { iatTime = parseInt(Date.now() / 1000); console.log(`\tRefreshing token after ${secsFromIssue} seconds.`); - authToken = createJwt(argv.project_id, argv.private_key_file, - argv.algorithm); + authToken = createJwt(argv.project_id, argv.private_key_file, argv.algorithm); } publishAsync(messageCount + 1, numMessages);