Skip to content

Commit c5a9883

Browse files
committed
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
1 parent c28b251 commit c5a9883

2 files changed

Lines changed: 54 additions & 11 deletions

File tree

app_check/src/desktop/debug_provider_desktop.cc

Lines changed: 53 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
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>
2022
#include <string>
@@ -26,6 +28,7 @@
2628
#include "app/rest/util.h"
2729
#include "app/src/log.h"
2830
#include "app/src/scheduler.h"
31+
#include "app/src/uuid.h"
2932
#include "app_check/src/desktop/debug_token_request.h"
3033
#include "app_check/src/desktop/token_response.h"
3134
#include "firebase/app_check/debug_provider.h"
@@ -34,6 +37,22 @@ namespace firebase {
3437
namespace app_check {
3538
namespace internal {
3639

40+
namespace {
41+
std::string GenerateUuidString() {
42+
firebase::internal::Uuid uuid;
43+
uuid.Generate();
44+
char uuid_str[37];
45+
snprintf(
46+
uuid_str, sizeof(uuid_str),
47+
"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
48+
uuid.data[0], uuid.data[1], uuid.data[2], uuid.data[3], uuid.data[4],
49+
uuid.data[5], uuid.data[6], uuid.data[7], uuid.data[8], uuid.data[9],
50+
uuid.data[10], uuid.data[11], uuid.data[12], uuid.data[13], uuid.data[14],
51+
uuid.data[15]);
52+
return std::string(uuid_str);
53+
}
54+
} // namespace
55+
3756
class DebugAppCheckProvider : public AppCheckProvider {
3857
public:
3958
DebugAppCheckProvider(App* app, const std::string& token);
@@ -115,23 +134,46 @@ void DebugAppCheckProvider::GetTokenInternal(
115134
bool limited_use,
116135
std::function<void(AppCheckToken, int, const std::string&)>
117136
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");
137+
if (debug_token_.empty()) {
138+
if (const char* env_token = std::getenv("APP_CHECK_DEBUG_TOKEN")) {
139+
debug_token_ = env_token;
140+
} else {
141+
debug_token_ = GenerateUuidString();
142+
}
124143
}
125144

126-
if (!debug_token_cstr) {
127-
completion_callback({}, kAppCheckErrorInvalidConfiguration,
128-
"Missing debug token");
129-
return;
145+
static bool logged_token = false;
146+
if (!logged_token && !debug_token_.empty()) {
147+
logged_token = true;
148+
std::string app_id = app_->options().app_id();
149+
std::string project_id = app_->options().project_id();
150+
std::string message =
151+
std::string("\nWARNING: Firebase App Check debug token: ") +
152+
debug_token_ + "\n\n" +
153+
"To use this token for app debugging, register it with your "
154+
"project.\n\n" +
155+
"You can do so in the Firebase Console: \n" +
156+
"https://console.firebase.google.com/project/" + project_id +
157+
"/appcheck/apps?selectedAppId=" + app_id + " \n\n" +
158+
"Or using the Firebase CLI: \n" +
159+
"firebase appcheck:debugtokens:create " + debug_token_ + " --app " +
160+
app_id + "\n\n" +
161+
"Note: To keep your project secure, please revoke and delete this "
162+
"token using the \n" +
163+
"Firebase Console or the CLI (`firebase appcheck:debugtokens:delete`) "
164+
"when you finish debugging.\n\n" +
165+
"Warning: This debug token is a secret and should not be shared or "
166+
"uploaded to source code.\n\n" +
167+
"Debug Token Guide: "
168+
"https://firebase.google.com/docs/app-check/ios/debug-provider\n" +
169+
"Firebase CLI install instructions: "
170+
"https://firebase.google.com/docs/cli\n";
171+
firebase::LogWarning("%s", message.c_str());
130172
}
131173

132174
// Exchange debug token with the backend to get a proper attestation token.
133175
auto request = std::make_shared<DebugTokenRequest>(app_);
134-
request->SetDebugToken(debug_token_cstr);
176+
request->SetDebugToken(debug_token_);
135177
request->SetLimitedUse(limited_use);
136178

137179
// 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)