Skip to content

Commit dd070dc

Browse files
jonsimantova-maurice
authored andcommitted
Fix integration of credential persistence to use the app's bundle name (or project ID) instead of the generic app name, so that individual projects installed on the same machine don't step on each others' storage.
Also moves "fake" storage to a subdirectory that it creates in SaveUserData and removes in DeleteAllData. PiperOrigin-RevId: 245854110
1 parent 474d4c7 commit dd070dc

9 files changed

+29
-21
lines changed

auth/src/desktop/auth_desktop.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,12 @@ void ResetTokenRefreshCounter(AuthData* auth_data) {
421421

422422
void InitializeUserDataPersist(AuthData* auth_data) {
423423
auto auth_impl = static_cast<AuthImpl*>(auth_data->auth_impl);
424+
// Combine bundle ID with project ID to create an app identifier.
425+
std::string app_identifier = auth_data->app->options().package_name();
426+
app_identifier += ".";
427+
app_identifier += auth_data->app->options().project_id();
428+
auth_impl->user_data_persist =
429+
MakeUnique<UserDataPersist>(app_identifier.c_str());
424430
auth_data->auth->AddAuthStateListener(auth_impl->user_data_persist.get());
425431
auth_impl->user_data_persist->LoadUserData(auth_data);
426432
}

auth/src/desktop/auth_desktop.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#define FIREBASE_AUTH_CLIENT_CPP_SRC_DESKTOP_AUTH_DESKTOP_H_
1717

1818
#include <memory>
19+
1920
#include "app/rest/request.h"
2021
#include "app/src/semaphore.h"
2122
#include "app/src/thread.h"
@@ -102,9 +103,7 @@ class IdTokenRefreshThread {
102103

103104
// The desktop-specific Auth implementation.
104105
struct AuthImpl {
105-
AuthImpl() : async_sem(0), active_async_calls(0) {
106-
user_data_persist = MakeUnique<UserDataPersist>();
107-
}
106+
AuthImpl() : async_sem(0), active_async_calls(0) {}
108107

109108
// The application's API key.
110109
std::string api_key;

auth/src/desktop/secure/user_secure_darwin_internal.mm

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,12 @@
2121
namespace auth {
2222
namespace secure {
2323

24-
static const char kServicePrefix[] = "firebase.auth.cpp.cred/";
24+
static const char kServicePrefix[] = "";
25+
static const char kServiceSuffix[] = " [Firebase Auth]";
2526
static const int kMaxAllowedKeychainEntries = INT_MAX;
2627

2728
UserSecureDarwinInternal::UserSecureDarwinInternal(const char* service) {
28-
service_ = std::string(kServicePrefix) + service;
29+
service_ = std::string(kServicePrefix) + service + std::string(kServiceSuffix);
2930
}
3031

3132
UserSecureDarwinInternal::~UserSecureDarwinInternal() {}

auth/src/desktop/secure/user_secure_fake_internal.cc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
#include "auth/src/desktop/secure/user_secure_fake_internal.h"
1616

1717
#include <dirent.h>
18+
#include <sys/stat.h>
19+
#include <sys/types.h>
1820

1921
#include <cstdio>
2022
#include <fstream>
@@ -52,6 +54,9 @@ std::string UserSecureFakeInternal::LoadUserData(const std::string& app_name) {
5254

5355
void UserSecureFakeInternal::SaveUserData(const std::string& app_name,
5456
const std::string& user_data) {
57+
// Make the directory in case it doesn't already exist, ignoring errors.
58+
mkdir(secure_path_.c_str(), 0700);
59+
5560
std::string filename = GetFilePath(app_name);
5661

5762
std::ofstream ofile(filename, std::ios::binary);
@@ -85,6 +90,9 @@ void UserSecureFakeInternal::DeleteAllData() {
8590
remove(filepath.c_str());
8691
}
8792
closedir(theFolder);
93+
94+
// Remove the directory if it's empty, ignoring errors.
95+
rmdir(secure_path_.c_str());
8896
}
8997

9098
std::string UserSecureFakeInternal::GetFilePath(const std::string& app_name) {

auth/src/desktop/secure/user_secure_manager.cc

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -49,17 +49,9 @@ Mutex UserSecureManager::s_scheduler_mutex_; // NOLINT
4949
scheduler::Scheduler* UserSecureManager::s_scheduler_;
5050
int32_t UserSecureManager::s_scheduler_ref_count_;
5151

52-
#if defined(_WIN32) || defined(TARGET_OS_OSX) && TARGET_OS_OSX || \
53-
defined(__linux__)
54-
const char kAuthKeyName[] = "com.google.firebase.auth.Keys";
55-
#else
56-
// fake internal would use current folder.
57-
const char kAuthKeyName[] = "./";
58-
#endif
59-
60-
UserSecureManager::UserSecureManager()
52+
UserSecureManager::UserSecureManager(const char* app_id)
6153
: future_api_(kUserSecureFnCount), safe_this_(this) {
62-
user_secure_ = MakeUnique<USER_SECURE_TYPE>(kAuthKeyName);
54+
user_secure_ = MakeUnique<USER_SECURE_TYPE>(app_id);
6355
CreateScheduler();
6456
}
6557

auth/src/desktop/secure/user_secure_manager.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ namespace secure {
2929

3030
class UserSecureManager {
3131
public:
32-
explicit UserSecureManager();
32+
explicit UserSecureManager(const char* app_id);
3333
~UserSecureManager();
3434

3535
// Overloaded constructor to set the internal instance.

auth/src/desktop/secure/user_secure_windows_internal.cc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,13 @@ namespace firebase {
2121
namespace auth {
2222
namespace secure {
2323

24-
static const char kNamespacePrefix[] = "firebase.auth.cpp.cred/";
24+
static const char kNamespacePrefix[] = "";
25+
static const char kNamespaceSuffix[] = " [Firebase Auth]";
2526

2627
UserSecureWindowsInternal::UserSecureWindowsInternal(
2728
const char* key_namespace) {
28-
namespace_ = std::string(kNamespacePrefix) + key_namespace;
29+
namespace_ = std::string(kNamespacePrefix) + key_namespace +
30+
std::string(kNamespaceSuffix);
2931
}
3032

3133
UserSecureWindowsInternal::~UserSecureWindowsInternal() {}

auth/src/desktop/user_desktop.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -396,8 +396,8 @@ Future<ResultT> DoReauthenticate(Promise<ResultT> promise,
396396

397397
} // namespace
398398

399-
UserDataPersist::UserDataPersist() {
400-
user_secure_manager_ = MakeUnique<secure::UserSecureManager>();
399+
UserDataPersist::UserDataPersist(const char* app_id) {
400+
user_secure_manager_ = MakeUnique<secure::UserSecureManager>(app_id);
401401
}
402402

403403
UserDataPersist::UserDataPersist(

auth/src/desktop/user_desktop.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ struct UserData : public UserInfoImpl {
9898
// os specific secret locations for security reason.
9999
class UserDataPersist : public firebase::auth::AuthStateListener {
100100
public:
101-
UserDataPersist();
101+
UserDataPersist(const char* app_id);
102102
~UserDataPersist() {}
103103

104104
// Overloaded constructor to set the internal instance.

0 commit comments

Comments
 (0)