Skip to content

Commit 81e92c1

Browse files
committed
libobs: Add libsecret keychain implementation
1 parent ce09848 commit 81e92c1

File tree

4 files changed

+117
-1
lines changed

4 files changed

+117
-1
lines changed

libobs/cmake/legacy.cmake

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,14 @@ elseif(OS_POSIX)
424424
target_link_libraries(libobs PRIVATE GIO::GIO)
425425

426426
target_sources(libobs PRIVATE util/platform-nix-dbus.c util/platform-nix-portal.c)
427+
428+
find_package(Libsecret)
429+
if(TARGET Libsecret::Libsecret)
430+
obs_status(STATUS "-> libsecret found, enabling keychain API")
431+
target_link_libraries(libobs PRIVATE Libsecret::Libsecret)
432+
target_compile_definitions(libobs PRIVATE USE_LIBSECRET)
433+
target_sources(libobs PRIVATE util/platform-nix-libsecret.c)
434+
endif()
427435
endif()
428436

429437
if(TARGET XCB::XINPUT)

libobs/cmake/os-linux.cmake

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ find_package(
99
OPTIONAL_COMPONENTS xcb-xinput
1010
QUIET)
1111
find_package(gio)
12+
find_package(Libsecret)
1213

1314
target_link_libraries(libobs PRIVATE X11::x11-xcb xcb::xcb LibUUID::LibUUID ${CMAKE_DL_LIBS})
1415

@@ -51,6 +52,12 @@ if(TARGET gio::gio)
5152
target_link_libraries(libobs PRIVATE gio::gio)
5253

5354
target_sources(libobs PRIVATE util/platform-nix-dbus.c util/platform-nix-portal.c)
55+
56+
if(TARGET Libsecret::Libsecret)
57+
target_link_libraries(libobs PRIVATE Libsecret::Libsecret)
58+
target_compile_definitions(libobs PRIVATE USE_LIBSECRET)
59+
target_sources(libobs PRIVATE util/platform-nix-libsecret.c)
60+
endif()
5461
endif()
5562

5663
if(ENABLE_WAYLAND)
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/*
2+
* Copyright (c) 2023 Dennis Sädtler <[email protected]>
3+
*
4+
* Permission to use, copy, modify, and distribute this software for any
5+
* purpose with or without fee is hereby granted, provided that the above
6+
* copyright notice and this permission notice appear in all copies.
7+
*
8+
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9+
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10+
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11+
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12+
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13+
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14+
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15+
*/
16+
17+
#include <gio/gio.h>
18+
#include <libsecret/secret.h>
19+
20+
#include "platform.h"
21+
#include "bmem.h"
22+
23+
static const SecretSchema obs_schema = {
24+
"com.obsproject.libobs.Secret",
25+
SECRET_SCHEMA_NONE,
26+
{
27+
{"key", SECRET_SCHEMA_ATTRIBUTE_STRING},
28+
{"NULL", 0},
29+
}};
30+
31+
bool os_keychain_available(void)
32+
{
33+
return true;
34+
}
35+
36+
bool os_keychain_save(const char *label, const char *key, const char *data)
37+
{
38+
if (!label || !key || !data)
39+
return false;
40+
41+
GError *error = NULL;
42+
secret_password_store_sync(&obs_schema, SECRET_COLLECTION_DEFAULT,
43+
label, data, NULL, &error, "key", key, NULL);
44+
if (error != NULL) {
45+
blog(LOG_ERROR,
46+
"Keychain item \"%s::%s\" could not be saved: %s", label,
47+
key, error->message);
48+
g_error_free(error);
49+
return false;
50+
}
51+
52+
return true;
53+
}
54+
55+
bool os_keychain_load(const char *label, const char *key, char **data)
56+
{
57+
if (!label || !key || !data)
58+
return false;
59+
60+
GError *error = NULL;
61+
gchar *password = secret_password_lookup_sync(&obs_schema, NULL, &error,
62+
"key", key, NULL);
63+
64+
if (error != NULL) {
65+
blog(LOG_ERROR,
66+
"Keychain item \"%s::%s\" could not be read: %s", label,
67+
key, error->message);
68+
g_error_free(error);
69+
return false;
70+
} else if (password == NULL) {
71+
return false;
72+
}
73+
74+
*data = bstrdup(password);
75+
secret_password_free(password);
76+
return true;
77+
}
78+
79+
bool os_keychain_delete(const char *label, const char *key)
80+
{
81+
if (!label || !key)
82+
return false;
83+
84+
GError *error = NULL;
85+
gboolean removed = secret_password_clear_sync(&obs_schema, NULL, &error,
86+
"key", key, NULL);
87+
88+
if (error != NULL) {
89+
if (error->code == SECRET_ERROR_NO_SUCH_OBJECT) {
90+
removed = true;
91+
} else {
92+
blog(LOG_ERROR,
93+
"Keychain item \"%s::%s\" could not be deleted: %s",
94+
label, key, error->message);
95+
}
96+
97+
g_error_free(error);
98+
}
99+
100+
return removed;
101+
}

libobs/util/platform-nix.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1149,7 +1149,7 @@ char *os_generate_uuid(void)
11491149
return out;
11501150
}
11511151

1152-
#ifndef __APPLE__
1152+
#if !defined(__APPLE__) && !defined(USE_LIBSECRET)
11531153
bool os_keychain_available(void)
11541154
{
11551155
return false;

0 commit comments

Comments
 (0)