Skip to content

Commit 6bf9316

Browse files
authored
Refactor the App Check internal call logic used by other products (#1215)
* Refactor AppCheck internal call logic * Formatting * Update app_check_desktop.h * Just use the internal class directly * Update app_check.h * Update app_check_desktop.cc * Update app_check_desktop.cc
1 parent d2776f0 commit 6bf9316

File tree

5 files changed

+60
-13
lines changed

5 files changed

+60
-13
lines changed

app_check/src/common/common.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ namespace internal {
2222
// Used by App Check functions that return a future
2323
enum AppCheckFn {
2424
kAppCheckFnGetAppCheckToken = 0,
25+
kAppCheckFnGetAppCheckStringInternal,
2526
kAppCheckFnCount,
2627
};
2728

app_check/src/desktop/app_check_desktop.cc

Lines changed: 46 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@ namespace internal {
2828
static AppCheckProviderFactory* g_provider_factory = nullptr;
2929

3030
AppCheckInternal::AppCheckInternal(App* app)
31-
: app_(app), cached_token_(), cached_provider_() {
31+
: app_(app),
32+
cached_token_(),
33+
cached_provider_(),
34+
is_token_auto_refresh_enabled_(true) {
3235
future_manager().AllocFutureApi(this, kAppCheckFnCount);
3336
InitRegistryCalls();
3437
}
@@ -76,7 +79,9 @@ void AppCheckInternal::SetAppCheckProviderFactory(
7679
}
7780

7881
void AppCheckInternal::SetTokenAutoRefreshEnabled(
79-
bool is_token_auto_refresh_enabled) {}
82+
bool is_token_auto_refresh_enabled) {
83+
is_token_auto_refresh_enabled_ = is_token_auto_refresh_enabled;
84+
}
8085

8186
Future<AppCheckToken> AppCheckInternal::GetAppCheckToken(bool force_refresh) {
8287
auto handle = future()->SafeAlloc<AppCheckToken>(kAppCheckFnGetAppCheckToken);
@@ -112,6 +117,42 @@ Future<AppCheckToken> AppCheckInternal::GetAppCheckTokenLastResult() {
112117
future()->LastResult(kAppCheckFnGetAppCheckToken));
113118
}
114119

120+
Future<std::string> AppCheckInternal::GetAppCheckTokenStringInternal() {
121+
auto handle =
122+
future()->SafeAlloc<std::string>(kAppCheckFnGetAppCheckStringInternal);
123+
if (HasValidCacheToken()) {
124+
future()->CompleteWithResult(handle, 0, cached_token_.token);
125+
} else if (is_token_auto_refresh_enabled_) {
126+
// Only refresh the token if it is enabled
127+
AppCheckProvider* provider = GetProvider();
128+
if (provider != nullptr) {
129+
// Get a new token, and pass the result into the future.
130+
// Note that this is slightly different from the one above, as the
131+
// Future result is just the string token, and not the full struct.
132+
auto token_callback{
133+
[this, handle](firebase::app_check::AppCheckToken token,
134+
int error_code, const std::string& error_message) {
135+
if (error_code == firebase::app_check::kAppCheckErrorNone) {
136+
UpdateCachedToken(token);
137+
future()->CompleteWithResult(handle, 0, token.token);
138+
} else {
139+
future()->Complete(handle, error_code, error_message.c_str());
140+
}
141+
}};
142+
provider->GetToken(token_callback);
143+
} else {
144+
future()->Complete(
145+
handle, firebase::app_check::kAppCheckErrorInvalidConfiguration,
146+
"No AppCheckProvider installed.");
147+
}
148+
} else {
149+
future()->Complete(
150+
handle, kAppCheckErrorUnknown,
151+
"No AppCheck token available, and auto refresh is disabled");
152+
}
153+
return MakeFuture(future(), handle);
154+
}
155+
115156
void AppCheckInternal::AddAppCheckListener(AppCheckListener* listener) {
116157
if (listener) {
117158
token_listeners_.push_back(listener);
@@ -153,17 +194,14 @@ void AppCheckInternal::CleanupRegistryCalls() {
153194
bool AppCheckInternal::GetAppCheckTokenAsyncForRegistry(App* app,
154195
void* /*unused*/,
155196
void* out) {
156-
Future<AppCheckToken>* out_future = static_cast<Future<AppCheckToken>*>(out);
197+
Future<std::string>* out_future = static_cast<Future<std::string>*>(out);
157198
if (!app || !out_future) {
158199
return false;
159200
}
160201

161202
AppCheck* app_check = AppCheck::GetInstance(app);
162-
if (app_check) {
163-
// TODO(amaurice): This should call some internal function instead of the
164-
// public one, since this will change the *LastResult value behind the
165-
// scenes.
166-
*out_future = app_check->GetAppCheckToken(false);
203+
if (app_check && app_check->internal_) {
204+
*out_future = app_check->internal_->GetAppCheckTokenStringInternal();
167205
return true;
168206
}
169207
return false;

app_check/src/desktop/app_check_desktop.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#define FIREBASE_APP_CHECK_SRC_DESKTOP_APP_CHECK_DESKTOP_H_
1717

1818
#include <list>
19+
#include <string>
1920

2021
#include "app/src/future_manager.h"
2122
#include "app/src/include/firebase/app.h"
@@ -42,6 +43,10 @@ class AppCheckInternal {
4243

4344
Future<AppCheckToken> GetAppCheckTokenLastResult();
4445

46+
// Gets the App Check token as just the string, to be used by
47+
// internal methods to not conflict with the publicly returned future.
48+
Future<std::string> GetAppCheckTokenStringInternal();
49+
4550
void AddAppCheckListener(AppCheckListener* listener);
4651

4752
void RemoveAppCheckListener(AppCheckListener* listener);
@@ -82,6 +87,9 @@ class AppCheckInternal {
8287
AppCheckToken cached_token_;
8388
// List of registered listeners for Token changes.
8489
std::list<AppCheckListener*> token_listeners_;
90+
// Should it automatically get an App Check token if there is not a valid
91+
// cached token.
92+
bool is_token_auto_refresh_enabled_;
8593
};
8694

8795
} // namespace internal

app_check/src/include/firebase/app_check.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,8 @@ class AppCheck {
157157

158158
void DeleteInternal();
159159

160+
// Make the Internal version a friend class, so that it can access itself.
161+
friend class internal::AppCheckInternal;
160162
internal::AppCheckInternal* internal_;
161163
};
162164

storage/src/desktop/storage_reference_desktop.cc

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
#include "app/src/function_registry.h"
3030
#include "app/src/include/firebase/app.h"
3131
#include "app/src/thread.h"
32-
#include "app_check/src/include/firebase/app_check.h"
3332
#include "storage/src/common/common_internal.h"
3433
#include "storage/src/desktop/controller_desktop.h"
3534
#include "storage/src/desktop/metadata_desktop.h"
@@ -259,15 +258,14 @@ void StorageReferenceInternal::PrepareRequestBlocking(
259258
storage_->user_agent().c_str());
260259

261260
// Use the function registry to get the App Check token.
262-
Future<::firebase::app_check::AppCheckToken> app_check_future;
261+
Future<std::string> app_check_future;
263262
bool succeeded = storage_->app()->function_registry()->CallFunction(
264263
::firebase::internal::FnAppCheckGetTokenAsync, storage_->app(), nullptr,
265264
&app_check_future);
266265
if (succeeded && app_check_future.status() != kFutureStatusInvalid) {
267-
const ::firebase::app_check::AppCheckToken* token =
268-
app_check_future.Await(kAppCheckTokenTimeoutMs);
266+
const std::string* token = app_check_future.Await(kAppCheckTokenTimeoutMs);
269267
if (token) {
270-
request->add_header("X-Firebase-AppCheck", token->token.c_str());
268+
request->add_header("X-Firebase-AppCheck", token->c_str());
271269
}
272270
}
273271
}

0 commit comments

Comments
 (0)