Skip to content

Commit

Permalink
Fix the time issue.
Browse files Browse the repository at this point in the history
  • Loading branch information
mobizt committed May 4, 2021
1 parent 29778d7 commit 5b627e0
Show file tree
Hide file tree
Showing 12 changed files with 3,638 additions and 3,562 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Google OAuth2.0 Access Token generation Arduino Library v1.0.3
# Google OAuth2.0 Access Token generation Arduino Library v1.0.4


This is the library will create the OAuth2.0 access token used in the Google API's http request (REST).
Expand Down
9 changes: 6 additions & 3 deletions examples/Basic/Basic.ino
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
/** These credentials are taken from Service Account key file (JSON)
* https://cloud.google.com/iam/docs/service-accounts
*/
#define PROJECT_ID "The project ID" //Taken from "project_id" key in JSON file.
#define CLIENT_EMAIL "Client Email" //Taken from "client_email" key in JSON file.
#define PRIVATE_KEY_ID "Private key ID" //Taken from "private_key_id" in JSON file.
#define PROJECT_ID "The project ID" //Taken from "project_id" key in JSON file.
#define CLIENT_EMAIL "Client Email" //Taken from "client_email" key in JSON file.
#define PRIVATE_KEY_ID "Private key ID" //Taken from "private_key_id" in JSON file.
const char PRIVATE_KEY[] PROGMEM = "-----BEGIN PRIVATE KEY-----\\n-----END PRIVATE KEY-----\n"; //Taken from "private_key" key in JSON file.

SignerConfig config;
Expand Down Expand Up @@ -66,6 +66,9 @@ void setup()
/** Assign the callback function for token ggeneration status (optional) */
config.token_status_callback = tokenStatusCallback;

//To set the device time without NTP time acquisition.
//Signer.setSystemTime(<timestamp>);

/* Create token */
Signer.begin(&config);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ void setup()
/** Assign the callback function for token ggeneration status (optional) */
config.token_status_callback = tokenStatusCallback;

//To set the device time without NTP time acquisition.
//Signer.setSystemTime(<timestamp>);

/* Create token */
Signer.begin(&config);

Expand Down
1 change: 1 addition & 0 deletions keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ getTokenError KEYWORD2
sdBegin KEYWORD2
getExpiredTimestamp KEYWORD2
refreshToken KEYWORD2
setSystemTime KEYWORD2


######################################
Expand Down
2 changes: 1 addition & 1 deletion library.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ESP Signer",
"version": "1.0.3",
"version": "1.0.4",
"keywords": "communication, REST, esp32, esp8266, arduino",
"description": "The Google OAuth2.0 access token generation for Google REST API's http request.",
"repository": {
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name=ESP Signer

version=1.0.3
version=1.0.4

author=Mobizt

Expand Down
24 changes: 21 additions & 3 deletions src/ESPSigner.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
/**
* Google's OAuth2.0 Access token Generation class, Signer.h version 1.0.3
* Google's OAuth2.0 Access token Generation class, Signer.h version 1.0.4
*
* This library use RS256 for signing algorithm.
*
* The signed JWT token will be generated and exchanged with the access token in the final generating process.
*
* This library supports Espressif ESP8266 and ESP32
*
* Created April 4, 2021
* Created May 4, 2021
*
* This work is a part of Firebase ESP Client library
* Copyright (c) 2021 K. Suwatchai (Mobizt)
Expand Down Expand Up @@ -207,6 +207,10 @@ bool ESP_Signer::handleToken()
if (!config)
return false;

//if the time was set (changed) after token has been generated, update its expiration
if (config->signer.tokens.expires > 0 && config->signer.tokens.expires < ESP_DEFAULT_TS && time(nullptr) > ESP_DEFAULT_TS)
config->signer.tokens.expires += time(nullptr) - (millis() - config->signer.tokens.last_millis) / 1000 - 60;

if (config->signer.tokens.token_type == esp_signer_token_type_oauth2_access_token && (time(nullptr) > config->signer.tokens.expires - config->signer.preRefreshSeconds || config->signer.tokens.expires == 0))
{

Expand Down Expand Up @@ -1137,7 +1141,12 @@ bool ESP_Signer::requestTokens()
config->signer.json->get(*config->signer.data, tmp);
ut->delS(tmp);
if (config->signer.data->success)
config->signer.tokens.expires = time(nullptr) + atoi(config->signer.data->stringValue.c_str());
{
time_t ts = time(nullptr);
unsigned long ms = millis();
config->signer.tokens.expires = ts + atoi(config->signer.data->stringValue.c_str());
config->signer.tokens.last_millis = ms;
}
}
return handleSignerError(0);
}
Expand All @@ -1152,6 +1161,10 @@ void ESP_Signer::checkToken()
if (!config)
return;

//if the time was set (changed) after token has been generated, update its expiration
if (config->signer.tokens.expires > 0 && config->signer.tokens.expires < ESP_DEFAULT_TS && time(nullptr) > ESP_DEFAULT_TS)
config->signer.tokens.expires += time(nullptr) - (millis() - config->signer.tokens.last_millis) / 1000 - 60;

if (config->signer.tokens.token_type == esp_signer_token_type_oauth2_access_token && (time(nullptr) > config->signer.tokens.expires - config->signer.preRefreshSeconds || config->signer.tokens.expires == 0))
handleToken();
}
Expand Down Expand Up @@ -1403,6 +1416,11 @@ void ESP_Signer::errorToString(int httpCode, std::string &buff)
}
}

bool ESP_Signer::setSystemTime(time_t ts)
{
return ut->setTimestamp(ts) == 0;
}

ESP_Signer Signer = ESP_Signer();

#endif
12 changes: 9 additions & 3 deletions src/ESPSigner.h
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
/**
* Google's OAuth2.0 Access token Generation class, Signer.h version 1.0.3
* Google's OAuth2.0 Access token Generation class, Signer.h version 1.0.4
*
* This library use RS256 for signing algorithm.
*
* The signed JWT token will be generated and exchanged with the access token in the final generating process.
*
* This library supports Espressif ESP8266 and ESP32
*
* Created April 4, 2021
* Created May 4, 2021
*
* This work is a part of Firebase ESP Client library
* Copyright (c) 2021 K. Suwatchai (Mobizt)
Expand Down Expand Up @@ -40,7 +40,6 @@
#include <Arduino.h>
#include "SignerUtils.h"


class ESP_Signer
{

Expand Down Expand Up @@ -117,6 +116,13 @@ class ESP_Signer
*/
void refreshToken();

/** Set system time with timestamp.
*
* @param ts timestamp in seconds from midnight Jan 1, 1970.
* @return Boolean type status indicates the success of the operation.
*/
bool setSystemTime(time_t ts);

/** SD card config with GPIO pins.
*
* @param ss - SPI Chip/Slave Select pin.
Expand Down
3 changes: 2 additions & 1 deletion src/SignerCommon.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
#define MAX_REDIRECT 5
#define WIFI_RECONNECT_TIMEOUT 10000
#define MAX_EXCHANGE_TOKEN_ATTEMPTS 5
#define ESP_DEFAULT_TS 1510644967
#define ESP_DEFAULT_TS 1618971013
#define ESP_SIGNER_DEFAULT_RESPONSE_BUFFER_SIZE 2560

enum esp_signer_mem_storage_type
Expand Down Expand Up @@ -123,6 +123,7 @@ struct esp_signer_auth_token_info_t
std::string jwt;
std::string scope;
unsigned long expires = 0;
unsigned long last_millis = 0;
esp_signer_auth_token_type token_type = esp_signer_token_type_undefined;
esp_signer_auth_token_status status = esp_signer_token_status_uninitialized;
struct esp_signer_auth_token_error_t error;
Expand Down
10 changes: 8 additions & 2 deletions src/SignerUtils.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/**
* Util class, SignerUtils.h version 1.0.0
* Util class, SignerUtils.h version 1.0.1
*
* This library supports Espressif ESP8266 and ESP32
*
* Created March 23, 2021
* Created May 4, 2021
*
* This work is a part of Firebase ESP Client library
* Copyright (c) 2021 K. Suwatchai (Mobizt)
Expand Down Expand Up @@ -1391,6 +1391,12 @@ class SignerUtils
return status;
}

int setTimestamp(time_t ts)
{
struct timeval tm = {ts, 0}; // sec, us
return settimeofday((const timeval *)&tm, 0);
}

private:
};

Expand Down
Loading

0 comments on commit 5b627e0

Please sign in to comment.