Skip to content

Commit 5b98f59

Browse files
cynthiajianga-maurice
authored andcommitted
[Auth] Add linux secure manager file.
+ Support Save/Load/Delete/DeleteAll + Add Tests + Don't included linux impl because of kokoro blockage. PiperOrigin-RevId: 244876444
1 parent bd9ca57 commit 5b98f59

File tree

4 files changed

+334
-0
lines changed

4 files changed

+334
-0
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// Copyright 2019 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#ifndef FIREBASE_AUTH_CLIENT_CPP_SRC_DESKTOP_SECURE_USER_SECURE_DATA_HANDLE_H_
16+
#define FIREBASE_AUTH_CLIENT_CPP_SRC_DESKTOP_SECURE_USER_SECURE_DATA_HANDLE_H_
17+
18+
#include <string>
19+
20+
#include "app/src/include/firebase/future.h"
21+
#include "app/src/reference_counted_future_impl.h"
22+
23+
namespace firebase {
24+
namespace auth {
25+
namespace secure {
26+
27+
enum UserSecureFn {
28+
kUserSecureFnLoad,
29+
kUserSecureFnSave,
30+
kUserSecureFnDelete,
31+
kUserSecureFnDeleteAll,
32+
kUserSecureFnCount
33+
};
34+
35+
enum UserSecureFutureResult {
36+
kSuccess,
37+
kNoEntry,
38+
kNoInternal,
39+
};
40+
41+
template <typename T>
42+
struct UserSecureDataHandle {
43+
UserSecureDataHandle(const std::string& appName, const std::string& userData,
44+
ReferenceCountedFutureImpl* futureApi,
45+
const SafeFutureHandle<T>& futureHandle)
46+
: app_name(appName),
47+
user_data(userData),
48+
future_api(futureApi),
49+
future_handle(futureHandle) {}
50+
51+
const std::string app_name;
52+
const std::string user_data;
53+
ReferenceCountedFutureImpl* future_api;
54+
SafeFutureHandle<T> future_handle;
55+
};
56+
57+
} // namespace secure
58+
} // namespace auth
59+
} // namespace firebase
60+
61+
#endif // FIREBASE_AUTH_CLIENT_CPP_SRC_DESKTOP_SECURE_USER_SECURE_DATA_HANDLE_H_
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Copyright 2019 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#ifndef FIREBASE_AUTH_CLIENT_CPP_SRC_DESKTOP_SECURE_USER_SECURE_INTERNAL_H_
16+
#define FIREBASE_AUTH_CLIENT_CPP_SRC_DESKTOP_SECURE_USER_SECURE_INTERNAL_H_
17+
18+
#include <string>
19+
20+
#include "app/src/scheduler.h"
21+
#include "auth/src/desktop/secure/user_secure_data_handle.h"
22+
23+
namespace firebase {
24+
namespace auth {
25+
namespace secure {
26+
27+
class UserSecureInternal {
28+
public:
29+
UserSecureInternal() = default;
30+
virtual ~UserSecureInternal() = default;
31+
32+
// Load persisted user data for given app name.
33+
virtual std::string LoadUserData(const std::string appName) = 0;
34+
35+
// Save user data under the key of given app name.
36+
virtual void SaveUserData(const std::string appName,
37+
const std::string userData) = 0;
38+
39+
// Delete user data under the given app name.
40+
virtual void DeleteUserData(const std::string appName) = 0;
41+
42+
// Delete all user data.
43+
virtual void DeleteAllData() = 0;
44+
};
45+
46+
} // namespace secure
47+
} // namespace auth
48+
} // namespace firebase
49+
50+
#endif // FIREBASE_AUTH_CLIENT_CPP_SRC_DESKTOP_SECURE_USER_SECURE_INTERNAL_H_
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
// Copyright 2019 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#include "auth/src/desktop/secure/user_secure_manager.h"
16+
17+
#include "app/src/callback.h"
18+
#include "auth/src/desktop/secure/user_secure_internal.h"
19+
20+
namespace firebase {
21+
namespace auth {
22+
namespace secure {
23+
24+
using callback::NewCallback;
25+
26+
UserSecureManager::UserSecureManager()
27+
: future_api_(kUserSecureFnCount), safe_this_(this) {}
28+
29+
UserSecureManager::UserSecureManager(
30+
UniquePtr<UserSecureInternal> userSecureInternal)
31+
: user_secure_(std::move(userSecureInternal)),
32+
future_api_(kUserSecureFnCount),
33+
safe_this_(this) {}
34+
35+
UserSecureManager::~UserSecureManager() {
36+
// Clear safe reference immediately so that scheduled callback can skip
37+
// executing code which requires reference to this.
38+
safe_this_.ClearReference();
39+
}
40+
41+
Future<std::string> UserSecureManager::LoadUserData(
42+
const std::string& appName) {
43+
const auto future_handle =
44+
future_api_.SafeAlloc<std::string>(kUserSecureFnLoad);
45+
46+
auto data_handle = MakeShared<UserSecureDataHandle<std::string>>(
47+
appName, "", &future_api_, future_handle);
48+
49+
auto callback = NewCallback(
50+
[](ThisRef ref, SharedPtr<UserSecureDataHandle<std::string>> handle,
51+
UserSecureInternal* internal) {
52+
if (internal == nullptr) {
53+
handle->future_api->Complete(handle->future_handle, kNoInternal,
54+
"manager doesn't have valid internal");
55+
return;
56+
}
57+
ThisRefLock lock(&ref);
58+
if (lock.GetReference() != nullptr) {
59+
std::string result = internal->LoadUserData(handle->app_name);
60+
std::string empty_str("");
61+
if (result.empty()) {
62+
handle->future_api->CompleteWithResult(handle->future_handle,
63+
kNoEntry, "", empty_str);
64+
} else {
65+
handle->future_api->CompleteWithResult(handle->future_handle,
66+
kSuccess, "", result);
67+
}
68+
}
69+
},
70+
safe_this_, data_handle, user_secure_.get());
71+
scheduler_.Schedule(callback);
72+
return MakeFuture(&future_api_, future_handle);
73+
}
74+
75+
Future<void> UserSecureManager::SaveUserData(const std::string& appName,
76+
const std::string& userData) {
77+
const auto future_handle = future_api_.SafeAlloc<void>(kUserSecureFnSave);
78+
79+
auto data_handle = MakeShared<UserSecureDataHandle<void>>(
80+
appName, userData, &future_api_, future_handle);
81+
82+
auto callback = NewCallback(
83+
[](ThisRef ref, SharedPtr<UserSecureDataHandle<void>> handle,
84+
UserSecureInternal* internal) {
85+
if (internal == nullptr) {
86+
handle->future_api->Complete(handle->future_handle, kNoInternal,
87+
"manager doesn't have valid internal");
88+
return;
89+
}
90+
ThisRefLock lock(&ref);
91+
if (lock.GetReference() != nullptr) {
92+
internal->SaveUserData(handle->app_name, handle->user_data);
93+
handle->future_api->Complete(handle->future_handle, kSuccess);
94+
}
95+
},
96+
safe_this_, data_handle, user_secure_.get());
97+
scheduler_.Schedule(callback);
98+
return MakeFuture(&future_api_, future_handle);
99+
}
100+
101+
Future<void> UserSecureManager::DeleteUserData(const std::string& appName) {
102+
const auto future_handle = future_api_.SafeAlloc<void>(kUserSecureFnDelete);
103+
104+
auto data_handle = MakeShared<UserSecureDataHandle<void>>(
105+
appName, "", &future_api_, future_handle);
106+
107+
auto callback = NewCallback(
108+
[](ThisRef ref, SharedPtr<UserSecureDataHandle<void>> handle,
109+
UserSecureInternal* internal) {
110+
if (internal == nullptr) {
111+
handle->future_api->Complete(handle->future_handle, kNoInternal,
112+
"manager doesn't have valid internal");
113+
return;
114+
}
115+
ThisRefLock lock(&ref);
116+
if (lock.GetReference() != nullptr) {
117+
internal->DeleteUserData(handle->app_name);
118+
handle->future_api->Complete(handle->future_handle, kSuccess);
119+
}
120+
},
121+
safe_this_, data_handle, user_secure_.get());
122+
scheduler_.Schedule(callback);
123+
return MakeFuture(&future_api_, future_handle);
124+
}
125+
126+
Future<void> UserSecureManager::DeleteAllData() {
127+
auto future_handle = future_api_.SafeAlloc<void>(kUserSecureFnDeleteAll);
128+
129+
auto data_handle = MakeShared<UserSecureDataHandle<void>>(
130+
"", "", &future_api_, future_handle);
131+
132+
auto callback = NewCallback(
133+
[](ThisRef ref, SharedPtr<UserSecureDataHandle<void>> handle,
134+
UserSecureInternal* internal) {
135+
if (internal == nullptr) {
136+
handle->future_api->Complete(handle->future_handle, kNoInternal,
137+
"manager doesn't have valid internal");
138+
return;
139+
}
140+
ThisRefLock lock(&ref);
141+
if (lock.GetReference() != nullptr) {
142+
internal->DeleteAllData();
143+
handle->future_api->Complete(handle->future_handle, kSuccess);
144+
}
145+
},
146+
safe_this_, data_handle, user_secure_.get());
147+
scheduler_.Schedule(callback);
148+
return MakeFuture(&future_api_, future_handle);
149+
}
150+
151+
} // namespace secure
152+
} // namespace auth
153+
} // namespace firebase
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// Copyright 2019 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#ifndef FIREBASE_AUTH_CLIENT_CPP_SRC_DESKTOP_SECURE_USER_SECURE_MANAGER_H_
16+
#define FIREBASE_AUTH_CLIENT_CPP_SRC_DESKTOP_SECURE_USER_SECURE_MANAGER_H_
17+
18+
#include <string>
19+
20+
#include "app/src/include/firebase/future.h"
21+
#include "app/src/reference_counted_future_impl.h"
22+
#include "app/src/safe_reference.h"
23+
#include "auth/src/desktop/secure/user_secure_data_handle.h"
24+
#include "auth/src/desktop/secure/user_secure_internal.h"
25+
26+
namespace firebase {
27+
namespace auth {
28+
namespace secure {
29+
30+
class UserSecureManager {
31+
public:
32+
explicit UserSecureManager();
33+
~UserSecureManager();
34+
35+
// Overloaded constructor to set the internal instance.
36+
explicit UserSecureManager(UniquePtr<UserSecureInternal> userSecureInternal);
37+
38+
// Load persisted user data for given app name.
39+
Future<std::string> LoadUserData(const std::string& appName);
40+
41+
// Save user data under the key of given app name.
42+
Future<void> SaveUserData(const std::string& appName,
43+
const std::string& userData);
44+
45+
// Delete user data under the given app name.
46+
Future<void> DeleteUserData(const std::string& appName);
47+
48+
// Delete all user data.
49+
Future<void> DeleteAllData();
50+
51+
private:
52+
UniquePtr<UserSecureInternal> user_secure_;
53+
ReferenceCountedFutureImpl future_api_;
54+
55+
// Static scheduler to shared among all auth instance.
56+
scheduler::Scheduler scheduler_;
57+
58+
// Safe reference to this. Set in constructor and cleared in destructor
59+
// Should be safe to be copied in any thread because the SharedPtr never
60+
// changes, until safe_this_ is completely destroyed.
61+
typedef firebase::internal::SafeReference<UserSecureManager> ThisRef;
62+
typedef firebase::internal::SafeReferenceLock<UserSecureManager> ThisRefLock;
63+
ThisRef safe_this_;
64+
};
65+
66+
} // namespace secure
67+
} // namespace auth
68+
} // namespace firebase
69+
70+
#endif // FIREBASE_AUTH_CLIENT_CPP_SRC_DESKTOP_SECURE_USER_SECURE_MANAGER_H_

0 commit comments

Comments
 (0)