From 629a85ef7b09bc6959cd773c2e96080035c9a22c Mon Sep 17 00:00:00 2001 From: unknown Date: Sat, 13 Apr 2024 18:13:39 +0700 Subject: [PATCH] Major bugs fixed --- README.md | 12 ++++---- library.json | 2 +- library.properties | 2 +- src/FirebaseClient.h | 6 ++-- src/core/AuthConfig.h | 38 +++++++++++++------------ src/core/FirebaseApp.h | 64 +++++++++++++++++++++++------------------- 6 files changed, 66 insertions(+), 58 deletions(-) diff --git a/README.md b/README.md index 66427e89..450e0e12 100644 --- a/README.md +++ b/README.md @@ -520,14 +520,14 @@ For example: `Database.get(, , , );` -From source 1, the async result (``) shall be defined globally to use in async application because of the static data is needed for use while running the async task. +From source 1, the async result (``) shall be defined globally to use in async application because of the static data buffer is needed for use while running the async task. From source 2, the async client (`AsyncClientClass`) shall be defined globally to use in async application too to make sure the instance of async result was existed or valid while running the async task. The async result from source 2 can be accessed from the async result callback. > [!NOTE] -> The async client object used in authentication task shoul be defined globally as it is async task. +> The async client object used in authentication task shoul be defined globally as it runs asynchronously and requires the static data buffer to store the result. The aync result provides two types of information, `app events` and `result data`. @@ -610,7 +610,7 @@ The parameters for the [CustomAuth](examples/App/AppInitialization/CustomAuth/Cu CustomAuth::CustomAuth(, , , , , , , , ); ``` -`` The time status callback that provide the UNIX timestamp value used for JWT token signing. +`` The time status callback that provides the UNIX timestamp value used for JWT token signing. `` The web API key of project. @@ -620,11 +620,11 @@ CustomAuth::CustomAuth(, , , ` The service account private key. -``The user ID. +`` The user ID. `` The OAuth scopes. -``The OAuth claims. +`` The OAuth claims. ``The expiry period in seconds (less than 3600), 3300 is the default value. @@ -724,7 +724,7 @@ AccessToken::AccessToken(, , , ` Auth token from OAuthe2.0 auth. -`` Expire period in seconds +`` Expire period in seconds `` Refresh token. diff --git a/library.json b/library.json index d96f40c5..a5558306 100644 --- a/library.json +++ b/library.json @@ -1,6 +1,6 @@ { "name": "FirebaseClient", - "version": "1.0.16", + "version": "1.1.0", "keywords": "communication, REST, esp32, esp8266, arduino", "description": "Async Firebase Client library for Arduino.", "repository": { diff --git a/library.properties b/library.properties index 58f69a9a..bd28e22e 100644 --- a/library.properties +++ b/library.properties @@ -1,6 +1,6 @@ name=FirebaseClient -version=1.0.16 +version=1.1.0 author=Mobizt diff --git a/src/FirebaseClient.h b/src/FirebaseClient.h index 8520743a..65d72207 100644 --- a/src/FirebaseClient.h +++ b/src/FirebaseClient.h @@ -1,5 +1,5 @@ /** - * Created April 12, 2024 + * Created April 13, 2024 * * The MIT License (MIT) * Copyright (c) 2024 K. Suwatchai (Mobizt) @@ -30,7 +30,7 @@ #undef FIREBASE_CLIENT_VERSION #endif -#define FIREBASE_CLIENT_VERSION "1.0.16" +#define FIREBASE_CLIENT_VERSION "1.1.0" #include #include "./core/FirebaseApp.h" @@ -133,7 +133,7 @@ namespace firebase #endif app.auth_data.app_token.authenticated = true; - resetTimer(app, false, 3600); + resetTimer(app, false, FIREBASE_DEFAULT_TOKEN_TTL); } else if (app.auth_data.user_auth.auth_data_type == user_auth_data_id_token) { diff --git a/src/core/AuthConfig.h b/src/core/AuthConfig.h index 2871e4b0..88fe143f 100644 --- a/src/core/AuthConfig.h +++ b/src/core/AuthConfig.h @@ -1,5 +1,5 @@ /** - * Created April 6, 2024 + * Created April 13, 2024 * * The MIT License (MIT) * Copyright (c) 2024 K. Suwatchai (Mobizt) @@ -30,6 +30,8 @@ #include "./core/AsyncResult/AsyncResult.h" +#define FIREBASE_DEFAULT_TOKEN_TTL 3300 + namespace sa_ns { enum data_item_type_t @@ -291,7 +293,7 @@ namespace firebase for (size_t i = 0; i < sa_ns::max_type; i++) val[i].remove(0, val[i].length()); timestatus_cb = NULL; - expire = 3300; + expire = FIREBASE_DEFAULT_TOKEN_TTL; step = jwt_step_begin; } @@ -299,7 +301,7 @@ namespace firebase String val[sa_ns::max_type]; jwt_step step = jwt_step_begin; TimeStatusCallback timestatus_cb = NULL; - size_t expire = 3300; + size_t expire = FIREBASE_DEFAULT_TOKEN_TTL; }; #endif @@ -332,7 +334,7 @@ namespace firebase struct user_data { String val[user_ns::max_type]; - size_t expire = 3300; + size_t expire = FIREBASE_DEFAULT_TOKEN_TTL; public: user_data() {} @@ -347,7 +349,7 @@ namespace firebase { for (size_t i = 0; i < user_ns::max_type; i++) val[i].remove(0, val[i].length()); - expire = 3300; + expire = FIREBASE_DEFAULT_TOKEN_TTL; } }; @@ -355,7 +357,7 @@ namespace firebase struct id_token_data { String val[id_tk_ns::max_type]; - size_t expire = 3300; + size_t expire = FIREBASE_DEFAULT_TOKEN_TTL; public: id_token_data() {} @@ -370,7 +372,7 @@ namespace firebase { for (size_t i = 0; i < id_tk_ns::max_type; i++) val[i].remove(0, val[i].length()); - expire = 3300; + expire = FIREBASE_DEFAULT_TOKEN_TTL; } }; #endif @@ -379,7 +381,7 @@ namespace firebase struct access_token_data { String val[access_tk_ns::max_type]; - size_t expire = 3300; + size_t expire = FIREBASE_DEFAULT_TOKEN_TTL; TimeStatusCallback timestatus_cb = NULL; public: @@ -396,7 +398,7 @@ namespace firebase { for (size_t i = 0; i < access_tk_ns::max_type; i++) val[i].remove(0, val[i].length()); - expire = 3300; + expire = FIREBASE_DEFAULT_TOKEN_TTL; timestatus_cb = NULL; } }; @@ -406,7 +408,7 @@ namespace firebase struct custom_token_data { String val[cust_tk_ns::max_type]; - size_t expire = 3300; + size_t expire = FIREBASE_DEFAULT_TOKEN_TTL; TimeStatusCallback timestatus_cb = NULL; public: @@ -421,7 +423,7 @@ namespace firebase void clear() { val[cust_tk_ns::token].remove(0, val[cust_tk_ns::token].length()); - expire = 3300; + expire = FIREBASE_DEFAULT_TOKEN_TTL; timestatus_cb = NULL; } }; @@ -787,7 +789,7 @@ namespace firebase friend class FirebaseApp; public: - UserAuth(const String &api_key, const String &email, const String &password, size_t expire = 3300) + UserAuth(const String &api_key, const String &email, const String &password, size_t expire = FIREBASE_DEFAULT_TOKEN_TTL) { data.clear(); data.user.val[user_ns::api_key] = api_key; @@ -849,7 +851,7 @@ namespace firebase friend class FirebaseApp; public: - ServiceAuth(TimeStatusCallback timeCb, const String &clientEmail, const String &projectId, const String &privateKey, size_t expire = 3300) + ServiceAuth(TimeStatusCallback timeCb, const String &clientEmail, const String &projectId, const String &privateKey, size_t expire = FIREBASE_DEFAULT_TOKEN_TTL) { data.clear(); data.sa.val[sa_ns::cm] = clientEmail; @@ -896,7 +898,7 @@ namespace firebase friend class FirebaseApp; public: - CustomAuth(TimeStatusCallback timeCb, const String &apiKey, const String &clientEmail, const String &projectId, const String &privateKey, const String &uid, const String &scope = "", const String &claims = "", size_t expire = 3300) + CustomAuth(TimeStatusCallback timeCb, const String &apiKey, const String &clientEmail, const String &projectId, const String &privateKey, const String &uid, const String &scope = "", const String &claims = "", size_t expire = FIREBASE_DEFAULT_TOKEN_TTL) { data.clear(); data.sa.val[sa_ns::cm] = clientEmail; @@ -997,7 +999,7 @@ namespace firebase friend class FirebaseApp; public: - IDToken(const String &api_key, const String &token, size_t expire = 3300, const String &refresh = "") + IDToken(const String &api_key, const String &token, size_t expire = FIREBASE_DEFAULT_TOKEN_TTL, const String &refresh = "") { this->data.clear(); this->data.user.val[user_ns::api_key] = api_key; @@ -1063,7 +1065,7 @@ namespace firebase friend class FirebaseApp; public: - AccessToken(const String &token, size_t expire = 3300, const String &refresh = "", const String &client_id = "", const String &client_secret = "") + AccessToken(const String &token, size_t expire = FIREBASE_DEFAULT_TOKEN_TTL, const String &refresh = "", const String &client_id = "", const String &client_secret = "") { this->data.clear(); this->data.access_token.val[access_tk_ns::token] = token; @@ -1130,7 +1132,7 @@ namespace firebase friend class FirebaseApp; public: - CustomToken(const String &api_key, const String &token, size_t expire = 3300) + CustomToken(const String &api_key, const String &token, size_t expire = FIREBASE_DEFAULT_TOKEN_TTL) { this->data.clear(); this->data.custom_token.val[cust_tk_ns::token] = token; @@ -1280,7 +1282,7 @@ namespace firebase { for (size_t i = 0; i < app_tk_ns::max_type; i++) val[i].remove(0, val[i].length()); - expire = 3300; + expire = FIREBASE_DEFAULT_TOKEN_TTL; authenticated = false; auth_type = auth_unknown_token; auth_data_type = user_auth_data_undefined; diff --git a/src/core/FirebaseApp.h b/src/core/FirebaseApp.h index 3f32ac96..10be0917 100644 --- a/src/core/FirebaseApp.h +++ b/src/core/FirebaseApp.h @@ -1,5 +1,5 @@ /** - * Created March 31, 2024 + * Created April 13, 2024 * * The MIT License (MIT) * Copyright (c) 2024 K. Suwatchai (Mobizt) @@ -63,7 +63,7 @@ namespace firebase Timer req_timer, auth_timer, err_timer; List vec; bool processing = false; - uint32_t expire = 3600; + uint32_t expire = FIREBASE_DEFAULT_TOKEN_TTL; JSONUtil json; String extras, subdomain, host; slot_options_t sop; @@ -192,6 +192,9 @@ namespace firebase void setEvent(firebase_auth_event_type event) { + if (auth_data.user_auth.status._event == event) + return; + auth_data.user_auth.status._event = event; if (event == auth_event_initializing || event == auth_event_authenticating) @@ -205,28 +208,16 @@ namespace firebase setEventResult(sData ? &sData->aResult : nullptr, auth_data.user_auth.status.authEventString(auth_data.user_auth.status._event), auth_data.user_auth.status._event); - if (resultCb && sData) - resultCb(sData->aResult); - if (event == auth_event_error || event == auth_event_ready) { processing = false; - if (getClient()) - stop(aClient); event = auth_event_uninitialized; clearLastError(sData ? &sData->aResult : nullptr); if (getClient()) - remove(aClient); + stop(aClient); } } - void remove(AsyncClientClass *aClient) - { - if (!aClient) - return; - aClient->handleRemove(); - } - void clearLastError(AsyncResult *aResult) { if (aResult) @@ -240,12 +231,14 @@ namespace firebase if (!isRes) aResult = new AsyncResult(); + aResult->app_event.setEvent(code, msg); + + if (resultCb) + resultCb(*aResult); + if (!isRes) { - if (resultCb) - resultCb(*aResult); - delete aResult; aResult = nullptr; } @@ -277,6 +270,8 @@ namespace firebase if (!aClient) return; + sys_idle(); + if (sData) { addGAPIsHost(host, subdomain.c_str()); @@ -299,23 +294,24 @@ namespace firebase aClient->process(true); aClient->handleRemove(); - - if (resultCb && aResult && aResult->error().code() != 0 && aResult->error_available) - { - aResult->data_available = false; - resultCb(*aResult); - } } + // Stop client and remove slot void stop(AsyncClientClass *aClient) { if (!aClient) return; + aClient->stop(sData); + if (sData) { - delete sData; + aClient->removeSlot(slot, false); + if (sData) + delete sData; + sData = nullptr; } + sData = nullptr; } @@ -544,8 +540,12 @@ namespace firebase if (auth_data.user_auth.status._event == auth_event_auth_request_sent) { - if (sData && (sData->aResult.error().code() != 0 || req_timer.remaining() == 0)) + sys_idle(); + + if (sData && ((sData->response.payloadLen > 0 && sData->aResult.error().code() != 0) || req_timer.remaining() == 0)) { + // In case of googleapis returns http status code >= 400 or request is timed out. + // Note that, only status line was read in case http status code >= 400 setEvent(auth_event_error); return false; } @@ -560,8 +560,8 @@ namespace firebase auth_data.app_token.authenticated = auth_data.user_auth.task_type != firebase_core_auth_task_type_delete_user && auth_data.user_auth.task_type != firebase_core_auth_task_type_reset_password; auth_data.app_token.auth_type = auth_data.user_auth.auth_type; auth_data.app_token.auth_data_type = auth_data.user_auth.auth_data_type; - auth_data.app_token.expire = 3600; - auth_timer.feed(3600); + auth_data.app_token.expire = FIREBASE_DEFAULT_TOKEN_TTL; + auth_timer.feed(FIREBASE_DEFAULT_TOKEN_TTL); setEvent(auth_event_ready); return true; } @@ -596,7 +596,13 @@ namespace firebase app_addr = reinterpret_cast(this); vec.addRemoveList(aVec, app_addr, true); }; - ~FirebaseApp() { vec.addRemoveList(aVec, app_addr, false); }; + ~FirebaseApp() + { + if (sData) + delete sData; + sData = nullptr; + vec.addRemoveList(aVec, app_addr, false); + }; bool isInitialized() const { return auth_data.user_auth.initialized; }