Skip to content

Commit

Permalink
Add support user defined JWT token processor to allow multi-threaded …
Browse files Browse the repository at this point in the history
…usage.
  • Loading branch information
mobizt committed May 29, 2024
1 parent 3b6322f commit cb2c63c
Show file tree
Hide file tree
Showing 183 changed files with 855 additions and 269 deletions.
34 changes: 29 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

![GitHub Actions Workflow Status](https://img.shields.io/github/actions/workflow/status/mobizt/FirebaseClient/.github%2Fworkflows%2Fcompile_library.yml?logo=github&label=compile) [![Github Stars](https://img.shields.io/github/stars/mobizt/FirebaseClient?logo=github)](https://github.com/mobizt/FirebaseClient/stargazers) ![Github Issues](https://img.shields.io/github/issues/mobizt/FirebaseClient?logo=github)

![GitHub Release](https://img.shields.io/github/v/release/mobizt/FirebaseClient) ![Arduino](https://img.shields.io/badge/Arduino-v1.2.5-57C207?logo=arduino) ![PlatformIO](https://badges.registry.platformio.org/packages/mobizt/library/FirebaseClient.svg) ![GitHub Release Date](https://img.shields.io/github/release-date/mobizt/FirebaseClient)
![GitHub Release](https://img.shields.io/github/v/release/mobizt/FirebaseClient) ![Arduino](https://img.shields.io/badge/Arduino-v1.2.6-57C207?logo=arduino) ![PlatformIO](https://badges.registry.platformio.org/packages/mobizt/library/FirebaseClient.svg) ![GitHub Release Date](https://img.shields.io/github/release-date/mobizt/FirebaseClient)

[![GitHub Sponsors](https://img.shields.io/github/sponsors/mobizt?logo=github)](https://github.com/sponsors/mobizt)

Revision `2024-05-28T08:55:05Z`
Revision `2024-05-29T02:58:24Z`

## Table of Contents

Expand Down Expand Up @@ -933,15 +933,36 @@ The auth token need to be re-created instead of refreshing.

The `CustomAuth` and `ServiceAuth` classes required the JWT token processor which is done via the function `JWTClass::loop(<auth_data_t*>)` which accepts the pointer to the `auth_data_t` from the `FirebaseApp::getAuth()`.

The examples in this library use the static object of `JWTClass` called `JWT` to save the stack memory usage which the processes data are stored in the internal `jwt_token_data_t`, which is not thread safe when using in multi-threaded operations.
The examples in this library, the static object of `JWTClass` called `JWT` will be used to save the stack memory usage and it is not thread safe when using in multi-threaded operations (`multi-FirebaseApp`) because of sharing internal `jwt_token_data_t`.

The following is the example code for JWT token processor that should be executed inside the main loop.

```cpp
JWT.loop(app.getAuth());
```

For thread safety, you have to define `JWTClass` for each `AsyncClientClass`'s auth task.
For thread safety, you have to define `JWTClass` for each `FirebaseApp` via `FirebaseApp::setJWTProcessor(<JWTClass>)`, before calling `initializeApp`.

The following is the partial code example for setting the JWT token processor to the `FirebaseApp`.

```cpp
FirebaseApp app;

JWTClass jwtProcessor;

void setup()
{
app.setJWTProcessor(jwtProcessor);
initializeApp(...);
}

void loop()
{
jwtProcessor.loop(app.getAuth());
app.loop();
}
```


- ### UserAuth (User Sign-In Authentication)

Expand Down Expand Up @@ -2208,7 +2229,10 @@ void authHandler()
unsigned long ms = millis();
while (app.isInitialized() && !app.ready() && millis() - ms < 120 * 1000)
{
// This JWT token process required for ServiceAuth and CustomAuth authentications
// The JWT token processor required for ServiceAuth and CustomAuth authentications.
// JWT is a static object of JWTClass and it's not thread safe.
// In multi-threaded operations (multi-FirebaseApp), you have to define JWTClass for each FirebaseApp,
// and set it to the FirebaseApp via FirebaseApp::setJWTProcessor(<JWTClass>), before calling initializeApp.
JWT.loop(app.getAuth());
printResult(aResult_no_callback);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,9 @@
* To reduce the stack usage of BearSSL engine crpto function, the JWT token creation process
* will be performed outside the FirebaseApp.
*
* For ServiceAuth and CustomAuth authentications, you need to check for JWT token geration process requirement,
* before running the JWT process function in the main loop as the following.
* The JWT token processor required for ServiceAuth and CustomAuth authentications.
*
* JWT.loop(app.getAuth());
* JWTClass::loop(<auth_data_t *>)
*
* The complete usage guidelines, please visit https://github.com/mobizt/FirebaseClient
*/
Expand Down Expand Up @@ -134,7 +133,10 @@ void loop()
// The async task handler should run inside the main loop
// without blocking delay or bypassing with millis code blocks.

// This JWT token process required for ServiceAuth and CustomAuth authentications
// The JWT token processor required for ServiceAuth and CustomAuth authentications.
// JWT is a static object of JWTClass and it's not thread safe.
// In multi-threaded operations (multi-FirebaseApp), you have to define JWTClass for each FirebaseApp,
// and set it to the FirebaseApp via FirebaseApp::setJWTProcessor(<JWTClass>), before calling initializeApp.
JWT.loop(app.getAuth());

app.loop();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,9 @@
* To reduce the stack usage of BearSSL engine crpto function, the JWT token creation process
* will be performed outside the FirebaseApp.
*
* For ServiceAuth and CustomAuth authentications, you need to check for JWT token geration process requirement,
* before running the JWT process function in the main loop as the following.
* The JWT token processor required for ServiceAuth and CustomAuth authentications.
*
* JWT.loop(app.getAuth());
* JWTClass::loop(<auth_data_t *>)
*
* The complete usage guidelines, please visit https://github.com/mobizt/FirebaseClient
*/
Expand Down Expand Up @@ -158,7 +157,10 @@ void loop()
// The async task handler should run inside the main loop
// without blocking delay or bypassing with millis code blocks.

// This JWT token process required for ServiceAuth and CustomAuth authentications
// The JWT token processor required for ServiceAuth and CustomAuth authentications.
// JWT is a static object of JWTClass and it's not thread safe.
// In multi-threaded operations (multi-FirebaseApp), you have to define JWTClass for each FirebaseApp,
// and set it to the FirebaseApp via FirebaseApp::setJWTProcessor(<JWTClass>), before calling initializeApp.
JWT.loop(app.getAuth());

app.loop();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
* <AsyncClient> - The async client.
* <FirebaseApp> - The authentication and access token handler.
* <user_auth_data> - The user auth data (user_auth_data struct) that holds the user input sign-in credentials and token.
*
*
* The <user_auth_data> can be obtained from the following sign-in credentials, access key, auth token providers classs via getAuth function i.e.
* CustomAuth, ServiceAuth, UserAuth, NoAuth, CustomToken, AccessToken, IDToken, LegacyToken.
* CustomAuth, ServiceAuth, UserAuth, NoAuth, CustomToken, AccessToken, IDToken, LegacyToken.
*
* SYNTAX:
*
Expand All @@ -25,11 +25,10 @@
* To reduce the stack usage of BearSSL engine crpto function, the JWT token creation process
* will be performed outside the FirebaseApp.
*
* For ServiceAuth and CustomAuth authentications, you need to check for JWT token geration process requirement,
* before running the JWT process function in the main loop as the following.
* The JWT token processor required for ServiceAuth and CustomAuth authentications.
*
* JWTClass::loop(<auth_data_t *>)
*
* JWT.loop(app.getAuth());
*
* The complete usage guidelines, please visit https://github.com/mobizt/FirebaseClient
*/

Expand Down Expand Up @@ -128,7 +127,10 @@ void loop()
// The async task handler should run inside the main loop
// without blocking delay or bypassing with millis code blocks.

// This JWT token process required for ServiceAuth and CustomAuth authentications
// The JWT token processor required for ServiceAuth and CustomAuth authentications.
// JWT is a static object of JWTClass and it's not thread safe.
// In multi-threaded operations (multi-FirebaseApp), you have to define JWTClass for each FirebaseApp,
// and set it to the FirebaseApp via FirebaseApp::setJWTProcessor(<JWTClass>), before calling initializeApp.
JWT.loop(app.getAuth());

app.loop();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,9 @@
* To reduce the stack usage of BearSSL engine crpto function, the JWT token creation process
* will be performed outside the FirebaseApp.
*
* For ServiceAuth and CustomAuth authentications, you need to check for JWT token geration process requirement,
* before running the JWT process function in the main loop as the following.
* The JWT token processor required for ServiceAuth and CustomAuth authentications.
*
* JWT.loop(app.getAuth());
* JWTClass::loop(<auth_data_t *>)
*
* The complete usage guidelines, please visit https://github.com/mobizt/FirebaseClient
*/
Expand Down Expand Up @@ -149,7 +148,10 @@ void loop()
// The async task handler should run inside the main loop
// without blocking delay or bypassing with millis code blocks.

// This JWT token process required for ServiceAuth and CustomAuth authentications
// The JWT token processor required for ServiceAuth and CustomAuth authentications.
// JWT is a static object of JWTClass and it's not thread safe.
// In multi-threaded operations (multi-FirebaseApp), you have to define JWTClass for each FirebaseApp,
// and set it to the FirebaseApp via FirebaseApp::setJWTProcessor(<JWTClass>), before calling initializeApp.
JWT.loop(app.getAuth());

app.loop();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,9 @@
* To reduce the stack usage of BearSSL engine crpto function, the JWT token creation process
* will be performed outside the FirebaseApp.
*
* For ServiceAuth and CustomAuth authentications, you need to check for JWT token geration process requirement,
* before running the JWT process function in the main loop as the following.
* The JWT token processor required for ServiceAuth and CustomAuth authentications.
*
* JWT.loop(app.getAuth());
* JWTClass::loop(<auth_data_t *>)
*
* The complete usage guidelines, please visit https://github.com/mobizt/FirebaseClient
*/
Expand Down Expand Up @@ -134,7 +133,10 @@ void loop()
// The async task handler should run inside the main loop
// without blocking delay or bypassing with millis code blocks.

// This JWT token process required for ServiceAuth and CustomAuth authentications
// The JWT token processor required for ServiceAuth and CustomAuth authentications.
// JWT is a static object of JWTClass and it's not thread safe.
// In multi-threaded operations (multi-FirebaseApp), you have to define JWTClass for each FirebaseApp,
// and set it to the FirebaseApp via FirebaseApp::setJWTProcessor(<JWTClass>), before calling initializeApp.
JWT.loop(app.getAuth());

app.loop();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,9 @@
* To reduce the stack usage of BearSSL engine crpto function, the JWT token creation process
* will be performed outside the FirebaseApp.
*
* For ServiceAuth and CustomAuth authentications, you need to check for JWT token geration process requirement,
* before running the JWT process function in the main loop as the following.
* The JWT token processor required for ServiceAuth and CustomAuth authentications.
*
* JWT.loop(app.getAuth());
* JWTClass::loop(<auth_data_t *>)
*
* The complete usage guidelines, please visit https://github.com/mobizt/FirebaseClient
*/
Expand Down Expand Up @@ -158,7 +157,10 @@ void loop()
// The async task handler should run inside the main loop
// without blocking delay or bypassing with millis code blocks.

// This JWT token process required for ServiceAuth and CustomAuth authentications
// The JWT token processor required for ServiceAuth and CustomAuth authentications.
// JWT is a static object of JWTClass and it's not thread safe.
// In multi-threaded operations (multi-FirebaseApp), you have to define JWTClass for each FirebaseApp,
// and set it to the FirebaseApp via FirebaseApp::setJWTProcessor(<JWTClass>), before calling initializeApp.
JWT.loop(app.getAuth());

app.loop();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,9 @@
* To reduce the stack usage of BearSSL engine crpto function, the JWT token creation process
* will be performed outside the FirebaseApp.
*
* For ServiceAuth and CustomAuth authentications, you need to check for JWT token geration process requirement,
* before running the JWT process function in the main loop as the following.
* The JWT token processor required for ServiceAuth and CustomAuth authentications.
*
* JWT.loop(app.getAuth());
* JWTClass::loop(<auth_data_t *>)
*
* The complete usage guidelines, please visit https://github.com/mobizt/FirebaseClient
*/
Expand Down Expand Up @@ -128,7 +127,10 @@ void loop()
// The async task handler should run inside the main loop
// without blocking delay or bypassing with millis code blocks.

// This JWT token process required for ServiceAuth and CustomAuth authentications
// The JWT token processor required for ServiceAuth and CustomAuth authentications.
// JWT is a static object of JWTClass and it's not thread safe.
// In multi-threaded operations (multi-FirebaseApp), you have to define JWTClass for each FirebaseApp,
// and set it to the FirebaseApp via FirebaseApp::setJWTProcessor(<JWTClass>), before calling initializeApp.
JWT.loop(app.getAuth());

app.loop();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,9 @@
* To reduce the stack usage of BearSSL engine crpto function, the JWT token creation process
* will be performed outside the FirebaseApp.
*
* For ServiceAuth and CustomAuth authentications, you need to check for JWT token geration process requirement,
* before running the JWT process function in the main loop as the following.
* The JWT token processor required for ServiceAuth and CustomAuth authentications.
*
* JWT.loop(app.getAuth());
* JWTClass::loop(<auth_data_t *>)
*
* The complete usage guidelines, please visit https://github.com/mobizt/FirebaseClient
*/
Expand Down Expand Up @@ -149,7 +148,10 @@ void loop()
// The async task handler should run inside the main loop
// without blocking delay or bypassing with millis code blocks.

// This JWT token process required for ServiceAuth and CustomAuth authentications
// The JWT token processor required for ServiceAuth and CustomAuth authentications.
// JWT is a static object of JWTClass and it's not thread safe.
// In multi-threaded operations (multi-FirebaseApp), you have to define JWTClass for each FirebaseApp,
// and set it to the FirebaseApp via FirebaseApp::setJWTProcessor(<JWTClass>), before calling initializeApp.
JWT.loop(app.getAuth());

app.loop();
Expand Down
10 changes: 6 additions & 4 deletions examples/App/AppInitialization/Sync/CustomAuth/CustomAuth.ino
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,9 @@
* To reduce the stack usage of BearSSL engine crpto function, the JWT token creation process
* will be performed outside the FirebaseApp.
*
* For ServiceAuth and CustomAuth authentications, you need to check for JWT token geration process requirement,
* before running the JWT process function in the main loop as the following.
* The JWT token processor required for ServiceAuth and CustomAuth authentications.
*
* JWT.loop(app.getAuth());
* JWTClass::loop(<auth_data_t *>)
*
* The complete usage guidelines, please visit https://github.com/mobizt/FirebaseClient
*/
Expand Down Expand Up @@ -147,7 +146,10 @@ void authHandler()
unsigned long ms = millis();
while (app.isInitialized() && !app.ready() && millis() - ms < 120 * 1000)
{
// This JWT token process required for ServiceAuth and CustomAuth authentications
// The JWT token processor required for ServiceAuth and CustomAuth authentications.
// JWT is a static object of JWTClass and it's not thread safe.
// In multi-threaded operations (multi-FirebaseApp), you have to define JWTClass for each FirebaseApp,
// and set it to the FirebaseApp via FirebaseApp::setJWTProcessor(<JWTClass>), before calling initializeApp.
JWT.loop(app.getAuth());
printResult(aResult_no_callback);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,9 @@
* To reduce the stack usage of BearSSL engine crpto function, the JWT token creation process
* will be performed outside the FirebaseApp.
*
* For ServiceAuth and CustomAuth authentications, you need to check for JWT token geration process requirement,
* before running the JWT process function in the main loop as the following.
* The JWT token processor required for ServiceAuth and CustomAuth authentications.
*
* JWT.loop(app.getAuth());
* JWTClass::loop(<auth_data_t *>)
*
* The complete usage guidelines, please visit https://github.com/mobizt/FirebaseClient
*/
Expand Down Expand Up @@ -170,7 +169,10 @@ void authHandler()
unsigned long ms = millis();
while (app.isInitialized() && !app.ready() && millis() - ms < 120 * 1000)
{
// This JWT token process required for ServiceAuth and CustomAuth authentications
// The JWT token processor required for ServiceAuth and CustomAuth authentications.
// JWT is a static object of JWTClass and it's not thread safe.
// In multi-threaded operations (multi-FirebaseApp), you have to define JWTClass for each FirebaseApp,
// and set it to the FirebaseApp via FirebaseApp::setJWTProcessor(<JWTClass>), before calling initializeApp.
JWT.loop(app.getAuth());
printResult(aResult_no_callback);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,10 @@ void authHandler()
unsigned long ms = millis();
while (app.isInitialized() && !app.ready() && millis() - ms < 120 * 1000)
{
// This JWT token process required for ServiceAuth and CustomAuth authentications
// The JWT token processor required for ServiceAuth and CustomAuth authentications.
// JWT is a static object of JWTClass and it's not thread safe.
// In multi-threaded operations (multi-FirebaseApp), you have to define JWTClass for each FirebaseApp,
// and set it to the FirebaseApp via FirebaseApp::setJWTProcessor(<JWTClass>), before calling initializeApp.
JWT.loop(app.getAuth());
printResult(aResult_no_callback);
}
Expand Down
10 changes: 6 additions & 4 deletions examples/App/AppInitialization/Sync/ServiceAuth/ServiceAuth.ino
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,9 @@
* To reduce the stack usage of BearSSL engine crpto function, the JWT token creation process
* will be performed outside the FirebaseApp.
*
* For ServiceAuth and CustomAuth authentications, you need to check for JWT token geration process requirement,
* before running the JWT process function in the main loop as the following.
* The JWT token processor required for ServiceAuth and CustomAuth authentications.
*
* JWT.loop(app.getAuth());
* JWTClass::loop(<auth_data_t *>)
*
* The complete usage guidelines, please visit https://github.com/mobizt/FirebaseClient
*/
Expand Down Expand Up @@ -141,7 +140,10 @@ void authHandler()
unsigned long ms = millis();
while (app.isInitialized() && !app.ready() && millis() - ms < 120 * 1000)
{
// This JWT token process required for ServiceAuth and CustomAuth authentications
// The JWT token processor required for ServiceAuth and CustomAuth authentications.
// JWT is a static object of JWTClass and it's not thread safe.
// In multi-threaded operations (multi-FirebaseApp), you have to define JWTClass for each FirebaseApp,
// and set it to the FirebaseApp via FirebaseApp::setJWTProcessor(<JWTClass>), before calling initializeApp.
JWT.loop(app.getAuth());
printResult(aResult_no_callback);
}
Expand Down
Loading

0 comments on commit cb2c63c

Please sign in to comment.