yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
wasm_secure_storage.h
Go to the documentation of this file.
1#ifndef YAZE_APP_PLATFORM_WASM_SECURE_STORAGE_H_
2#define YAZE_APP_PLATFORM_WASM_SECURE_STORAGE_H_
3
4#ifdef __EMSCRIPTEN__
5
6#include <string>
7#include <vector>
8
9#include "absl/status/status.h"
10#include "absl/status/statusor.h"
11
12namespace yaze {
13namespace app {
14namespace platform {
15
34class WasmSecureStorage {
35 public:
40 enum class StorageType {
41 kSession, // sessionStorage (cleared on tab close)
42 kLocal // localStorage (persistent)
43 };
44
52 static absl::Status StoreApiKey(const std::string& service,
53 const std::string& key,
54 StorageType storage_type = StorageType::kSession);
55
62 static absl::StatusOr<std::string> RetrieveApiKey(
63 const std::string& service,
64 StorageType storage_type = StorageType::kSession);
65
72 static absl::Status ClearApiKey(const std::string& service,
73 StorageType storage_type = StorageType::kSession);
74
81 static bool HasApiKey(const std::string& service,
82 StorageType storage_type = StorageType::kSession);
83
91 static absl::Status StoreSecret(const std::string& key,
92 const std::string& value,
93 StorageType storage_type = StorageType::kSession);
94
101 static absl::StatusOr<std::string> RetrieveSecret(
102 const std::string& key,
103 StorageType storage_type = StorageType::kSession);
104
111 static absl::Status ClearSecret(const std::string& key,
112 StorageType storage_type = StorageType::kSession);
113
119 static std::vector<std::string> ListStoredApiKeys(
120 StorageType storage_type = StorageType::kSession);
121
127 static absl::Status ClearAllApiKeys(StorageType storage_type = StorageType::kSession);
128
133 static bool IsStorageAvailable();
134
140 struct StorageQuota {
141 size_t used_bytes = 0;
142 size_t available_bytes = 0;
143 };
144 static absl::StatusOr<StorageQuota> GetStorageQuota(
145 StorageType storage_type = StorageType::kSession);
146
147 private:
148 // Key prefixes for different types of data
149 static constexpr const char* kApiKeyPrefix = "yaze_secure_api_";
150 static constexpr const char* kSecretPrefix = "yaze_secure_secret_";
151
157 static std::string BuildApiKeyStorageKey(const std::string& service);
158
164 static std::string BuildSecretStorageKey(const std::string& key);
165
171 static std::string ExtractServiceFromKey(const std::string& storage_key);
172};
173
174} // namespace platform
175} // namespace app
176} // namespace yaze
177
178#else // !__EMSCRIPTEN__
179
180// Stub for non-WASM builds
181#include <string>
182#include <vector>
183
184#include "absl/status/status.h"
185#include "absl/status/statusor.h"
186
187namespace yaze {
188namespace app {
189namespace platform {
190
196 public:
197 enum class StorageType { kSession, kLocal };
198
199 static absl::Status StoreApiKey(const std::string&, const std::string&,
201 return absl::UnimplementedError("Secure storage requires WASM build");
202 }
203
204 static absl::StatusOr<std::string> RetrieveApiKey(
205 const std::string&, StorageType = StorageType::kSession) {
206 return absl::NotFoundError("Secure storage requires WASM build");
207 }
208
209 static absl::Status ClearApiKey(const std::string&,
211 return absl::UnimplementedError("Secure storage requires WASM build");
212 }
213
214 static bool HasApiKey(const std::string&,
216 return false;
217 }
218
219 static absl::Status StoreSecret(const std::string&, const std::string&,
221 return absl::UnimplementedError("Secure storage requires WASM build");
222 }
223
224 static absl::StatusOr<std::string> RetrieveSecret(
225 const std::string&, StorageType = StorageType::kSession) {
226 return absl::NotFoundError("Secure storage requires WASM build");
227 }
228
229 static absl::Status ClearSecret(const std::string&,
231 return absl::UnimplementedError("Secure storage requires WASM build");
232 }
233
234 static std::vector<std::string> ListStoredApiKeys(
236 return {};
237 }
238
240 return absl::UnimplementedError("Secure storage requires WASM build");
241 }
242
243 static bool IsStorageAvailable() { return false; }
244
246 size_t used_bytes = 0;
247 size_t available_bytes = 0;
248 };
249
250 static absl::StatusOr<StorageQuota> GetStorageQuota(
252 return absl::UnimplementedError("Secure storage requires WASM build");
253 }
254};
255
256} // namespace platform
257} // namespace app
258} // namespace yaze
259
260#endif // __EMSCRIPTEN__
261
262#endif // YAZE_APP_PLATFORM_WASM_SECURE_STORAGE_H_
static absl::Status ClearApiKey(const std::string &, StorageType=StorageType::kSession)
static absl::Status StoreSecret(const std::string &, const std::string &, StorageType=StorageType::kSession)
static bool HasApiKey(const std::string &, StorageType=StorageType::kSession)
static absl::Status ClearSecret(const std::string &, StorageType=StorageType::kSession)
static absl::Status ClearAllApiKeys(StorageType=StorageType::kSession)
static std::vector< std::string > ListStoredApiKeys(StorageType=StorageType::kSession)
static absl::Status StoreApiKey(const std::string &, const std::string &, StorageType=StorageType::kSession)
static absl::StatusOr< std::string > RetrieveSecret(const std::string &, StorageType=StorageType::kSession)
static absl::StatusOr< std::string > RetrieveApiKey(const std::string &, StorageType=StorageType::kSession)
static absl::StatusOr< StorageQuota > GetStorageQuota(StorageType=StorageType::kSession)