-
Notifications
You must be signed in to change notification settings - Fork 136
Expand file tree
/
Copy pathapp_check_desktop.cc
More file actions
363 lines (323 loc) · 12.7 KB
/
app_check_desktop.cc
File metadata and controls
363 lines (323 loc) · 12.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
// Copyright 2022 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "app_check/src/desktop/app_check_desktop.h"
#include <algorithm>
#include <ctime>
#include <string>
#include "app/src/function_registry.h"
#include "app/src/log.h"
#include "app_check/src/common/common.h"
namespace firebase {
namespace app_check {
namespace internal {
static AppCheckProviderFactory* g_provider_factory = nullptr;
AppCheckInternal::AppCheckInternal(App* app)
: app_(app),
cached_token_(),
cached_provider_(),
is_token_auto_refresh_enabled_(true) {
future_manager().AllocFutureApi(this, kAppCheckFnCount);
AddAppCheckListener(&internal_listener_);
InitRegistryCalls();
}
AppCheckInternal::~AppCheckInternal() {
future_manager().ReleaseFutureApi(this);
CleanupRegistryCalls();
app_ = nullptr;
// Clear the cached token by setting the expiration
cached_token_.expire_time_millis = 0;
}
::firebase::App* AppCheckInternal::app() const { return app_; }
ReferenceCountedFutureImpl* AppCheckInternal::future() {
return future_manager().GetFutureApi(this);
}
bool AppCheckInternal::HasValidCacheToken() const {
// Get the current time, in milliseconds (Done in two lines because of x86)
int64_t current_time = std::time(nullptr);
current_time *= 1000;
// TODO(amaurice): Add some additional time to the check
return cached_token_.expire_time_millis > current_time;
}
void AppCheckInternal::UpdateCachedToken(AppCheckToken token) {
cached_token_ = token;
// Call the token listeners
for (AppCheckListener* listener : token_listeners_) {
listener->OnAppCheckTokenChanged(token);
}
}
AppCheckProvider* AppCheckInternal::GetProvider() {
if (!cached_provider_ && g_provider_factory && app_) {
cached_provider_ = g_provider_factory->CreateProvider(app_);
}
return cached_provider_;
}
// static
void AppCheckInternal::SetAppCheckProviderFactory(
AppCheckProviderFactory* factory) {
g_provider_factory = factory;
}
void AppCheckInternal::SetTokenAutoRefreshEnabled(
bool is_token_auto_refresh_enabled) {
is_token_auto_refresh_enabled_ = is_token_auto_refresh_enabled;
}
Future<AppCheckToken> AppCheckInternal::GetAppCheckToken(bool force_refresh) {
auto handle = future()->SafeAlloc<AppCheckToken>(kAppCheckFnGetAppCheckToken);
if (!force_refresh && HasValidCacheToken()) {
// If the cached token is valid, and not told to refresh, return the cache
future()->CompleteWithResult(handle, 0, cached_token_);
} else {
// Get a new token, and pass the result into the future.
AppCheckProvider* provider = GetProvider();
if (provider != nullptr) {
auto token_callback{
[this, handle](firebase::app_check::AppCheckToken token,
int error_code, const std::string& error_message) {
if (error_code == firebase::app_check::kAppCheckErrorNone) {
UpdateCachedToken(token);
future()->CompleteWithResult(handle, 0, token);
} else {
future()->Complete(handle, error_code, error_message.c_str());
}
}};
provider->GetToken(token_callback);
} else {
future()->Complete(
handle, firebase::app_check::kAppCheckErrorInvalidConfiguration,
"No AppCheckProvider installed.");
}
}
return MakeFuture(future(), handle);
}
Future<AppCheckToken> AppCheckInternal::GetAppCheckTokenLastResult() {
return static_cast<const Future<AppCheckToken>&>(
future()->LastResult(kAppCheckFnGetAppCheckToken));
}
Future<AppCheckToken> AppCheckInternal::GetLimitedUseAppCheckToken() {
auto handle =
future()->SafeAlloc<AppCheckToken>(kAppCheckFnGetLimitedUseAppCheckToken);
// Get a new token, and pass the result into the future.
AppCheckProvider* provider = GetProvider();
if (provider != nullptr) {
auto token_callback{[this, handle](firebase::app_check::AppCheckToken token,
int error_code,
const std::string& error_message) {
if (error_code == firebase::app_check::kAppCheckErrorNone) {
// Note that we do NOT update the cached token for limited-use tokens.
future()->CompleteWithResult(handle, 0, token);
} else {
future()->Complete(handle, error_code, error_message.c_str());
}
}};
provider->GetLimitedUseToken(token_callback);
} else {
future()->Complete(handle,
firebase::app_check::kAppCheckErrorInvalidConfiguration,
"No AppCheckProvider installed.");
}
return MakeFuture(future(), handle);
}
Future<AppCheckToken> AppCheckInternal::GetLimitedUseAppCheckTokenLastResult() {
return static_cast<const Future<AppCheckToken>&>(
future()->LastResult(kAppCheckFnGetLimitedUseAppCheckToken));
}
Future<std::string> AppCheckInternal::GetAppCheckTokenStringInternal() {
auto handle =
future()->SafeAlloc<std::string>(kAppCheckFnGetAppCheckStringInternal);
if (HasValidCacheToken()) {
future()->CompleteWithResult(handle, 0, cached_token_.token);
} else if (is_token_auto_refresh_enabled_) {
// Only refresh the token if it is enabled
AppCheckProvider* provider = GetProvider();
if (provider != nullptr) {
// Get a new token, and pass the result into the future.
// Note that this is slightly different from the one above, as the
// Future result is just the string token, and not the full struct.
auto token_callback{
[this, handle](firebase::app_check::AppCheckToken token,
int error_code, const std::string& error_message) {
if (error_code == firebase::app_check::kAppCheckErrorNone) {
UpdateCachedToken(token);
future()->CompleteWithResult(handle, 0, token.token);
} else {
future()->Complete(handle, error_code, error_message.c_str());
}
}};
provider->GetToken(token_callback);
} else {
future()->Complete(
handle, firebase::app_check::kAppCheckErrorInvalidConfiguration,
"No AppCheckProvider installed.");
}
} else {
future()->Complete(
handle, kAppCheckErrorUnknown,
"No AppCheck token available, and auto refresh is disabled");
}
return MakeFuture(future(), handle);
}
Future<std::string>
AppCheckInternal::GetLimitedUseAppCheckTokenStringInternal() {
auto handle = future()->SafeAlloc<std::string>(
kAppCheckFnGetLimitedUseAppCheckStringInternal);
AppCheckProvider* provider = GetProvider();
if (provider != nullptr) {
auto token_callback{[this, handle](firebase::app_check::AppCheckToken token,
int error_code,
const std::string& error_message) {
if (error_code == firebase::app_check::kAppCheckErrorNone) {
future()->CompleteWithResult(handle, 0, token.token);
} else {
future()->Complete(handle, error_code, error_message.c_str());
}
}};
provider->GetLimitedUseToken(token_callback);
} else {
future()->Complete(handle,
firebase::app_check::kAppCheckErrorInvalidConfiguration,
"No AppCheckProvider installed.");
}
return MakeFuture(future(), handle);
}
void AppCheckInternal::AddAppCheckListener(AppCheckListener* listener) {
if (listener) {
token_listeners_.push_back(listener);
// Following the Android pattern, if there is a cached token, call the
// listener. Note that the iOS implementation does not do this.
if (HasValidCacheToken()) {
listener->OnAppCheckTokenChanged(cached_token_);
}
}
}
void AppCheckInternal::RemoveAppCheckListener(AppCheckListener* listener) {
if (listener) {
token_listeners_.remove(listener);
}
}
static int g_app_check_registry_count = 0;
void AppCheckInternal::InitRegistryCalls() {
if (g_app_check_registry_count == 0) {
app_->function_registry()->RegisterFunction(
::firebase::internal::FnAppCheckGetTokenAsync,
AppCheckInternal::GetAppCheckTokenAsyncForRegistry);
app_->function_registry()->RegisterFunction(
::firebase::internal::FnAppCheckGetLimitedUseTokenAsync,
AppCheckInternal::GetLimitedUseAppCheckTokenAsyncForRegistry);
app_->function_registry()->RegisterFunction(
::firebase::internal::FnAppCheckAddListener,
AppCheckInternal::AddAppCheckListenerForRegistry);
app_->function_registry()->RegisterFunction(
::firebase::internal::FnAppCheckRemoveListener,
AppCheckInternal::RemoveAppCheckListenerForRegistry);
}
g_app_check_registry_count++;
}
void AppCheckInternal::CleanupRegistryCalls() {
g_app_check_registry_count--;
if (g_app_check_registry_count == 0) {
app_->function_registry()->UnregisterFunction(
::firebase::internal::FnAppCheckGetTokenAsync);
app_->function_registry()->UnregisterFunction(
::firebase::internal::FnAppCheckGetLimitedUseTokenAsync);
app_->function_registry()->UnregisterFunction(
::firebase::internal::FnAppCheckAddListener);
app_->function_registry()->UnregisterFunction(
::firebase::internal::FnAppCheckRemoveListener);
}
}
// Static functions used by the internal registry tool
bool AppCheckInternal::GetAppCheckTokenAsyncForRegistry(App* app,
void* /*unused*/,
void* out) {
Future<std::string>* out_future = static_cast<Future<std::string>*>(out);
if (!app || !out_future) {
return false;
}
AppCheck* app_check = AppCheck::GetInstance(app);
if (app_check && app_check->internal_) {
*out_future = app_check->internal_->GetAppCheckTokenStringInternal();
return true;
}
return false;
}
// static
bool AppCheckInternal::GetLimitedUseAppCheckTokenAsyncForRegistry(
App* app, void* /*unused*/, void* out) {
Future<std::string>* out_future = static_cast<Future<std::string>*>(out);
if (!app || !out_future) {
return false;
}
AppCheck* app_check = AppCheck::GetInstance(app);
if (app_check && app_check->internal_) {
*out_future =
app_check->internal_->GetLimitedUseAppCheckTokenStringInternal();
return true;
}
return false;
}
void FunctionRegistryAppCheckListener::AddListener(
FunctionRegistryCallback callback, void* context) {
callbacks_.emplace_back(callback, context);
}
void FunctionRegistryAppCheckListener::RemoveListener(
FunctionRegistryCallback callback, void* context) {
Entry entry = {callback, context};
auto iter = std::find(callbacks_.begin(), callbacks_.end(), entry);
if (iter != callbacks_.end()) {
callbacks_.erase(iter);
}
}
void FunctionRegistryAppCheckListener::OnAppCheckTokenChanged(
const AppCheckToken& token) {
for (const Entry& entry : callbacks_) {
entry.first(token.token, entry.second);
}
}
// static
bool AppCheckInternal::AddAppCheckListenerForRegistry(App* app, void* callback,
void* context) {
auto typed_callback = reinterpret_cast<FunctionRegistryCallback>(callback);
if (!app || !typed_callback) {
return false;
}
AppCheck* app_check = AppCheck::GetInstance(app);
if (app_check && app_check->internal_) {
app_check->internal_->internal_listener_.AddListener(typed_callback,
context);
// If there is a cached token, pass it along to the callback
if (app_check->internal_->HasValidCacheToken()) {
typed_callback(app_check->internal_->cached_token_.token, context);
}
return true;
}
return false;
}
// static
bool AppCheckInternal::RemoveAppCheckListenerForRegistry(App* app,
void* callback,
void* context) {
auto typed_callback = reinterpret_cast<FunctionRegistryCallback>(callback);
if (!app || !typed_callback) {
return false;
}
AppCheck* app_check = AppCheck::GetInstance(app);
if (app_check && app_check->internal_) {
app_check->internal_->internal_listener_.RemoveListener(typed_callback,
context);
return true;
}
return false;
}
} // namespace internal
} // namespace app_check
} // namespace firebase