@@ -28,7 +28,10 @@ namespace internal {
28
28
static AppCheckProviderFactory* g_provider_factory = nullptr ;
29
29
30
30
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 ) {
32
35
future_manager ().AllocFutureApi (this , kAppCheckFnCount );
33
36
InitRegistryCalls ();
34
37
}
@@ -76,7 +79,9 @@ void AppCheckInternal::SetAppCheckProviderFactory(
76
79
}
77
80
78
81
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
+ }
80
85
81
86
Future<AppCheckToken> AppCheckInternal::GetAppCheckToken (bool force_refresh) {
82
87
auto handle = future ()->SafeAlloc <AppCheckToken>(kAppCheckFnGetAppCheckToken );
@@ -112,6 +117,42 @@ Future<AppCheckToken> AppCheckInternal::GetAppCheckTokenLastResult() {
112
117
future ()->LastResult (kAppCheckFnGetAppCheckToken ));
113
118
}
114
119
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
+
115
156
void AppCheckInternal::AddAppCheckListener (AppCheckListener* listener) {
116
157
if (listener) {
117
158
token_listeners_.push_back (listener);
@@ -153,17 +194,14 @@ void AppCheckInternal::CleanupRegistryCalls() {
153
194
bool AppCheckInternal::GetAppCheckTokenAsyncForRegistry (App* app,
154
195
void * /* unused*/ ,
155
196
void * out) {
156
- Future<AppCheckToken >* out_future = static_cast <Future<AppCheckToken >*>(out);
197
+ Future<std::string >* out_future = static_cast <Future<std::string >*>(out);
157
198
if (!app || !out_future) {
158
199
return false ;
159
200
}
160
201
161
202
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 ();
167
205
return true ;
168
206
}
169
207
return false ;
0 commit comments