Skip to content

Commit 491f22f

Browse files
jonsimantova-maurice
authored andcommitted
Add GenerateAppId, to generate a random 64-bit app ID encoded in base64.
PiperOrigin-RevId: 248593579
1 parent 2a05a6f commit 491f22f

File tree

2 files changed

+40
-6
lines changed

2 files changed

+40
-6
lines changed

app/instance_id/instance_id_desktop_impl.cc

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@
1818

1919
#include "app/instance_id/iid_data_generated.h"
2020
#include "app/src/app_identifier.h"
21+
#include "app/src/base64.h"
2122
#include "app/src/cleanup_notifier.h"
2223
#include "app/src/time.h"
24+
#include "app/src/uuid.h"
2325
#include "flatbuffers/flexbuffers.h"
2426

2527
namespace firebase {
@@ -190,9 +192,9 @@ bool InstanceIdDesktopImpl::SaveToStorage() {
190192

191193
std::vector<flatbuffers::Offset<Token>> token_offsets;
192194
for (auto it = tokens_.begin(); it != tokens_.end(); ++it) {
193-
token_offsets.push_back(CreateTokenDirect(
194-
builder, it->first.c_str() /* scope */,
195-
it->second.c_str() /* token */));
195+
token_offsets.push_back(CreateTokenDirect(builder,
196+
it->first.c_str() /* scope */,
197+
it->second.c_str() /* token */));
196198
}
197199
auto iid_data_table = CreateInstanceIdDesktopDataDirect(
198200
builder, instance_id_.c_str(), checkin_data_.device_id.c_str(),
@@ -301,6 +303,11 @@ bool InstanceIdDesktopImpl::InitialOrRefreshCheckin() {
301303
LoadFromStorage();
302304
}
303305

306+
// If we don't have an instance_id_ yet, generate one now.
307+
if (instance_id_.empty()) {
308+
instance_id_ = GenerateAppId();
309+
}
310+
304311
// If we've already checked in.
305312
if (checkin_data_.last_checkin_time_ms > 0) {
306313
FIREBASE_ASSERT(!checkin_data_.device_id.empty() &&
@@ -433,6 +440,29 @@ bool InstanceIdDesktopImpl::InitialOrRefreshCheckin() {
433440
return true;
434441
}
435442

443+
std::string InstanceIdDesktopImpl::GenerateAppId() {
444+
firebase::internal::Uuid uuid;
445+
uuid.Generate();
446+
// Collapse into 8 bytes, forcing the top 4 bits to be 0x70.
447+
uint8_t buffer[8];
448+
buffer[0] = ((uuid.data[0] ^ uuid.data[8]) & 0x0f) | 0x70;
449+
buffer[1] = uuid.data[1] ^ uuid.data[9];
450+
buffer[2] = uuid.data[2] ^ uuid.data[10];
451+
buffer[3] = uuid.data[3] ^ uuid.data[11];
452+
buffer[4] = uuid.data[4] ^ uuid.data[12];
453+
buffer[5] = uuid.data[5] ^ uuid.data[13];
454+
buffer[6] = uuid.data[6] ^ uuid.data[14];
455+
buffer[7] = uuid.data[7] ^ uuid.data[15];
456+
457+
std::string input(reinterpret_cast<char*>(buffer), sizeof(buffer));
458+
std::string output;
459+
460+
if (firebase::internal::Base64Encode(input, &output)) {
461+
return output;
462+
}
463+
return ""; // Error encoding.
464+
}
465+
436466
} // namespace internal
437467
} // namespace instance_id
438468
} // namespace firebase

app/instance_id/instance_id_desktop_impl.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -199,13 +199,17 @@ class InstanceIdDesktopImpl {
199199
// Delete the instance ID from local secure storage. Blocking.
200200
bool DeleteFromStorage();
201201

202-
// Used to wait for async storage functions to finish.
203-
Semaphore storage_semaphore_;
204-
205202
// Fetch the current device ID and security token or check-in to retrieve
206203
// a new device ID and security token.
207204
bool InitialOrRefreshCheckin();
208205

206+
// Generate a new appid. This is 4 bits of 0x7 followed by 60 bits of
207+
// randomness, then base64-encoded until golden brown.
208+
std::string GenerateAppId();
209+
210+
// Used to wait for async storage functions to finish.
211+
Semaphore storage_semaphore_;
212+
209213
// Future manager of this object
210214
FutureManager future_manager_;
211215

0 commit comments

Comments
 (0)