@@ -28,7 +28,10 @@ namespace internal {
2828static AppCheckProviderFactory* g_provider_factory = nullptr ;
2929
3030AppCheckInternal::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
7881void 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
8186Future<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+
115156void AppCheckInternal::AddAppCheckListener (AppCheckListener* listener) {
116157 if (listener) {
117158 token_listeners_.push_back (listener);
@@ -153,17 +194,14 @@ void AppCheckInternal::CleanupRegistryCalls() {
153194bool 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 ;
0 commit comments