Skip to content

Commit 86b3f8c

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 7ef76be commit 86b3f8c

1 file changed

Lines changed: 40 additions & 11 deletions

File tree

app_check/src/desktop/debug_provider_desktop.cc

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,15 @@
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>
2123
#include <utility>
2224

25+
#include "app/src/uuid.h"
26+
2327
#include "app/rest/response.h"
2428
#include "app/rest/transport_builder.h"
2529
#include "app/rest/transport_curl.h"
@@ -111,27 +115,52 @@ void DebugAppCheckProvider::GetLimitedUseToken(
111115
GetTokenInternal(true, completion_callback);
112116
}
113117

118+
firebase::internal::Uuid GenerateDebugToken
119+
114120
void DebugAppCheckProvider::GetTokenInternal(
115121
bool limited_use,
116122
std::function<void(AppCheckToken, int, const std::string&)>
117123
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");
124+
if (debug_token_.empty()) {
125+
if (std::getenv("APP_CHECK_DEBUG_TOKEN")) {
126+
debug_token_ = std::getenv("APP_CHECK_DEBUG_TOKEN");
127+
} else {
128+
// Generate UUID
129+
firebase::internal::Uuid uuid;
130+
uuid.Generate();
131+
char uuid_str[37];
132+
snprintf(uuid_str, sizeof(uuid_str),
133+
"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
134+
uuid.data[0], uuid.data[1], uuid.data[2], uuid.data[3],
135+
uuid.data[4], uuid.data[5], uuid.data[6], uuid.data[7],
136+
uuid.data[8], uuid.data[9], uuid.data[10], uuid.data[11],
137+
uuid.data[12], uuid.data[13], uuid.data[14], uuid.data[15]);
138+
debug_token_ = uuid_str;
139+
}
124140
}
125141

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

132161
// Exchange debug token with the backend to get a proper attestation token.
133162
auto request = std::make_shared<DebugTokenRequest>(app_);
134-
request->SetDebugToken(debug_token_cstr);
163+
request->SetDebugToken(debug_token_);
135164
request->SetLimitedUse(limited_use);
136165

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

0 commit comments

Comments
 (0)