Skip to content

Commit 777531e

Browse files
authored
Update app check debug provider to match other firebase sdks (#1911)
* Update app check debug provider to match other firebase sdks To alight with other SDK's the cpp app check debug provider will create a token on behalf of the user if they have not already created and registered one. Users will be prompted to register token * Update debug token log message with persistent instructions * Move debug token regeneration note higher up in log message * Format debug token log message with script
1 parent c28b251 commit 777531e

2 files changed

Lines changed: 72 additions & 11 deletions

File tree

app_check/src/desktop/debug_provider_desktop.cc

Lines changed: 71 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,11 @@
1515
#include "app_check/src/desktop/debug_provider_desktop.h"
1616

1717
#include <cstdint>
18+
#include <cstdio>
19+
#include <cstdlib>
1820
#include <map>
1921
#include <memory>
22+
#include <mutex>
2023
#include <string>
2124
#include <utility>
2225

@@ -26,6 +29,7 @@
2629
#include "app/rest/util.h"
2730
#include "app/src/log.h"
2831
#include "app/src/scheduler.h"
32+
#include "app/src/uuid.h"
2933
#include "app_check/src/desktop/debug_token_request.h"
3034
#include "app_check/src/desktop/token_response.h"
3135
#include "firebase/app_check/debug_provider.h"
@@ -34,6 +38,22 @@ namespace firebase {
3438
namespace app_check {
3539
namespace internal {
3640

41+
namespace {
42+
std::string GenerateUuidString() {
43+
firebase::internal::Uuid uuid;
44+
uuid.Generate();
45+
char uuid_str[37];
46+
snprintf(
47+
uuid_str, sizeof(uuid_str),
48+
"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
49+
uuid.data[0], uuid.data[1], uuid.data[2], uuid.data[3], uuid.data[4],
50+
uuid.data[5], uuid.data[6], uuid.data[7], uuid.data[8], uuid.data[9],
51+
uuid.data[10], uuid.data[11], uuid.data[12], uuid.data[13], uuid.data[14],
52+
uuid.data[15]);
53+
return std::string(uuid_str);
54+
}
55+
} // namespace
56+
3757
class DebugAppCheckProvider : public AppCheckProvider {
3858
public:
3959
DebugAppCheckProvider(App* app, const std::string& token);
@@ -115,23 +135,63 @@ void DebugAppCheckProvider::GetTokenInternal(
115135
bool limited_use,
116136
std::function<void(AppCheckToken, int, const std::string&)>
117137
completion_callback) {
118-
// Identify the user's debug token
119-
const char* debug_token_cstr;
120-
if (!debug_token_.empty()) {
121-
debug_token_cstr = debug_token_.c_str();
122-
} else {
123-
debug_token_cstr = std::getenv("APP_CHECK_DEBUG_TOKEN");
138+
static std::mutex token_mutex;
139+
static bool logged_token = false;
140+
std::string message_to_log;
141+
142+
{
143+
std::lock_guard<std::mutex> lock(token_mutex);
144+
if (debug_token_.empty()) {
145+
if (const char* env_token = std::getenv("APP_CHECK_DEBUG_TOKEN")) {
146+
debug_token_ = env_token;
147+
} else {
148+
debug_token_ = GenerateUuidString();
149+
}
150+
}
151+
152+
if (!logged_token && !debug_token_.empty()) {
153+
logged_token = true;
154+
std::string app_id = app_->options().app_id();
155+
std::string project_id = app_->options().project_id();
156+
message_to_log =
157+
std::string("\nWARNING: Firebase App Check debug token: ") +
158+
debug_token_ + "\n\n" +
159+
"To use this token for app debugging, register it with your "
160+
"project.\n\n" +
161+
"You can do so in the Firebase Console: \n" +
162+
"https://console.firebase.google.com/project/" + project_id +
163+
"/appcheck/apps?selectedAppId=" + app_id + " \n\n" +
164+
"Or using the Firebase CLI: \n" +
165+
"firebase appcheck:debugtokens:create " + debug_token_ + " --app " +
166+
app_id + "\n\n" +
167+
"This debug token will regenerate every time the application is "
168+
"run.\n" +
169+
"For more persistent methods of setting the debug token, please "
170+
"review the \n" +
171+
"\"Debug & test providers\" section of the Firebase App Check "
172+
"documentation:\n" +
173+
"https://firebase.google.com/docs/app-check\n\n" +
174+
"Note: To keep your project secure, please revoke and delete this "
175+
"token using the \n" +
176+
"Firebase Console or the CLI (`firebase "
177+
"appcheck:debugtokens:delete`) "
178+
"when you finish debugging.\n\n" +
179+
"Warning: This debug token is a secret and should not be shared or "
180+
"uploaded to source code.\n\n" +
181+
"Debug Token Guide: "
182+
"https://firebase.google.com/docs/app-check/ios/debug-provider\n" +
183+
"Firebase CLI install instructions: "
184+
"https://firebase.google.com/docs/cli\n";
185+
}
124186
}
125187

126-
if (!debug_token_cstr) {
127-
completion_callback({}, kAppCheckErrorInvalidConfiguration,
128-
"Missing debug token");
129-
return;
188+
if (!message_to_log.empty()) {
189+
firebase::LogWarning("%s", message_to_log.c_str());
130190
}
131191

132192
// Exchange debug token with the backend to get a proper attestation token.
133193
auto request = std::make_shared<DebugTokenRequest>(app_);
134-
request->SetDebugToken(debug_token_cstr);
194+
request->SetDebugToken(debug_token_);
135195
request->SetLimitedUse(limited_use);
136196

137197
// Use an async call, since we don't want to block on the server response.

release_build_files/readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -615,6 +615,7 @@ code.
615615
## Release Notes
616616
### Upcoming
617617
- Changes
618+
- App Check (Desktop): The App Check Debug Provider will now automatically generate a local debug token if one isn't provided, and print instructions for registering it via the Firebase Console or CLI.
618619
- General (iOS): Fixed an issue where prebuilt iOS/tvOS frameworks had an incorrect minimum deployment target (minos), which caused linker warnings.
619620
- Auth (Desktop): Fixed log spam and high CPU utilization when offline by moving `USE_AUTH_EMULATOR` environment variable check to initialization and eliminating per-request logging (#1629).
620621
- Messaging: Added new Registration methods using Installation Ids.

0 commit comments

Comments
 (0)