Skip to content
This repository was archived by the owner on Jan 6, 2023. It is now read-only.

Commit b82ab92

Browse files
committed
Removes JWT refresh guards and adds parameter for expiration
1 parent a41c002 commit b82ab92

File tree

6 files changed

+22
-24
lines changed

6 files changed

+22
-24
lines changed

examples/Esp32-lwmqtt/ciotc_config.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ const char *private_key_str =
4545
"63:38:b0:90:57:57:e0:c0:9a:e8:6f:06:0c:d9:ee:"
4646
"31:41";
4747

48+
// Time (seconds) to expire token += 20 minutes for drift
49+
const int jwt_exp_secs = 3600; // Maximum 24H (3600*24)
50+
4851
// To get the certificate for your region run:
4952
// openssl s_client -showcerts -connect mqtt.googleapis.com:8883
5053
// Copy the certificate (all lines between and including ---BEGIN CERTIFICATE---

examples/Esp32-lwmqtt/esp32-mqtt.h

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,9 @@ String getDefaultSensor() {
5050
}
5151

5252
String getJwt() {
53-
if (iss == 0 || time(nullptr) - iss > 3600) { // TODO: exp in device
54-
iss = time(nullptr);
55-
Serial.println("Refreshing JWT");
56-
jwt = device->createJWT(iss);
57-
} else {
58-
Serial.println("Reusing still-valid JWT");
59-
}
53+
iss = time(nullptr);
54+
Serial.println("Refreshing JWT");
55+
jwt = device->createJWT(iss, jwt_exp_secs);
6056
return jwt;
6157
}
6258

examples/Esp8266-lwmqtt/ciotc_config.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ const char* private_key_str =
4141
"07:fd:ed:22:0d:03:2b:a6:b1:b6:04:0b:d5:9b:49:"
4242
"7d:ca";
4343

44+
// Time (seconds) to expire token += 20 minutes for drift
45+
const int jwt_exp_secs = 3600; // Maximum 24H (3600*24)
46+
4447
// Use the root certificate to verify tls connection rather than
4548
// using a fingerprint.
4649
//

examples/Esp8266-lwmqtt/esp8266_mqtt.h

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,12 @@ String getDefaultSensor() {
5050
}
5151

5252
String getJwt() {
53-
if (iss == 0 || time(nullptr) - iss > 3600) { // TODO: exp in device
54-
// Disable software watchdog as these operations can take a while.
55-
ESP.wdtDisable();
56-
iss = time(nullptr);
57-
Serial.println("Refreshing JWT");
58-
jwt = device->createJWT(iss);
59-
ESP.wdtEnable(0);
60-
}
53+
// Disable software watchdog as these operations can take a while.
54+
ESP.wdtDisable();
55+
iss = time(nullptr);
56+
Serial.println("Refreshing JWT");
57+
jwt = device->createJWT(iss, jwt_exp_secs);
58+
ESP.wdtEnable(0);
6159
return jwt;
6260
}
6361

@@ -142,9 +140,6 @@ void setupCloudIoT() {
142140
netClient = new WiFiClientSecure();
143141
setupWifi();
144142

145-
// Device/Time OK, ESP8266 refresh JWT
146-
Serial.println(getJwt());
147-
148143
// ESP8266 WiFi secure initialization
149144
setupCert();
150145

examples/MKR1000-lwmqtt/ciotc_config.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ const char* private_key_str =
4141
"07:fd:ed:22:0d:03:2b:a6:b1:b6:04:0b:d5:9b:49:"
4242
"7d:ca";
4343

44+
// Time (seconds) to expire token += 20 minutes for drift
45+
const int jwt_exp_secs = 3600; // Maximum 24H (3600*24)
46+
4447
// In case we ever need extra topics
4548
const int ex_num_topics = 0;
4649
const char* ex_topics[ex_num_topics];

examples/MKR1000-lwmqtt/mkr1000-mqtt.h

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,10 @@ String getDefaultSensor() {
4848
}
4949

5050
String getJwt() {
51-
if (iss == 0 || WiFi.getTime() - iss > 3600) { // TODO: exp in device
52-
// Disable software watchdog as these operations can take a while.
53-
Serial.println("Refreshing JWT");
54-
iss = WiFi.getTime();
55-
jwt = device->createJWT(iss);
56-
}
51+
// Disable software watchdog as these operations can take a while.
52+
Serial.println("Refreshing JWT");
53+
iss = WiFi.getTime();
54+
jwt = device->createJWT(iss, jwt_exp_secs);
5755
return jwt;
5856
}
5957

0 commit comments

Comments
 (0)