Skip to content

Commit

Permalink
Allowed access in Test Mode, fixed FirebaseJson bug and rename the cJ…
Browse files Browse the repository at this point in the history
…SON functions and variables.
  • Loading branch information
mobizt committed Dec 20, 2021
1 parent 746bc68 commit 6e01758
Show file tree
Hide file tree
Showing 32 changed files with 518 additions and 403 deletions.
36 changes: 26 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Firebase Arduino Client Library for ESP8266 and ESP32


Google's Firebase Arduino Client Library for ESP8266 and ESP32 v2.7.2
Google's Firebase Arduino Client Library for ESP8266 and ESP32 v2.7.3


This library supports ESP8266 and ESP32 MCU from Espressif. The following are platforms in which the libraries are also available (RTDB only).
Expand Down Expand Up @@ -40,7 +40,7 @@ The library focused on WiFi and Ethernet operations, adding the ability for mobi

To make this library to support all-in-one module that has ESP32 and GSM modem on board, devided this library into small variant which is not compatible with native connectivity and can make the library too complicated.

In addition, some mobile modem can’t handle the SSL certificate and out date TLS supported.
In addition, some mobile modem can’t handle the SSL certificate and out dated TLS supported.

Creating the new Firebase library that specific to only GSM connectivity concerns the scope of supported MCUs and the SSL library to use on that device and memory available which are most important.

Expand All @@ -61,7 +61,9 @@ Creating the new Firebase library that specific to only GSM connectivity concern

* **Supports Cloud Functions for Firebase**

* **Built-in JSON parser and builder.**
* **Built-in JSON editor and deserializer.**

* **Support external Heap via SRAM/PSRAM in ESP8266 and ESP32.**

* **Supports ethernet in ESP32 using LAN8720, TLK110 and IP101 Ethernet modules and ESP8266 using ENC28J60, W5100 and W5500 Ethernet modules.**

Expand Down Expand Up @@ -117,9 +119,9 @@ For Arduino IDE, download zip file from the repository (Github page) by select *

From Arduino IDE, select menu **Sketch** -> **Include Library** -> **Add .ZIP Library...**.

Choose **Firebase-ESP8266-master.zip** that previously downloaded.
Choose **Firebase-ESP-Client-main.zip** that previously downloaded.

Go to menu **Files** -> **Examples** -> **Firebase-ESP-Client-master** and choose one from examples.
Go to menu **Files** -> **Examples** -> **Firebase-ESP-Client-main** and choose one from examples.



Expand All @@ -137,9 +139,7 @@ See [function description](/src/README.md) for all available functions.

```cpp


//Include WiFi library
#include <FirebaseESP8266.h>
#if defined(ESP32)
#include <WiFi.h>
#elif defined(ESP8266)
Expand Down Expand Up @@ -367,9 +367,6 @@ The authenticate using the legacy token (database secret) does not have these de
This library focuses on the user privacy and user data protection which follows Google authentication processes. Setting the security rules to allow public access read and write, is not recommended even the data transmision time in this case was significantly reduced as it does not require any auth token then the overall data size was reduced, but anyone can steal, modify, or delete data in your database.


Some users may have the question why the time for sending/receiving data with this library was increased when using the different authentication methods which someone compares this with other libraries and platforms which some claims to be fast or has low latency in operation as it does not use any auth token and always requires public read/write allowance security rules which is not good for your privacy and data.


Once the auth token is importance and when it was created and ready for authentication process, the data transmission time will depend on the time used in SSL/TLS handshake process (only for new session opening), the size of http header (included auth token size) and payload to be transmitted and the SSL client buffer reserved size especially in ESP8266.


Expand Down Expand Up @@ -415,6 +412,25 @@ With pushAsync and setAsync, the payload response will be ignored and the next d



### Access in Test Mode (No Auth)

In Test Mode, token generation will be ignored and no authentication applied to the request.

For RTDB, you can access RTDB database in Test Mode by set the security rules like this.

```json
{
"rules": {
".read": true,
".write": true
}
}
```
And set the `config.signer.test_mode = true;`, see [TestMode.ino](/examples/Authentications/TestMode/TestMode.ino) example.

For Cloud Firestore and Firebase Storage, also set `config.signer.test_mode = true;` and modify the rules for the public access to test.


### The authenication credentials and prerequisites


Expand Down
101 changes: 101 additions & 0 deletions examples/Authentications/TestMode/TestMode.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@

/**
* Created by K. Suwatchai (Mobizt)
*
* Email: [email protected]
*
* Github: https://github.com/mobizt
*
* Copyright (c) 2021 mobizt
*
*/

/** This example will show how to access the RTDB in Test Mode (no authentication).
*/

#if defined(ESP32)
#include <WiFi.h>
#elif defined(ESP8266)
#include <ESP8266WiFi.h>
#endif
#include <Firebase_ESP_Client.h>

//Provide the RTDB payload printing info and other helper functions.
#include <addons/RTDBHelper.h>

/* 1. Define the WiFi credentials */
#define WIFI_SSID "WIFI_AP"
#define WIFI_PASSWORD "WIFI_PASSWORD"

/* 2. Define the RTDB URL */
#define DATABASE_URL "URL" //<databaseName>.firebaseio.com or <databaseName>.<region>.firebasedatabase.app

/* 3. Define the Firebase Data object */
FirebaseData fbdo;

/* 4, Define the FirebaseAuth data for authentication data */
FirebaseAuth auth;

/* Define the FirebaseConfig data for config data */
FirebaseConfig config;

unsigned long dataMillis = 0;
int count = 0;

void setup()
{

Serial.begin(115200);

WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
Serial.print("Connecting to Wi-Fi");
while (WiFi.status() != WL_CONNECTED)
{
Serial.print(".");
delay(300);
}
Serial.println();
Serial.print("Connected with IP: ");
Serial.println(WiFi.localIP());
Serial.println();

Serial.printf("Firebase Client v%s\n\n", FIREBASE_CLIENT_VERSION);

/* Assign the certificate file (optional) */
//config.cert.file = "/cert.cer";
//config.cert.file_storage = StorageType::FLASH;

/* Assign the database URL(required) */
config.database_url = DATABASE_URL;

config.signer.test_mode = true;

/**
Set the database rules to allow public read and write.
{
"rules": {
".read": true,
".write": true
}
}
*/

Firebase.reconnectWiFi(true);

/* Initialize the library with the Firebase authen and config */
Firebase.begin(&config, &auth);

//Or use legacy authenticate method
//Firebase.begin(DATABASE_URL, DATABASE_SECRET);
}

void loop()
{
if (millis() - dataMillis > 5000)
{
dataMillis = millis();
Serial.printf("Set int... %s\n", Firebase.RTDB.setInt(&fbdo, "/test/int", count++) ? "ok" : fbdo.errorReason().c_str());
}
}
2 changes: 2 additions & 0 deletions examples/FirebaseJson/Client/ArduinoMQTT/ArduinoMQTT.ino
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
#include <ArduinoMqttClient.h>

//Enable Arduino MQTT library after include the library and before include the FirebaseJson.
//If you are using the library that built in the FirebaseJson and get the compilation error,
//move #define FBJS_ENABLE_ARDUINO_MQTT to the top above that library inclusion.
#define FBJS_ENABLE_ARDUINO_MQTT
#include <FirebaseJson.h>

Expand Down
2 changes: 2 additions & 0 deletions examples/FirebaseJson/Client/LWMQTTClient/LWMQTTClient.ino
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
#include <MQTTClient.h>

//Enable LW MQTT library after include the library and before include the FirebaseJson.
//If you are using the library that built in the FirebaseJson and get the compilation error,
//move #define FBJS_ENABLE_LW_MQTT to the top above that library inclusion.
#define FBJS_ENABLE_LW_MQTT
#include <FirebaseJson.h>

Expand Down
2 changes: 2 additions & 0 deletions examples/FirebaseJson/Client/WiFiClient/WiFiClient.ino
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
#include <WiFiClient.h>

//Enable WiFiClientSecure library after include the library and before include the FirebaseJson.
//If you are using the library that built in the FirebaseJson and get the compilation error,
//move #define FBJS_ENABLE_WIFI_CLIENT to the top above that library inclusion.
#define FBJS_ENABLE_WIFI_CLIENT
#include <FirebaseJson.h>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
#include <WiFiClientSecure.h>

//Enable WiFiClientSecure library after include the library and before include the FirebaseJson.
//If you are using the library that built in the FirebaseJson and get the compilation error,
//move #define FBJS_ENABLE_WIFI_CLIENT_SECURE to the top above that library inclusion.
#define FBJS_ENABLE_WIFI_CLIENT_SECURE
#include <FirebaseJson.h>

Expand Down
4 changes: 3 additions & 1 deletion examples/FirebaseJson/Stream/File/File.ino
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@ void loop()
File file2 = DEFAULT_FLASH_FS.open("/test2.txt", "a");
json.toString(file2);

//if you want to append new line to separate each JSON data (recommended)
//Due to bugs in SPIFFS print
//https://github.com/esp8266/Arduino/issues/8372
//append new line to separate each JSON data (recommended)
file2.println();

file2.close();
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": "Firebase Arduino Client Library for ESP8266 and ESP32",
"version": "2.7.2",
"version": "2.7.3",
"keywords": "communication, REST, esp32, esp8266, arduino",
"description": "The library supports Firebase products e.g. Realtime database, Cloud Firestore database, Firebase Storage and Google Cloud Storage, Cloud Functions for Firebase and Cloud Messaging.",
"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=Firebase Arduino Client Library for ESP8266 and ESP32

version=2.7.2
version=2.7.3

author=Mobizt

Expand Down
89 changes: 46 additions & 43 deletions src/Firebase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,51 +51,54 @@ Firebase_ESP_Client::~Firebase_ESP_Client()
void Firebase_ESP_Client::begin(FirebaseConfig *config, FirebaseAuth *auth)
{
init(config, auth);

if (cfg->service_account.json.path.length() > 0)
{
if (!Signer.parseSAFile())
cfg->signer.tokens.status = token_status_uninitialized;
}

if (strlen(cfg->signer.tokens.legacy_token) > 0)
{
Signer.setTokenType(token_type_legacy_token);
cfg->_int.auth_token = cfg->signer.tokens.legacy_token;
cfg->_int.ltok_len = strlen(cfg->signer.tokens.legacy_token);
cfg->_int.rtok_len = 0;
cfg->_int.atok_len = 0;
}
else if (Signer.tokenSigninDataReady())
{
cfg->signer.idTokenCutomSet = false;

if (auth->token.uid.length() == 0)
Signer.setTokenType(token_type_oauth2_access_token);
else
Signer.setTokenType(token_type_custom_token);
}
else if (Signer.userSigninDataReady() || cfg->signer.anonymous)
Signer.setTokenType(token_type_id_token);

struct fb_esp_url_info_t uinfo;
cfg->_int.fb_auth_uri = cfg->signer.tokens.token_type == token_type_legacy_token || cfg->signer.tokens.token_type == token_type_id_token;

if (cfg->host.length() > 0)
cfg->database_url = cfg->host;

if (cfg->database_url.length() > 0)
if (!cfg->signer.test_mode)
{
ut->getUrlInfo(cfg->database_url.c_str(), uinfo);
cfg->database_url = uinfo.host.c_str();
}

if (cfg->cert.file.length() > 0)
{
if (cfg->cert.file_storage == mem_storage_type_sd && !cfg->_int.fb_sd_rdy)
cfg->_int.fb_sd_rdy = ut->sdTest(cfg->_int.fb_file);
else if ((cfg->cert.file_storage == mem_storage_type_flash) && !cfg->_int.fb_flash_rdy)
ut->flashTest();
if (cfg->service_account.json.path.length() > 0)
{
if (!Signer.parseSAFile())
cfg->signer.tokens.status = token_status_uninitialized;
}

if (strlen(cfg->signer.tokens.legacy_token) > 0)
{
Signer.setTokenType(token_type_legacy_token);
cfg->_int.auth_token = cfg->signer.tokens.legacy_token;
cfg->_int.ltok_len = strlen(cfg->signer.tokens.legacy_token);
cfg->_int.rtok_len = 0;
cfg->_int.atok_len = 0;
}
else if (Signer.tokenSigninDataReady())
{
cfg->signer.idTokenCutomSet = false;

if (auth->token.uid.length() == 0)
Signer.setTokenType(token_type_oauth2_access_token);
else
Signer.setTokenType(token_type_custom_token);
}
else if (Signer.userSigninDataReady() || cfg->signer.anonymous)
Signer.setTokenType(token_type_id_token);

struct fb_esp_url_info_t uinfo;
cfg->_int.fb_auth_uri = cfg->signer.tokens.token_type == token_type_legacy_token || cfg->signer.tokens.token_type == token_type_id_token;

if (cfg->host.length() > 0)
cfg->database_url = cfg->host;

if (cfg->database_url.length() > 0)
{
ut->getUrlInfo(cfg->database_url.c_str(), uinfo);
cfg->database_url = uinfo.host.c_str();
}

if (cfg->cert.file.length() > 0)
{
if (cfg->cert.file_storage == mem_storage_type_sd && !cfg->_int.fb_sd_rdy)
cfg->_int.fb_sd_rdy = ut->sdTest(cfg->_int.fb_file);
else if ((cfg->cert.file_storage == mem_storage_type_flash) && !cfg->_int.fb_flash_rdy)
ut->flashTest();
}
}

Signer.handleToken();
Expand Down
9 changes: 5 additions & 4 deletions src/Firebase_ESP_Client.h
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
#ifndef FIREBASE_CLIENT_VERSION
#define FIREBASE_CLIENT_VERSION "2.7.2"
#define FIREBASE_CLIENT_VERSION "2.7.3"
#endif

/**
* Google's Firebase ESP Client Main class, Firebase_ESP_Client.h v2.7.2
* Google's Firebase ESP Client Main class, Firebase_ESP_Client.h v2.7.3
*
* This library supports Espressif ESP8266 and ESP32 MCUs
*
* Created December 16, 2021
* Created December 20, 2021
*
* Updates:
* - Fixed RTDB setTimestamp and pushTimestamp bugs.
* - Fixed FirebaseJson bug and rename the cJSON functions and variables.
* - Allowed access in Test Mode (no authentication).
*
*
* This work is a part of Firebase ESP Client library
Expand Down
2 changes: 1 addition & 1 deletion src/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Firebase Arduino Client Library for ESP8266 and ESP32


Google's Firebase Arduino Client Library for ESP8266 and ESP32 v2.7.2
Google's Firebase Arduino Client Library for ESP8266 and ESP32 v2.7.3


The default filessystem used in the library is flash and SD.
Expand Down
3 changes: 2 additions & 1 deletion src/common.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

/**
* Created December 19, 2021
* Created December 20, 2021
*
* This work is a part of Firebase ESP Client library
* Copyright (c) 2021 K. Suwatchai (Mobizt)
Expand Down Expand Up @@ -652,6 +652,7 @@ struct fb_esp_token_signer_resources_t
{
int step = 0;
int attempts = 0;
bool test_mode = false;
bool signup = false;
bool anonymous = false;
bool idTokenCutomSet = false;
Expand Down
Loading

0 comments on commit 6e01758

Please sign in to comment.