Skip to content

Commit

Permalink
libobs: Add libsecret keychain implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
derrod committed Jun 25, 2023
1 parent 199df6b commit d6132da
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 1 deletion.
8 changes: 8 additions & 0 deletions libobs/cmake/legacy.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,14 @@ elseif(OS_POSIX)
target_link_libraries(libobs PRIVATE GIO::GIO)

target_sources(libobs PRIVATE util/platform-nix-dbus.c util/platform-nix-portal.c)

find_package(Libsecret)
if(TARGET Libsecret::Libsecret)
obs_status(STATUS "-> libsecret found, enabling keychain API")
target_link_libraries(libobs PRIVATE Libsecret::Libsecret)
target_compile_definitions(libobs PRIVATE USE_LIBSECRET)
target_sources(libobs PRIVATE util/platform-nix-libsecret.c)
endif()
endif()

if(TARGET XCB::XINPUT)
Expand Down
7 changes: 7 additions & 0 deletions libobs/cmake/os-linux.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ find_package(
OPTIONAL_COMPONENTS xcb-xinput
QUIET)
find_package(gio)
find_package(Libsecret)

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

Expand Down Expand Up @@ -51,6 +52,12 @@ if(TARGET gio::gio)
target_link_libraries(libobs PRIVATE gio::gio)

target_sources(libobs PRIVATE util/platform-nix-dbus.c util/platform-nix-portal.c)

if(TARGET Libsecret::Libsecret)
target_link_libraries(libobs PRIVATE Libsecret::Libsecret)
target_compile_definitions(libobs PRIVATE USE_LIBSECRET)
target_sources(libobs PRIVATE util/platform-nix-libsecret.c)
endif()
endif()

if(ENABLE_WAYLAND)
Expand Down
95 changes: 95 additions & 0 deletions libobs/util/platform-nix-libsecret.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
* Copyright (c) 2023 Dennis Sädtler <[email protected]>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/

#include <gio/gio.h>
#include <libsecret/secret.h>

#include "platform.h"
#include "bmem.h"

static const SecretSchema obs_schema = {
"com.obsproject.Studio.Secret",
SECRET_SCHEMA_NONE,
{
{"key", SECRET_SCHEMA_ATTRIBUTE_STRING},
{"NULL", 0},
}};

bool os_keychain_save(const char *key, const char *data)
{
if (!key || !data)
return false;

GError *error = NULL;
secret_password_store_sync(&obs_schema, SECRET_COLLECTION_DEFAULT,
"OBS Studio", data, NULL, &error, "key", key,
NULL);
if (error != NULL) {
blog(LOG_ERROR, "Keychain item \"%s\" could not be saved: %s",
key, error->message);
g_error_free(error);
return false;
}

return true;
}

bool os_keychain_load(const char *key, char **data)
{
if (!key || !data)
return false;

GError *error = NULL;
gchar *password = secret_password_lookup_sync(&obs_schema, NULL, &error,
"key", key, NULL);

if (error != NULL) {
blog(LOG_ERROR, "Keychain item \"%s\" could not be read: %s",
key, error->message);
g_error_free(error);
return false;
} else if (password == NULL) {
return false;
}

*data = bstrdup(password);
secret_password_free(password);
return true;
}

bool os_keychain_delete(const char *key)
{
if (!key)
return false;

GError *error = NULL;
gboolean removed = secret_password_clear_sync(&obs_schema, NULL, &error,
"key", key, NULL);

if (error != NULL) {
if (error->code == SECRET_ERROR_NO_SUCH_OBJECT) {
removed = true;
} else {
blog(LOG_ERROR,
"Keychain item \"%s\" could not be deleted: %s",
key, error->message);
}

g_error_free(error);
}

return removed;
}
2 changes: 1 addition & 1 deletion libobs/util/platform-nix.c
Original file line number Diff line number Diff line change
Expand Up @@ -1149,7 +1149,7 @@ char *os_generate_uuid(void)
return out;
}

#ifndef __APPLE__
#if !defined(__APPLE__) && !defined(USE_LIBSECRET)
bool os_keychain_save(const char *key, const char *data)
{
/* Not implemented */
Expand Down

0 comments on commit d6132da

Please sign in to comment.