|
| 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_linux_internal.h" |
| 16 | + |
| 17 | +#include <dlfcn.h> |
| 18 | + |
| 19 | +#include <iostream> |
| 20 | + |
| 21 | +#include "auth/src/desktop/secure/user_secure_data_handle.h" |
| 22 | + |
| 23 | +namespace firebase { |
| 24 | +namespace auth { |
| 25 | +namespace secure { |
| 26 | + |
| 27 | +namespace { |
| 28 | + |
| 29 | +// key entry for the app name in the schema. When save the user data with a |
| 30 | +// given app name, the app name is the attribute of this key inside schema. |
| 31 | +const char kAppNameKey[] = "auth_app_name"; |
| 32 | +// A common attribute-value pair is added to all the device keys. This makes it |
| 33 | +// possible to match all the keys easily (and remove them all at once). |
| 34 | +const char kCommonKeyId[] = "common_key_id"; |
| 35 | +const char kCommonKeyValue[] = "common_key_value"; |
| 36 | +// Helper function to build the right schema using the provided namespace, for |
| 37 | +// storing keys. For instance, helps create separate storage schema for storing |
| 38 | +// actual vs testing keys |
| 39 | +const char kAuthKeyName[] = "com.google.firebase.auth.Keys"; |
| 40 | +SecretSchema BuildSchema(const char key_namespace[]) { |
| 41 | + SecretSchema schema = {key_namespace, |
| 42 | + SECRET_SCHEMA_NONE, |
| 43 | + { |
| 44 | + {kAppNameKey, SECRET_SCHEMA_ATTRIBUTE_STRING}, |
| 45 | + {kCommonKeyId, SECRET_SCHEMA_ATTRIBUTE_STRING}, |
| 46 | + }}; |
| 47 | + return schema; |
| 48 | +} |
| 49 | + |
| 50 | +} // namespace |
| 51 | + |
| 52 | +UserSecureLinuxInternal::UserSecureLinuxInternal() |
| 53 | + : UserSecureLinuxInternal(kAuthKeyName) {} |
| 54 | + |
| 55 | +UserSecureLinuxInternal::UserSecureLinuxInternal(const char key_namespace[]) |
| 56 | + : storage_schema_(BuildSchema(key_namespace)) {} |
| 57 | + |
| 58 | +UserSecureLinuxInternal::~UserSecureLinuxInternal() {} |
| 59 | + |
| 60 | +std::string UserSecureLinuxInternal::LoadUserData(const std::string appName) { |
| 61 | + fprintf(stderr, "internal loading start\n"); |
| 62 | + std::string empty_str(""); |
| 63 | + GError* error = nullptr; |
| 64 | + char* result = |
| 65 | + secret_password_lookup_sync(&storage_schema_, |
| 66 | + /* cancellable= */ nullptr, |
| 67 | + /* error= */ &error, |
| 68 | + /* key1= */ kAppNameKey, |
| 69 | + /* value1= */ appName.c_str(), nullptr); |
| 70 | + if (error) { |
| 71 | + g_error_free(error); |
| 72 | + return empty_str; |
| 73 | + } |
| 74 | + |
| 75 | + if (result == nullptr) { |
| 76 | + return empty_str; |
| 77 | + } |
| 78 | + std::string str_result(result); |
| 79 | + secret_password_free(result); |
| 80 | + |
| 81 | + return str_result; |
| 82 | +} |
| 83 | + |
| 84 | +void UserSecureLinuxInternal::SaveUserData(const std::string appName, |
| 85 | + const std::string userData) { |
| 86 | + secret_password_store_sync( |
| 87 | + &storage_schema_, SECRET_COLLECTION_DEFAULT, /* label= */ "UserSecure", |
| 88 | + /* password= */ userData.c_str(), /* cancellable= */ nullptr, |
| 89 | + /* error= */ nullptr, /* key1= */ kAppNameKey, |
| 90 | + /* value1= */ appName.c_str(), |
| 91 | + /* key2= */ kCommonKeyId, /* value2= */ kCommonKeyValue, nullptr); |
| 92 | +} |
| 93 | + |
| 94 | +void UserSecureLinuxInternal::DeleteUserData(const std::string appName) { |
| 95 | + secret_password_clear_sync(&storage_schema_, |
| 96 | + /* cancellable= */ nullptr, /* error= */ nullptr, |
| 97 | + /* key1= */ kAppNameKey, |
| 98 | + /* value1= */ appName.c_str(), nullptr); |
| 99 | +} |
| 100 | + |
| 101 | +void UserSecureLinuxInternal::DeleteAllData() { |
| 102 | + secret_password_clear_sync(&storage_schema_, /* cancellable= */ nullptr, |
| 103 | + /* error= */ nullptr, /* key2= */ kCommonKeyId, |
| 104 | + /* value2= */ kCommonKeyValue, nullptr); |
| 105 | +} |
| 106 | + |
| 107 | +} // namespace secure |
| 108 | +} // namespace auth |
| 109 | +} // namespace firebase |
0 commit comments