Skip to content

Commit

Permalink
Add support user re-authentication.
Browse files Browse the repository at this point in the history
  • Loading branch information
mobizt committed Oct 6, 2021
1 parent 5edccfe commit 517d237
Show file tree
Hide file tree
Showing 13 changed files with 226 additions and 20 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/compile_examples.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ jobs:
- "examples/Authentications/SignInAsUser/CustomToken/CustomToken.ino"
- "examples/Authentications/SignInAsUser/CustomTokenFile/CustomTokenFile.ino"
- "examples/Authentications/SignInAsUser/EmailPassword/EmailPassword.ino"
- "examples/Authentications/ReAuthenticate/ReAuthenticate.ino"
- "examples/Authentications/UserManagement/ResetPassword/ResetPassword.ino"
- "examples/Authentications/UserManagement/SendVerification/SendVerification.ino"
- "examples/Authentications/UserManagement/Signup/Signup.ino"
Expand Down Expand Up @@ -147,6 +148,7 @@ jobs:
- "examples/Authentications/SignInAsUser/CustomToken/CustomToken.ino"
- "examples/Authentications/SignInAsUser/CustomTokenFile/CustomTokenFile.ino"
- "examples/Authentications/SignInAsUser/EmailPassword/EmailPassword.ino"
- "examples/Authentications/ReAuthenticate/ReAuthenticate.ino"
- "examples/Authentications/UserManagement/ResetPassword/ResetPassword.ino"
- "examples/Authentications/UserManagement/SendVerification/SendVerification.ino"
- "examples/Authentications/UserManagement/Signup/Signup.ino"
Expand Down
54 changes: 51 additions & 3 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.5.2
Google's Firebase Arduino Client Library for ESP8266 and ESP32 v2.5.3


This library supports ESP8266 and ESP32 MCU from Espressif. The following are platforms in which the libraries are also available (RTDB only).
Expand All @@ -26,9 +26,23 @@ This library supports ESP8266 and ESP32 MCU from Espressif. The following are pl



## Unsupported AT command and Mobile Network modem Bridge
## Unsupported AT command and mobile modem bridge

The library access to the Firebase server through the WiFi for the internet connection. The others UART/Serial mobile network modem bridge connection and AT commands were not supported.
The library required the access to the Firebase server through the native WiFi or Ethernet for the internet connection.

The library does not support the mobile GPRS/3G/4G modem connected to MCU via serial port or any stand alone, all in one ESP32/GSM module.

There are incompatibilities and security concerns when adding mobile modem with this library.

Mobile modem is not native and it required library (driver) to handle network connection using AT commands.

The library focused on WiFi and Ethernet operations, adding the ability for mobile modem breaks the compatibilities among the network connections.

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.

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 Down Expand Up @@ -423,6 +437,40 @@ To get the database URL and secret (legacy token).



For server SSL authentication by providing the server root certificate.

Server SSL certificate verification is the process to ensure that the server that client is being connected is a trusted (valid) server instead of fake server.

The Google's GlobalSign R2 root certificate can be download from https://pki.goog/repository/

Select the .PEM (base-64 encoded string) or .DER (binary) file to download.

From the test as of July 2021, GlobalSign Root CA was missing from Google server, the certificate chain, GTS Root R1 can be used instead of root certificate.

![Firebase Host](/media/images/PEM_Download.png)

Below is how to assign the certificate data for server verification.

```cpp
/* In case the certificate data was used */
config.cert.data = rootCACert;

//Or custom set the root certificate for each FirebaseData object
fbdo.setCert(rootCACert);

/* Or assign the certificate file */

/** From the test as of July 2021, GlobalSign Root CA was missing from Google server
* as described above, GTS Root R1 (gsr1.pem or gsr1.der) can be used instead.
* ESP32 Arduino SDK supports PEM format only even mBedTLS supports DER format too.
* ESP8266 SDK supports both PEM and DER format certificates.
*/
//config.cert.file = "/gsr1.pem";
//config.cert.file_storage = mem_storage_type_flash; //or mem_storage_type_sd
```



## Excludes the unused classes to save memory


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

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

/** This example will show how to re-authenticate after signed in with Email and password.
*/

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

//Provide the token generation process info.
#include <addons/TokenHelper.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 API key
*
* The API key (required) can be obtained since you created the project and set up
* the Authentication in Firebase console. Then you will get the API key from
* Firebase project Web API key in Project settings, on General tab should show the
* Web API Key.
*
* You may need to enable the Identity provider at https://console.cloud.google.com/customer-identity/providers
* Select your project, click at ENABLE IDENTITY PLATFORM button.
* The API key also available by click at the link APPLICATION SETUP DETAILS.
*
*/
#define API_KEY "API_KEY"

/* 3. Define the user Email and password that already registerd or added in your project */
#define USER_EMAIL1 "USER_EMAIL1"
#define USER_PASSWORD1 "USER_PASSWORD1"

#define USER_EMAIL2 "USER_EMAIL2"
#define USER_PASSWORD2 "USER_PASSWORD2"

/* 4. If work with RTDB, define the RTDB URL */
#define DATABASE_URL "URL" //<databaseName>.firebaseio.com or <databaseName>.<region>.firebasedatabase.app

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

/* 6. Define the FirebaseAuth data for authentication data */
FirebaseAuth auth;

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

unsigned long dataMillis = 0;
int count = 0;
bool toggleUser = false;

void signIn(const char *email, const char *password)
{
/* Assign the user sign in credentials */
auth.user.email = email;
auth.user.password = password;

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

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 api key (required) */
config.api_key = API_KEY;

/* Assign the RTDB URL */
config.database_url = DATABASE_URL;

Firebase.reconnectWiFi(true);
fbdo.setResponseSize(4096);

/* Assign the callback function for the long running token generation task */
config.token_status_callback = tokenStatusCallback; //see addons/TokenHelper.h

/** Assign the maximum retry of token generation */
config.max_token_generation_retry = 5;

/** Sign in as user 1 */
signIn(USER_EMAIL1, USER_PASSWORD1);
}

void loop()
{
if (millis() - dataMillis > 5000)
{
dataMillis = millis();

if (Firebase.ready())
{
String path = "/UsersData/";
path += auth.token.uid.c_str(); //<- user uid of current user that sign in with Emal/Password
path += "/test/int";
Serial.printf("Current UID: %s\n", auth.token.uid.c_str());
Serial.printf("Set int... %s\n", Firebase.RTDB.setInt(&fbdo, path.c_str(), count++) ? "ok" : fbdo.errorReason().c_str());

//Swap users every 5 times
if (count % 5 == 0)
{
Serial.println();

if (toggleUser)
signIn(USER_EMAIL1, USER_PASSWORD1); /** Sign in as user 1 */
else
signIn(USER_EMAIL2, USER_PASSWORD2); /** Sign in as user 2 */
toggleUser = !toggleUser;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ void setup()
Serial.printf("Firebase Client v%s\n\n", FIREBASE_CLIENT_VERSION);

/* Assign the certificate file (optional) */
/** From the test on July 2021, GlobalSign Root CA was missing from Google server
/** From the test as of July 2021, GlobalSign Root CA was missing from Google server
* when checking with https://www.sslchecker.com/sslchecker.
* The certificate chain, GTS Root R1 can be used instead.
*
Expand Down
10 changes: 6 additions & 4 deletions examples/RTDB/Basic/Basic.ino
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ void setup()
//config.database_url = DATABASE_URL;
//config.signer.tokens.legacy_token = "<database secret>";

//////////////////////////////////////////////////////////////////////////////////////////////
//Please make sure the device free Heap is not lower than 80 k for ESP32 and 10 k for ESP8266,
//otherwise the SSL connection will fail.
//////////////////////////////////////////////////////////////////////////////////////////////

Firebase.begin(&config, &auth);

//Comment or pass false value when WiFi reconnection will control by your code or third party library
Expand Down Expand Up @@ -153,7 +158,7 @@ void loop()
Serial.printf("Get string... %s\n", Firebase.RTDB.getString(&fbdo, "/test/string") ? fbdo.to<const char *>() : fbdo.errorReason().c_str());

Serial.println();

//For generic set/get functions.

//For generic set, use Firebase.RTDB.set(&fbdo, <path>, <any variable or value>)
Expand All @@ -170,13 +175,10 @@ void loop()
//fb_esp_rtdb_data_type_boolean, fb_esp_rtdb_data_type_string, fb_esp_rtdb_data_type_json,
//fb_esp_rtdb_data_type_array, fb_esp_rtdb_data_type_blob, and fb_esp_rtdb_data_type_file (10)


count++;
}
}



/// PLEASE AVOID THIS ////

//Please avoid the following inappropriate and inefficient use cases
Expand Down
2 changes: 1 addition & 1 deletion examples/RTDB/BasicCert/BasicCert.ino
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@

/* Google Root CA can be downloaded from https://pki.goog/repository/ */

/** From the test on July 2021, GlobalSign Root CA was missing from Google server
/** From the test as of July 2021, GlobalSign Root CA was missing from Google server
* when checking with https://www.sslchecker.com/sslchecker.
* The certificate chain, GTS Root R1 can be used instead.
*/
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.5.2",
"version": "2.5.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.5.2
version=2.5.3

author=Mobizt

Expand Down
Binary file added media/images/PEM_Download.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 10 additions & 2 deletions src/Firebase.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* The Firebase class, Firebase.cpp v1.0.4
* The Firebase class, Firebase.cpp v1.0.5
*
* Created September 26, 2021
* Created October 6, 2021
*
* The MIT License (MIT)
* Copyright (c) 2021 K. Suwatchai (Mobizt)
Expand Down Expand Up @@ -150,6 +150,10 @@ void Firebase_ESP_Client::init(FirebaseConfig *config, FirebaseAuth *auth)

cfg->_int.fb_reconnect_wifi = WiFi.getAutoReconnect();

cfg->signer.lastReqMillis = 0;
cfg->signer.tokens.expires = 0;
ut->clearS(cfg->_int.auth_token);

cfg->signer.signup = false;
Signer.begin(ut, cfg, auth);
ut->clearS(cfg->signer.tokens.error.message);
Expand Down Expand Up @@ -347,6 +351,10 @@ void FIREBASE_CLASS::init(FirebaseConfig *config, FirebaseAuth *auth)
#endif
cfg->_int.fb_reconnect_wifi = WiFi.getAutoReconnect();

cfg->signer.lastReqMillis = 0;
cfg->signer.tokens.expires = 0;
ut->clearS(cfg->_int.auth_token);

cfg->signer.signup = false;
cfg->signer.signup = false;
Signer.begin(ut, this->cfg, this->auth);
Expand Down
4 changes: 2 additions & 2 deletions src/Firebase.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@

/**
* The Firebase class, Firebase.h v1.0.4
* The Firebase class, Firebase.h v1.0.5
*
* Created September 26, 2021
* Created October 6, 2021
*
* The MIT License (MIT)
* Copyright (c) 2021 K. Suwatchai (Mobizt)
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.5.2"
#define FIREBASE_CLIENT_VERSION "2.5.3"
#endif

/**
* Google's Firebase ESP Client Main class, Firebase_ESP_Client.h v2.5.2
* Google's Firebase ESP Client Main class, Firebase_ESP_Client.h v2.5.3
*
* This library supports Espressif ESP8266 and ESP32 MCUs
*
* Created September 26, 2021
* Created October 6, 2021
*
* Updates:
* - Fix #139 compilation error in ESP32 Core SDK v1.0.4 and earlier.
* - Add support user re-authentication.
* - Add re-authenticate example.
*
* This work is a part of Firebase ESP Client library
* Copyright (c) 2021 K. Suwatchai (Mobizt)
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.5.2
Google's Firebase Arduino Client Library for ESP8266 and ESP32 v2.5.3


The default filessystem used in the library is flash and SD.
Expand Down

0 comments on commit 517d237

Please sign in to comment.