Skip to content

Commit cb2c63c

Browse files
committed
Add support user defined JWT token processor to allow multi-threaded usage.
1 parent 3b6322f commit cb2c63c

File tree

183 files changed

+855
-269
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

183 files changed

+855
-269
lines changed

README.md

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
![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)
44

5-
![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)
5+
![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)
66

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

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

1111
## Table of Contents
1212

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

934934
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()`.
935935

936-
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.
936+
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`.
937937

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

940940
```cpp
941941
JWT.loop(app.getAuth());
942942
```
943943

944-
For thread safety, you have to define `JWTClass` for each `AsyncClientClass`'s auth task.
944+
For thread safety, you have to define `JWTClass` for each `FirebaseApp` via `FirebaseApp::setJWTProcessor(<JWTClass>)`, before calling `initializeApp`.
945+
946+
The following is the partial code example for setting the JWT token processor to the `FirebaseApp`.
947+
948+
```cpp
949+
FirebaseApp app;
950+
951+
JWTClass jwtProcessor;
952+
953+
void setup()
954+
{
955+
app.setJWTProcessor(jwtProcessor);
956+
initializeApp(...);
957+
}
958+
959+
void loop()
960+
{
961+
jwtProcessor.loop(app.getAuth());
962+
app.loop();
963+
}
964+
```
965+
945966

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

@@ -2208,7 +2229,10 @@ void authHandler()
22082229
unsigned long ms = millis();
22092230
while (app.isInitialized() && !app.ready() && millis() - ms < 120 * 1000)
22102231
{
2211-
// This JWT token process required for ServiceAuth and CustomAuth authentications
2232+
// The JWT token processor required for ServiceAuth and CustomAuth authentications.
2233+
// JWT is a static object of JWTClass and it's not thread safe.
2234+
// In multi-threaded operations (multi-FirebaseApp), you have to define JWTClass for each FirebaseApp,
2235+
// and set it to the FirebaseApp via FirebaseApp::setJWTProcessor(<JWTClass>), before calling initializeApp.
22122236
JWT.loop(app.getAuth());
22132237
printResult(aResult_no_callback);
22142238
}

examples/App/AppInitialization/Async/Callback/CustomAuth/CustomAuth.ino

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,9 @@
2929
* To reduce the stack usage of BearSSL engine crpto function, the JWT token creation process
3030
* will be performed outside the FirebaseApp.
3131
*
32-
* For ServiceAuth and CustomAuth authentications, you need to check for JWT token geration process requirement,
33-
* before running the JWT process function in the main loop as the following.
32+
* The JWT token processor required for ServiceAuth and CustomAuth authentications.
3433
*
35-
* JWT.loop(app.getAuth());
34+
* JWTClass::loop(<auth_data_t *>)
3635
*
3736
* The complete usage guidelines, please visit https://github.com/mobizt/FirebaseClient
3837
*/
@@ -134,7 +133,10 @@ void loop()
134133
// The async task handler should run inside the main loop
135134
// without blocking delay or bypassing with millis code blocks.
136135

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

140142
app.loop();

examples/App/AppInitialization/Async/Callback/CustomAuthFile/CustomAuthFile.ino

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,9 @@
3030
* To reduce the stack usage of BearSSL engine crpto function, the JWT token creation process
3131
* will be performed outside the FirebaseApp.
3232
*
33-
* For ServiceAuth and CustomAuth authentications, you need to check for JWT token geration process requirement,
34-
* before running the JWT process function in the main loop as the following.
33+
* The JWT token processor required for ServiceAuth and CustomAuth authentications.
3534
*
36-
* JWT.loop(app.getAuth());
35+
* JWTClass::loop(<auth_data_t *>)
3736
*
3837
* The complete usage guidelines, please visit https://github.com/mobizt/FirebaseClient
3938
*/
@@ -158,7 +157,10 @@ void loop()
158157
// The async task handler should run inside the main loop
159158
// without blocking delay or bypassing with millis code blocks.
160159

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

164166
app.loop();

examples/App/AppInitialization/Async/Callback/ServiceAuth/ServiceAuth.ino

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
* <AsyncClient> - The async client.
77
* <FirebaseApp> - The authentication and access token handler.
88
* <user_auth_data> - The user auth data (user_auth_data struct) that holds the user input sign-in credentials and token.
9-
*
9+
*
1010
* The <user_auth_data> can be obtained from the following sign-in credentials, access key, auth token providers classs via getAuth function i.e.
11-
* CustomAuth, ServiceAuth, UserAuth, NoAuth, CustomToken, AccessToken, IDToken, LegacyToken.
11+
* CustomAuth, ServiceAuth, UserAuth, NoAuth, CustomToken, AccessToken, IDToken, LegacyToken.
1212
*
1313
* SYNTAX:
1414
*
@@ -25,11 +25,10 @@
2525
* To reduce the stack usage of BearSSL engine crpto function, the JWT token creation process
2626
* will be performed outside the FirebaseApp.
2727
*
28-
* For ServiceAuth and CustomAuth authentications, you need to check for JWT token geration process requirement,
29-
* before running the JWT process function in the main loop as the following.
28+
* The JWT token processor required for ServiceAuth and CustomAuth authentications.
29+
*
30+
* JWTClass::loop(<auth_data_t *>)
3031
*
31-
* JWT.loop(app.getAuth());
32-
*
3332
* The complete usage guidelines, please visit https://github.com/mobizt/FirebaseClient
3433
*/
3534

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

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

134136
app.loop();

examples/App/AppInitialization/Async/Callback/ServiceAuthFile/ServiceAuthFile.ino

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,9 @@
2323
* To reduce the stack usage of BearSSL engine crpto function, the JWT token creation process
2424
* will be performed outside the FirebaseApp.
2525
*
26-
* For ServiceAuth and CustomAuth authentications, you need to check for JWT token geration process requirement,
27-
* before running the JWT process function in the main loop as the following.
26+
* The JWT token processor required for ServiceAuth and CustomAuth authentications.
2827
*
29-
* JWT.loop(app.getAuth());
28+
* JWTClass::loop(<auth_data_t *>)
3029
*
3130
* The complete usage guidelines, please visit https://github.com/mobizt/FirebaseClient
3231
*/
@@ -149,7 +148,10 @@ void loop()
149148
// The async task handler should run inside the main loop
150149
// without blocking delay or bypassing with millis code blocks.
151150

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

155157
app.loop();

examples/App/AppInitialization/Async/NoCallback/CustomAuth/CustomAuth.ino

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,9 @@
2929
* To reduce the stack usage of BearSSL engine crpto function, the JWT token creation process
3030
* will be performed outside the FirebaseApp.
3131
*
32-
* For ServiceAuth and CustomAuth authentications, you need to check for JWT token geration process requirement,
33-
* before running the JWT process function in the main loop as the following.
32+
* The JWT token processor required for ServiceAuth and CustomAuth authentications.
3433
*
35-
* JWT.loop(app.getAuth());
34+
* JWTClass::loop(<auth_data_t *>)
3635
*
3736
* The complete usage guidelines, please visit https://github.com/mobizt/FirebaseClient
3837
*/
@@ -134,7 +133,10 @@ void loop()
134133
// The async task handler should run inside the main loop
135134
// without blocking delay or bypassing with millis code blocks.
136135

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

140142
app.loop();

examples/App/AppInitialization/Async/NoCallback/CustomAuthFile/CustomAuthFile.ino

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,9 @@
3030
* To reduce the stack usage of BearSSL engine crpto function, the JWT token creation process
3131
* will be performed outside the FirebaseApp.
3232
*
33-
* For ServiceAuth and CustomAuth authentications, you need to check for JWT token geration process requirement,
34-
* before running the JWT process function in the main loop as the following.
33+
* The JWT token processor required for ServiceAuth and CustomAuth authentications.
3534
*
36-
* JWT.loop(app.getAuth());
35+
* JWTClass::loop(<auth_data_t *>)
3736
*
3837
* The complete usage guidelines, please visit https://github.com/mobizt/FirebaseClient
3938
*/
@@ -158,7 +157,10 @@ void loop()
158157
// The async task handler should run inside the main loop
159158
// without blocking delay or bypassing with millis code blocks.
160159

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

164166
app.loop();

examples/App/AppInitialization/Async/NoCallback/ServiceAuth/ServiceAuth.ino

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,9 @@
2525
* To reduce the stack usage of BearSSL engine crpto function, the JWT token creation process
2626
* will be performed outside the FirebaseApp.
2727
*
28-
* For ServiceAuth and CustomAuth authentications, you need to check for JWT token geration process requirement,
29-
* before running the JWT process function in the main loop as the following.
28+
* The JWT token processor required for ServiceAuth and CustomAuth authentications.
3029
*
31-
* JWT.loop(app.getAuth());
30+
* JWTClass::loop(<auth_data_t *>)
3231
*
3332
* The complete usage guidelines, please visit https://github.com/mobizt/FirebaseClient
3433
*/
@@ -128,7 +127,10 @@ void loop()
128127
// The async task handler should run inside the main loop
129128
// without blocking delay or bypassing with millis code blocks.
130129

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

134136
app.loop();

examples/App/AppInitialization/Async/NoCallback/ServiceAuthFile/ServiceAuthFile.ino

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,9 @@
2323
* To reduce the stack usage of BearSSL engine crpto function, the JWT token creation process
2424
* will be performed outside the FirebaseApp.
2525
*
26-
* For ServiceAuth and CustomAuth authentications, you need to check for JWT token geration process requirement,
27-
* before running the JWT process function in the main loop as the following.
26+
* The JWT token processor required for ServiceAuth and CustomAuth authentications.
2827
*
29-
* JWT.loop(app.getAuth());
28+
* JWTClass::loop(<auth_data_t *>)
3029
*
3130
* The complete usage guidelines, please visit https://github.com/mobizt/FirebaseClient
3231
*/
@@ -149,7 +148,10 @@ void loop()
149148
// The async task handler should run inside the main loop
150149
// without blocking delay or bypassing with millis code blocks.
151150

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

155157
app.loop();

examples/App/AppInitialization/Sync/CustomAuth/CustomAuth.ino

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,9 @@
2929
* To reduce the stack usage of BearSSL engine crpto function, the JWT token creation process
3030
* will be performed outside the FirebaseApp.
3131
*
32-
* For ServiceAuth and CustomAuth authentications, you need to check for JWT token geration process requirement,
33-
* before running the JWT process function in the main loop as the following.
32+
* The JWT token processor required for ServiceAuth and CustomAuth authentications.
3433
*
35-
* JWT.loop(app.getAuth());
34+
* JWTClass::loop(<auth_data_t *>)
3635
*
3736
* The complete usage guidelines, please visit https://github.com/mobizt/FirebaseClient
3837
*/
@@ -147,7 +146,10 @@ void authHandler()
147146
unsigned long ms = millis();
148147
while (app.isInitialized() && !app.ready() && millis() - ms < 120 * 1000)
149148
{
150-
// This JWT token process required for ServiceAuth and CustomAuth authentications
149+
// The JWT token processor required for ServiceAuth and CustomAuth authentications.
150+
// JWT is a static object of JWTClass and it's not thread safe.
151+
// In multi-threaded operations (multi-FirebaseApp), you have to define JWTClass for each FirebaseApp,
152+
// and set it to the FirebaseApp via FirebaseApp::setJWTProcessor(<JWTClass>), before calling initializeApp.
151153
JWT.loop(app.getAuth());
152154
printResult(aResult_no_callback);
153155
}

0 commit comments

Comments
 (0)